Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
84076318
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看板
未验证
提交
84076318
编写于
2月 08, 2023
作者:
O
openharmony_ci
提交者:
Gitee
2月 08, 2023
浏览文件
操作
浏览文件
下载
差异文件
!14266 修改relationalStore模块的导入名称
Merge pull request !14266 from LeiiYB/master
上级
1a858c97
d00cc8e3
变更
2
展开全部
隐藏空白更改
内联
并排
Showing
2 changed file
with
678 addition
and
627 deletion
+678
-627
zh-cn/application-dev/database/database-relational-guidelines.md
...pplication-dev/database/database-relational-guidelines.md
+132
-110
zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md
...cation-dev/reference/apis/js-apis-data-relationalStore.md
+546
-517
未找到文件。
zh-cn/application-dev/database/database-relational-guidelines.md
浏览文件 @
84076318
...
...
@@ -201,76 +201,81 @@
FA模型示例:
```js
import
data_rdb
from '@ohos.data.relationalStore'
import
relationalStore
from '@ohos.data.relationalStore'
import featureAbility from '@ohos.ability.featureAbility'
var store;
// 获取context
let context = featureAbility.getContext()
let context = featureAbility.getContext()
;
const STORE_CONFIG = {
name: "RdbTest.db",
securityLevel:
data_rdb
.SecurityLevel.S1
}
securityLevel:
relationalStore
.SecurityLevel.S1
}
;
// 假设当前数据库版本为3
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
// 当数据库创建时,数据库默认版本为0
if (rdbStore.version == 0) {
rdbStore.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null)
// 设置数据库的版本,入参为大于0的整数
rdbStore.version = 3
}
relationalStore.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
store = rdbStore;
// 当数据库创建时,数据库默认版本为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 (rdbStore.version != 3 && rdbS
tore.version == 1) {
// version = 1:表结构:student (id, age) => version = 2:表结构:student (id, age, score)
rdbStore.executeSql("ALTER TABLE student ADD COLUMN score REAL", null)
rdbStore.version = 2
}
// 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本
if (store.version != 3 && s
tore.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 (rdbStore.version != 3 && rdbS
tore.version == 2) {
// version = 2:表结构:student (id, age, score) => version = 3:表结构:student (id, score)
rdbStore.executeSql("ALTER TABLE student DROP COLUMN age INTEGER", null)
rdbStore.version = 3
}
// 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本
if (store.version != 3 && s
tore.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;
}
})
```
Stage模型示例:
```ts
import
data_rdb
from '@ohos.data.relationalStore'
import
relationalStore
from '@ohos.data.relationalStore'
import UIAbility from '@ohos.app.ability.UIAbility'
class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) {
const STORE_CONFIG = {
name: "rdbstore.db",
securityLevel: data_rdb.SecurityLevel.S1
}
var store;
const STORE_CONFIG = {
name: "RdbTest.db",
securityLevel: relationalStore.SecurityLevel.S1
};
// 假设当前数据库版本为3
data_rdb.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) {
// 当数据库创建时,数据库默认版本为0
if (rdbStore.version == 0) {
rdbStore.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null)
// 设置数据库的版本,入参为大于0的整数
rdbStore.version = 3
}
// 假设当前数据库版本为3
relationalStore.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) {
store = rdbStore;
// 当数据库创建时,数据库默认版本为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 (rdbStore.version != 3 && rdbS
tore.version == 1) {
// version = 1:表结构:student (id, age) => version = 2:表结构:student (id, age, score)
rdbStore.executeSql("ALTER TABLE student ADD COLUMN score REAL", null)
rdbStore.version = 2
}
// 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本
if (store.version != 3 && s
tore.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 (rdbStore.version != 3 && rdbS
tore.version == 2) {
// version = 2:表结构:student (id, age, score) => version = 3:表结构:student (id, score)
rdbStore.executeSql("ALTER TABLE student DROP COLUMN age INTEGER", null)
rdbStore.version = 3
}
})
// 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本
if (store.version != 3 && s
tore.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;
}
})
}
}
```
...
...
@@ -284,23 +289,24 @@
示例代码如下:
```
js
let u8 = new Uint8Array([1, 2, 3])
const valueBucket = { "name": "Tom", "age": 18, "salary": 100.5, "blobType": u8 }
let insertPromise =
rdbStore.insert("test", valueBucket)
let u8 = new Uint8Array([1, 2, 3])
;
const valueBucket = { "name": "Tom", "age": 18, "salary": 100.5, "blobType": u8 }
;
let insertPromise =
store.insert("test", valueBucket);
```
```
js
//使用事务插入数据
beginTransaction()
try {
let u8 = new Uint8Array([1, 2, 3])
const valueBucket1 = { "name": "Tom", "age": 18, "salary": 100.5, "blobType": u8 }
const valueBucket2 = { "name": "Jam", "age": 19, "salary": 200.5, "blobType": u8 }
let insertPromise1 = rdbStore.insert("test", valueBucket1)
let insertPromise2 = rdbStore.insert("test", valueBucket2)
commit()
} catch (e) {
rollBack()
store.beginTransaction();
let u8 = new Uint8Array([1, 2, 3]);
const valueBucket = { "name": "Tom", "age": 18, "salary": 100.5, "blobType": u8 };
let promise = store.insert("test", valueBucket);
promise.then(() => {
store.commit();
})
} catch (err) {
console.error(
`Transaction failed, err: ${err}`
);
store.rollBack();
}
```
...
...
@@ -315,17 +321,17 @@
示例代码如下:
```
js
let predicates = new
data_rdb
.RdbPredicates("test");
predicates.equalTo("name", "Tom")
let promisequery =
rdbStore.query(predicates)
let predicates = new
relationalStore
.RdbPredicates("test");
predicates.equalTo("name", "Tom")
;
let promisequery =
store.query(predicates);
promisequery.then((resultSet) => {
resultSet.goToFirstRow()
const id = resultSet.getLong(resultSet.getColumnIndex("id"))
const name = resultSet.getString(resultSet.getColumnIndex("name"))
const age = resultSet.getLong(resultSet.getColumnIndex("age"))
const salary = resultSet.getDouble(resultSet.getColumnIndex("salary"))
const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType"))
resultSet.close()
resultSet.goToFirstRow();
const id = resultSet.getLong(resultSet.getColumnIndex("id"));
const name = resultSet.getString(resultSet.getColumnIndex("name"));
const age = resultSet.getLong(resultSet.getColumnIndex("age"));
const salary = resultSet.getDouble(resultSet.getColumnIndex("salary"));
const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType"));
resultSet.close();
})
```
...
...
@@ -335,9 +341,9 @@
```
json
"requestPermissions":
{
"name": "ohos.permission.DISTRIBUTED_DATASYNC"
}
{
"name": "ohos.permission.DISTRIBUTED_DATASYNC"
}
```
(2) 获取应用权限。
...
...
@@ -351,13 +357,13 @@
```
js
let context = featureAbility.getContext();
context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666, function (result) {
console.info(
`result.requestCode=${result.requestCode}`
)
console.info(
`result.requestCode=${result.requestCode}`
);
})
let promise =
rdbStore.setDistributedTables(["test"])
let promise =
store.setDistributedTables(["test"]);
promise.then(() => {
console.info("setDistributedTables success.")
console.info(
`setDistributedTables success.`
);
}).catch((err) => {
console.info("setDistributedTables failed.")
console.error(
`setDistributedTables failed, ${err}`
);
})
```
...
...
@@ -372,16 +378,16 @@
示例代码如下:
```
js
let predicate = new
data_rdb.RdbPredicates('test')
predicate.inDevices(['12345678abcde'])
let promise =
rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicate)
let predicate = new
relationalStore.RdbPredicates('test');
predicate.inDevices(['12345678abcde'])
;
let promise =
store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicate);
promise.then((result) => {
console.log('sync done.')
for (let i = 0; i < result.length; i++) {
console.log('device=' + result
[
i
][
0
]
+ 'status=' + result
[
i
][
1
]
)
}
console.info(
`sync done.`
);
for (let i = 0; i < result.length; i++) {
console.info(
`device=${result[i][0]}, status=${result[i][1]}`
);
}
}).catch((err) => {
console.log('sync failed')
console.error(
`sync failed, err: ${err}`
);
})
```
...
...
@@ -395,15 +401,15 @@
```
js
function storeObserver(devices) {
for (let i = 0; i < devices.length; i++) {
console.log('device=' + device[i] + 'data changed')
}
for (let i = 0; i < devices.length; i++) {
console.info(
`device= ${device[i]} data changed`
);
}
}
try {
rdbStore.on('dataChange', data_rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver)
store.on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
} catch (err) {
console.log('register observer failed')
console.error(
`register observer failed, err: ${err}`
);
}
```
...
...
@@ -416,8 +422,24 @@
示例代码如下:
```
js
let tableName = rdbStore.obtainDistributedTableName(deviceId, "test");
let resultSet = rdbStore.querySql("SELECT
*
FROM " + tableName)
import deviceManager from '@ohos.distributedHardware.deviceManager'
let deviceIds = [];
deviceManager.createDeviceManager('bundleName', (err, value) => {
if (!err) {
let devManager = value;
if (devManager != null) {
// 获取deviceIds
let devices = devManager.getTrustedDeviceListSync();
for (let i = 0; i < devices.length; i++) {
deviceIds[i] = devices[i].deviceId;
}
}
}
})
let tableName = store.obtainDistributedTableName(deviceIds[0], "test");
let resultSet = store.querySql("SELECT
*
FROM " + tableName);
```
8. 远程查询。
...
...
@@ -429,19 +451,19 @@
示例代码如下:
```
js
let rdbPredicate = new
data_rdb.RdbPredicates('employee')
predicates.greaterThan("id", 0)
let promiseQuery =
rdbStore.remoteQuery('12345678abcde', 'employee', rdbPredicate)
let rdbPredicate = new
relationalStore.RdbPredicates('employee');
predicates.greaterThan("id", 0)
;
let promiseQuery =
store.remoteQuery('12345678abcde', 'employee', rdbPredicate);
promiseQuery.then((resultSet) => {
while (resultSet.goToNextRow()) {
let idx = resultSet.getLong(0);
let name = resultSet.getString(1);
let age = resultSet.getLong(2);
console.info(idx + " " + name + " " + age
);
}
resultSet.close();
while (resultSet.goToNextRow()) {
let idx = resultSet.getLong(0);
let name = resultSet.getString(1);
let age = resultSet.getLong(2);
console.info(
`indx: ${idx}, name: ${name}, age: ${age}`
);
}
resultSet.close();
}).catch((err) => {
console.info("failed to remoteQuery, err: " + err)
console.error(
`failed to remoteQuery, err: ${err}`
);
})
```
...
...
@@ -452,11 +474,11 @@
示例代码如下:
```
js
let promiseBackup =
rdbStore.backup("dbBackup.db")
let promiseBackup =
store.backup("dbBackup.db");
promiseBackup.then(() => {
console.info('Backup success.')
console.info(
`Backup success.`
);
}).catch((err) => {
console.info('Backup failed, err: ' + err)
console.error(
`Backup failed, err: ${err}`
);
})
```
...
...
@@ -465,11 +487,11 @@
示例代码如下:
```
js
let promiseRestore =
rdbStore.restore("dbBackup.db")
let promiseRestore =
store.restore("dbBackup.db");
promiseRestore.then(() => {
console.info('Restore success.')
console.info(
`Restore success.`
);
}).catch((err) => {
console.info('Restore failed, err: ' + err)
console.error(
`Restore failed, err: ${err}`
);
})
```
...
...
zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md
浏览文件 @
84076318
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录