提交 e39714aa 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 a2cecc46
...@@ -37,14 +37,14 @@ The RDB provides APIs for inserting, deleting, updating, and querying data in th ...@@ -37,14 +37,14 @@ The RDB provides APIs for inserting, deleting, updating, and querying data in th
- **Updating Data** - **Updating Data**
Call **update()** to update data based on the passed data and the conditions specified by **RdbPredicates**. If the data is updated, the number of rows of the updated data will be returned; otherwise, **0** will be returned. Call **update()** to pass the new data and specify the update conditions by using **RdbPredicates**. If the data is updated, the number of rows of the updated data will be returned; otherwise, **0** will be returned.
**Table 3** API for updating data **Table 3** API for updating data
| Class | API | Description | | Class | API | Description |
| ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RdbStore | update(values: ValuesBucket, predicates: RdbPredicates): Promise&lt;number&gt; | Updates data based on the specified **RdbPredicates** object. This API uses a promise to return the number of rows updated.<br>- **values**: data to update, which is stored in **ValuesBucket**.<br>- **predicates**: conditions for updating data. | | RdbStore | update(values: ValuesBucket, predicates: RdbPredicates): Promise&lt;number&gt; | Updates data based on the specified **RdbPredicates** object. This API uses a promise to return the number of rows updated.<br>- **values**: data to update, which is stored in **ValuesBucket**.<br>- **predicates**: conditions for updating data.|
- **Deleting Data** - **Deleting Data**
...@@ -55,7 +55,7 @@ The RDB provides APIs for inserting, deleting, updating, and querying data in th ...@@ -55,7 +55,7 @@ The RDB provides APIs for inserting, deleting, updating, and querying data in th
| Class | API | Description | | Class | API | Description |
| ---------- | ---------------------------------------------------------- | ------------------------------------------------------------ | | ---------- | ---------------------------------------------------------- | ------------------------------------------------------------ |
| RdbStore | delete(predicates: RdbPredicates): Promise&lt;number&gt; | Deletes data from the RDB store based on the specified **RdbPredicates** object. This API uses a promise to return the number of rows deleted.<br>- **predicates**: conditions for deleting data. | | RdbStore | delete(predicates: RdbPredicates): Promise&lt;number&gt; | Deletes data from the RDB store based on the specified **RdbPredicates** object. This API uses a promise to return the number of rows deleted.<br>- **predicates**: conditions for deleting data.|
- **Querying Data** - **Querying Data**
...@@ -201,74 +201,79 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -201,74 +201,79 @@ You can obtain the distributed table name for a remote device based on the local
FA model: FA model:
```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;
// Obtain the context. // Obtain the 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
} };
// Assume that the current RDB store version is 3. // Assume that the current RDB store version is 3.
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) { relationalStore.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
store = rdbStore;
// When an RDB store is created, the default version is 0. // When an RDB store is created, the default version is 0.
if (rdbStore.version == 0) { if (store.version == 0) {
rdbStore.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null) store.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null);
// Set the RDB store version. The input parameter must be an integer greater than 0. // Set the RDB store version. The input parameter must be an integer greater than 0.
rdbStore.version = 3 store.version = 3;
} }
// When an app is updated to the current version, the RDB store needs to be updated from version 1 to version 2. // When an app is updated to the current version, the RDB store needs to be updated from version 1 to version 2.
if (rdbStore.version != 3 && rdbStore.version == 1) { if (store.version != 3 && store.version == 1) {
// version = 1: table structure: student (id, age) => version = 2: table structure: student (id, age, score) // version = 1: table structure: student (id, age) => version = 2: table structure: 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;
} }
// When an app is updated to the current version, the RDB store needs to be updated from version 2 to version 3. // When an app is updated to the current version, the RDB store needs to be updated from version 2 to version 3.
if (rdbStore.version != 3 && rdbStore.version == 2) { if (store.version != 3 && store.version == 2) {
// version = 2: table structure: student (id, age, score) => version = 3: table structure: student (id, score) // version = 2: table structure: student (id, age, score) => version = 3: table structure: 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 model: Stage model:
```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) {
var store;
const STORE_CONFIG = { const STORE_CONFIG = {
name: "rdbstore.db", name: "RdbTest.db",
securityLevel: data_rdb.SecurityLevel.S1 securityLevel: relationalStore.SecurityLevel.S1
} };
// Assume that the current RDB store version is 3. // Assume that the current RDB store version is 3.
data_rdb.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) { relationalStore.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) {
store = rdbStore;
// When an RDB store is created, the default version is 0. // When an RDB store is created, the default version is 0.
if (rdbStore.version == 0) { if (store.version == 0) {
rdbStore.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null) store.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, score REAL);", null);
// Set the RDB store version. The input parameter must be an integer greater than 0. // Set the RDB store version. The input parameter must be an integer greater than 0.
rdbStore.version = 3 store.version = 3;
} }
// When an app is updated to the current version, the RDB store needs to be updated from version 1 to version 2. // When an app is updated to the current version, the RDB store needs to be updated from version 1 to version 2.
if (rdbStore.version != 3 && rdbStore.version == 1) { if (store.version != 3 && store.version == 1) {
// version = 1: table structure: student (id, age) => version = 2: table structure: student (id, age, score) // version = 1: table structure: student (id, age) => version = 2: table structure: 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;
} }
// When an app is updated to the current version, the RDB store needs to be updated from version 2 to version 3. // When an app is updated to the current version, the RDB store needs to be updated from version 2 to version 3.
if (rdbStore.version != 3 && rdbStore.version == 2) { if (store.version != 3 && store.version == 2) {
// version = 2: table structure: student (id, age, score) => version = 3: table structure: student (id, score) // version = 2: table structure: student (id, age, score) => version = 3: table structure: 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 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -284,23 +289,24 @@ You can obtain the distributed table name for a remote device based on the local
The sample code is as follows: The sample code is as follows:
```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
// Use a transaction to insert data. // Use a transaction to insert data.
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 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -315,17 +321,17 @@ You can obtain the distributed table name for a remote device based on the local
The sample code is as follows: The sample code is as follows:
```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();
}) })
``` ```
...@@ -351,13 +357,13 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -351,13 +357,13 @@ You can obtain the distributed table name for a remote device based on the local
```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 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -372,16 +378,16 @@ You can obtain the distributed table name for a remote device based on the local
The sample code is as follows: The sample code is as follows:
```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}`);
}) })
``` ```
...@@ -396,14 +402,14 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -396,14 +402,14 @@ You can obtain the distributed table name for a remote device based on the local
```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= ${devices[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 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -416,8 +422,24 @@ You can obtain the distributed table name for a remote device based on the local
The sample code is as follows: The sample code is as follows:
```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) {
// Obtain device IDs.
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. Query data of a remote device. 8. Query data of a remote device.
...@@ -429,19 +451,19 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -429,19 +451,19 @@ You can obtain the distributed table name for a remote device based on the local
The sample code is as follows: The sample code is as follows:
```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 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -452,11 +474,11 @@ You can obtain the distributed table name for a remote device based on the local
The sample code is as follows: The sample code is as follows:
```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,10 +487,10 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -465,10 +487,10 @@ You can obtain the distributed table name for a remote device based on the local
The sample code is as follows: The sample code is as follows:
```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.
先完成此消息的编辑!
想要评论请 注册