“f371123ff774eda4d69758f613fab7afc04c43c8”上不存在“communication/bluetooth_profile/BUILD.gn”
database-relational-guidelines.md 21.5 KB
Newer Older
A
annie_wangli 已提交
1
# RDB Development
Z
zengyawen 已提交
2

A
annie_wangli 已提交
3
## When to Use
Z
zengyawen 已提交
4

A
Annie_wang 已提交
5
A relational database (RDB) store allows you to operate local data with or without native SQL statements based on SQLite.
Z
zengyawen 已提交
6

A
annie_wangli 已提交
7 8

## Available APIs
Z
zengyawen 已提交
9

A
Annie_wang 已提交
10
Most of the RDB store APIs are asynchronous interfaces, which can use a callback or promise to return the result. This document uses the promise-based APIs as an example. For details about the APIs, see [Relational Database](../reference/apis/js-apis-data-rdb.md).
A
Annie_wang 已提交
11

A
annie_wangli 已提交
12
### Creating or Deleting an RDB Store
Z
zengyawen 已提交
13

A
Annie_wang 已提交
14
The table below describes the APIs available for creating and deleting an RDB store.
Z
zengyawen 已提交
15

A
annie_wangli 已提交
16 17
**Table 1** APIs for creating and deleting an RDB store

A
Annie_wang 已提交
18 19 20 21
| 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.|
| 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.|
Z
zengyawen 已提交
22

A
annie_wangli 已提交
23
### Managing Data in an RDB Store
Z
zengyawen 已提交
24

A
annie_wangli 已提交
25 26
The RDB provides APIs for inserting, deleting, updating, and querying data in the local RDB store.

A
Annie_wang 已提交
27
- **Inserting Data**
A
annie_wangli 已提交
28
  
A
annie_wangli 已提交
29
  The RDB provides APIs for inserting data through a **ValuesBucket** in a data table. If the data is inserted, the row ID of the data inserted will be returned; otherwise, **-1** will be returned.
A
annie_wangli 已提交
30
  
A
Annie_wang 已提交
31
  **Table 2** API for inserting data
A
annie_wangli 已提交
32
  
A
Annie_wang 已提交
33

A
Annie_wang 已提交
34 35
  | Class    | API                                                      | Description                                                        |
  | -------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
A
Annie_wang 已提交
36
  | 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.|
A
annie_wangli 已提交
37
  
A
Annie_wang 已提交
38
- **Updating Data**
A
annie_wangli 已提交
39
  
A
annie_wangli 已提交
40
  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.
A
annie_wangli 已提交
41
  
A
Annie_wang 已提交
42
  **Table 3** API for updating data
A
annie_wangli 已提交
43
  
A
Annie_wang 已提交
44 45

  | Class    | API                                                      | Description                                                        |
A
Annie_wang 已提交
46
  | -------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
A
Annie_wang 已提交
47
  | 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.|
A
annie_wangli 已提交
48
  
A
Annie_wang 已提交
49
- **Deleting Data**
A
annie_wangli 已提交
50
  
A
annie_wangli 已提交
51
  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.
A
annie_wangli 已提交
52
  
A
Annie_wang 已提交
53
  **Table 4** API for deleting data
A
annie_wangli 已提交
54
  
A
Annie_wang 已提交
55 56 57 58

  | 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.|
A
Annie_wang 已提交
59 60
  
- **Querying Data**
A
annie_wangli 已提交
61 62 63 64 65 66 67

  You can query data in an RDB store in either of the following ways:

  - Call the **query()** method to query data based on the predicates, without passing any SQL statement.
  - Run the native SQL statement.

  **Table 5** APIs for querying data
A
annie_wangli 已提交
68

A
Annie_wang 已提交
69

A
Annie_wang 已提交
70 71
  | Class    | API                                                      | Description                                                        |
  | -------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
A
Annie_wang 已提交
72 73
  | 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.|
  | 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.|
A
Annie_wang 已提交
74
  | 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.|
Z
zengyawen 已提交
75

A
annie_wangli 已提交
76
### Using Predicates
Z
zengyawen 已提交
77

A
annie_wangli 已提交
78 79
The RDB provides **RdbPredicates** for you to set database operation conditions.

