Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
5e4504d2
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
5e4504d2
编写于
8月 08, 2022
作者:
葛
葛亚芳
提交者:
Gitee
8月 08, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update zh-cn/application-dev/database/database-preference-guidelines.md.
Signed-off-by:
N
@ge-yafang
<
geyafang@huawei.com
>
上级
72e94e48
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
21 addition
and
21 deletion
+21
-21
zh-cn/application-dev/database/database-preference-guidelines.md
...pplication-dev/database/database-preference-guidelines.md
+21
-21
未找到文件。
zh-cn/application-dev/database/database-preference-guidelines.md
浏览文件 @
5e4504d2
...
...
@@ -82,14 +82,14 @@
1.
准备工作,导入@ohos.data.preferences以及相关的模块到开发环境。
```
js
import
data_preferences
from
'
@ohos.data.preferences
'
import
data_preferences
from
'
@ohos.data.preferences
'
;
```
2.
获取Preferences实例。
读取指定文件,将数据加载到Preferences实例,用于数据操作。
```
js
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
;
```
3.
存入数据。
...
...
@@ -98,14 +98,14 @@
```
js
promise
.
then
((
preferences
)
=>
{
let
putPromise
=
preferences
.
put
(
'
startup
'
,
'
auto
'
)
let
putPromise
=
preferences
.
put
(
'
startup
'
,
'
auto
'
)
;
putPromise
.
then
(()
=>
{
console
.
info
(
"
Put the value of startup successfully.
"
)
console
.
info
(
"
Succeeded in putting the value of 'startup'.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Put the value of startup failed with err:
"
+
err
)
console
.
info
(
"
Failed to put the value of 'startup'. Cause:
"
+
err
);
})
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the storage failed
"
)
console
.
info
(
"
Failed to get preferences.
"
);
})
```
...
...
@@ -115,14 +115,14 @@
```
js
promise
.
then
((
preferences
)
=>
{
let
getPromise
=
preferences
.
get
(
'
startup
'
,
'
default
'
)
let
getPromise
=
preferences
.
get
(
'
startup
'
,
'
default
'
)
;
getPromise
.
then
((
value
)
=>
{
console
.
info
(
"
The value of
startup is
"
+
value
)
console
.
info
(
"
The value of
'startup' is
"
+
value
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the value of startup failed with err:
"
+
err
)
console
.
info
(
"
Failed to get the value of 'startup'. Cause:
"
+
err
);
})
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the storage failed
"
)})
console
.
info
(
"
Failed to get preferences.
"
)});
```
5.
数据持久化。
...
...
@@ -139,21 +139,21 @@
```
js
var
observer
=
function
(
key
)
{
console
.
info
(
"
The key of
"
+
key
+
"
changed.
"
)
console
.
info
(
"
The key of
"
+
key
+
"
changed.
"
)
;
}
preferences
.
on
(
'
change
'
,
observer
)
preferences
.
on
(
'
change
'
,
observer
)
;
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed with err:
"
+
err
)
return
console
.
info
(
"
Failed to put the value of 'startup'. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Put the value of startup successfully.
"
)
console
.
info
(
"
Succeeded in putting the value of 'startup'.
"
);
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
F
lush to file failed with err:
"
+
err
)
return
console
.
info
(
"
F
ailed to flush. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Flushed to file successfully.
"
)
// observer will be called.
console
.
info
(
"
Succeeded in flushing.
"
);
// observer will be called.
})
})
```
...
...
@@ -163,11 +163,11 @@
使用deletePreferences方法从内存中移除指定文件对应的Preferences单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。删除后,数据及文件将不可恢复。
```
js
let
proDelete
=
data_preferences
.
deletePreferences
(
context
,
'
mystore
'
)
let
proDelete
=
data_preferences
.
deletePreferences
(
context
,
'
mystore
'
)
;
proDelete
.
then
(()
=>
{
console
.
info
(
"
Deleted successfully.
"
)
console
.
info
(
"
Succeeded in deleting.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Deleted failed with err:
"
+
err
)
console
.
info
(
"
Failed to delete. Cause:
"
+
err
);
})
```
## 相关实例
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录