提交 c591037c 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 c2569dcb
...@@ -11,14 +11,14 @@ Most of the RDB store APIs are asynchronous interfaces, which can use a callback ...@@ -11,14 +11,14 @@ Most of the RDB store APIs are asynchronous interfaces, which can use a callback
### Creating or Deleting an RDB Store ### Creating or Deleting an RDB Store
The table below describes the APIs available for creating and deleting an RDB store. The following table describes the APIs for creating and deleting an RDB store.
**Table 1** APIs for creating and deleting an RDB store **Table 1** APIs for creating and deleting an RDB store
| API | Description | | API | Description |
| ------------------------------------------------------------ | ------------------------------------------------------------ | | ------------------------------------------------------------ | ------------------------------------------------------------ |
| getRdbStore(context: Context, config: StoreConfig, version: number): 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**: context of the application or function.<br>- **config**: configuration of the RDB store.<br>- **version**: version of the RDB store.| | getRdbStoreV9(context: Context, config: StoreConfigV9, version: number): Promise&lt;RdbStoreV9&gt; | Obtains an **RdbStoreV9** instance. 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**: context of the application or function.<br>- **config**: configuration of the RDB store.<br>- **version**: version of the RDB store. Currently, automatic RDB upgrades and downgrades performed based on **version** is not supported.|
| deleteRdbStore(context: Context, name: string): Promise&lt;void&gt; | Deletes an RDB store. This API uses a promise to return the result.<br>- **context**: context of the application or function.<br>- **name**: name of the RDB store to delete.| | deleteRdbStoreV9(context: Context, name: string): Promise&lt;void&gt; | Deletes an RDB store. This API uses a promise to return the result.<br>- **context**: context of the application or function.<br>- **name**: name of the RDB store to delete.|
### Managing Data in an RDB Store ### Managing Data in an RDB Store
...@@ -32,30 +32,30 @@ The RDB provides APIs for inserting, deleting, updating, and querying data in th ...@@ -32,30 +32,30 @@ The RDB provides APIs for inserting, deleting, updating, and querying data in th
| Class | API | Description | | Class | API | Description |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RdbStore | insert(table: string, values: ValuesBucket): Promise&lt;number&gt; | Inserts a row of data into a table. This API uses a promise to return the result.<br>If the operation is successful, the row ID will be returned; otherwise, **-1** will be returned.<br>- **table**: name of the target table.<br>- **values**: data to be inserted into the table.| | RdbStoreV9 | insert(table: string, values: ValuesBucket): Promise&lt;number&gt; | Inserts a row of data into a table. This API uses a promise to return the result.<br>If the operation is successful, the row ID will be returned; otherwise, **-1** will be returned.<br>- **table**: name of the target table.<br>- **values**: data to be inserted into the table.|
- **Updating Data** - **Updating Data**
Call the **update()** method to pass 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. Call **update()** to update data based on the passed data and the conditions specified by **RdbPredicatesV9**. 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 result.<br>the number of rows updated.<br>- **values**: data to update, which is stored in **ValuesBucket**.<br>- **predicates**: conditions for updating data.| | RdbStoreV9 | update(values: ValuesBucket, predicates: RdbPredicatesV9): Promise&lt;number&gt; | Updates data based on the specified **RdbPredicatesV9** 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**
Call the **delete()** method to delete data meeting the conditions specified by **RdbPredicates**. If the data is deleted, the number of rows of the deleted data will be returned; otherwise, **0** will be returned. Call **delete()** to delete the data that meets the conditions specified by **RdbPredicatesV9**. If the data is deleted, the number of rows of the deleted data will be returned; otherwise, **0** will be returned.
**Table 4** API for deleting data **Table 4** API for deleting data
| 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<br>the number of rows updated.<br>- **predicates**: conditions for deleting data.| | RdbStoreV9 | delete(predicates: RdbPredicatesV9): Promise&lt;number&gt; | Deletes data from the RDB store based on the specified **RdbPredicatesV9** object. This API uses a promise to return the number of rows updated.<br>- **predicates**: conditions for deleting data. |
- **Querying Data** - **Querying Data**
...@@ -67,62 +67,66 @@ The RDB provides APIs for inserting, deleting, updating, and querying data in th ...@@ -67,62 +67,66 @@ The RDB provides APIs for inserting, deleting, updating, and querying data in th
**Table 5** APIs for querying data **Table 5** APIs for querying data
| Class | API | Description | | Class | API | Description |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RdbStore | query(predicates: RdbPredicates, columns?: Array&lt;string&gt;): Promise&lt;ResultSet&gt; | Queries data from the RDB store based on specified conditions. This API uses a promise to return the result.<br>- **predicates**: conditions for querying data.<br>- **columns**: columns to query. If this parameter is not specified, the query applies to all columns.| | RdbStoreV9 | query(predicates: RdbPredicatesV9, columns?: Array&lt;string&gt;): Promise&lt;ResultSetV9&gt; | Queries data from the RDB store based on specified conditions. This API uses a promise to return the result.<br>- **predicates**: conditions for querying data.<br>- **columns**: columns to query. If this parameter is not specified, the query applies to all columns.|
| RdbStore | querySql(sql: string, bindArgs?: Array&lt;ValueType&gt;): Promise&lt;ResultSet&gt; | Queries data using the specified SQL statement. This API uses a promise to return the result.<br>- **sql**: SQL statement.<br>- **bindArgs**: arguments in the SQL statement.| | RdbStoreV9 | querySql(sql: string, bindArgs?: Array&lt;ValueType&gt;): Promise&lt;ResultSetV9&gt; | Queries data using the specified SQL statement. This API uses a promise to return the result.<br>- **sql**: SQL statement.<br>- **bindArgs**: arguments in the SQL statement.|
| RdbStore | remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array&lt;string&gt;): Promise&lt;ResultSet&gt; | Queries data from the database of a remote device based on specified conditions. This API uses a promise to return the result.<br>- **device**: network ID of the remote device.<br>- **table**: name of the table to be queried.<br>- **predicates**: **RdbPredicates** that specifies the query condition.<br>- **columns**: columns to query. If this parameter is not specified, the query applies to all columns.| | RdbStoreV9 | remoteQuery(device: string, table: string, predicates: RdbPredicatesV9, columns: Array&lt;string&gt;): Promise&lt;ResultSetV9&gt; | Queries data from the database of a remote device based on specified conditions. This API uses a promise to return the result.<br>- **device**: network ID of the remote device.<br>- **table**: name of the table to be queried.<br>- **predicates**: **RdbPredicatesV9** that specifies the query conditions.<br>- **columns**: columns to query. If this parameter is not specified, the query applies to all columns.|
### Using Predicates ### Using Predicates
The RDB provides **RdbPredicates** for you to set database operation conditions. The **RDB** module provides **RdbPredicatesV9** for you to set database operation conditions.
The following lists common predicates. For more information about predicates, see [**RdbPredicates**](../reference/apis/js-apis-data-rdb.md#rdbpredicates). The following table lists common predicates. For more information about predicates, see [**RdbPredicates**](../reference/apis/js-apis-data-rdb.md#rdbpredicates).
**Table 6** APIs for using RDB store predicates **Table 6** APIs for using RDB store predicates
| Class | API | Description | | Class | API | Description |
| ------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | --------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RdbPredicates | equalTo(field: string, value: ValueType): RdbPredicates | Sets an **RdbPredicates** to match the field with data type **ValueType** and value equal to the specified value.<br>- **field**: column name in the database table.<br>- **value**: value to match the **RdbPredicates**.<br>- **RdbPredicates**: **RdbPredicates** object that matches the specified field.| | RdbPredicatesV9 | equalTo(field: string, value: ValueType): RdbPredicatesV9 | Sets an **RdbPredicatesV9** to search for the data that is equal to the specified value.<br>- **field**: column name in the database table.<br>- **value**: value to match.<br>- **RdbPredicatesV9**: **RdbPredicatesV9** object created. |
| RdbPredicates | notEqualTo(field: string, value: ValueType): RdbPredicates | Sets an **RdbPredicates** to match the field with data type **ValueType** and value not equal to the specified value.<br>- **field**: column name in the database table.<br>- **value**: value to match the **RdbPredicates**.<br>- **RdbPredicates**: **RdbPredicates** object that matches the specified field.| | RdbPredicatesV9 | notEqualTo(field: string, value: ValueType): RdbPredicatesV9 | Sets an **RdbPredicatesV9** to search for the data that is not equal to the specified value.<br>- **field**: column name in the database table.<br>- **value**: value to match.<br>- **RdbPredicatesV9**: **RdbPredicatesV9** object created. |
| RdbPredicates | or(): RdbPredicates | Adds the OR condition to the **RdbPredicates**.<br>- **RdbPredicates**: **RdbPredicates** with the OR condition.| | RdbPredicatesV9 | or(): RdbPredicatesV9 | Adds the OR condition to the **RdbPredicatesV9**.<br>- **RdbPredicatesV9**: **RdbPredicatesV9** with the OR condition. |
| RdbPredicates | and(): RdbPredicates | Adds the AND condition to the **RdbPredicates**.<br>- **RdbPredicates**: **RdbPredicates** with the AND condition.| | RdbPredicatesV9 | and(): RdbPredicatesV9 | Adds the AND condition to the **RdbPredicatesV9**.<br>- **RdbPredicatesV9**: **RdbPredicatesV9** with the AND condition. |
| RdbPredicates | contains(field: string, value: string): RdbPredicates | Sets an **RdbPredicates** to match a string containing the specified value.<br>- **field**: column name in the database table.<br>- **value**: value to match the **RdbPredicates**.<br>- **RdbPredicates**: **RdbPredicates** object that matches the specified field.| | RdbPredicatesV9 | contains(field: string, value: string): RdbPredicatesV9 | Sets an **RdbPredicatesV9** to search for the data that contains the specified value.<br>- **field**: column name in the database table.<br>- **value**: value to match.<br>- **RdbPredicatesV9**: **RdbPredicatesV9** object created. |
### Using the Result Set ### Using the Result Set
A result set can be regarded as a row of data in the queried results. It allows you to traverse and access the data you have queried. You can use the APIs provided by **ResultSetV9** to traverse and access the data you have queried. A result set can be regarded as a row of data in the queried result.
For details about how to use result set APIs, see [Result Set](../reference/apis/js-apis-data-resultset.md). For details about how to use result set APIs, see [Result Set](../reference/apis/js-apis-data-resultset.md).
> **NOTICE**<br> > **NOTICE**
>
> After a result set is used, you must call the **close()** method to close it explicitly. > After a result set is used, you must call the **close()** method to close it explicitly.
**Table 7** APIs for using the result set **Table 7** APIs for using the result set
| Class | API | Description | | Class | API | Description |
| --------- | ---------------------------------------------------- | ------------------------------------------ | | ----------- | ---------------------------------------- | ------------------------------------------ |
| ResultSet | goToFirstRow(): boolean | Moves to the first row of the result set. | | ResultSetV9 | goToFirstRow(): boolean | Moves to the first row of the result set. |
| ResultSet | getString(columnIndex: number): string | Obtains the value in the form of a string based on the specified column and current row. | | ResultSetV9 | getString(columnIndex: number): string | Obtains the value in the form of a string based on the specified column and current row. |
| ResultSet | getBlob(columnIndex: number): Uint8Array | Obtains the value in the form of a byte array based on the specified column and the current row.| | ResultSetV9 | getBlob(columnIndex: number): Uint8Array | Obtains the value in the form of a byte array based on the specified column and the current row.|
| ResultSet | getDouble(columnIndex: number): number | Obtains the value in the form of double based on the specified column and current row. | | ResultSetV9 | getDouble(columnIndex: number): number | Obtains the value in the form of double based on the specified column and current row. |
| ResultSet | getLong(columnIndex: number): number | Obtains the value in the form of a long integer based on the specified column and current row. | | ResultSetV9 | getLong(columnIndex: number): number | Obtains the value in the form of a long integer based on the specified column and current row. |
| ResultSet | close(): void | Closes the result set. | | ResultSetV9 | close(): void | Closes the result set. |
### Setting Distributed Tables ### Setting Distributed Tables
>**CAUTION**<br>ohos.permission.DISTRIBUTED_DATASYNC is required for using the **setDistributedTables**, **obtainDistributedTableName**, **sync**, **on**, and **off** APIs of **RdbStore**. > **NOTE**
>
> - The **ohos.permission.DISTRIBUTED_DATASYNC** permission is required for calling the **setDistributedTables**, **obtainDistributedTableName**, **sync**, **on** and **off** APIs of **RdbStore V9**.
> - The devices must be connected over network before the distributed tables are used. For details about the APIs and usage, see [Device Management](../reference/apis/js-apis-device-manager.md).
**Setting Distributed Tables** **Setting Distributed Tables**
**Table 8** API for setting distributed tables **Table 8** API for setting distributed tables
| Class | API | Description | | Class | API | Description |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RdbStore | setDistributedTables(tables: Array\<string>): Promise\<void> | Sets distributed tables. This API uses a promise to return the result.<br>- **tables**: names of the distributed tables to set.| | RdbStoreV9 | setDistributedTables(tables: Array\<string>): Promise\<void> | Sets distributed tables. This API uses a promise to return the result.<br>- **tables**: names of the distributed tables to set.|
**Obtaining the Distributed Table Name for a Remote Device** **Obtaining the Distributed Table Name for a Remote Device**
...@@ -131,32 +135,32 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -131,32 +135,32 @@ You can obtain the distributed table name for a remote device based on the local
**Table 9** API for obtaining the distributed table name of a remote device **Table 9** API for obtaining the distributed table name of a remote device
| Class | API | Description | | Class | API | Description |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RdbStore | obtainDistributedTableName(device: string, table: string): Promise\<string> | Obtains the distributed table name for a remote device based on the local table name. The distributed table name is required when the RDB store of a remote device is queried. This API uses a promise to return the result.<br>- **device**: remote device.<br>- **table**: local table name.| | RdbStoreV9 | obtainDistributedTableName(device: string, table: string): Promise\<string> | Obtains the distributed table name for a remote device based on the local table name. The distributed table name is required when the RDB store of a remote device is queried. This API uses a promise to return the result.<br>- **device**: remote device.<br>- **table**: local table name.|
**Synchronizing Data Between Devices** **Synchronizing Data Between Devices**
**Table 10** API for synchronizing data between devices **Table 10** API for synchronizing data between devices
| Class | API | Description | | Class | API | Description |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RdbStore | sync(mode: SyncMode, predicates: RdbPredicates): Promise\<Array\<[string, number]>> | Synchronizes data between devices. This API uses a promise to return the result.<br>- **mode**: synchronization mode. **SYNC_MODE_PUSH** means to push data from the local device to a remote device. **SYNC_MODE_PULL** means to pull data from a remote device to the local device.<br>- **predicates**: specifies the data and devices to synchronize.<br>- **string**: device ID. <br>- **number**: synchronization status of each device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure.| | RdbStoreV9 | sync(mode: SyncMode, predicates: RdbPredicatesV9): Promise\<Array\<[string, number]>> | Synchronizes data between devices. This API uses a promise to return the result.<br>- **mode**: synchronization mode. **SYNC_MODE_PUSH** means to push data from the local device to a remote device. **SYNC_MODE_PULL** means to pull data from a remote device to the local device.<br>- **predicates**: specifies the data and devices to synchronize.<br>- **string**: device ID. <br>- **number**: synchronization status of each device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure.|
**Registering an RDB Store Observer** **Registering an RDB Store Observer**
**Table 11** API for registering an observer **Table 11** API for registering an observer
| Class | API | Description | | Class | API | Description |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RdbStore | on(event: 'dataChange', type: SubscribeType, observer: Callback\<Array\<string>>): void | Registers an observer for this RDB store to subscribe to distributed data changes. When data in the RDB store changes, a callback will be invoked to return the data changes.<br>- **type**: subscription type. **SUBSCRIBE_TYPE_REMOTE** means to subscribe to remote data changes.<br>- **observer**: observer that listens for data changes in the RDB store.| | RdbStoreV9 | on(event: 'dataChange', type: SubscribeType, observer: Callback\<Array\<string>>): void | Registers an observer for this RDB store to subscribe to distributed data changes. When data in the RDB store changes, a callback will be invoked to return the data changes.<br>- **type**: subscription type. **SUBSCRIBE_TYPE_REMOTE**: subscribes to remote data changes.<br>- **observer**: observer that listens for data changes in the RDB store.|
**Unregistering an RDB Store Observer** **Unregistering an RDB Store Observer**
**Table 12** API for unregistering an observer **Table 12** API for unregistering an observer
| Class | API | Description | | Class | API | Description |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ---------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RdbStore | off(event:'dataChange', type: SubscribeType, observer: Callback\<Array\<string>>): void; | Unregisters the observer of the specified type from the RDB store. This API uses an asynchronous callback to return the result.<br>- **type**: subscription type. **SUBSCRIBE_TYPE_REMOTE** means to subscribe to remote data changes.<br>- **observer**: observer to unregister.| | RdbStoreV9 | off(event:'dataChange', type: SubscribeType, observer: Callback\<Array\<string>>): void; | Unregisters the observer of the specified type from the RDB store. This API uses an asynchronous callback to return the result.<br>- **type**: subscription type. **SUBSCRIBE_TYPE_REMOTE**: subscribes to remote data changes.<br>- **observer**: observer to unregister.|
### Backing Up and Restoring an RDB Store ### Backing Up and Restoring an RDB Store
...@@ -165,16 +169,16 @@ You can obtain the distributed table name for a remote device based on the local ...@@ -165,16 +169,16 @@ You can obtain the distributed table name for a remote device based on the local
**Table 13** API for backing up an RDB store **Table 13** API for backing up an RDB store
| Class | API | Description | | Class | API | Description |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ---------- | --------------------------------------------- | ------------------------------------------------------------ |
| RdbStore | backup(destName: string): Promise&lt;void&gt; | Backs up an RDB store. This API uses a promise to return the result.<br>- **destName**: name of the RDB backup file.| | RdbStoreV9 | backup(destName: string): Promise&lt;void&gt; | Backs up an RDB store. This API uses a promise to return the result.<br>- **destName**: name of the RDB backup file.|
**Restoring an RDB Store** **Restoring an RDB Store**
**Table 14** API for restoring an RDB store **Table 14** API for restoring an RDB store
| Class | API | Description | | Class | API | Description |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | ---------- | --------------------------------------------- | ------------------------------------------------------------ |
| RdbStore | restore(srcName: string): Promise&lt;void&gt; | Restores an RDB store from a backup file. This API uses a promise to return the result.<br>- **srcName**: name of the backup file used to restore the RDB store.| | RdbStoreV9 | restore(srcName: string): Promise&lt;void&gt; | Restores an RDB store from a backup file. This API uses a promise to return the result.<br>- **srcName**: name of the backup file used to restore the RDB store.|
**Transaction** **Transaction**
...@@ -182,9 +186,9 @@ Table 15 Transaction APIs ...@@ -182,9 +186,9 @@ Table 15 Transaction APIs
| Class | API | Description | | Class | API | Description |
| -------- | ----------------------- | --------------------------------- | | -------- | ----------------------- | --------------------------------- |
| RdbStore | beginTransaction(): void | Starts the transaction before executing SQL statements.| | RdbStoreV9 | beginTransaction(): void | Starts the transaction before executing SQL statements.|
| RdbStore | commit(): void | Commits the executed SQL statements. | | RdbStoreV9 | commit(): void | Commits the executed SQL statements. |
| RdbStore | rollBack(): void | Rolls back the SQL statements that have been executed. | | RdbStoreV9 | rollBack(): void | Rolls back the SQL statements that have been executed. |
## How to Develop ## How to Develop
...@@ -206,9 +210,10 @@ Table 15 Transaction APIs ...@@ -206,9 +210,10 @@ Table 15 Transaction APIs
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 CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)";
const STORE_CONFIG = { name: "RdbTest.db" } const STORE_CONFIGV9 = { name: "RdbTest.db",
data_rdb.getRdbStore(context, STORE_CONFIG, 1, function (err, rdbStore) { securityLevel: data_rdb.SecurityLevel.S1}
rdbStore.executeSql(CREATE_TABLE_TEST) data_rdb.getRdbStoreV9(context, STORE_CONFIGV9, 1, function (err, rdbStoreV9) {
rdbStoreV9.executeSql(CREATE_TABLE_TEST)
console.info('create table done.') console.info('create table done.')
}) })
``` ```
...@@ -226,9 +231,10 @@ Table 15 Transaction APIs ...@@ -226,9 +231,10 @@ Table 15 Transaction APIs
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 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" } const STORE_CONFIGV9 = { name: "rdbstore.db",
data_rdb.getRdbStore(context, STORE_CONFIG, 1, function (err, rdbStore) { securityLevel: data_rdb.SecurityLevel.S1}
rdbStore.executeSql(CREATE_TABLE_TEST) data_rdb.getRdbStoreV9(context, STORE_CONFIGV9, 1, function (err, rdbStoreV9) {
rdbStoreV9.executeSql(CREATE_TABLE_TEST)
console.info('create table done.') console.info('create table done.')
}) })
``` ```
...@@ -244,12 +250,12 @@ Table 15 Transaction APIs ...@@ -244,12 +250,12 @@ Table 15 Transaction APIs
```js ```js
var u8 = new Uint8Array([1, 2, 3]) var 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 = rdbStoreV9.insert("test", valueBucket)
``` ```
3. Query data. 3. Query data.
(1) Create an **RdbPredicates** object to specify query conditions. (1) Create an **RdbPredicatesV9** object to specify query conditions.
(2) Call the **query()** API to query data. (2) Call the **query()** API to query data.
...@@ -258,17 +264,17 @@ Table 15 Transaction APIs ...@@ -258,17 +264,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 predicatesV9 = new data_rdb.RdbPredicatesV9("test");
predicates.equalTo("name", "Tom") predicatesV9.equalTo("name", "Tom")
let promisequery = rdbStore.query(predicates) let promisequery = rdbStoreV9.query(predicatesV9)
promisequery.then((resultSet) => { promisequery.then((resultSetV9) => {
resultSet.goToFirstRow() resultSetV9.goToFirstRow()
const id = resultSet.getLong(resultSet.getColumnIndex("id")) const id = resultSetV9.getLong(resultSetV9.getColumnIndex("id"))
const name = resultSet.getString(resultSet.getColumnIndex("name")) const name = resultSetV9.getString(resultSetV9.getColumnIndex("name"))
const age = resultSet.getLong(resultSet.getColumnIndex("age")) const age = resultSetV9.getLong(resultSetV9.getColumnIndex("age"))
const salary = resultSet.getDouble(resultSet.getColumnIndex("salary")) const salary = resultSetV9.getDouble(resultSetV9.getColumnIndex("salary"))
const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType")) const blobType = resultSetV9.getBlob(resultSetV9.getColumnIndex("blobType"))
resultSet.close() resultSetV9.close()
}) })
``` ```
...@@ -296,7 +302,7 @@ Table 15 Transaction APIs ...@@ -296,7 +302,7 @@ Table 15 Transaction APIs
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 = rdbStoreV9.setDistributedTables(["test"])
promise.then(() => { promise.then(() => {
console.info("setDistributedTables success.") console.info("setDistributedTables success.")
}).catch((err) => { }).catch((err) => {
...@@ -306,7 +312,7 @@ Table 15 Transaction APIs ...@@ -306,7 +312,7 @@ Table 15 Transaction APIs
5. Synchronize data across devices. 5. Synchronize data across devices.
(1) Construct an **RdbPredicates** object to specify remote devices within the network to be synchronized. (1) Construct an **RdbPredicatesV9** object to specify remote devices within the network to be synchronized.
(2) Call **rdbStore.sync()** to synchronize data. (2) Call **rdbStore.sync()** to synchronize data.
...@@ -315,9 +321,9 @@ Table 15 Transaction APIs ...@@ -315,9 +321,9 @@ 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 predicateV9 = new data_rdb.RdbPredicatesV9('test')
predicate.inDevices(['12345678abcde']) predicateV9.inDevices(['12345678abcde'])
let promise = rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicate) let promise = rdbStoreV9.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicateV9)
promise.then((result) => { promise.then((result) => {
console.log('sync done.') console.log('sync done.')
for (let i = 0; i < result.length; i++) { for (let i = 0; i < result.length; i++) {
...@@ -344,7 +350,7 @@ Table 15 Transaction APIs ...@@ -344,7 +350,7 @@ Table 15 Transaction APIs
} }
try { try {
rdbStore.on('dataChange', data_rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver) rdbStoreV9.on('dataChange', data_rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver)
} catch (err) { } catch (err) {
console.log('register observer failed') console.log('register observer failed')
} }
...@@ -359,8 +365,8 @@ Table 15 Transaction APIs ...@@ -359,8 +365,8 @@ Table 15 Transaction APIs
The sample code is as follows: The sample code is as follows:
```js ```js
let tableName = rdbStore.obtainDistributedTableName(deviceId, "test"); let tableName = rdbStoreV9.obtainDistributedTableName(deviceId, "test");
let resultSet = rdbStore.querySql("SELECT * FROM " + tableName) let resultSetV9 = rdbStoreV9.querySql("SELECT * FROM " + tableName)
``` ```
8. Query data of a remote device. 8. Query data of a remote device.
...@@ -373,17 +379,17 @@ Table 15 Transaction APIs ...@@ -373,17 +379,17 @@ Table 15 Transaction APIs
The sample code is as follows: The sample code is as follows:
```js ```js
let rdbPredicate = new data_rdb.RdbPredicates('employee') let rdbPredicateV9 = new data_rdb.RdbPredicatesV9('employee')
predicates.greaterThan("id", 0) predicatesV9.greaterThan("id", 0)
let promiseQuery = rdbStore.remoteQuery('12345678abcde', 'employee', rdbPredicate) let promiseQuery = rdbStoreV9.remoteQuery('12345678abcde', 'employee', rdbPredicateV9)
promiseQuery.then((resultSet) => { promiseQuery.then((resultSetV9) => {
while (resultSet.goToNextRow()) { while (resultSetV9.goToNextRow()) {
let idx = resultSet.getLong(0); let idx = resultSetV9.getLong(0);
let name = resultSet.getString(1); let name = resultSetV9.getString(1);
let age = resultSet.getLong(2); let age = resultSetV9.getLong(2);
console.info(idx + " " + name + " " + age); console.info(idx + " " + name + " " + age);
} }
resultSet.close(); resultSetV9.close();
}).catch((err) => { }).catch((err) => {
console.info("failed to remoteQuery, err: " + err) console.info("failed to remoteQuery, err: " + err)
}) })
...@@ -396,7 +402,7 @@ Table 15 Transaction APIs ...@@ -396,7 +402,7 @@ Table 15 Transaction APIs
The sample code is as follows: The sample code is as follows:
```js ```js
let promiseBackup = rdbStore.backup("dbBackup.db") let promiseBackup = rdbStoreV9.backup("dbBackup.db")
promiseBackup.then(() => { promiseBackup.then(() => {
console.info('Backup success.') console.info('Backup success.')
}).catch((err) => { }).catch((err) => {
...@@ -408,7 +414,7 @@ Table 15 Transaction APIs ...@@ -408,7 +414,7 @@ Table 15 Transaction APIs
The sample code is as follows: The sample code is as follows:
```js ```js
let promiseRestore = rdbStore.restore("dbBackup.db") let promiseRestore = rdbStoreV9.restore("dbBackup.db")
promiseRestore.then(() => { promiseRestore.then(() => {
console.info('Restore success.') console.info('Restore success.')
}).catch((err) => { }).catch((err) => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册