A
Annie_wang 已提交
80 81
The following lists common predicates. For more information about predicates, see [**RdbPredicates**](../reference/apis/js-apis-data-rdb.md#rdbpredicates).

A
annie_wangli 已提交
82 83
**Table 6** APIs for using RDB store predicates

A
Annie_wang 已提交
84 85
| Class         | API                                                      | Description                                                        |
| ------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
A
Annie_wang 已提交
86 87 88 89 90
| 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.|
| 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.|
| RdbPredicates | or(): RdbPredicates                                     | Adds the OR condition to the **RdbPredicates**.<br>- **RdbPredicates**: **RdbPredicates** with the OR condition.|
| RdbPredicates | and(): RdbPredicates                                    | Adds the AND condition to the **RdbPredicates**.<br>- **RdbPredicates**: **RdbPredicates** 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.|
A
Annie_wang 已提交
91

Z
zengyawen 已提交
92

A
annie_wangli 已提交
93
### Using the Result Set
Z
zengyawen 已提交
94

A
Annie_wang 已提交
95 96 97
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.

For details about how to use result set APIs, see [Result Set](../reference/apis/js-apis-data-resultset.md).
A
annie_wangli 已提交
98

99
> **NOTICE**<br>
A
Annie_wang 已提交
100
> After a result set is used, you must call the **close()** method to close it explicitly.
A
annie_wangli 已提交
101 102 103

**Table 7** APIs for using the result set

A
Annie_wang 已提交
104 105
| Class     | API                                              | Description                                      |
| --------- | ---------------------------------------------------- | ------------------------------------------ |
A
Annie_wang 已提交
106 107 108 109 110 111
| ResultSet | 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.  |
| ResultSet | 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.    |
| ResultSet | 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.                              |
A
annie_wangli 已提交
112

Z
zengyawen 已提交
113 114


A
annie_wangli 已提交
115
### Setting Distributed Tables
A
annie_wangli 已提交
116

A
Annie_wang 已提交
117
>**CAUTION**<br>ohos.permission.DISTRIBUTED_DATASYNC is required for using the **setDistributedTables**, **obtainDistributedTableName**, **sync**, **on**, and **off** APIs of **RdbStore**.
A
Annie_wang 已提交
118

A
annie_wangli 已提交
119 120
**Setting Distributed Tables**

A
Annie_wang 已提交
121
**Table 8** API for setting distributed tables
A
annie_wangli 已提交
122

A
Annie_wang 已提交
123 124 125
| 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.|
A
annie_wangli 已提交
126 127 128 129 130

**Obtaining the Distributed Table Name for a Remote Device**

You can obtain the distributed table name for a remote device based on the local table name. The distributed table name can be used to query the RDB store of the remote device.

A
Annie_wang 已提交
131
**Table 9** API for obtaining the distributed table name of a remote device
A
annie_wangli 已提交
132

A
Annie_wang 已提交
133 134 135
| 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.|
A
annie_wangli 已提交
136 137 138

**Synchronizing Data Between Devices**

A
Annie_wang 已提交
139
**Table 10** API for synchronizing data between devices
A
annie_wangli 已提交
140

A
Annie_wang 已提交
141 142 143
| 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 that device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure.|
A
annie_wangli 已提交
144 145

**Registering an RDB Store Observer**
Z
zengyawen 已提交
146

A
annie_wangli 已提交
147
**Table 11** API for registering an observer
Z
zengyawen 已提交
148

A
Annie_wang 已提交
149 150 151
| 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.|
A
annie_wangli 已提交
152 153 154

**Unregistering an RDB Store Observer**

A
annie_wangli 已提交
155
**Table 12** API for unregistering an observer
A
annie_wangli 已提交
156

A
Annie_wang 已提交
157 158 159
| 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.|
A
Annie_wang 已提交
160

A
Annie_wang 已提交
161
### Backing Up and Restoring an RDB Store
A
Annie_wang 已提交
162 163 164

**Backing Up an RDB Store**

A
Annie_wang 已提交
165
**Table 13** API for backing up an RDB store
A
Annie_wang 已提交
166

A
Annie_wang 已提交
167 168
| Class    | API                                                      | Description                                                        |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
A
Annie_wang 已提交
169
| 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.|
A
annie_wangli 已提交
170

A
Annie_wang 已提交
171 172
**Restoring an RDB Store**

A
Annie_wang 已提交
173
**Table 14** API for restoring an RDB store
A
Annie_wang 已提交
174

A
Annie_wang 已提交
175 176
| Class    | API                                                      | Description                                                        |
| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
A
Annie_wang 已提交
177
| 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.|
A
annie_wangli 已提交
178

A
Annie_wang 已提交
179 180 181 182
**Transaction**

Table 15 Transaction APIs

A
Annie_wang 已提交
183 184
| Class    | API                 | Description                             |
| -------- | ----------------------- | --------------------------------- |
A
Annie_wang 已提交
185 186 187
| RdbStore | beginTransaction(): void | Starts the transaction before executing SQL statements.|
| RdbStore | commit(): void           | Commits the executed SQL statements.            |
| RdbStore | rollBack(): void         | Rolls back the SQL statements that have been executed.          |
A
Annie_wang 已提交
188

A
annie_wangli 已提交
189 190 191
## How to Develop

1. Create an RDB store.
A
Annie_wang 已提交
192 193 194 195 196 197

   (1) Configure the RDB store attributes, including the RDB store name, storage mode, and whether read-only mode is used.

   (2) Initialize the table structure and related data in the RDB store.

   (3) Create an RDB store.
A
annie_wangli 已提交
198 199 200

   The sample code is as follows:

A
Annie_wang 已提交
201 202 203 204
    ```js
    import data_rdb from '@ohos.data.rdb'

    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)";
A
Annie_wang 已提交
205
    const STORE_CONFIG = { name: "rdbstore.db" }
A
Annie_wang 已提交
206
    data_rdb.getRdbStore(this.context, STORE_CONFIG, 1, function (err, rdbStore) {
A
Annie_wang 已提交
207 208
      rdbStore.executeSql(CREATE_TABLE_TEST)
      console.info('create table done.')
A
Annie_wang 已提交
209 210
    })
    ```
A
annie_wangli 已提交
211 212

2. Insert data.
A
Annie_wang 已提交
213 214 215 216

   (1) Create a **ValuesBucket** to store the data you need to insert.

   (2) Call the **insert()** method to insert data into the RDB store.
A
annie_wangli 已提交
217 218 219

   The sample code is as follows:

A
Annie_wang 已提交
220 221
    ```js
    var u8 = new Uint8Array([1, 2, 3])
A
Annie_wang 已提交
222
    const valueBucket = { "name": "Tom", "age": 18, "salary": 100.5, "blobType": u8 }
A
Annie_wang 已提交
223 224
    let insertPromise = rdbStore.insert("test", valueBucket)
    ```
A
annie_wangli 已提交
225 226

3. Query data.
A
Annie_wang 已提交
227 228 229

   (1) Create an **RdbPredicates** object to specify query conditions.

A
Annie_wang 已提交
230
   (2) Call the **query()** API to query data.
A
Annie_wang 已提交
231

A
Annie_wang 已提交
232
   (3) Call the **resultSet()** API to obtain the result.
A
annie_wangli 已提交
233 234 235

   The sample code is as follows:

A
Annie_wang 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248
    ```js
    let predicates = new data_rdb.RdbPredicates("test");
    predicates.equalTo("name", "Tom")
    let promisequery = rdbStore.query(predicates)
    promisequery.then((resultSet) => {
        resultSet.goToFirstRow()
        const id = resultSet.getLong(resultSet.getColumnIndex("id"))
        const name = resultSet.getString(resultSet.getColumnIndex("name"))
        const age = resultSet.getLong(resultSet.getColumnIndex("age"))
        const salary = resultSet.getDouble(resultSet.getColumnIndex("salary"))
        const blobType = resultSet.getBlob(resultSet.getColumnIndex("blobType"))
        resultSet.close()
    })
A
Annie_wang 已提交
249
    ```
A
annie_wangli 已提交
250 251

4. Set the distributed tables to be synchronized.
A
Annie_wang 已提交
252 253 254

    (1) Add the following permission to the permission configuration file:   

255
    ```json
A
Annie_wang 已提交
256
    "requestPermissions": 
257
      {
A
Annie_wang 已提交
258
        "name": "ohos.permission.DISTRIBUTED_DATASYNC"
259
      }
A
Annie_wang 已提交
260
    ```
A
Annie_wang 已提交
261

A
Annie_wang 已提交
262
    (2) Obtain the required permissions.
A
Annie_wang 已提交
263 264 265 266

    (3) Set the distributed tables.

    (4) Check whether the setting is successful.
A
annie_wangli 已提交
267 268 269

   The sample code is as follows:

A
Annie_wang 已提交
270 271 272 273 274 275 276 277 278 279 280 281
    ```js
    let context = featureAbility.getContext();
    context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666, function (result) {
        console.info(`result.requestCode=${result.requestCode}`)
    })
    let promise = rdbStore.setDistributedTables(["test"])
    promise.then(() => {
        console.info("setDistributedTables success.")
    }).catch((err) => {
        console.info("setDistributedTables failed.")
    })
    ```
A
annie_wangli 已提交
282

A
Annie_wang 已提交
283
5. Synchronize data across devices.
A
Annie_wang 已提交
284 285 286 287 288 289

    (1) Construct an **RdbPredicates** object to specify remote devices within the network to be synchronized.

    (2) Call **rdbStore.sync()** to synchronize data.

    (3) Check whether the data synchronization is successful.
Z
zengyawen 已提交
290 291 292

    The sample code is as follows:

293
    ```js
A
annie_wangli 已提交
294
    let predicate = new data_rdb.RdbPredicates('test')
A
annie_wangli 已提交
295
    predicate.inDevices(['12345678abcde'])
A
Annie_wang 已提交
296
    let promise = rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicate)
A
annie_wangli 已提交
297
    promise.then((result) => {
A
annie_wangli 已提交
298 299
        console.log('sync done.')
        for (let i = 0; i < result.length; i++) {
A
Annie_wang 已提交
300
            console.log('device=' + result[i][0] + 'status=' + result[i][1])
A
annie_wangli 已提交
301 302 303 304
        }
    }).catch((err) => {
        console.log('sync failed')
    })
Z
zengyawen 已提交
305 306
    ```

A
annie_wangli 已提交
307
6. Subscribe to distributed data.
A
Annie_wang 已提交
308 309 310 311
  
    (1) Register an observer to listen for distributed data changes.

    (2) When data in the RDB store changes, a callback will be invoked to return the data changes.
Z
zengyawen 已提交
312 313 314

    The sample code is as follows:

315
    ```js
A
annie_wangli 已提交
316 317
    function storeObserver(devices) {
        for (let i = 0; i < devices.length; i++) {
A
Annie_wang 已提交
318
            console.log('device=' + device[i] + 'data changed')
A
annie_wangli 已提交
319 320
        }
    }
A
Annie_wang 已提交
321

A
annie_wangli 已提交
322
    try {
A
Annie_wang 已提交
323
        rdbStore.on('dataChange', data_rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver)
A
annie_wangli 已提交
324 325 326
    } catch (err) {
        console.log('register observer failed')
    }
Z
zengyawen 已提交
327 328
    ```

A
annie_wangli 已提交
329
7. Query data across devices.
A
Annie_wang 已提交
330 331 332 333
   
    (1) Obtain the distributed table name for a remote device based on the local table name.

    (2) Call the resultSet() API to obtain the result.
A
annie_wangli 已提交
334 335

    The sample code is as follows:
Z
zengyawen 已提交
336

337
    ```js
A
annie_wangli 已提交
338 339 340
    let tableName = rdbStore.obtainDistributedTableName(deviceId, "test");
    let resultSet = rdbStore.querySql("SELECT * FROM " + tableName)
    ```
A
Annie_wang 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    
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.
   
   (2) Call the resultSet() API to obtain the result.
   
   The sample code is as follows:
   
    ```js
    let rdbPredicate = new data_rdb.RdbPredicates('employee')
    predicates.greaterThan("id", 0) 
    let promiseQuery = rdbStore.remoteQuery('12345678abcde', 'employee', rdbPredicate)
    promiseQuery.then((resultSet) => {
        while (resultSet.goToNextRow()) {
            let idx = resultSet.getLong(0);
            let name = resultSet.getString(1);
            let age = resultSet.getLong(2);
            console.info(idx + " " + name + " " + age);
        }
        resultSet.close();
    }).catch((err) => {
        console.info("failed to remoteQuery, err: " + err)
    })
    ```
   
9. Back up and restore an RDB store.
A
Annie_wang 已提交
369 370 371

   (1) Back up the current RDB store.

A
Annie_wang 已提交
372
    The sample code is as follows:
A
Annie_wang 已提交
373 374 375

    ```js
    let promiseBackup = rdbStore.backup("dbBackup.db")
A
Annie_wang 已提交
376
    promiseBackup.then(() => {
A
Annie_wang 已提交
377
        console.info('Backup success.')
A
Annie_wang 已提交
378
    }).catch((err) => {
A
Annie_wang 已提交
379 380 381
        console.info('Backup failed, err: ' + err)
    })
    ```
A
Annie_wang 已提交
382 383 384 385
   (2) Restore the RDB store using the backup file.
   
    The sample code is as follows:

A
Annie_wang 已提交
386 387
    ```js
    let promiseRestore = rdbStore.restore("dbBackup.db")
A
Annie_wang 已提交
388
    promiseRestore.then(() => {
A
Annie_wang 已提交
389
        console.info('Restore success.')
A
Annie_wang 已提交
390
    }).catch((err) => {
A
Annie_wang 已提交
391 392 393
        console.info('Restore failed, err: ' + err)
    })
    ```