Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
858f230d
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
858f230d
编写于
1月 11, 2023
作者:
L
leiiyb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add database upGrade code cases
Signed-off-by:
N
leiiyb
<
leiyanbo@huawei.com
>
上级
cbf9948a
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
89 addition
and
58 deletion
+89
-58
zh-cn/application-dev/database/database-relational-guidelines.md
...pplication-dev/database/database-relational-guidelines.md
+69
-26
zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md
...cation-dev/reference/apis/js-apis-data-relationalStore.md
+20
-32
未找到文件。
zh-cn/application-dev/database/database-relational-guidelines.md
浏览文件 @
858f230d
...
...
@@ -237,7 +237,50 @@
})
```
2. 插入数据。
2. 数据库升级:
```
js
import data_relationalStore from '@ohos.data.relationalStore'
import application_Ability from '@ohos.application.Ability'
const STORE_CONFIG = {
name: "upGrade.db",
securityLevel: data_relationalStore.SecurityLevel.S1
}
// 获取context
let context = null
class MainAbility extends application_Ability {
onWindowStageCreate(windowStage) {
context = this.context
}
}
// 假设当前数据库版本为3
data_relationalStore.getRdbStore(context, STORE_CONFIG, function (err, store) {
// 当数据库创建时,数据库默认版本为0
if (store.version == 0) {
store.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null)
// 设置数据库的版本,入参为大于0的整数
store.version = 3
}
// 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本
if (store.version != 3 && store.version == 1) {
// version = 1:表结构:student (id, age) => version = 2:表结构:student (id, age, score)
store.executeSql("ALTER TABLE student ADD COLUMN score REAL", null)
store.version = 2
}
// 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本
if (store.version != 3 && store.version == 2) {
// version = 2:表结构:student (id, age, score) => version = 3:表结构:student (id, score)
store.executeSql("ALTER TABLE student DROP COLUMN age INTEGER", null)
store.version = 3
}
})
3.
插入数据。
(1) 构造要插入的数据,以ValuesBucket形式存储。
...
...
@@ -266,7 +309,7 @@
}
```
3
. 查询数据。
4
.
查询数据。
(1) 构造用于查询的谓词对象,设置查询条件。
...
...
@@ -291,7 +334,7 @@
})
```
4
. 设置分布式同步表。
5
.
设置分布式同步表。
(1) 权限配置文件中增加以下配置。
...
...
@@ -323,7 +366,7 @@
})
```
5
. 分布式数据同步。
6
.
分布式数据同步。
(1) 构造用于同步分布式表的谓词对象,指定组网内的远程设备。
...
...
@@ -347,7 +390,7 @@
})
```
6
. 分布式数据订阅。
7
.
分布式数据订阅。
(1) 调用分布式数据订阅接口,注册数据库的观察者。
...
...
@@ -369,7 +412,7 @@
}
```
7
. 跨设备查询。
8
. 跨设备查询。
(1) 根据本地表名获取指定远程设备的分布式表名。
...
...
@@ -382,8 +425,7 @@
let resultSet = rdbStore.querySql("SELECT
*
FROM " + tableName)
```
8. 远程查询。
9. 远程查询。
(1) 构造用于查询分布式表的谓词对象,指定组网内的远程分布式表名和设备。
...
...
@@ -408,7 +450,7 @@
})
```
9
. 数据库的备份和恢复。
10
. 数据库的备份和恢复。
(1) 调用数据库的备份接口,备份当前数据库文件。
...
...
@@ -422,6 +464,7 @@
console.info('Backup failed, err: ' + err)
})
```
(2) 调用数据库的恢复接口,从数据库的备份文件恢复数据库文件。
示例代码如下:
...
...
zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md
浏览文件 @
858f230d
...
...
@@ -57,18 +57,15 @@ const STORE_CONFIG = {
name
:
"
RdbTest.db
"
,
securityLevel
:
data_rdb
.
SecurityLevel
.
S1
}
data_rdb
.
getRdbStore
(
context
,
STORE_CONFIG
,
function
(
err
,
rdbS
tore
)
{
data_rdb
.
getRdbStore
(
context
,
STORE_CONFIG
,
function
(
err
,
s
tore
)
{
if
(
err
)
{
console
.
info
(
"
Get RdbStore failed, err:
"
+
err
)
return
}
if
(
rdbStore
.
openStatus
==
data_rdb
.
OpenStatus
.
ON_CREATA
)
{
console
.
log
(
"
RdbStore status is ON_CREATA
"
)
}
else
if
(
rdbStore
.
openStatus
==
data_rdb
.
OpenStatus
.
ON_OPEN
)
{
console
.
log
(
"
RdbStore status is ON_OPEN
"
)
}
else
{
return
}
// 设置数据库版本应为大于0的正整数
store
.
version
=
3
let
version
=
store
.
version
console
.
log
(
"
Get RdbStore version is
"
+
version
)
console
.
log
(
"
Get RdbStore successfully.
"
)
})
```
...
...
@@ -90,18 +87,15 @@ const STORE_CONFIG = {
name
:
"
RdbTest.db
"
,
securityLevel
:
data_rdb
.
SecurityLevel
.
S1
}
data_rdb
.
getRdbStore
(
context
,
STORE_CONFIG
,
function
(
err
,
rdbS
tore
)
{
data_rdb
.
getRdbStore
(
context
,
STORE_CONFIG
,
function
(
err
,
s
tore
)
{
if
(
err
)
{
console
.
info
(
"
Get RdbStore failed, err:
"
+
err
)
return
}
if
(
rdbStore
.
openStatus
==
data_rdb
.
OpenStatus
.
ON_CREATA
)
{
console
.
log
(
"
RdbStore status is ON_CREATA
"
)
}
else
if
(
rdbStore
.
openStatus
==
data_rdb
.
OpenStatus
.
ON_OPEN
)
{
console
.
log
(
"
RdbStore status is ON_OPEN
"
)
}
else
{
return
}
// 设置数据库版本应为大于0的正整数
store
.
version
=
3
let
version
=
store
.
version
console
.
log
(
"
Get RdbStore version is
"
+
version
)
console
.
log
(
"
Get RdbStore successfully.
"
)
})
```
...
...
@@ -151,14 +145,11 @@ const STORE_CONFIG = {
securityLevel
:
data_rdb
.
SecurityLevel
.
S1
}
let
promise
=
data_rdb
.
getRdbStore
(
context
,
STORE_CONFIG
);
promise
.
then
(
async
(
rdbStore
)
=>
{
if
(
rdbStore
.
openStatus
==
data_rdb
.
OpenStatus
.
ON_CREATA
)
{
console
.
log
(
"
RdbStore status is ON_CREATA
"
)
}
else
if
(
rdbStore
.
openStatus
==
data_rdb
.
OpenStatus
.
ON_OPEN
)
{
console
.
log
(
"
RdbStore status is ON_OPEN
"
)
}
else
{
return
}
promise
.
then
(
async
(
store
)
=>
{
// 设置数据库版本应为大于0的正整数
store
.
version
=
3
let
version
=
store
.
version
console
.
log
(
"
Get RdbStore version is
"
+
version
)
console
.
log
(
"
Get RdbStore successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
log
(
"
Get RdbStore failed, err:
"
+
err
)
...
...
@@ -183,14 +174,11 @@ const STORE_CONFIG = {
securityLevel
:
data_rdb
.
SecurityLevel
.
S1
}
let
promise
=
data_rdb
.
getRdbStore
(
context
,
STORE_CONFIG
);
promise
.
then
(
async
(
rdbStore
)
=>
{
if
(
rdbStore
.
openStatus
==
data_rdb
.
OpenStatus
.
ON_CREATA
)
{
console
.
log
(
"
RdbStore status is ON_CREATA
"
)
}
else
if
(
rdbStore
.
openStatus
==
data_rdb
.
OpenStatus
.
ON_OPEN
)
{
console
.
log
(
"
RdbStore status is ON_OPEN
"
)
}
else
{
return
}
promise
.
then
(
async
(
store
)
=>
{
// 设置数据库版本应为大于0的正整数
store
.
version
=
3
let
version
=
store
.
version
console
.
log
(
"
Get RdbStore version is
"
+
version
)
console
.
log
(
"
Get RdbStore successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
log
(
"
Get RdbStore failed, err:
"
+
err
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录