提交 858f230d 编写于 作者: L leiiyb

add database upGrade code cases

Signed-off-by: Nleiiyb <leiyanbo@huawei.com>
上级 cbf9948a
......@@ -209,7 +209,7 @@
const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)";
const STORE_CONFIG = { name: "RdbTest.db",
securityLevel: data_rdb.SecurityLevel.S1}
securityLevel: data_rdb.SecurityLevel.S1}
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
rdbStore.executeSql(CREATE_TABLE_TEST)
console.info('create table done.')
......@@ -230,14 +230,57 @@
const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)";
const STORE_CONFIG = { name: "rdbstore.db",
securityLevel: data_rdb.SecurityLevel.S1}
securityLevel: data_rdb.SecurityLevel.S1}
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
rdbStore.executeSql(CREATE_TABLE_TEST)
console.info('create table done.')
})
```
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,16 +425,15 @@
let resultSet = rdbStore.querySql("SELECT * FROM " + tableName)
```
8. 远程查询。
9. 远程查询。
(1) 构造用于查询分布式表的谓词对象,指定组网内的远程分布式表名和设备。
(2) 调用结果集接口,返回查询结果。
示例代码如下:
```js
```js
let rdbPredicate = new data_rdb.RdbPredicates('employee')
predicates.greaterThan("id", 0)
let promiseQuery = rdbStore.remoteQuery('12345678abcde', 'employee', rdbPredicate)
......@@ -406,34 +448,35 @@
}).catch((err) => {
console.info("failed to remoteQuery, err: " + err)
})
```
9. 数据库的备份和恢复。
```
10. 数据库的备份和恢复。
(1) 调用数据库的备份接口,备份当前数据库文件。
示例代码如下:
示例代码如下:
```js
```js
let promiseBackup = rdbStore.backup("dbBackup.db")
promiseBackup.then(() => {
console.info('Backup success.')
console.info('Backup success.')
}).catch((err) => {
console.info('Backup failed, err: ' + err)
console.info('Backup failed, err: ' + err)
})
```
(2) 调用数据库的恢复接口,从数据库的备份文件恢复数据库文件。
```
示例代码如下:
(2) 调用数据库的恢复接口,从数据库的备份文件恢复数据库文件。
```js
示例代码如下:
```js
let promiseRestore = rdbStore.restore("dbBackup.db")
promiseRestore.then(() => {
console.info('Restore success.')
console.info('Restore success.')
}).catch((err) => {
console.info('Restore failed, err: ' + err)
console.info('Restore failed, err: ' + err)
})
```
```
## 相关实例
针对关系型数据库开发,有以下相关实例可供参考:
......
......@@ -57,18 +57,15 @@ const STORE_CONFIG = {
name: "RdbTest.db",
securityLevel: data_rdb.SecurityLevel.S1
}
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, store) {
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, rdbStore) {
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, store) {
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.
先完成此消息的编辑!
想要评论请 注册