提交 56ec2216 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 4083258f
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
## When to Use ## When to Use
A relational database (RDB) store allows you to operate local data with or without native SQL statements based on SQLite. A relational database (RDB) store allows you to manage local data with or without native SQL statements based on SQLite.
## Available APIs ## Available APIs
...@@ -17,12 +17,12 @@ The following table describes the APIs for creating and deleting an RDB store. ...@@ -17,12 +17,12 @@ The following table describes the APIs for creating and deleting an RDB store.
| API | Description | | API | Description |
| ------------------------------------------------------------ | ------------------------------------------------------------ | | ------------------------------------------------------------ | ------------------------------------------------------------ |
| getRdbStore(context: Context, config: StoreConfig): Promise&lt;RdbStore&gt; | Obtains an **RdbStore** object. This API uses a promise to return the result. You can set parameters for the **RdbStore** object based on service requirements and use **RdbStore** APIs to perform data operations.<br>- **context**: application context.<br>- **config**: configuration of the RDB store.| | getRdbStore(context: Context, config: StoreConfig): Promise&lt;RdbStore&gt; | Obtains an RDB store. This API uses a promise to return the result. You can set parameters for the RDB store based on service requirements and call APIs to perform data operations.<br>- **context**: application context.<br>- **config**: configuration of the RDB store.|
| deleteRdbStore(context: Context, name: string): Promise&lt;void&gt; | Deletes an RDB store. This API uses a promise to return the result.<br>- **context**: application context.<br>- **name**: name of the RDB store to delete.| | deleteRdbStore(context: Context, name: string): Promise&lt;void&gt; | Deletes an RDB store. This API uses a promise to return the result.<br>- **context**: application context.<br>- **name**: name of the RDB store to delete.|
### Managing Data in an RDB Store ### Managing Data in an RDB Store
The RDB provides APIs for inserting, deleting, updating, and querying data in the local RDB store. The RDB provides APIs for inserting, deleting, updating, and querying data in a local RDB store.
- **Inserting Data** - **Inserting Data**
...@@ -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 in 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**
...@@ -180,7 +180,7 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -180,7 +180,7 @@ You can obtain the distributed table name for a remote device based on the local
### Transaction ### Transaction
Table 15 Transaction APIs **Table 15** Transaction APIs
| Class | API | Description | | Class | API | Description |
| -------- | ----------------------- | --------------------------------- | | -------- | ----------------------- | --------------------------------- |
...@@ -201,40 +201,51 @@ Table 15 Transaction APIs ...@@ -201,40 +201,51 @@ Table 15 Transaction APIs
FA model: FA model:
```js ```js
import data_rdb from '@ohos.data.relationalStore' import relationalStore from '@ohos.data.relationalStore'
// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
let context = featureAbility.getContext()
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)"; var store;
const STORE_CONFIG = { name: "RdbTest.db", // Obtain the context.
securityLevel: data_rdb.SecurityLevel.S1} let context = featureAbility.getContext();
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
rdbStore.executeSql(CREATE_TABLE_TEST) const STORE_CONFIG = {
console.info('create table done.') name: "RdbTest.db",
securityLevel: relationalStore.SecurityLevel.S1
};
relationalStore.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
store = rdbStore;
if (err) {
console.error(`Get RdbStore failed, err: ${err}`);
return;
}
console.info(`Get RdbStore successfully.`);
}) })
``` ```
Stage model: Stage model:
```ts ```ts
import data_rdb from '@ohos.data.relationalStore' import relationalStore from '@ohos.data.relationalStore'
// Obtain the context. import UIAbility from '@ohos.app.ability.UIAbility'
import UIAbility from '@ohos.app.ability.UIAbility';
let context = null
class EntryAbility extends UIAbility { class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage) {
context = this.context var store;
const STORE_CONFIG = {
name: "RdbTest.db",
securityLevel: relationalStore.SecurityLevel.S1
};
relationalStore.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) {
store = rdbStore;
if (err) {
console.error(`Get RdbStore failed, err: ${err}`);
return;
}
console.info(`Get RdbStore successfully.`);
})
} }
} }
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}
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
rdbStore.executeSql(CREATE_TABLE_TEST)
console.info('create table done.')
})
``` ```
2. Insert data. 2. Insert data.
...@@ -246,23 +257,24 @@ Table 15 Transaction APIs ...@@ -246,23 +257,24 @@ Table 15 Transaction APIs
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();
} }
``` ```
...@@ -277,17 +289,17 @@ Table 15 Transaction APIs ...@@ -277,17 +289,17 @@ Table 15 Transaction APIs
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();
}) })
``` ```
...@@ -297,9 +309,9 @@ Table 15 Transaction APIs ...@@ -297,9 +309,9 @@ Table 15 Transaction APIs
```json ```json
"requestPermissions": "requestPermissions":
{ {
"name": "ohos.permission.DISTRIBUTED_DATASYNC" "name": "ohos.permission.DISTRIBUTED_DATASYNC"
} }
``` ```
(2) Obtain the required permissions. (2) Obtain the required permissions.
...@@ -313,13 +325,13 @@ Table 15 Transaction APIs ...@@ -313,13 +325,13 @@ Table 15 Transaction APIs
```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}`);
}) })
``` ```
...@@ -334,16 +346,16 @@ Table 15 Transaction APIs ...@@ -334,16 +346,16 @@ Table 15 Transaction APIs
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}`);
}) })
``` ```
...@@ -357,15 +369,15 @@ Table 15 Transaction APIs ...@@ -357,15 +369,15 @@ Table 15 Transaction APIs
```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}`);
} }
``` ```
...@@ -378,59 +390,75 @@ Table 15 Transaction APIs ...@@ -378,59 +390,75 @@ Table 15 Transaction APIs
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.
(1) Construct a predicate object for querying distributed tables, and specify the remote distributed table name and the remote device. (1) Construct a predicate object for querying distributed tables, and specify the remote distributed table name and the remote device.
(2) Call the resultSet() API to obtain the result. (2) Call the resultSet() API to obtain the result.
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}`);
}) })
``` ```
9. Back up and restore an RDB store. 9. Back up and restore an RDB store.
(1) Back up the current RDB store. (1) Back up the current RDB store.
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}`);
}) })
``` ```
(2) Restore the RDB store using the backup file.
The sample code is as follows: (2) Restore the RDB store using the backup file.
```js The sample code is as follows:
let promiseRestore = rdbStore.restore("dbBackup.db")
```js
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}`);
}) })
``` ```
...@@ -15,10 +15,10 @@ The **relationalStore** module provides the following functions: ...@@ -15,10 +15,10 @@ The **relationalStore** module provides the following functions:
## Modules to Import ## Modules to Import
```js ```js
import data_rdb from '@ohos.data.relationalStore'; import relationalStore from '@ohos.data.relationalStore'
``` ```
## data_rdb.getRdbStore ## relationalStore.getRdbStore
getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback&lt;RdbStore&gt;): void getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback&lt;RdbStore&gt;): void
...@@ -48,51 +48,55 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -48,51 +48,55 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
FA model: FA model:
```js ```js
// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
let context = featureAbility.getContext()
// Call getRdbStore. var store;
// Obtain the context.
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
} };
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, RdbStore) {
if (err) { relationalStore.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
console.info("Failed to get RdbStore, err: " + err) store = rdbStore;
return if (err) {
} console.error(`Get RdbStore failed, err: ${err}`);
console.log("Got RdbStore successfully.") return;
}
console.info(`Get RdbStore successfully.`);
}) })
``` ```
Stage model: Stage model:
```ts ```ts
// Obtain the context. import UIAbility from '@ohos.app.ability.UIAbility'
import Ability from '@ohos.application.Ability'
let context class EntryAbility extends UIAbility {
class MainAbility extends Ability{ onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage){ var store;
context = this.context const STORE_CONFIG = {
} name: "RdbTest.db",
} securityLevel: relationalStore.SecurityLevel.S1
};
// Call getRdbStore.
const STORE_CONFIG = { relationalStore.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) {
name: "RdbTest.db", store = rdbStore;
securityLevel: data_rdb.SecurityLevel.S1 if (err) {
console.error(`Get RdbStore failed, err: ${err}`);
return;
}
console.info(`Get RdbStore successfully.`);
})
}
} }
data_rdb.getRdbStore(context, STORE_CONFIG, function (err, RdbStore) {
if (err) {
console.info("Failed to get RdbStore, err: " + err)
return
}
console.log("Got RdbStore successfully.")
})
``` ```
## data_rdb.getRdbStore ## relationalStore.getRdbStore
getRdbStore(context: Context, config: StoreConfig): Promise&lt;RdbStore&gt; getRdbStore(context: Context, config: StoreConfig): Promise&lt;RdbStore&gt;
...@@ -127,49 +131,52 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -127,49 +131,52 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
FA model: FA model:
```js ```js
// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
let context = featureAbility.getContext()
// Call getRdbStore. var store;
// Obtain the context.
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
} };
let promise = data_rdb.getRdbStore(context, STORE_CONFIG);
let promise = relationalStore.getRdbStore(context, STORE_CONFIG);
promise.then(async (rdbStore) => { promise.then(async (rdbStore) => {
console.log("Got RdbStore successfully.") store = rdbStore;
console.info(`Get RdbStore successfully.`);
}).catch((err) => { }).catch((err) => {
console.log("Failed to get RdbStore, err: " + err) console.error(`Get RdbStore failed, err: ${err}`);
}) })
``` ```
Stage model: Stage model:
```ts ```ts
// Obtain the context. import UIAbility from '@ohos.app.ability.UIAbility'
import Ability from '@ohos.application.Ability'
let context class EntryAbility extends UIAbility {
class MainAbility extends Ability{ onWindowStageCreate(windowStage) {
onWindowStageCreate(windowStage){ var store;
context = this.context const STORE_CONFIG = {
} name: "RdbTest.db",
} securityLevel: relationalStore.SecurityLevel.S1
};
// Call getRdbStore.
const STORE_CONFIG = { let promise = relationalStore.getRdbStore(this.context, STORE_CONFIG);
name: "RdbTest.db", promise.then(async (rdbStore) => {
securityLevel: data_rdb.SecurityLevel.S1 store = rdbStore;
console.info(`Get RdbStore successfully.`)
}).catch((err) => {
console.error(`Get RdbStore failed, err: ${err}`);
})
}
} }
let promise = data_rdb.getRdbStore(context, STORE_CONFIG);
promise.then(async (rdbStore) => {
console.log("Got RdbStore successfully.")
}).catch((err) => {
console.log("Failed to get RdbStore, err: " + err)
})
``` ```
## data_rdb.deleteRdbStore ## relationalStore.deleteRdbStore
deleteRdbStore(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void deleteRdbStore(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void
...@@ -198,43 +205,39 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -198,43 +205,39 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
FA model: FA model:
```js ```js
// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
// Obtain the context.
let context = featureAbility.getContext() let context = featureAbility.getContext()
// Call deleteRdbStore. relationalStore.deleteRdbStore(context, "RdbTest.db", function (err) {
data_rdb.deleteRdbStore(context, "RdbTest.db", function (err) { if (err) {
if (err) { console.error(`Delete RdbStore failed, err: ${err}`);
console.info("Failed to delete RdbStore, err: " + err) return;
return }
} console.info(`Delete RdbStore successfully.`);
console.log("Deleted RdbStore successfully.")
}) })
``` ```
Stage model: Stage model:
```ts ```ts
// Obtain the context. import UIAbility from '@ohos.app.ability.UIAbility'
import Ability from '@ohos.application.Ability'
let context
class MainAbility extends Ability{
onWindowStageCreate(windowStage){
context = this.context
}
}
// Call deleteRdbStore. class EntryAbility extends UIAbility {
data_rdb.deleteRdbStore(context, "RdbTest.db", function (err) { onWindowStageCreate(windowStage){
if (err) { relationalStore.deleteRdbStore(this.context, "RdbTest.db", function (err) {
console.info("Failed to delete RdbStore, err: " + err) if (err) {
return console.error(`Delete RdbStore failed, err: ${err}`);
} return;
console.log("Deleted RdbStore successfully.") }
}) console.info(`Delete RdbStore successfully.`);
})
}
}
``` ```
## data_rdb.deleteRdbStore ## relationalStore.deleteRdbStore
deleteRdbStore(context: Context, name: string): Promise&lt;void&gt; deleteRdbStore(context: Context, name: string): Promise&lt;void&gt;
...@@ -268,38 +271,34 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -268,38 +271,34 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
FA model: FA model:
```js ```js
// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
let context = featureAbility.getContext()
// Call deleteRdbStore. // Obtain the context.
let promise = data_rdb.deleteRdbStore(context, "RdbTest.db") let context = featureAbility.getContext();
let promise = relationalStore.deleteRdbStore(context, "RdbTest.db");
promise.then(()=>{ promise.then(()=>{
console.log("Deleted RdbStore successfully.") console.info(`Delete RdbStore successfully.`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to delete RdbStore, err: " + err) console.error(`Delete RdbStore failed, err: ${err}`);
}) })
``` ```
Stage model: Stage model:
```ts ```ts
// Obtain the context. import UIAbility from '@ohos.app.ability.UIAbility'
import Ability from '@ohos.application.Ability'
let context class EntryAbility extends UIAbility {
class MainAbility extends Ability{ onWindowStageCreate(windowStage){
onWindowStageCreate(windowStage){ let promise = relationalStore.deleteRdbStore(this.context, "RdbTest.db");
context = this.context promise.then(()=>{
} console.info(`Delete RdbStore successfully.`);
}).catch((err) => {
console.error(`Delete RdbStore failed, err: ${err}`);
})
}
} }
// Call deleteRdbStore.
let promise = data_rdb.deleteRdbStore(context, "RdbTest.db")
promise.then(()=>{
console.log("Deleted RdbStore successfully.")
}).catch((err) => {
console.info("Failed to delete RdbStore, err: " + err)
})
``` ```
## StoreConfig ## StoreConfig
...@@ -391,7 +390,7 @@ A constructor used to create an **RdbPredicates** object. ...@@ -391,7 +390,7 @@ A constructor used to create an **RdbPredicates** object.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
``` ```
### inDevices ### inDevices
...@@ -418,8 +417,8 @@ Sets an **RdbPredicates** to specify the remote devices to connect on the networ ...@@ -418,8 +417,8 @@ Sets an **RdbPredicates** to specify the remote devices to connect on the networ
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.inDevices(['12345678abcde']) predicates.inDevices(['12345678abcde']);
``` ```
### inAllDevices ### inAllDevices
...@@ -440,8 +439,8 @@ Sets an **RdbPredicates** to specify all remote devices on the network to connec ...@@ -440,8 +439,8 @@ Sets an **RdbPredicates** to specify all remote devices on the network to connec
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.inAllDevices() predicates.inAllDevices();
``` ```
### equalTo ### equalTo
...@@ -469,8 +468,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va ...@@ -469,8 +468,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "lisi") predicates.equalTo("NAME", "lisi");
``` ```
...@@ -499,8 +498,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va ...@@ -499,8 +498,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.notEqualTo("NAME", "lisi") predicates.notEqualTo("NAME", "lisi");
``` ```
...@@ -522,7 +521,7 @@ Adds a left parenthesis to the **RdbPredicates**. ...@@ -522,7 +521,7 @@ Adds a left parenthesis to the **RdbPredicates**.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "lisi") predicates.equalTo("NAME", "lisi")
.beginWrap() .beginWrap()
.equalTo("AGE", 18) .equalTo("AGE", 18)
...@@ -548,7 +547,7 @@ Adds a right parenthesis to the **RdbPredicates**. ...@@ -548,7 +547,7 @@ Adds a right parenthesis to the **RdbPredicates**.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "lisi") predicates.equalTo("NAME", "lisi")
.beginWrap() .beginWrap()
.equalTo("AGE", 18) .equalTo("AGE", 18)
...@@ -574,7 +573,7 @@ Adds the OR condition to the **RdbPredicates**. ...@@ -574,7 +573,7 @@ Adds the OR condition to the **RdbPredicates**.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa") predicates.equalTo("NAME", "Lisa")
.or() .or()
.equalTo("NAME", "Rose") .equalTo("NAME", "Rose")
...@@ -597,7 +596,7 @@ Adds the AND condition to the **RdbPredicates**. ...@@ -597,7 +596,7 @@ Adds the AND condition to the **RdbPredicates**.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa") predicates.equalTo("NAME", "Lisa")
.and() .and()
.equalTo("SALARY", 200.5) .equalTo("SALARY", 200.5)
...@@ -627,8 +626,8 @@ Sets an **RdbPredicates** to match a string containing the specified value. ...@@ -627,8 +626,8 @@ Sets an **RdbPredicates** to match a string containing the specified value.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.contains("NAME", "os") predicates.contains("NAME", "os");
``` ```
### beginsWith ### beginsWith
...@@ -655,8 +654,8 @@ Sets an **RdbPredicates** to match a string that starts with the specified value ...@@ -655,8 +654,8 @@ Sets an **RdbPredicates** to match a string that starts with the specified value
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.beginsWith("NAME", "os") predicates.beginsWith("NAME", "os");
``` ```
### endsWith ### endsWith
...@@ -683,8 +682,8 @@ Sets an **RdbPredicates** to match a string that ends with the specified value. ...@@ -683,8 +682,8 @@ Sets an **RdbPredicates** to match a string that ends with the specified value.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.endsWith("NAME", "se") predicates.endsWith("NAME", "se");
``` ```
### isNull ### isNull
...@@ -710,8 +709,8 @@ Sets an **RdbPredicates** to match the field whose value is null. ...@@ -710,8 +709,8 @@ Sets an **RdbPredicates** to match the field whose value is null.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.isNull("NAME") predicates.isNull("NAME");
``` ```
### isNotNull ### isNotNull
...@@ -737,8 +736,8 @@ Sets an **RdbPredicates** to match the field whose value is not null. ...@@ -737,8 +736,8 @@ Sets an **RdbPredicates** to match the field whose value is not null.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.isNotNull("NAME") predicates.isNotNull("NAME");
``` ```
### like ### like
...@@ -765,8 +764,8 @@ Sets an **RdbPredicates** to match a string that is similar to the specified val ...@@ -765,8 +764,8 @@ Sets an **RdbPredicates** to match a string that is similar to the specified val
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.like("NAME", "%os%") predicates.like("NAME", "%os%");
``` ```
### glob ### glob
...@@ -793,8 +792,8 @@ Sets an **RdbPredicates** to match the specified string. ...@@ -793,8 +792,8 @@ Sets an **RdbPredicates** to match the specified string.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.glob("NAME", "?h*g") predicates.glob("NAME", "?h*g");
``` ```
### between ### between
...@@ -822,8 +821,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va ...@@ -822,8 +821,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.between("AGE", 10, 50) predicates.between("AGE", 10, 50);
``` ```
### notBetween ### notBetween
...@@ -851,8 +850,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va ...@@ -851,8 +850,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.notBetween("AGE", 10, 50) predicates.notBetween("AGE", 10, 50);
``` ```
### greaterThan ### greaterThan
...@@ -879,8 +878,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va ...@@ -879,8 +878,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.greaterThan("AGE", 18) predicates.greaterThan("AGE", 18);
``` ```
### lessThan ### lessThan
...@@ -907,8 +906,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va ...@@ -907,8 +906,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.lessThan("AGE", 20) predicates.lessThan("AGE", 20);
``` ```
### greaterThanOrEqualTo ### greaterThanOrEqualTo
...@@ -935,8 +934,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va ...@@ -935,8 +934,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.greaterThanOrEqualTo("AGE", 18) predicates.greaterThanOrEqualTo("AGE", 18);
``` ```
### lessThanOrEqualTo ### lessThanOrEqualTo
...@@ -963,8 +962,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va ...@@ -963,8 +962,8 @@ Sets an **RdbPredicates** to match the field with data type **ValueType** and va
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.lessThanOrEqualTo("AGE", 20) predicates.lessThanOrEqualTo("AGE", 20);
``` ```
### orderByAsc ### orderByAsc
...@@ -990,8 +989,8 @@ Sets an **RdbPredicates** to match the column with values sorted in ascending or ...@@ -990,8 +989,8 @@ Sets an **RdbPredicates** to match the column with values sorted in ascending or
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.orderByAsc("NAME") predicates.orderByAsc("NAME");
``` ```
### orderByDesc ### orderByDesc
...@@ -1017,8 +1016,8 @@ Sets an **RdbPredicates** to match the column with values sorted in descending o ...@@ -1017,8 +1016,8 @@ Sets an **RdbPredicates** to match the column with values sorted in descending o
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.orderByDesc("AGE") predicates.orderByDesc("AGE");
``` ```
### distinct ### distinct
...@@ -1038,8 +1037,8 @@ Sets an **RdbPredicates** to filter out duplicate records. ...@@ -1038,8 +1037,8 @@ Sets an **RdbPredicates** to filter out duplicate records.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose").distinct() predicates.equalTo("NAME", "Rose").distinct();
``` ```
### limitAs ### limitAs
...@@ -1065,8 +1064,8 @@ Sets an **RdbPredicates** to specify the maximum number of records. ...@@ -1065,8 +1064,8 @@ Sets an **RdbPredicates** to specify the maximum number of records.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose").limitAs(3) predicates.equalTo("NAME", "Rose").limitAs(3);
``` ```
### offsetAs ### offsetAs
...@@ -1092,8 +1091,8 @@ Sets an **RdbPredicates** to specify the start position of the returned result. ...@@ -1092,8 +1091,8 @@ Sets an **RdbPredicates** to specify the start position of the returned result.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose").offsetAs(3) predicates.equalTo("NAME", "Rose").offsetAs(3);
``` ```
### groupBy ### groupBy
...@@ -1119,8 +1118,8 @@ Sets an **RdbPredicates** to group rows that have the same value into summary ro ...@@ -1119,8 +1118,8 @@ Sets an **RdbPredicates** to group rows that have the same value into summary ro
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.groupBy(["AGE", "NAME"]) predicates.groupBy(["AGE", "NAME"]);
``` ```
### indexedBy ### indexedBy
...@@ -1147,8 +1146,8 @@ Sets an **RdbPredicates** object to specify the index column. ...@@ -1147,8 +1146,8 @@ Sets an **RdbPredicates** object to specify the index column.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.indexedBy("SALARY_INDEX") predicates.indexedBy("SALARY_INDEX");
``` ```
### in ### in
...@@ -1175,8 +1174,8 @@ Sets an **RdbPredicates** to match the field with data type **Array&#60;ValueTyp ...@@ -1175,8 +1174,8 @@ Sets an **RdbPredicates** to match the field with data type **Array&#60;ValueTyp
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.in("AGE", [18, 20]) predicates.in("AGE", [18, 20]);
``` ```
### notIn ### notIn
...@@ -1203,8 +1202,8 @@ Sets an **RdbPredicates** to match the field with data type **Array&#60;ValueTyp ...@@ -1203,8 +1202,8 @@ Sets an **RdbPredicates** to match the field with data type **Array&#60;ValueTyp
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.notIn("NAME", ["Lisa", "Rose"]) predicates.notIn("NAME", ["Lisa", "Rose"]);
``` ```
## RdbStore ## RdbStore
...@@ -1233,19 +1232,21 @@ Inserts a row of data into a table. This API uses an asynchronous callback to re ...@@ -1233,19 +1232,21 @@ Inserts a row of data into a table. This API uses an asynchronous callback to re
```js ```js
const valueBucket = { const valueBucket = {
"NAME": "Lisa", "NAME": "Lisa",
"AGE": 18, "AGE": 18,
"SALARY": 100.5, "SALARY": 100.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]), "CODES": new Uint8Array([1, 2, 3, 4, 5]),
} };
rdbStore.insert("EMPLOYEE", valueBucket, function (status, rowId) { store.insert("EMPLOYEE", valueBucket, function (err, rowId) {
if (status) { if (err) {
console.log("Failed to insert data"); console.error(`Insert is failed, err: ${err}`);
return; return;
} }
console.log("Inserted data successfully, rowId = " + rowId); console.info(`Insert is successful, rowId = ${rowId}`);
}) })
``` ```
### insert ### insert
insert(table: string, values: ValuesBucket):Promise&lt;number&gt; insert(table: string, values: ValuesBucket):Promise&lt;number&gt;
...@@ -1271,19 +1272,20 @@ Inserts a row of data into a table. This API uses a promise to return the result ...@@ -1271,19 +1272,20 @@ Inserts a row of data into a table. This API uses a promise to return the result
```js ```js
const valueBucket = { const valueBucket = {
"NAME": "Lisa", "NAME": "Lisa",
"AGE": 18, "AGE": 18,
"SALARY": 100.5, "SALARY": 100.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]), "CODES": new Uint8Array([1, 2, 3, 4, 5]),
} };
let promise = rdbStore.insert("EMPLOYEE", valueBucket) let promise = store.insert("EMPLOYEE", valueBucket);
promise.then((rowId) => { promise.then((rowId) => {
console.log("Inserted data successfully, rowId = " + rowId); console.info(`Insert is successful, rowId = ${rowId}`);
}).catch((status) => { }).catch((err) => {
console.log("Failed to insert data"); console.error(`Insert is failed, err: ${err}`);
}) })
``` ```
### batchInsert ### batchInsert
batchInsert(table: string, values: Array&lt;ValuesBucket&gt;, callback: AsyncCallback&lt;number&gt;):void batchInsert(table: string, values: Array&lt;ValuesBucket&gt;, callback: AsyncCallback&lt;number&gt;):void
...@@ -1304,31 +1306,31 @@ Batch inserts data into a table. This API uses an asynchronous callback to retur ...@@ -1304,31 +1306,31 @@ Batch inserts data into a table. This API uses an asynchronous callback to retur
```js ```js
const valueBucket1 = { const valueBucket1 = {
"NAME": "Lisa", "NAME": "Lisa",
"AGE": 18, "AGE": 18,
"SALARY": 100.5, "SALARY": 100.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]) "CODES": new Uint8Array([1, 2, 3, 4, 5])
} };
const valueBucket2 = { const valueBucket2 = {
"NAME": "Jack", "NAME": "Jack",
"AGE": 19, "AGE": 19,
"SALARY": 101.5, "SALARY": 101.5,
"CODES": new Uint8Array([6, 7, 8, 9, 10]) "CODES": new Uint8Array([6, 7, 8, 9, 10])
} };
const valueBucket3 = { const valueBucket3 = {
"NAME": "Tom", "NAME": "Tom",
"AGE": 20, "AGE": 20,
"SALARY": 102.5, "SALARY": 102.5,
"CODES": new Uint8Array([11, 12, 13, 14, 15]) "CODES": new Uint8Array([11, 12, 13, 14, 15])
} };
let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3);
rdbStore.batchInsert("EMPLOYEE", valueBuckets, function(status, insertNum) { store.batchInsert("EMPLOYEE", valueBuckets, function(err, insertNum) {
if (status) { if (err) {
console.log("batchInsert is failed, status = " + status); console.error(`batchInsert is failed, err: ${err}`);
return; return;
} }
console.log("batchInsert is successful, the number of values that were inserted = " + insertNum); console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`);
}) })
``` ```
...@@ -1357,30 +1359,30 @@ Batch inserts data into a table. This API uses a promise to return the result. ...@@ -1357,30 +1359,30 @@ Batch inserts data into a table. This API uses a promise to return the result.
```js ```js
const valueBucket1 = { const valueBucket1 = {
"NAME": "Lisa", "NAME": "Lisa",
"AGE": 18, "AGE": 18,
"SALARY": 100.5, "SALARY": 100.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]) "CODES": new Uint8Array([1, 2, 3, 4, 5])
} };
const valueBucket2 = { const valueBucket2 = {
"NAME": "Jack", "NAME": "Jack",
"AGE": 19, "AGE": 19,
"SALARY": 101.5, "SALARY": 101.5,
"CODES": new Uint8Array([6, 7, 8, 9, 10]) "CODES": new Uint8Array([6, 7, 8, 9, 10])
} };
const valueBucket3 = { const valueBucket3 = {
"NAME": "Tom", "NAME": "Tom",
"AGE": 20, "AGE": 20,
"SALARY": 102.5, "SALARY": 102.5,
"CODES": new Uint8Array([11, 12, 13, 14, 15]) "CODES": new Uint8Array([11, 12, 13, 14, 15])
} };
let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3);
let promise = rdbStore.batchInsert("EMPLOYEE", valueBuckets); let promise = store.batchInsert("EMPLOYEE", valueBuckets);
promise.then((insertNum) => { promise.then((insertNum) => {
console.log("batchInsert is successful, the number of values that were inserted = " + insertNum); console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`);
}).catch((status) => { }).catch((err) => {
console.log("batchInsert is failed, status = " + status); console.error(`batchInsert is failed, err: ${err}`);
}) })
``` ```
...@@ -1404,22 +1406,23 @@ Updates data in the RDB store based on the specified **RdbPredicates** object. T ...@@ -1404,22 +1406,23 @@ Updates data in the RDB store based on the specified **RdbPredicates** object. T
```js ```js
const valueBucket = { const valueBucket = {
"NAME": "Rose", "NAME": "Rose",
"AGE": 22, "AGE": 22,
"SALARY": 200.5, "SALARY": 200.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]), "CODES": new Uint8Array([1, 2, 3, 4, 5]),
} };
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa") predicates.equalTo("NAME", "Lisa");
rdbStore.update(valueBucket, predicates, function (err, rows) { store.update(valueBucket, predicates, function (err, rows) {
if (err) { if (err) {
console.info("Failed to update data, err: " + err) console.error(`Update failed, err: ${err}`);
return return;
} }
console.log("Updated row count: " + rows) console.info(`Updated row count: ${rows}`);
}) })
``` ```
### update ### update
update(values: ValuesBucket, predicates: RdbPredicates):Promise&lt;number&gt; update(values: ValuesBucket, predicates: RdbPredicates):Promise&lt;number&gt;
...@@ -1445,21 +1448,22 @@ Updates data based on the specified **RdbPredicates** object. This API uses a pr ...@@ -1445,21 +1448,22 @@ Updates data based on the specified **RdbPredicates** object. This API uses a pr
```js ```js
const valueBucket = { const valueBucket = {
"NAME": "Rose", "NAME": "Rose",
"AGE": 22, "AGE": 22,
"SALARY": 200.5, "SALARY": 200.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]), "CODES": new Uint8Array([1, 2, 3, 4, 5]),
} };
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa") predicates.equalTo("NAME", "Lisa");
let promise = rdbStore.update(valueBucket, predicates) let promise = store.update(valueBucket, predicates);
promise.then(async (rows) => { promise.then(async (rows) => {
console.log("Updated row count: " + rows) console.info(`Updated row count: ${rows}`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to update data, err: " + err) console.error(`Update failed, err: ${err}`);
}) })
``` ```
### update ### update
update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback&lt;number&gt;):void update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback&lt;number&gt;):void
...@@ -1488,15 +1492,15 @@ const valueBucket = { ...@@ -1488,15 +1492,15 @@ const valueBucket = {
"AGE": 22, "AGE": 22,
"SALARY": 200.5, "SALARY": 200.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]), "CODES": new Uint8Array([1, 2, 3, 4, 5]),
} };
let predicates = new dataSharePredicates.DataSharePredicates() let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa") predicates.equalTo("NAME", "Lisa");
rdbStore.update("EMPLOYEE", valueBucket, predicates, function (err, rows) { store.update("EMPLOYEE", valueBucket, predicates, function (err, rows) {
if (err) { if (err) {
console.info("Failed to update data, err: " + err) console.error(`Update failed, err: ${err}`);
return return;
} }
console.log("Updated row count: " + rows) console.info(`Updated row count: ${rows}`);
}) })
``` ```
...@@ -1529,18 +1533,18 @@ Updates data based on the specified **DataSharePredicates** object. This API use ...@@ -1529,18 +1533,18 @@ Updates data based on the specified **DataSharePredicates** object. This API use
```js ```js
import dataSharePredicates from '@ohos.data.dataSharePredicates' import dataSharePredicates from '@ohos.data.dataSharePredicates'
const valueBucket = { const valueBucket = {
"NAME": "Rose", "NAME": "Rose",
"AGE": 22, "AGE": 22,
"SALARY": 200.5, "SALARY": 200.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]), "CODES": new Uint8Array([1, 2, 3, 4, 5]),
} };
let predicates = new dataSharePredicates.DataSharePredicates() let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa") predicates.equalTo("NAME", "Lisa");
let promise = rdbStore.update("EMPLOYEE", valueBucket, predicates) let promise = store.update("EMPLOYEE", valueBucket, predicates);
promise.then(async (rows) => { promise.then(async (rows) => {
console.log("Updated row count: " + rows) console.info(`Updated row count: ${rows}`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to update data, err: " + err) console.error(`Updated failed, err: ${err}`);
}) })
``` ```
...@@ -1562,14 +1566,14 @@ Deletes data from the RDB store based on the specified **RdbPredicates** object. ...@@ -1562,14 +1566,14 @@ Deletes data from the RDB store based on the specified **RdbPredicates** object.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa") predicates.equalTo("NAME", "Lisa");
rdbStore.delete(predicates, function (err, rows) { store.delete(predicates, function (err, rows) {
if (err) { if (err) {
console.info("Failed to delete data, err: " + err) console.error(`Delete failed, err: ${err}`);
return return;
} }
console.log("Deleted rows: " + rows) console.info(`Delete rows: ${rows}`);
}) })
``` ```
...@@ -1596,13 +1600,13 @@ Deletes data from the RDB store based on the specified **RdbPredicates** object. ...@@ -1596,13 +1600,13 @@ Deletes data from the RDB store based on the specified **RdbPredicates** object.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa") predicates.equalTo("NAME", "Lisa");
let promise = rdbStore.delete(predicates) let promise = store.delete(predicates);
promise.then((rows) => { promise.then((rows) => {
console.log("Deleted rows: " + rows) console.info(`Delete rows: ${rows}`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to delete data, err: " + err) console.error(`Delete failed, err: ${err}`);
}) })
``` ```
...@@ -1628,14 +1632,14 @@ Deletes data from the RDB store based on the specified **DataSharePredicates** o ...@@ -1628,14 +1632,14 @@ Deletes data from the RDB store based on the specified **DataSharePredicates** o
```js ```js
import dataSharePredicates from '@ohos.data.dataSharePredicates' import dataSharePredicates from '@ohos.data.dataSharePredicates'
let predicates = new dataSharePredicates.DataSharePredicates() let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa") predicates.equalTo("NAME", "Lisa");
rdbStore.delete("EMPLOYEE", predicates, function (err, rows) { store.delete("EMPLOYEE", predicates, function (err, rows) {
if (err) { if (err) {
console.info("Failed to delete data, err: " + err) console.error(`Delete failed, err: ${err}`);
return return;
} }
console.log("Deleted rows: " + rows) console.info(`Delete rows: ${rows}`);
}) })
``` ```
...@@ -1666,13 +1670,13 @@ Deletes data from the RDB store based on the specified **DataSharePredicates** o ...@@ -1666,13 +1670,13 @@ Deletes data from the RDB store based on the specified **DataSharePredicates** o
```js ```js
import dataSharePredicates from '@ohos.data.dataSharePredicates' import dataSharePredicates from '@ohos.data.dataSharePredicates'
let predicates = new dataSharePredicates.DataSharePredicates() let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa") predicates.equalTo("NAME", "Lisa");
let promise = rdbStore.delete("EMPLOYEE", predicates) let promise = store.delete("EMPLOYEE", predicates);
promise.then((rows) => { promise.then((rows) => {
console.log("Deleted rows: " + rows) console.info(`Delete rows: ${rows}`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to delete data, err: " + err) console.error(`Delete failed, err: ${err}`);
}) })
``` ```
...@@ -1695,15 +1699,15 @@ Queries data from the RDB store based on specified conditions. This API uses an ...@@ -1695,15 +1699,15 @@ Queries data from the RDB store based on specified conditions. This API uses an
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose") predicates.equalTo("NAME", "Rose");
rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) { store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
if (err) { if (err) {
console.info("Failed to query data, err: " + err) console.error(`Query failed, err: ${err}`);
return return;
} }
console.log("ResultSet column names: " + resultSet.columnNames) console.info(`ResultSet column names: ${resultSet.columnNames}`);
console.log("ResultSet column count: " + resultSet.columnCount) console.info(`ResultSet column count: ${resultSet.columnCount}`);
}) })
``` ```
...@@ -1731,14 +1735,14 @@ Queries data from the RDB store based on specified conditions. This API uses a p ...@@ -1731,14 +1735,14 @@ Queries data from the RDB store based on specified conditions. This API uses a p
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE") let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose") predicates.equalTo("NAME", "Rose");
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]) let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => { promise.then((resultSet) => {
console.log("ResultSet column names: " + resultSet.columnNames) console.info(`ResultSet column names: ${resultSet.columnNames}`);
console.log("ResultSet column count: " + resultSet.columnCount) console.info(`ResultSet column count: ${resultSet.columnCount}`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to query data, err: " + err) console.error(`Query failed, err: ${err}`);
}) })
``` ```
...@@ -1765,15 +1769,15 @@ Queries data from the RDB store based on specified conditions. This API uses an ...@@ -1765,15 +1769,15 @@ Queries data from the RDB store based on specified conditions. This API uses an
```js ```js
import dataSharePredicates from '@ohos.data.dataSharePredicates' import dataSharePredicates from '@ohos.data.dataSharePredicates'
let predicates = new dataSharePredicates.DataSharePredicates() let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Rose") predicates.equalTo("NAME", "Rose");
rdbStore.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) { store.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
if (err) { if (err) {
console.info("Failed to query data, err: " + err) console.error(`Query failed, err: ${err}`);
return return;
} }
console.log("ResultSet column names: " + resultSet.columnNames) console.info(`ResultSet column names: ${resultSet.columnNames}`);
console.log("ResultSet column count: " + resultSet.columnCount) console.info(`ResultSet column count: ${resultSet.columnCount}`);
}) })
``` ```
...@@ -1805,14 +1809,14 @@ Queries data from the RDB store based on specified conditions. This API uses a p ...@@ -1805,14 +1809,14 @@ Queries data from the RDB store based on specified conditions. This API uses a p
```js ```js
import dataSharePredicates from '@ohos.data.dataSharePredicates' import dataSharePredicates from '@ohos.data.dataSharePredicates'
let predicates = new dataSharePredicates.DataSharePredicates() let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Rose") predicates.equalTo("NAME", "Rose");
let promise = rdbStore.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]) let promise = store.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => { promise.then((resultSet) => {
console.log("ResultSet column names: " + resultSet.columnNames) console.info(`ResultSet column names: ${resultSet.columnNames}`);
console.log("ResultSet column count: " + resultSet.columnCount) console.info(`ResultSet column count: ${resultSet.columnCount}`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to query data, err: " + err) console.error(`Query failed, err: ${err}`);
}) })
``` ```
...@@ -1837,17 +1841,18 @@ Queries data from the RDB store of a remote device based on specified conditions ...@@ -1837,17 +1841,18 @@ Queries data from the RDB store of a remote device based on specified conditions
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates('EMPLOYEE') let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.greaterThan("id", 0) predicates.greaterThan("id", 0);
rdbStore.remoteQuery("deviceId", "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], store.remoteQuery("deviceId", "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"],
function(err, resultSet){ function(err, resultSet) {
if (err) { if (err) {
console.info("Failed to remoteQuery, err: " + err) console.error(`Failed to remoteQuery, err: ${err}`);
return return;
} }
console.info("ResultSet column names: " + resultSet.columnNames) console.info(`ResultSet column names: ${resultSet.columnNames}`);
console.info("ResultSet column count: " + resultSet.columnCount) console.info(`ResultSet column count: ${resultSet.columnCount}`);
}) }
)
``` ```
### remoteQuery ### remoteQuery
...@@ -1876,14 +1881,14 @@ Queries data from the RDB store of a remote device based on specified conditions ...@@ -1876,14 +1881,14 @@ Queries data from the RDB store of a remote device based on specified conditions
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates('EMPLOYEE') let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.greaterThan("id", 0) predicates.greaterThan("id", 0);
let promise = rdbStore.remoteQuery("deviceId", "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]) let promise = store.remoteQuery("deviceId", "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => { promise.then((resultSet) => {
console.info("ResultSet column names: " + resultSet.columnNames) console.info(`ResultSet column names: ${resultSet.columnNames}`);
console.info("ResultSet column count: " + resultSet.columnCount) console.info(`ResultSet column count: ${resultSet.columnCount}`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to remoteQuery , err: " + err) console.error(`Failed to remoteQuery, err: ${err}`);
}) })
``` ```
...@@ -1906,13 +1911,13 @@ Queries data using the specified SQL statement. This API uses an asynchronous ca ...@@ -1906,13 +1911,13 @@ Queries data using the specified SQL statement. This API uses an asynchronous ca
**Example** **Example**
```js ```js
rdbStore.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], function (err, resultSet) { store.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], function (err, resultSet) {
if (err) { if (err) {
console.info("Failed to query data, err: " + err) console.error(`Query failed, err: ${err}`);
return return;
} }
console.log("ResultSet column names: " + resultSet.columnNames) console.info(`ResultSet column names: ${resultSet.columnNames}`);
console.log("ResultSet column count: " + resultSet.columnCount) console.info(`ResultSet column count: ${resultSet.columnCount}`);
}) })
``` ```
...@@ -1940,12 +1945,12 @@ Queries data using the specified SQL statement. This API uses a promise to retur ...@@ -1940,12 +1945,12 @@ Queries data using the specified SQL statement. This API uses a promise to retur
**Example** **Example**
```js ```js
let promise = rdbStore.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo']) let promise = store.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo']);
promise.then((resultSet) => { promise.then((resultSet) => {
console.log("ResultSet column names: " + resultSet.columnNames) console.info(`ResultSet column names: ${resultSet.columnNames}`);
console.log("ResultSet column count: " + resultSet.columnCount) console.info(`ResultSet column count: ${resultSet.columnCount}`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to query data, err: " + err) console.error(`Query failed, err: ${err}`);
}) })
``` ```
...@@ -1969,12 +1974,12 @@ Executes an SQL statement that contains specified arguments but returns no value ...@@ -1969,12 +1974,12 @@ Executes an SQL statement that contains specified arguments but returns no value
```js ```js
const SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)" const SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)"
rdbStore.executeSql(SQL_CREATE_TABLE, null, function(err) { store.executeSql(SQL_CREATE_TABLE, null, function(err) {
if (err) { if (err) {
console.info("Failed to execute SQL, err: " + err) console.error(`ExecuteSql failed, err: ${err}`);
return return;
} }
console.info('Create table done.') console.info(`Create table done.`);
}) })
``` ```
...@@ -2003,11 +2008,11 @@ Executes an SQL statement that contains specified arguments but returns no value ...@@ -2003,11 +2008,11 @@ Executes an SQL statement that contains specified arguments but returns no value
```js ```js
const SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)" const SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)"
let promise = rdbStore.executeSql(SQL_CREATE_TABLE) let promise = store.executeSql(SQL_CREATE_TABLE);
promise.then(() => { promise.then(() => {
console.info('Create table done.') console.info(`Create table done.`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to execute SQL, err: " + err) console.error(`ExecuteSql failed, err: ${err}`);
}) })
``` ```
...@@ -2023,19 +2028,25 @@ Starts the transaction before executing an SQL statement. ...@@ -2023,19 +2028,25 @@ Starts the transaction before executing an SQL statement.
```js ```js
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
let context = featureAbility.getContext() let context = featureAbility.getContext();
const STORE_CONFIG = { name: "RdbTest.db", const STORE_CONFIG = {
securityLevel: data_rdb.SecurityLevel.S1} name: "RdbTest.db",
data_rdb.getRdbStore(context, STORE_CONFIG, async function (err, rdbStore) { securityLevel: relationalStore.SecurityLevel.S1
rdbStore.beginTransaction() };
const valueBucket = { relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) {
"name": "lisi", if (err) {
"age": 18, console.error(`GetRdbStore failed, err: ${err}`);
"salary": 100.5, return;
"blobType": new Uint8Array([1, 2, 3]), }
} store.beginTransaction();
await rdbStore.insert("test", valueBucket) const valueBucket = {
rdbStore.commit() "name": "lisi",
"age": 18,
"salary": 100.5,
"blobType": new Uint8Array([1, 2, 3]),
};
await store.insert("test", valueBucket);
store.commit();
}) })
``` ```
...@@ -2051,19 +2062,25 @@ Commits the executed SQL statements. ...@@ -2051,19 +2062,25 @@ Commits the executed SQL statements.
```js ```js
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
let context = featureAbility.getContext() let context = featureAbility.getContext();
const STORE_CONFIG = { name: "RdbTest.db", const STORE_CONFIG = {
securityLevel: data_rdb.SecurityLevel.S1} name: "RdbTest.db",
data_rdb.getRdbStore(context, STORE_CONFIG, async function (err, rdbStore) { securityLevel: relationalStore.SecurityLevel.S1
rdbStore.beginTransaction() };
const valueBucket = { relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) {
"name": "lisi", if (err) {
"age": 18, console.error(`GetRdbStore failed, err: ${err}`);
"salary": 100.5, return;
"blobType": new Uint8Array([1, 2, 3]), }
} store.beginTransaction();
await rdbStore.insert("test", valueBucket) const valueBucket = {
rdbStore.commit() "name": "lisi",
"age": 18,
"salary": 100.5,
"blobType": new Uint8Array([1, 2, 3]),
};
await store.insert("test", valueBucket);
store.commit();
}) })
``` ```
...@@ -2079,24 +2096,31 @@ Rolls back the SQL statements that have been executed. ...@@ -2079,24 +2096,31 @@ Rolls back the SQL statements that have been executed.
```js ```js
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
let context = featureAbility.getContext() let context = featureAbility.getContext();
const STORE_CONFIG = { name: "RdbTest.db", const STORE_CONFIG = {
securityLevel: data_rdb.SecurityLevel.S1} name: "RdbTest.db",
data_rdb.getRdbStore(context, STORE_CONFIG, async function (err, rdbStore) { securityLevel: relationalStore.SecurityLevel.S1
try { };
rdbStore.beginTransaction() relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) {
const valueBucket = { if (err) {
"id": 1, console.error(`GetRdbStore failed, err: ${err}`);
"name": "lisi", return;
"age": 18, }
"salary": 100.5, try {
"blobType": new Uint8Array([1, 2, 3]), store.beginTransaction()
} const valueBucket = {
await rdbStore.insert("test", valueBucket) "id": 1,
rdbStore.commit() "name": "lisi",
} catch (e) { "age": 18,
rdbStore.rollBack() "salary": 100.5,
} "blobType": new Uint8Array([1, 2, 3]),
};
await store.insert("test", valueBucket);
store.commit();
} catch (err) {
console.error(`Transaction failed, err: ${err}`);
store.rollBack();
}
}) })
``` ```
...@@ -2118,12 +2142,12 @@ Backs up an RDB store. This API uses an asynchronous callback to return the resu ...@@ -2118,12 +2142,12 @@ Backs up an RDB store. This API uses an asynchronous callback to return the resu
**Example** **Example**
```js ```js
rdbStore.backup("dbBackup.db", function(err) { store.backup("dbBackup.db", function(err) {
if (err) { if (err) {
console.info('Backup failed, err: ' + err) console.error(`Backup failed, err: ${err}`);
return return;
} }
console.info('Backup success.') console.info(`Backup success.`);
}) })
``` ```
...@@ -2150,11 +2174,11 @@ Backs up an RDB store. This API uses a promise to return the result. ...@@ -2150,11 +2174,11 @@ Backs up an RDB store. This API uses a promise to return the result.
**Example** **Example**
```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}`);
}) })
``` ```
...@@ -2176,12 +2200,12 @@ Restores an RDB store from a backup file. This API uses an asynchronous callback ...@@ -2176,12 +2200,12 @@ Restores an RDB store from a backup file. This API uses an asynchronous callback
**Example** **Example**
```js ```js
rdbStore.restore("dbBackup.db", function(err) { store.restore("dbBackup.db", function(err) {
if (err) { if (err) {
console.info('Restore failed, err: ' + err) console.error(`Restore failed, err: ${err}`);
return return;
} }
console.info('Restore success.') console.info(`Restore success.`);
}) })
``` ```
...@@ -2208,11 +2232,11 @@ Restores an RDB store from a backup file. This API uses a promise to return the ...@@ -2208,11 +2232,11 @@ Restores an RDB store from a backup file. This API uses a promise to return the
**Example** **Example**
```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}`);
}) })
``` ```
...@@ -2236,12 +2260,12 @@ Sets distributed tables. This API uses an asynchronous callback to return the re ...@@ -2236,12 +2260,12 @@ Sets distributed tables. This API uses an asynchronous callback to return the re
**Example** **Example**
```js ```js
rdbStore.setDistributedTables(["EMPLOYEE"], function (err) { store.setDistributedTables(["EMPLOYEE"], function (err) {
if (err) { if (err) {
console.info('Failed to set distributed tables, err: ' + err) console.error(`SetDistributedTables failed, err: ${err}`);
return return;
} }
console.info('Set distributed tables successfully.') console.info(`SetDistributedTables successfully.`);
}) })
``` ```
...@@ -2270,11 +2294,11 @@ Sets distributed tables. This API uses a promise to return the result. ...@@ -2270,11 +2294,11 @@ Sets distributed tables. This API uses a promise to return the result.
**Example** **Example**
```js ```js
let promise = rdbStore.setDistributedTables(["EMPLOYEE"]) let promise = store.setDistributedTables(["EMPLOYEE"]);
promise.then(() => { promise.then(() => {
console.info("Set distributed tables successfully.") console.info(`SetDistributedTables successfully.`);
}).catch((err) => { }).catch((err) => {
console.info("Failed to set distributed tables, err: " + err) console.error(`SetDistributedTables failed, err: ${err}`);
}) })
``` ```
...@@ -2299,12 +2323,12 @@ Obtains the distributed table name for a remote device based on the local table ...@@ -2299,12 +2323,12 @@ Obtains the distributed table name for a remote device based on the local table
**Example** **Example**
```js ```js
rdbStore.obtainDistributedTableName("12345678abcde", "EMPLOYEE", function (err, tableName) { store.obtainDistributedTableName("12345678abcde", "EMPLOYEE", function (err, tableName) {
if (err) { if (err) {
console.info('Failed to obtain DistributedTableName, err: ' + err) console.error(`ObtainDistributedTableName failed, err: ${err}`);
return return;
} }
console.info('Obtained distributed table name successfully, tableName=.' + tableName) console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`);
}) })
``` ```
...@@ -2334,11 +2358,11 @@ Obtains the distributed table name for a remote device based on the local table ...@@ -2334,11 +2358,11 @@ Obtains the distributed table name for a remote device based on the local table
**Example** **Example**
```js ```js
let promise = rdbStore.obtainDistributedTableName("12345678abcde", "EMPLOYEE") let promise = store.obtainDistributedTableName("12345678abcde", "EMPLOYEE");
promise.then((tableName) => { promise.then((tableName) => {
console.info('Obtained distributed table name successfully, tableName= ' + tableName) console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`);
}).catch((err) => { }).catch((err) => {
console.info('Failed to obtain DistributedTableName, err: ' + err) console.error(`ObtainDistributedTableName failed, err: ${err}`);
}) })
``` ```
...@@ -2363,17 +2387,17 @@ Synchronizes data between devices. This API uses an asynchronous callback to ret ...@@ -2363,17 +2387,17 @@ Synchronizes data between devices. This API uses an asynchronous callback to ret
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates('EMPLOYEE') let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.inDevices(['12345678abcde']) predicates.inDevices(['12345678abcde']);
rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicates, function (err, result) { store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates, function (err, result) {
if (err) { if (err) {
console.log('Sync failed, err: ' + err) console.error(`Sync failed, err: ${err}`);
return return;
} }
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]}`);
} }
}) })
``` ```
...@@ -2403,16 +2427,16 @@ Synchronizes data between devices. This API uses a promise to return the result. ...@@ -2403,16 +2427,16 @@ Synchronizes data between devices. This API uses a promise to return the result.
**Example** **Example**
```js ```js
let predicates = new data_rdb.RdbPredicates('EMPLOYEE') let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.inDevices(['12345678abcde']) predicates.inDevices(['12345678abcde']);
let promise = rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicates) let promise = store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates);
promise.then((resultSet) =>{ promise.then((resultSet) =>{
console.log('Sync done.') console.info(`Sync done.`);
for (let i = 0; i < resultSet.length; i++) { for (let i = 0; i < resultSet.length; i++) {
console.log('device=' + resultSet[i][0] + ' status=' + resultSet[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}`);
}) })
``` ```
...@@ -2428,22 +2452,22 @@ Registers an observer for this RDB store. When the data in the RDB store changes ...@@ -2428,22 +2452,22 @@ Registers an observer for this RDB store. When the data in the RDB store changes
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ----------------------------------- | ---- | ------------------------------------------- | | -------- | ----------------------------------- | ---- | ------------------------------------------- |
| event | string | Yes | The value is'dataChange', which indicates a data change event. | | event | string | Yes | Event to observe. The value is **dataChange**, which indicates a data change event. |
| type | [SubscribeType](#subscribetype) | Yes | Subscription type to register.| | type | [SubscribeType](#subscribetype) | Yes | Subscription type to register.|
| observer | Callback&lt;Array&lt;string&gt;&gt; | Yes | Observer that listens for the data changes in the RDB store. | | observer | Callback&lt;Array&lt;string&gt;&gt; | Yes | Callback invoked to return the data change. |
**Example** **Example**
```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=' + devices[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('Failed to register observer') console.error(`Register observer failed, err: ${err}`);
} }
``` ```
...@@ -2459,22 +2483,22 @@ Unregisters the observer of the specified type from the RDB store. This API uses ...@@ -2459,22 +2483,22 @@ Unregisters the observer of the specified type from the RDB store. This API uses
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ---------------------------------- | ---- | ------------------------------------------ | | -------- | ---------------------------------- | ---- | ------------------------------------------ |
| event | string | Yes | The value is'dataChange', which indicates a data change event. | | event | string | Yes | Event to observe. The value is **dataChange**, which indicates a data change event. |
| type | [SubscribeType](#subscribetype) | Yes | Subscription type to register. | | type | [SubscribeType](#subscribetype) | Yes | Subscription type to unregister. |
| observer | Callback&lt;Array&lt;string&gt;&gt; | Yes | Data change observer registered. | | observer | Callback&lt;Array&lt;string&gt;&gt; | Yes | Callback for the data change. |
**Example** **Example**
```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=' + devices[i] + ' data changed') console.info(`device= ${devices[i]} data changed`);
} }
} }
try { try {
rdbStore.off('dataChange', data_rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver) store.off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
} catch (err) { } catch (err) {
console.log('Failed to unregister observer') console.error(`Unregister observer failed, err: ${err}`);
} }
``` ```
...@@ -2484,16 +2508,15 @@ Provides APIs to access the result set obtained by querying the RDB store. A res ...@@ -2484,16 +2508,15 @@ Provides APIs to access the result set obtained by querying the RDB store. A res
### Usage ### Usage
Obtain the **resultSet** object by [RdbStore.query()](#query). Obtain the **resultSet** object first.
```js ```js
import dataRdb from '@ohos.data.rdb'; let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let predicates = new dataRdb.RdbPredicates("EMPLOYEE");
predicates.equalTo("AGE", 18); predicates.equalTo("AGE", 18);
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => { promise.then((resultSet) => {
console.log(TAG + "resultSet columnNames:" + resultSet.columnNames); console.info(`resultSet columnNames: ${resultSet.columnNames}`);
console.log(TAG + "resultSet columnCount:" + resultSet.columnCount); console.info(`resultSet columnCount: ${resultSet.columnCount}`);
}); });
``` ```
...@@ -2618,13 +2641,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -2618,13 +2641,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
**Example** **Example**
```js ```js
let predicates = new dataRdb.RdbPredicates("EMPLOYEE"); let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise= rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); let promise= store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => { promise.then((resultSet) => {
resultSet.goTo(1); resultSet.goTo(1);
resultSet.close(); resultSet.close();
}).catch((err) => { }).catch((err) => {
console.log('query failed'); console.error(`query failed, err: ${err}`);
}); });
``` ```
...@@ -2659,13 +2682,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -2659,13 +2682,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
**Example** **Example**
```js ```js
let predicates = new dataRdb.RdbPredicates("EMPLOYEE"); let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => { promise.then((resultSet) => {
resultSet.(5); resultSet.(5);
resultSet.close(); resultSet.close();
}).catch((err) => { }).catch((err) => {
console.log('query failed'); console.error(`query failed, err: ${err}`);
}); });
``` ```
...@@ -2695,13 +2718,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -2695,13 +2718,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
**Example** **Example**
```js ```js
let predicates = new dataRdb.RdbPredicates("EMPLOYEE"); let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => { promise.then((resultSet) => {
resultSet.goToFirstRow(); resultSet.goToFirstRow();
resultSet.close(); resultSet.close();
}).catch((err) => { }).catch((err) => {
console.log('query failed'); console.error(`query failed, err: ${err}`);
}); });
``` ```
...@@ -2730,13 +2753,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -2730,13 +2753,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
**Example** **Example**
```js ```js
let predicates = new dataRdb.RdbPredicates("EMPLOYEE"); let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => { promise.then((resultSet) => {
resultSet.goToLastRow(); resultSet.goToLastRow();
resultSet.close(); resultSet.close();
}).catch((err) => { }).catch((err) => {
console.log('query failed'); console.error(`query failed, err: ${err}`);
}); });
``` ```
...@@ -2765,13 +2788,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -2765,13 +2788,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
**Example** **Example**
```js ```js
let predicates = new dataRdb.RdbPredicates("EMPLOYEE"); let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => { promise.then((resultSet) => {
resultSet.goToNextRow(); resultSet.goToNextRow();
resultSet.close(); resultSet.close();
}).catch((err) => { }).catch((err) => {
console.log('query failed'); console.error(`query failed, err: ${err}`);
}); });
``` ```
...@@ -2800,13 +2823,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -2800,13 +2823,13 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
**Example** **Example**
```js ```js
let predicates = new dataRdb.RdbPredicates("EMPLOYEE"); let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => { promise.then((resultSet) => {
resultSet.goToPreviousRow(); resultSet.goToPreviousRow();
resultSet.close(); resultSet.close();
}).catch((err) => { }).catch((err) => {
console.log('query failed'); console.error(`query failed, err: ${err}`);
}); });
``` ```
...@@ -2894,9 +2917,9 @@ Obtains the value of the Long type based on the specified column and the current ...@@ -2894,9 +2917,9 @@ Obtains the value of the Long type based on the specified column and the current
**Return value** **Return value**
| Type | Description | | Type | Description |
| ------ | -------------------------- | | ------ | ------------------------------------------------------------ |
| number | Value obtained.| | number | Value obtained.<br>The value range supported by this API is **Number.MIN_SAFE_INTEGER** to **Number.MAX_SAFE_INTEGER**. If the value is out of this range, use [getDouble](#getdouble).|
**Error codes** **Error codes**
...@@ -2991,12 +3014,12 @@ Closes this result set. ...@@ -2991,12 +3014,12 @@ Closes this result set.
**Example** **Example**
```js ```js
let predicatesClose = new dataRdb.RdbPredicates("EMPLOYEE"); let predicatesClose = new relationalStore.RdbPredicates("EMPLOYEE");
let promiseClose = rdbStore.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"]); let promiseClose = store.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promiseClose.then((resultSet) => { promiseClose.then((resultSet) => {
resultSet.close(); resultSet.close();
}).catch((err) => { }).catch((err) => {
console.log('resultset close failed'); console.error(`resultset close failed, err: ${err}`);
}); });
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册