@@ -97,14 +102,14 @@ Use the following APIs to delete a **Storage** instance or data file.
...
@@ -97,14 +102,14 @@ Use the following APIs to delete a **Storage** instance or data file.
```js
```js
promise.then((storage) => {
promise.then((storage) => {
let getPromise = storage.put('startup', 'auto') // Save data to the Storage instance.
let getPromise = storage.put('startup', 'auto'); // Save data to the Storage instance.
getPromise.then(() => {
getPromise.then(() => {
console.info("Put the value of startup successfully.")
console.info("Succeeded in putting the value of startup.");
}).catch((err) => {
}).catch((err) => {
console.info("Put the value of startup failed with err: " + err)
console.info("Failed to put the value of startup with err: " + err);
})
})
}).catch((err) => {
}).catch((err) => {
console.info("Get the storage failed")
console.info("Failed to get the storage.");
})
})
```
```
...
@@ -115,14 +120,14 @@ Use the following APIs to delete a **Storage** instance or data file.
...
@@ -115,14 +120,14 @@ Use the following APIs to delete a **Storage** instance or data file.
```js
```js
promise.then((storage) => {
promise.then((storage) => {
let getPromise = storage.get('startup', 'default')
let getPromise = storage.get('startup', 'default');
getPromise.then((value) => {
getPromise.then((value) => {
console.info("The value of startup is " + value)
console.info("The value of startup is " + value);
}).catch((err) => {
}).catch((err) => {
console.info("Get the value of startup failed with err: " + err)
console.info("Failed to get the value of startup with err: " + err);
})
})
}).catch((err) => {
}).catch((err) => {
console.info("Get the storage failed")
console.info("Failed to get the storage.");
})
})
```
```
...
@@ -142,15 +147,15 @@ Use the following APIs to delete a **Storage** instance or data file.
...
@@ -142,15 +147,15 @@ Use the following APIs to delete a **Storage** instance or data file.
```js
```js
promise.then((storage) => {
promise.then((storage) => {
var observer = function (key) {
var observer = function (key) {
console.info("The key of " + key + " changed.")
console.info("The key of " + key + " changed.");
}
}
storage.on('change', observer)
storage.on('change', observer);
storage.putSync('startup', 'auto') // Modify data in the Storage instance.
storage.putSync('startup', 'auto'); // Modify data in the Storage instance.
storage.flushSync() // Trigger the StorageObserver callback.
storage.flushSync(); // Trigger the StorageObserver callback.
storage.off(...change..., observer) // Unsubscribe from the data changes.
storage.off('change', observer); // Unsubscribe from the data changes.
}).catch((err) => {
}).catch((err) => {
console.info("Get the storage failed")
console.info("Failed to get the storage.");
})
})
```
```
...
@@ -160,11 +165,11 @@ Use the following APIs to delete a **Storage** instance or data file.
...
@@ -160,11 +165,11 @@ Use the following APIs to delete a **Storage** instance or data file.
Use the **deleteStorage** method to delete the **Storage** singleton of the specified file from the memory, and delete the specified file, its backup file, and damaged files. After the specified files are deleted, the application cannot use that instance to perform any data operation. Otherwise, data inconsistency will occur. The deleted data and files cannot be restored.
Use the **deleteStorage** method to delete the **Storage** singleton of the specified file from the memory, and delete the specified file, its backup file, and damaged files. After the specified files are deleted, the application cannot use that instance to perform any data operation. Otherwise, data inconsistency will occur. The deleted data and files cannot be restored.
```js
```js
let promise = dataStorage.deleteStorage(path + '/mystore')
let promise = dataStorage.deleteStorage(path + '/mystore');
promise.then(() => {
promise.then(() => {
console.info("Deleted successfully.")
console.info("Succeeded in deleting the storage.");
}).catch((err) => {
}).catch((err) => {
console.info("Deleted failed with err: " + err)
console.info("Failed to deleted the storage with err: " + err);
Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type.
Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type.
> **NOTE**<br/>
> **NOTE**
>
>
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
## Modules to Import
```js
```js
...
@@ -20,10 +19,10 @@ import data_storage from '@ohos.data.storage';
...
@@ -20,10 +19,10 @@ import data_storage from '@ohos.data.storage';
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned if the value of the specified key does not exist. It can be a number, string, or Boolean value.|
| key | string | Yes | Key of the data. It cannot be empty. |
| defValue | [ValueType](#valuetype) | Yes | Default value to be returned if the value of the specified key does not exist. It can be a number, string, or Boolean value. |
**Return value**
**Return value**
| Type| Description|
| -------- | -------- |
| Type | Description |
| ValueType | Value corresponding to the specified key. If the value is null or not in the default value format, the default value is returned.|