未验证 提交 1c0ed2c2 编写于 作者: 葛亚芳 提交者: Gitee

update zh-cn/application-dev/database/database-storage-guidelines.md.

Signed-off-by: N@ge-yafang <geyafang@huawei.com>
Signed-off-by: N葛亚芳 <geyafang@huawei.com>
上级 e306dfa1
...@@ -75,20 +75,22 @@ ...@@ -75,20 +75,22 @@
1. 准备工作,导入@ohos.data.storage以及相关的模块到开发环境。 1. 准备工作,导入@ohos.data.storage以及相关的模块到开发环境。
```js ```js
import dataStorage from '@ohos.data.storage' import dataStorage from '@ohos.data.storage';
import featureAbility from '@ohos.ability.featureAbility' // 用于获取文件存储路径 import featureAbility from '@ohos.ability.featureAbility'; // 用于获取文件存储路径
``` ```
2. 获取Storage实例。 2. 获取Storage实例。
读取指定文件,将数据加载到Storage实例,用于数据操作。 读取指定文件,将数据加载到Storage实例,用于数据操作。
```js ```js
var context = featureAbility.getContext() var path;
context.getFilesDir().then(() => { var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>"); console.info("======================>getFilesDirPromsie====================>");
}); });
let promise = dataStorage.getStorage(path + '/mystore') let promise = dataStorage.getStorage(path + '/mystore');
``` ```
3. 存入数据。 3. 存入数据。
...@@ -97,14 +99,14 @@ ...@@ -97,14 +99,14 @@
```js ```js
promise.then((storage) => { promise.then((storage) => {
let getPromise = storage.put('startup', 'auto') // 保存数据到缓存的storage示例中。 let getPromise = storage.put('startup', 'auto'); // 保存数据到缓存的storage示例中。
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 failed with err: " + err);
}) })
}).catch((err) => { }).catch((err) => {
console.info("Get the storage failed") console.info("Failed to get the storage.");
}) })
``` ```
...@@ -116,12 +118,14 @@ ...@@ -116,12 +118,14 @@
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.")
})
``` ```
5. 数据持久化。 5. 数据持久化。
...@@ -139,15 +143,15 @@ ...@@ -139,15 +143,15 @@
```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') // 修改storage存储数据 storage.putSync('startup', 'auto'); // 修改storage存储数据
storage.flushSync() // 触发订阅者回调方法 storage.flushSync(); // 触发订阅者回调方法
storage.off('change', observer) // 注销数据变化订阅 storage.off('change', observer); // 注销数据变化订阅
}).catch((err) => { }).catch((err) => {
console.info("Get the storage failed") console.info("Failed to get the storage.");
}) })
``` ```
...@@ -156,11 +160,13 @@ ...@@ -156,11 +160,13 @@
使用deleteStorage方法从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。删除后,数据及文件将不可恢复。 使用deleteStorage方法从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。删除后,数据及文件将不可恢复。
```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 delete the storage with err: " + err);
})
``` ```
## 相关实例 ## 相关实例
针对轻量级数据存储开发,有以下相关实例可供参考: 针对轻量级数据存储开发,有以下相关实例可供参考:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册