提交 d00cc8e3 编写于 作者: L leiiyb

Modify the relationalStore module import name

Signed-off-by: Nleiiyb <leiyanbo@huawei.com>
上级 84040aa6
...@@ -201,76 +201,81 @@ ...@@ -201,76 +201,81 @@
FA模型示例: FA模型示例:
```js ```js
import data_rdb from '@ohos.data.relationalStore' import relationalStore from '@ohos.data.relationalStore'
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
var store;
// 获取context // 获取context
let context = featureAbility.getContext() let context = featureAbility.getContext();
const STORE_CONFIG = { const STORE_CONFIG = {
name: "RdbTest.db", name: "RdbTest.db",
securityLevel: data_rdb.SecurityLevel.S1 securityLevel: relationalStore.SecurityLevel.S1
} };
// 假设当前数据库版本为3 // 假设当前数据库版本为3
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) { relationalStore.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
// 当数据库创建时,数据库默认版本为0 store = rdbStore;
if (rdbStore.version == 0) { // 当数据库创建时,数据库默认版本为0
rdbStore.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null) if (store.version == 0) {
// 设置数据库的版本,入参为大于0的整数 store.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null);
rdbStore.version = 3 // 设置数据库的版本,入参为大于0的整数
} store.version = 3;
}
// 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本 // 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本
if (rdbStore.version != 3 && rdbStore.version == 1) { if (store.version != 3 && store.version == 1) {
// version = 1:表结构:student (id, age) => version = 2:表结构:student (id, age, score) // version = 1:表结构:student (id, age) => version = 2:表结构:student (id, age, score)
rdbStore.executeSql("ALTER TABLE student ADD COLUMN score REAL", null) store.executeSql("ALTER TABLE student ADD COLUMN score REAL", null);
rdbStore.version = 2 store.version = 2;
} }
// 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本 // 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本
if (rdbStore.version != 3 && rdbStore.version == 2) { if (store.version != 3 && store.version == 2) {
// version = 2:表结构:student (id, age, score) => version = 3:表结构:student (id, score) // version = 2:表结构:student (id, age, score) => version = 3:表结构:student (id, score)
rdbStore.executeSql("ALTER TABLE student DROP COLUMN age INTEGER", null) store.executeSql("ALTER TABLE student DROP COLUMN age INTEGER", null);
rdbStore.version = 3 store.version = 3;
} }
}) })
``` ```
Stage模型示例: Stage模型示例:
```ts ```ts
import data_rdb from '@ohos.data.relationalStore' import relationalStore from '@ohos.data.relationalStore'
import UIAbility from '@ohos.app.ability.UIAbility' import UIAbility from '@ohos.app.ability.UIAbility'
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage) {
const STORE_CONFIG = { var store;
name: "rdbstore.db", const STORE_CONFIG = {
securityLevel: data_rdb.SecurityLevel.S1 name: "RdbTest.db",
} securityLevel: relationalStore.SecurityLevel.S1
};
// 假设当前数据库版本为3 // 假设当前数据库版本为3
data_rdb.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) { relationalStore.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) {
// 当数据库创建时,数据库默认版本为0 store = rdbStore;
if (rdbStore.version == 0) { // 当数据库创建时,数据库默认版本为0
rdbStore.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null) if (store.version == 0) {
// 设置数据库的版本,入参为大于0的整数 store.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null);
rdbStore.version = 3 // 设置数据库的版本,入参为大于0的整数
} store.version = 3;
}
// 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本 // 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本
if (rdbStore.version != 3 && rdbStore.version == 1) { if (store.version != 3 && store.version == 1) {
// version = 1:表结构:student (id, age) => version = 2:表结构:student (id, age, score) // version = 1:表结构:student (id, age) => version = 2:表结构:student (id, age, score)
rdbStore.executeSql("ALTER TABLE student ADD COLUMN score REAL", null) store.executeSql("ALTER TABLE student ADD COLUMN score REAL", null);
rdbStore.version = 2 store.version = 2;
} }
// 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本 // 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本
if (rdbStore.version != 3 && rdbStore.version == 2) { if (store.version != 3 && store.version == 2) {
// version = 2:表结构:student (id, age, score) => version = 3:表结构:student (id, score) // version = 2:表结构:student (id, age, score) => version = 3:表结构:student (id, score)
rdbStore.executeSql("ALTER TABLE student DROP COLUMN age INTEGER", null) store.executeSql("ALTER TABLE student DROP COLUMN age INTEGER", null);
rdbStore.version = 3 store.version = 3;
} }
}) })
} }
} }
``` ```
...@@ -284,23 +289,24 @@ ...@@ -284,23 +289,24 @@
示例代码如下: 示例代码如下:
```js ```js
let u8 = new Uint8Array([1, 2, 3]) let u8 = new Uint8Array([1, 2, 3]);
const valueBucket = { "name": "Tom", "age": 18, "salary": 100.5, "blobType": u8 } const valueBucket = { "name": "Tom", "age": 18, "salary": 100.5, "blobType": u8 };
let insertPromise = rdbStore.insert("test", valueBucket) let insertPromise = store.insert("test", valueBucket);
``` ```
```js ```js
//使用事务插入数据 //使用事务插入数据
beginTransaction()
try { try {
let u8 = new Uint8Array([1, 2, 3]) store.beginTransaction();
const valueBucket1 = { "name": "Tom", "age": 18, "salary": 100.5, "blobType": u8 } let u8 = new Uint8Array([1, 2, 3]);
const valueBucket2 = { "name": "Jam", "age": 19, "salary": 200.5, "blobType": u8 } const valueBucket = { "name": "Tom", "age": 18, "salary": 100.5, "blobType": u8 };
let insertPromise1 = rdbStore.insert("test", valueBucket1) let promise = store.insert("test", valueBucket);
let insertPromise2 = rdbStore.insert("test", valueBucket2) promise.then(() => {
commit() store.commit();
} catch (e) { })
rollBack() } catch (err) {
console.error(`Transaction failed, err: ${err}`);
store.rollBack();
} }
``` ```
...@@ -315,17 +321,17 @@ ...@@ -315,17 +321,17 @@
示例代码如下: 示例代码如下:
```js ```js
let predicates = new data_rdb.RdbPredicates("test"); let predicates = new relationalStore.RdbPredicates("test");
predicates.equalTo("name", "Tom") predicates.equalTo("name", "Tom");
let promisequery = rdbStore.query(predicates) let promisequery = store.query(predicates);
promisequery.then((resultSet) => { promisequery.then((resultSet) => {
resultSet.goToFirstRow() resultSet.goToFirstRow();
const id = resultSet.getLong(resultSet.getColumnIndex("id")) const id = resultSet.getLong(resultSet.getColumnIndex("id"));
const name = resultSet.getString(resultSet.getColumnIndex("name")) const name = resultSet.getString(resultSet.getColumnIndex("name"));
const age = resultSet.getLong(resultSet.getColumnIndex("age")) const age = resultSet.getLong(resultSet.getColumnIndex("age"));
const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) const salary = resultSet.getDouble(resultSet.getColumnIndex("salary"));
const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType"));
resultSet.close() resultSet.close();
}) })
``` ```
...@@ -335,9 +341,9 @@ ...@@ -335,9 +341,9 @@
```json ```json
"requestPermissions": "requestPermissions":
{ {
"name": "ohos.permission.DISTRIBUTED_DATASYNC" "name": "ohos.permission.DISTRIBUTED_DATASYNC"
} }
``` ```
(2) 获取应用权限。 (2) 获取应用权限。
...@@ -351,13 +357,13 @@ ...@@ -351,13 +357,13 @@
```js ```js
let context = featureAbility.getContext(); let context = featureAbility.getContext();
context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666, function (result) { 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(() => { promise.then(() => {
console.info("setDistributedTables success.") console.info(`setDistributedTables success.`);
}).catch((err) => { }).catch((err) => {
console.info("setDistributedTables failed.") console.error(`setDistributedTables failed, ${err}`);
}) })
``` ```
...@@ -372,16 +378,16 @@ ...@@ -372,16 +378,16 @@
示例代码如下: 示例代码如下:
```js ```js
let predicate = new data_rdb.RdbPredicates('test') let predicate = new relationalStore.RdbPredicates('test');
predicate.inDevices(['12345678abcde']) predicate.inDevices(['12345678abcde']);
let promise = rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicate) let promise = store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicate);
promise.then((result) => { promise.then((result) => {
console.log('sync done.') console.info(`sync done.`);
for (let i = 0; i < result.length; i++) { for (let i = 0; i < result.length; i++) {
console.log('device=' + result[i][0] + 'status=' + result[i][1]) console.info(`device=${result[i][0]}, status=${result[i][1]}`);
} }
}).catch((err) => { }).catch((err) => {
console.log('sync failed') console.error(`sync failed, err: ${err}`);
}) })
``` ```
...@@ -395,15 +401,15 @@ ...@@ -395,15 +401,15 @@
```js ```js
function storeObserver(devices) { function storeObserver(devices) {
for (let i = 0; i < devices.length; i++) { for (let i = 0; i < devices.length; i++) {
console.log('device=' + device[i] + 'data changed') console.info(`device= ${device[i]} data changed`);
} }
} }
try { try {
rdbStore.on('dataChange', data_rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver) store.on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
} catch (err) { } catch (err) {
console.log('register observer failed') console.error(`register observer failed, err: ${err}`);
} }
``` ```
...@@ -416,8 +422,24 @@ ...@@ -416,8 +422,24 @@
示例代码如下: 示例代码如下:
```js ```js
let tableName = rdbStore.obtainDistributedTableName(deviceId, "test"); import deviceManager from '@ohos.distributedHardware.deviceManager'
let resultSet = rdbStore.querySql("SELECT * FROM " + tableName)
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. 远程查询。 8. 远程查询。
...@@ -429,19 +451,19 @@ ...@@ -429,19 +451,19 @@
示例代码如下: 示例代码如下:
```js ```js
let rdbPredicate = new data_rdb.RdbPredicates('employee') let rdbPredicate = new relationalStore.RdbPredicates('employee');
predicates.greaterThan("id", 0) predicates.greaterThan("id", 0) ;
let promiseQuery = rdbStore.remoteQuery('12345678abcde', 'employee', rdbPredicate) let promiseQuery = store.remoteQuery('12345678abcde', 'employee', rdbPredicate);
promiseQuery.then((resultSet) => { promiseQuery.then((resultSet) => {
while (resultSet.goToNextRow()) { while (resultSet.goToNextRow()) {
let idx = resultSet.getLong(0); let idx = resultSet.getLong(0);
let name = resultSet.getString(1); let name = resultSet.getString(1);
let age = resultSet.getLong(2); let age = resultSet.getLong(2);
console.info(idx + " " + name + " " + age); console.info(`indx: ${idx}, name: ${name}, age: ${age}`);
} }
resultSet.close(); resultSet.close();
}).catch((err) => { }).catch((err) => {
console.info("failed to remoteQuery, err: " + err) console.error(`failed to remoteQuery, err: ${err}`);
}) })
``` ```
...@@ -452,11 +474,11 @@ ...@@ -452,11 +474,11 @@
示例代码如下: 示例代码如下:
```js ```js
let promiseBackup = rdbStore.backup("dbBackup.db") let promiseBackup = store.backup("dbBackup.db");
promiseBackup.then(() => { promiseBackup.then(() => {
console.info('Backup success.') console.info(`Backup success.`);
}).catch((err) => { }).catch((err) => {
console.info('Backup failed, err: ' + err) console.error(`Backup failed, err: ${err}`);
}) })
``` ```
...@@ -465,11 +487,11 @@ ...@@ -465,11 +487,11 @@
示例代码如下: 示例代码如下:
```js ```js
let promiseRestore = rdbStore.restore("dbBackup.db") let promiseRestore = store.restore("dbBackup.db");
promiseRestore.then(() => { promiseRestore.then(() => {
console.info('Restore success.') console.info(`Restore success.`);
}).catch((err) => { }).catch((err) => {
console.info('Restore failed, err: ' + err) console.error(`Restore failed, err: ${err}`);
}) })
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册