js-apis-data-rdb.md 59.7 KB
Newer Older
A
Annie_wang 已提交
1
# @ohos.data.rdb (RDB)
Z
zengyawen 已提交
2

A
Annie_wang 已提交
3 4 5 6
The relational database (RDB) manages data based on relational models. With the underlying SQLite database, the RDB provides a complete mechanism for managing local databases. To satisfy different needs in complicated scenarios, the RDB offers a series of methods for performing operations such as adding, deleting, modifying, and querying data, and supports direct execution of SQL statements.

This module provides the following RDB-related functions:

A
Annie_wang 已提交
7
- [RdbPredicates](#rdbpredicates): provides predicates indicating the nature, feature, or relationship of a data entity in an RDB store. It is used to define the operation conditions for an RDB store.
A
Annie_wang 已提交
8 9
- [RdbStore](#rdbstore): provides APIs for managing an RDB store.

A
Annie_wang 已提交
10
> **NOTE**<br/>
A
Annie_wang 已提交
11
>
A
annie_wangli 已提交
12
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A
Annie_wang 已提交
13 14 15
> 
> The APIs of this module are no longer maintained since API version 9. You are advised to use [@ohos.data.relationalStore](js-apis-data-relationalStore.md).

Z
zengyawen 已提交
16

A
annie_wangli 已提交
17
## Modules to Import
Z
zengyawen 已提交
18

A
Annie_wang 已提交
19 20
```js
import data_rdb from '@ohos.data.rdb';
Z
zengyawen 已提交
21
```
A
annie_wangli 已提交
22 23
## data_rdb.getRdbStore

A
Annie_wang 已提交
24 25
getRdbStore(context: Context, config: StoreConfig, version: number, callback: AsyncCallback&lt;RdbStore&gt;): void

A
Annie_wang 已提交
26
Obtains an RDB store. This API uses an asynchronous callback to return the result. You can set parameters for the RDB store based on service requirements and call APIs to perform data operations.
A
Annie_wang 已提交
27 28 29 30 31

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

**Parameters**

A
Annie_wang 已提交
32 33 34 35 36 37
| Name  | Type                                      | Mandatory| Description                                                        |
| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ |
| context  | Context                                    | Yes  | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-ability-context.md).|
| config   | [StoreConfig](#storeconfig)                | Yes  | Configuration of the RDB store.                               |
| version  | number                                     | Yes  | RDB store version.<br>Currently, automatic RDB upgrades and downgrades performed based on **version** is not supported.                                                |
| callback | AsyncCallback&lt;[RdbStore](#rdbstore)&gt; | Yes  | Callback invoked to return the RDB store obtained.                    |
A
Annie_wang 已提交
38 39 40

**Example**

A
Annie_wang 已提交
41 42
FA model:

A
Annie_wang 已提交
43
```js
A
Annie_wang 已提交
44 45
// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility'
A
Annie_wang 已提交
46
let context = featureAbility.getContext()
A
Annie_wang 已提交
47 48

// Call getRdbStore.
A
Annie_wang 已提交
49
const STORE_CONFIG = { name: "RdbTest.db"}
A
Annie_wang 已提交
50
data_rdb.getRdbStore(context, STORE_CONFIG, 1, function (err, rdbStore) {
A
Annie_wang 已提交
51
    if (err) {
A
Annie_wang 已提交
52
        console.info("Failed to get RdbStore, err: " + err)
A
Annie_wang 已提交
53 54
        return
    }
A
Annie_wang 已提交
55
    console.log("Got RdbStore successfully.")
A
Annie_wang 已提交
56
})
A
annie_wangli 已提交
57
```
A
Annie_wang 已提交
58

A
Annie_wang 已提交
59 60 61 62
Stage model:

```ts
// Obtain the context.
A
Annie_wang 已提交
63 64 65 66
import UIAbility from '@ohos.app.ability.UIAbility';

let context;
class EntryAbility extends UIAbility {
A
Annie_wang 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    onWindowStageCreate(windowStage){
        context = this.context
    }
}

// Call getRdbStore.
const STORE_CONFIG = { name: "RdbTest.db"}
data_rdb.getRdbStore(context, STORE_CONFIG, 1, function (err, rdbStore) {
    if (err) {
        console.info("Failed to get RdbStore, err: " + err)
        return
    }
    console.log("Got RdbStore successfully.")
})
```
A
Annie_wang 已提交
82

A
Annie_wang 已提交
83
## data_rdb.getRdbStore
A
Annie_wang 已提交
84 85 86 87 88 89 90 91 92

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.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

**Parameters**

A
Annie_wang 已提交
93 94 95 96 97
| Name | Type                       | Mandatory| Description                                                        |
| ------- | --------------------------- | ---- | ------------------------------------------------------------ |
| context | Context                     | Yes  | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-ability-context.md).|
| config  | [StoreConfig](#storeconfig) | Yes  | Configuration of the RDB store.                               |
| version | number                      | Yes  | RDB store version.<br>Currently, automatic RDB upgrades and downgrades performed based on **version** is not supported.                                                |
A
Annie_wang 已提交
98 99 100

**Return value**

A
Annie_wang 已提交
101 102
| Type                                | Description                           |
| ------------------------------------ | ------------------------------- |
A
Annie_wang 已提交
103 104 105 106
| Promise&lt;[RdbStore](#rdbstore)&gt; | Promise used to return the RDB store obtained.|

**Example**

A
Annie_wang 已提交
107 108
FA model:

A
Annie_wang 已提交
109
```js
A
Annie_wang 已提交
110 111
// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility'
A
Annie_wang 已提交
112
let context = featureAbility.getContext()
A
Annie_wang 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

// Call getRdbStore.
const STORE_CONFIG = { name: "RdbTest.db" }
let promise = data_rdb.getRdbStore(context, STORE_CONFIG, 1);
promise.then(async (rdbStore) => {
    console.log("Got RdbStore successfully.")
}).catch((err) => {
    console.log("Failed to get RdbStore, err: " + err)
})
```

Stage model:

```ts
// Obtain the context.
A
Annie_wang 已提交
128 129 130 131
import UIAbility from '@ohos.app.ability.UIAbility';

let context;
class EntryAbility extends UIAbility {
A
Annie_wang 已提交
132 133 134 135 136 137
    onWindowStageCreate(windowStage){
        context = this.context
    }
}

// Call getRdbStore.
A
Annie_wang 已提交
138
const STORE_CONFIG = { name: "RdbTest.db" }
A
Annie_wang 已提交
139
let promise = data_rdb.getRdbStore(context, STORE_CONFIG, 1);
A
Annie_wang 已提交
140
promise.then(async (rdbStore) => {
A
Annie_wang 已提交
141
    console.log("Got RdbStore successfully.")
A
Annie_wang 已提交
142
}).catch((err) => {
A
Annie_wang 已提交
143
    console.log("Failed to get RdbStore, err: " + err)
A
Annie_wang 已提交
144
})
A
annie_wangli 已提交
145
```
A
annie_wangli 已提交
146 147 148

## data_rdb.deleteRdbStore

A
Annie_wang 已提交
149 150
deleteRdbStore(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
151
Deletes an RDB store. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
152 153 154 155

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

**Parameters**
A
Annie_wang 已提交
156

A
Annie_wang 已提交
157 158 159 160 161
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| context  | Context                   | Yes  | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-ability-context.md).|
| name     | string                    | Yes  | Name of the RDB store to delete.                                                |
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback invoked to return the result.                                      |
A
Annie_wang 已提交
162 163

**Example**
A
Annie_wang 已提交
164 165 166

FA model:

A
Annie_wang 已提交
167
```js
A
Annie_wang 已提交
168 169
// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility'
A
Annie_wang 已提交
170
let context = featureAbility.getContext()
A
Annie_wang 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

// Call deleteRdbStore.
data_rdb.deleteRdbStore(context, "RdbTest.db", function (err) {
    if (err) {
        console.info("Failed to delete RdbStore, err: " + err)
        return
    }
    console.log("Deleted RdbStore successfully.")
})
```

Stage model:

```ts
// Obtain the context.
A
Annie_wang 已提交
186 187 188 189
import UIAbility from '@ohos.app.ability.UIAbility';

let context;
class EntryAbility extends UIAbility {
A
Annie_wang 已提交
190 191 192 193 194 195 196
    onWindowStageCreate(windowStage){
        context = this.context
    }
}

// Call deleteRdbStore.
data_rdb.deleteRdbStore(context, "RdbTest.db", function (err) {
A
Annie_wang 已提交
197
    if (err) {
A
Annie_wang 已提交
198
        console.info("Failed to delete RdbStore, err: " + err)
A
Annie_wang 已提交
199 200
        return
    }
A
Annie_wang 已提交
201
    console.log("Deleted RdbStore successfully.")
A
Annie_wang 已提交
202 203
})
```
A
Annie_wang 已提交
204

A
Annie_wang 已提交
205
## data_rdb.deleteRdbStore
A
annie_wangli 已提交
206 207

deleteRdbStore(context: Context, name: string): Promise&lt;void&gt;
Z
zengyawen 已提交
208

A
Annie_wang 已提交
209
Deletes an RDB store. This API uses a promise to return the result.
Z
zengyawen 已提交
210

A
annie_wangli 已提交
211
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
212

A
annie_wangli 已提交
213
**Parameters**
A
Annie_wang 已提交
214

A
Annie_wang 已提交
215 216 217 218
| Name | Type   | Mandatory| Description                                                        |
| ------- | ------- | ---- | ------------------------------------------------------------ |
| context | Context | Yes  | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-ability-context.md).|
| name    | string  | Yes  | Name of the RDB store to delete.                                                |
A
annie_wangli 已提交
219

A
annie_wangli 已提交
220
**Return value**
A
Annie_wang 已提交
221

A
Annie_wang 已提交
222 223 224
| Type               | Description                     |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
225 226

**Example**
A
Annie_wang 已提交
227 228 229

FA model:

A
Annie_wang 已提交
230
```js
A
Annie_wang 已提交
231 232
// Obtain the context.
import featureAbility from '@ohos.ability.featureAbility'
A
Annie_wang 已提交
233
let context = featureAbility.getContext()
A
Annie_wang 已提交
234 235 236

// Call deleteRdbStore.
let promise = data_rdb.deleteRdbStore(context, "RdbTest.db")
A
Annie_wang 已提交
237
promise.then(() => {
A
Annie_wang 已提交
238
    console.log("Deleted RdbStore successfully.")
A
Annie_wang 已提交
239
}).catch((err) => {
A
Annie_wang 已提交
240
    console.info("Failed to delete RdbStore, err: " + err)
A
Annie_wang 已提交
241 242
})
```
A
annie_wangli 已提交
243

A
Annie_wang 已提交
244 245 246 247
Stage model:

```ts
// Obtain the context.
A
Annie_wang 已提交
248 249 250 251
import UIAbility from '@ohos.app.ability.UIAbility';

let context;
class EntryAbility extends UIAbility {
A
Annie_wang 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264
    onWindowStageCreate(windowStage){
        context = this.context
    }
}

// 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)
})
```
A
annie_wangli 已提交
265

A
Annie_wang 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
## ValueType

Defines the data types allowed.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

| Type   | Description                |
| ------- | -------------------- |
| number  | Number.  |
| string  | String.  |
| boolean | Boolean.|


## ValuesBucket

Defines the types of the key and value in a KV pair.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

| Key Type| Value Type                                                     |
| ------ | ----------------------------------------------------------- |
| string | [ValueType](#valuetype)\|&nbsp;Uint8Array&nbsp;\|&nbsp;null |

## SyncMode<sup>8+</sup>

Defines the database synchronization mode.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

| Name          | Value  | Description                              |
| -------------- | ---- | ---------------------------------- |
| SYNC_MODE_PUSH | 0    | Data is pushed from a local device to a remote device.|
| SYNC_MODE_PULL | 1    | Data is pulled from a remote device to a local device.  |

## SubscribeType<sup>8+</sup>

Defines the subscription type.

**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

| Name                 | Value  | Description              |
| --------------------- | ---- | ------------------ |
| SUBSCRIBE_TYPE_REMOTE | 0    | Subscribe to remote data changes.|

## StoreConfig

Defines the RDB store configuration.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Database file name.|

A
annie_wangli 已提交
322
## RdbPredicates
Z
zengyawen 已提交
323 324 325

Defines predicates for an RDB store. This class determines whether the conditional expression for the RDB store is true or false.

A
annie_wangli 已提交
326 327 328 329 330 331
### constructor

constructor(name: string)

A constructor used to create an **RdbPredicates** object.

A
annie_wangli 已提交
332
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
333

A
annie_wangli 已提交
334
**Parameters**
A
Annie_wang 已提交
335

A
annie_wangli 已提交
336 337 338
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Database table name.|
A
annie_wangli 已提交
339

A
annie_wangli 已提交
340
**Example**
A
Annie_wang 已提交
341

A
Annie_wang 已提交
342 343 344
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
```
A
annie_wangli 已提交
345

A
annie_wangli 已提交
346
### inDevices<sup>8+</sup>
A
annie_wangli 已提交
347

A
annie_wangli 已提交
348
inDevices(devices: Array&lt;string&gt;): RdbPredicates
A
annie_wangli 已提交
349

A
Annie_wang 已提交
350
Sets an **RdbPredicates** to specify the remote devices to connect on the network during distributed database synchronization.
A
annie_wangli 已提交
351

A
annie_wangli 已提交
352
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
353

A
annie_wangli 已提交
354
**Parameters**
A
Annie_wang 已提交
355

A
annie_wangli 已提交
356 357
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
358
| devices | Array&lt;string&gt; | Yes| IDs of the remote devices in the same network.|
A
annie_wangli 已提交
359

A
annie_wangli 已提交
360
**Return value**
A
Annie_wang 已提交
361

A
annie_wangli 已提交
362 363
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
364
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
365

A
annie_wangli 已提交
366
**Example**
A
Annie_wang 已提交
367

A
Annie_wang 已提交
368 369 370 371
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.inDevices(['12345678abcde'])
```
A
annie_wangli 已提交
372

A
annie_wangli 已提交
373
### inAllDevices<sup>8+</sup>
A
annie_wangli 已提交
374

A
annie_wangli 已提交
375
inAllDevices(): RdbPredicates
A
annie_wangli 已提交
376

A
Annie_wang 已提交
377
Sets an **RdbPredicates** to specify all remote devices on the network to connect during distributed database synchronization.
A
annie_wangli 已提交
378

A
annie_wangli 已提交
379
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
380

A
annie_wangli 已提交
381
**Return value**
A
Annie_wang 已提交
382

A
annie_wangli 已提交
383 384
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
385
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
386

A
annie_wangli 已提交
387
**Example**
A
Annie_wang 已提交
388

A
Annie_wang 已提交
389 390 391 392
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.inAllDevices()
```
A
annie_wangli 已提交
393 394 395 396 397

### equalTo

equalTo(field: string, value: ValueType): RdbPredicates

A
Annie_wang 已提交
398
Sets an **RdbPredicates** to match the field with data type **ValueType** and value equal to the specified value.
A
annie_wangli 已提交
399

A
annie_wangli 已提交
400
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
401

A
annie_wangli 已提交
402
**Parameters**
A
Annie_wang 已提交
403

A
annie_wangli 已提交
404 405 406 407
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|
A
annie_wangli 已提交
408

A
annie_wangli 已提交
409
**Return value**
A
Annie_wang 已提交
410

A
annie_wangli 已提交
411 412
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
413
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
414

A
annie_wangli 已提交
415
**Example**
A
Annie_wang 已提交
416

A
Annie_wang 已提交
417 418 419 420
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "lisi")
```
A
annie_wangli 已提交
421 422 423 424 425 426


### notEqualTo

notEqualTo(field: string, value: ValueType): RdbPredicates

A
Annie_wang 已提交
427
Sets an **RdbPredicates** to match the field with data type **ValueType** and value not equal to the specified value.
A
annie_wangli 已提交
428

A
annie_wangli 已提交
429
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
430

A
annie_wangli 已提交
431
**Parameters**
A
Annie_wang 已提交
432

A
annie_wangli 已提交
433 434 435 436
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|
A
annie_wangli 已提交
437

A
annie_wangli 已提交
438
**Return value**
A
Annie_wang 已提交
439

A
annie_wangli 已提交
440 441
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
442
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
443

A
annie_wangli 已提交
444
**Example**
A
Annie_wang 已提交
445

A
Annie_wang 已提交
446 447 448 449
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.notEqualTo("NAME", "lisi")
```
A
annie_wangli 已提交
450 451 452 453 454 455 456 457


### beginWrap

beginWrap(): RdbPredicates

Adds a left parenthesis to the **RdbPredicates**.

A
annie_wangli 已提交
458
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
459

A
annie_wangli 已提交
460
**Return value**
A
Annie_wang 已提交
461

A
annie_wangli 已提交
462 463 464
| Type| Description|
| -------- | -------- |
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** with a left parenthesis.|
A
annie_wangli 已提交
465

A
annie_wangli 已提交
466
**Example**
A
Annie_wang 已提交
467

A
Annie_wang 已提交
468 469 470 471 472 473 474 475 476
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "lisi")
    .beginWrap()
    .equalTo("AGE", 18)
    .or()
    .equalTo("SALARY", 200.5)
    .endWrap()
```
A
annie_wangli 已提交
477 478 479 480 481 482 483

### endWrap

endWrap(): RdbPredicates

Adds a right parenthesis to the **RdbPredicates**.

A
annie_wangli 已提交
484
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
485

A
annie_wangli 已提交
486
**Return value**
A
Annie_wang 已提交
487

A
annie_wangli 已提交
488 489 490
| Type| Description|
| -------- | -------- |
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** with a right parenthesis.|
A
annie_wangli 已提交
491

A
annie_wangli 已提交
492
**Example**
A
Annie_wang 已提交
493

A
Annie_wang 已提交
494 495 496 497 498 499 500 501 502
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "lisi")
    .beginWrap()
    .equalTo("AGE", 18)
    .or()
    .equalTo("SALARY", 200.5)
    .endWrap()
```
A
annie_wangli 已提交
503 504 505 506 507 508 509

### or

or(): RdbPredicates

Adds the OR condition to the **RdbPredicates**.

A
annie_wangli 已提交
510
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
511

A
annie_wangli 已提交
512
**Return value**
A
Annie_wang 已提交
513

A
annie_wangli 已提交
514 515 516
| Type| Description|
| -------- | -------- |
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** with the OR condition.|
A
annie_wangli 已提交
517

A
annie_wangli 已提交
518
**Example**
A
Annie_wang 已提交
519

A
Annie_wang 已提交
520 521 522 523 524 525
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
    .or()
    .equalTo("NAME", "Rose")
```
A
annie_wangli 已提交
526 527 528 529 530 531 532

### and

and(): RdbPredicates

Adds the AND condition to the **RdbPredicates**.

A
annie_wangli 已提交
533
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
534

A
annie_wangli 已提交
535
**Return value**
A
Annie_wang 已提交
536

A
annie_wangli 已提交
537 538 539
| Type| Description|
| -------- | -------- |
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** with the AND condition.|
A
annie_wangli 已提交
540

A
annie_wangli 已提交
541
**Example**
A
Annie_wang 已提交
542

A
Annie_wang 已提交
543 544 545 546 547 548
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
    .and()
    .equalTo("SALARY", 200.5)
```
A
annie_wangli 已提交
549 550 551

### contains

A
annie_wangli 已提交
552
contains(field: string, value: string): RdbPredicates
A
annie_wangli 已提交
553

A
Annie_wang 已提交
554
Sets an **RdbPredicates** to match a string containing the specified value.
A
annie_wangli 已提交
555

A
annie_wangli 已提交
556
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
557

A
annie_wangli 已提交
558
**Parameters**
A
Annie_wang 已提交
559

A
annie_wangli 已提交
560 561 562 563
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | string | Yes| Value to match the **RdbPredicates**.|
A
annie_wangli 已提交
564

A
annie_wangli 已提交
565
**Return value**
A
Annie_wang 已提交
566

A
annie_wangli 已提交
567 568
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
569
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
570

A
annie_wangli 已提交
571
**Example**
A
Annie_wang 已提交
572

A
Annie_wang 已提交
573 574 575 576
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.contains("NAME", "os")
```
A
annie_wangli 已提交
577 578 579 580 581

### beginsWith

beginsWith(field: string, value: string): RdbPredicates

A
Annie_wang 已提交
582
Sets an **RdbPredicates** to match a string that starts with the specified value.
A
annie_wangli 已提交
583

A
annie_wangli 已提交
584
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
585

A
annie_wangli 已提交
586
**Parameters**
A
Annie_wang 已提交
587

A
annie_wangli 已提交
588 589 590 591
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | string | Yes| Value to match the **RdbPredicates**.|
A
annie_wangli 已提交
592

A
annie_wangli 已提交
593
**Return value**
A
Annie_wang 已提交
594

A
annie_wangli 已提交
595 596
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
597
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
598

A
annie_wangli 已提交
599
**Example**
A
Annie_wang 已提交
600

A
Annie_wang 已提交
601 602 603 604
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.beginsWith("NAME", "os")
```
A
annie_wangli 已提交
605 606 607 608 609

### endsWith

endsWith(field: string, value: string): RdbPredicates

A
Annie_wang 已提交
610
Sets an **RdbPredicates** to match a string that ends with the specified value.
A
annie_wangli 已提交
611

A
annie_wangli 已提交
612
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
613

A
annie_wangli 已提交
614
**Parameters**
A
Annie_wang 已提交
615

A
annie_wangli 已提交
616 617 618 619
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | string | Yes| Value to match the **RdbPredicates**.|
A
annie_wangli 已提交
620

A
annie_wangli 已提交
621
**Return value**
A
Annie_wang 已提交
622

A
annie_wangli 已提交
623 624
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
625
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
626

A
annie_wangli 已提交
627
**Example**
A
Annie_wang 已提交
628

A
Annie_wang 已提交
629 630 631 632
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.endsWith("NAME", "se")
```
A
annie_wangli 已提交
633 634 635 636 637

### isNull

isNull(field: string): RdbPredicates

A
Annie_wang 已提交
638
Sets an **RdbPredicates** to match the field whose value is null.
A
annie_wangli 已提交
639

A
annie_wangli 已提交
640
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
641

A
annie_wangli 已提交
642
**Parameters**
A
Annie_wang 已提交
643

A
annie_wangli 已提交
644 645 646
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
A
annie_wangli 已提交
647

A
annie_wangli 已提交
648
**Return value**
A
Annie_wang 已提交
649

A
annie_wangli 已提交
650 651
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
652
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
653

A
Annie_wang 已提交
654
**Example**
A
Annie_wang 已提交
655 656 657 658
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.isNull("NAME")
```
A
annie_wangli 已提交
659 660 661 662 663

### isNotNull

isNotNull(field: string): RdbPredicates

A
Annie_wang 已提交
664
Sets an **RdbPredicates** to match the field whose value is not null.
A
annie_wangli 已提交
665

A
annie_wangli 已提交
666
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
667

A
annie_wangli 已提交
668
**Parameters**
A
Annie_wang 已提交
669

A
annie_wangli 已提交
670 671 672
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
A
annie_wangli 已提交
673

A
annie_wangli 已提交
674
**Return value**
A
Annie_wang 已提交
675

A
annie_wangli 已提交
676 677
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
678
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
679

A
annie_wangli 已提交
680
**Example**
A
Annie_wang 已提交
681

A
Annie_wang 已提交
682 683 684 685
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.isNotNull("NAME")
```
A
annie_wangli 已提交
686 687 688 689 690

### like

like(field: string, value: string): RdbPredicates

A
Annie_wang 已提交
691
Sets an **RdbPredicates** to match a string that is similar to the specified value.
A
annie_wangli 已提交
692

A
annie_wangli 已提交
693
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
694

A
annie_wangli 已提交
695
**Parameters**
A
Annie_wang 已提交
696

A
annie_wangli 已提交
697 698 699 700
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | string | Yes| Value to match the **RdbPredicates**.|
A
annie_wangli 已提交
701

A
annie_wangli 已提交
702
**Return value**
A
Annie_wang 已提交
703

A
annie_wangli 已提交
704 705
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
706
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
707

A
annie_wangli 已提交
708
**Example**
A
Annie_wang 已提交
709

A
Annie_wang 已提交
710 711 712 713
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.like("NAME", "%os%")
```
A
annie_wangli 已提交
714 715 716 717 718

### glob

glob(field: string, value: string): RdbPredicates

A
Annie_wang 已提交
719
Sets an **RdbPredicates** to match the specified string.
A
annie_wangli 已提交
720

A
annie_wangli 已提交
721
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
722

A
annie_wangli 已提交
723
**Parameters**
A
Annie_wang 已提交
724

A
annie_wangli 已提交
725 726 727
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
A
Annie_wang 已提交
728
| value | string | Yes| Value to match the **RdbPredicates**.<br><br>Wildcards are supported. * indicates zero, one, or multiple digits or characters. **?** indicates a single digit or character.|
A
annie_wangli 已提交
729

A
annie_wangli 已提交
730
**Return value**
A
Annie_wang 已提交
731

A
annie_wangli 已提交
732 733
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
734
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
735

A
annie_wangli 已提交
736
**Example**
A
Annie_wang 已提交
737

A
Annie_wang 已提交
738 739 740 741
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.glob("NAME", "?h*g")
```
A
annie_wangli 已提交
742 743 744 745 746

### between

between(field: string, low: ValueType, high: ValueType): RdbPredicates

A
Annie_wang 已提交
747
Sets an **RdbPredicates** to match the field with data type **ValueType** and value within the specified range.
A
annie_wangli 已提交
748

A
annie_wangli 已提交
749
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
750

A
annie_wangli 已提交
751
**Parameters**
A
Annie_wang 已提交
752

A
annie_wangli 已提交
753 754 755 756 757
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| low | [ValueType](#valuetype) | Yes| Minimum value to match the **RdbPredicates**.|
| high | [ValueType](#valuetype) | Yes| Maximum value to match the **RdbPredicates**.|
A
annie_wangli 已提交
758

A
annie_wangli 已提交
759
**Return value**
A
Annie_wang 已提交
760

A
annie_wangli 已提交
761 762
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
763
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
764

A
annie_wangli 已提交
765
**Example**
A
Annie_wang 已提交
766

A
Annie_wang 已提交
767 768 769 770
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.between("AGE", 10, 50)
```
A
annie_wangli 已提交
771 772 773 774 775

### notBetween

notBetween(field: string, low: ValueType, high: ValueType): RdbPredicates

A
Annie_wang 已提交
776
Sets an **RdbPredicates** to match the field with data type **ValueType** and value out of the specified range.
A
annie_wangli 已提交
777

A
annie_wangli 已提交
778
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
779

A
annie_wangli 已提交
780
**Parameters**
A
Annie_wang 已提交
781

A
annie_wangli 已提交
782 783 784 785 786
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| low | [ValueType](#valuetype) | Yes| Minimum value to match the **RdbPredicates**.|
| high | [ValueType](#valuetype) | Yes| Maximum value to match the **RdbPredicates**.|
A
annie_wangli 已提交
787

A
annie_wangli 已提交
788
**Return value**
A
Annie_wang 已提交
789

A
annie_wangli 已提交
790 791
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
792
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
793

A
annie_wangli 已提交
794
**Example**
A
Annie_wang 已提交
795

A
Annie_wang 已提交
796 797 798 799
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.notBetween("AGE", 10, 50)
```
A
annie_wangli 已提交
800 801 802

### greaterThan

A
annie_wangli 已提交
803
greaterThan(field: string, value: ValueType): RdbPredicates
A
annie_wangli 已提交
804

A
Annie_wang 已提交
805
Sets an **RdbPredicates** to match the field with data type **ValueType** and value greater than the specified value.
A
annie_wangli 已提交
806

A
annie_wangli 已提交
807
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
808

A
annie_wangli 已提交
809
**Parameters**
A
Annie_wang 已提交
810

A
annie_wangli 已提交
811 812 813 814
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|
A
annie_wangli 已提交
815

A
annie_wangli 已提交
816
**Return value**
A
Annie_wang 已提交
817

A
annie_wangli 已提交
818 819
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
820
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
821

A
annie_wangli 已提交
822
**Example**
A
Annie_wang 已提交
823

A
Annie_wang 已提交
824 825 826 827
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.greaterThan("AGE", 18)
```
A
annie_wangli 已提交
828 829 830 831 832

### lessThan

lessThan(field: string, value: ValueType): RdbPredicates

A
Annie_wang 已提交
833
Sets an **RdbPredicates** to match the field with data type **ValueType** and value less than the specified value.
A
annie_wangli 已提交
834

A
annie_wangli 已提交
835
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
836

A
annie_wangli 已提交
837
**Parameters**
A
Annie_wang 已提交
838

A
annie_wangli 已提交
839 840 841 842
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|
A
annie_wangli 已提交
843

A
annie_wangli 已提交
844
**Return value**
A
Annie_wang 已提交
845

A
annie_wangli 已提交
846 847
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
848
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
849

A
annie_wangli 已提交
850
**Example**
A
Annie_wang 已提交
851

A
Annie_wang 已提交
852 853 854 855
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.lessThan("AGE", 20)
```
A
annie_wangli 已提交
856 857 858 859 860

### greaterThanOrEqualTo

greaterThanOrEqualTo(field: string, value: ValueType): RdbPredicates

A
Annie_wang 已提交
861
Sets an **RdbPredicates** to match the field with data type **ValueType** and value greater than or equal to the specified value.
A
annie_wangli 已提交
862

A
annie_wangli 已提交
863
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
864

A
annie_wangli 已提交
865
**Parameters**
A
Annie_wang 已提交
866

A
annie_wangli 已提交
867 868 869 870
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|
A
annie_wangli 已提交
871

A
annie_wangli 已提交
872
**Return value**
A
Annie_wang 已提交
873

A
annie_wangli 已提交
874 875
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
876
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
877

A
annie_wangli 已提交
878
**Example**
A
Annie_wang 已提交
879

A
Annie_wang 已提交
880 881 882 883
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.greaterThanOrEqualTo("AGE", 18)
```
A
annie_wangli 已提交
884 885 886 887 888

### lessThanOrEqualTo

lessThanOrEqualTo(field: string, value: ValueType): RdbPredicates

A
Annie_wang 已提交
889
Sets an **RdbPredicates** to match the field with data type **ValueType** and value less than or equal to the specified value.
A
annie_wangli 已提交
890

A
annie_wangli 已提交
891
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
892

A
annie_wangli 已提交
893
**Parameters**
A
Annie_wang 已提交
894

A
annie_wangli 已提交
895 896 897 898
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|
A
annie_wangli 已提交
899

A
annie_wangli 已提交
900
**Return value**
A
Annie_wang 已提交
901

A
annie_wangli 已提交
902 903
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
904
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
905

A
annie_wangli 已提交
906
**Example**
A
Annie_wang 已提交
907

A
Annie_wang 已提交
908 909 910 911
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.lessThanOrEqualTo("AGE", 20)
```
A
annie_wangli 已提交
912 913 914 915 916

### orderByAsc

orderByAsc(field: string): RdbPredicates

A
Annie_wang 已提交
917
Sets an **RdbPredicates** to match the column with values sorted in ascending order.
A
annie_wangli 已提交
918

A
annie_wangli 已提交
919
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
920

A
annie_wangli 已提交
921
**Parameters**
A
Annie_wang 已提交
922

A
annie_wangli 已提交
923 924 925
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
A
annie_wangli 已提交
926

A
annie_wangli 已提交
927
**Return value**
A
Annie_wang 已提交
928

A
annie_wangli 已提交
929 930
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
931
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
932

A
annie_wangli 已提交
933
**Example**
A
Annie_wang 已提交
934

A
Annie_wang 已提交
935 936 937 938
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.orderByAsc("NAME")
```
A
annie_wangli 已提交
939 940 941 942 943

### orderByDesc

orderByDesc(field: string): RdbPredicates

A
Annie_wang 已提交
944
Sets an **RdbPredicates** to match the column with values sorted in descending order.
A
annie_wangli 已提交
945

A
annie_wangli 已提交
946
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
947

A
annie_wangli 已提交
948
**Parameters**
A
Annie_wang 已提交
949

A
annie_wangli 已提交
950 951 952
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
A
annie_wangli 已提交
953

A
annie_wangli 已提交
954
**Return value**
A
Annie_wang 已提交
955

A
annie_wangli 已提交
956 957
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
958
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
959

A
annie_wangli 已提交
960
**Example**
A
Annie_wang 已提交
961

A
Annie_wang 已提交
962 963 964 965
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.orderByDesc("AGE")
```
A
annie_wangli 已提交
966 967 968 969 970

### distinct

distinct(): RdbPredicates

A
Annie_wang 已提交
971
Sets an **RdbPredicates** to filter out duplicate records.
A
annie_wangli 已提交
972

A
annie_wangli 已提交
973
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
974

A
annie_wangli 已提交
975
**Return value**
A
Annie_wang 已提交
976

A
annie_wangli 已提交
977 978 979
| Type| Description|
| -------- | -------- |
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that can filter out duplicate records.|
A
annie_wangli 已提交
980

A
annie_wangli 已提交
981
**Example**
A
Annie_wang 已提交
982

A
Annie_wang 已提交
983 984
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
A
Annie_wang 已提交
985
predicates.equalTo("NAME", "Rose").distinct()
A
Annie_wang 已提交
986
```
A
annie_wangli 已提交
987 988 989 990 991

### limitAs

limitAs(value: number): RdbPredicates

A
Annie_wang 已提交
992
Sets an **RdbPredicates** to specify the maximum number of records.
A
annie_wangli 已提交
993

A
annie_wangli 已提交
994
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
995

A
annie_wangli 已提交
996
**Parameters**
A
Annie_wang 已提交
997

A
annie_wangli 已提交
998 999 1000
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | number | Yes| Maximum number of records.|
A
annie_wangli 已提交
1001

A
annie_wangli 已提交
1002
**Return value**
A
Annie_wang 已提交
1003

A
annie_wangli 已提交
1004 1005 1006
| Type| Description|
| -------- | -------- |
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that specifies the maximum number of records.|
A
annie_wangli 已提交
1007

A
annie_wangli 已提交
1008
**Example**
A
Annie_wang 已提交
1009

A
Annie_wang 已提交
1010 1011 1012 1013
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Rose").limitAs(3)
```
A
annie_wangli 已提交
1014 1015 1016 1017 1018

### offsetAs

offsetAs(rowOffset: number): RdbPredicates

A
Annie_wang 已提交
1019
Sets an **RdbPredicates** to specify the start position of the returned result.
A
annie_wangli 已提交
1020

A
annie_wangli 已提交
1021
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1022

A
annie_wangli 已提交
1023
**Parameters**
A
Annie_wang 已提交
1024

A
annie_wangli 已提交
1025 1026 1027
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| rowOffset | number | Yes| Number of rows to offset from the beginning. The value is a positive integer.|
A
annie_wangli 已提交
1028

A
annie_wangli 已提交
1029
**Return value**
A
Annie_wang 已提交
1030

A
annie_wangli 已提交
1031 1032 1033
| Type| Description|
| -------- | -------- |
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that specifies the start position of the returned result.|
A
annie_wangli 已提交
1034

A
annie_wangli 已提交
1035
**Example**
A
Annie_wang 已提交
1036

A
Annie_wang 已提交
1037 1038 1039 1040
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Rose").offsetAs(3)
```
A
annie_wangli 已提交
1041 1042 1043 1044 1045

### groupBy

groupBy(fields: Array&lt;string&gt;): RdbPredicates

A
Annie_wang 已提交
1046
Sets an **RdbPredicates** to group rows that have the same value into summary rows.
A
annie_wangli 已提交
1047

A
annie_wangli 已提交
1048
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1049

A
annie_wangli 已提交
1050
**Parameters**
A
Annie_wang 已提交
1051

A
annie_wangli 已提交
1052 1053 1054
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fields | Array&lt;string&gt; | Yes| Names of columns to group.|
A
annie_wangli 已提交
1055

A
annie_wangli 已提交
1056
**Return value**
A
Annie_wang 已提交
1057

A
annie_wangli 已提交
1058 1059 1060
| Type| Description|
| -------- | -------- |
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that groups rows with the same value.|
A
annie_wangli 已提交
1061

A
annie_wangli 已提交
1062
**Example**
A
Annie_wang 已提交
1063

A
Annie_wang 已提交
1064 1065 1066 1067
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.groupBy(["AGE", "NAME"])
```
A
annie_wangli 已提交
1068 1069 1070

### indexedBy

A
annie_wangli 已提交
1071
indexedBy(field: string): RdbPredicates
A
annie_wangli 已提交
1072

A
Annie_wang 已提交
1073
Sets an **RdbPredicates** object to specify the index column.
A
annie_wangli 已提交
1074

A
annie_wangli 已提交
1075
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1076

A
annie_wangli 已提交
1077
**Parameters**
A
Annie_wang 已提交
1078

A
annie_wangli 已提交
1079 1080 1081
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Name of the index column.|
A
annie_wangli 已提交
1082

A
annie_wangli 已提交
1083
**Return value**
A
annie_wangli 已提交
1084

A
Annie_wang 已提交
1085

A
annie_wangli 已提交
1086 1087 1088 1089 1090
| Type| Description|
| -------- | -------- |
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that specifies the index column.|

**Example**
A
Annie_wang 已提交
1091

A
Annie_wang 已提交
1092 1093 1094 1095
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.indexedBy("SALARY_INDEX")
```
A
annie_wangli 已提交
1096 1097 1098 1099 1100

### in

in(field: string, value: Array&lt;ValueType&gt;): RdbPredicates

A
Annie_wang 已提交
1101
Sets an **RdbPredicates** to match the field with data type **Array&#60;ValueType&#62;** and value within the specified range.
A
annie_wangli 已提交
1102

A
annie_wangli 已提交
1103
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1104

A
annie_wangli 已提交
1105
**Parameters**
A
Annie_wang 已提交
1106

A
annie_wangli 已提交
1107 1108 1109 1110
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | Array&lt;[ValueType](#valuetype)&gt; | Yes| Array of **ValueType**s to match.|
A
annie_wangli 已提交
1111

A
annie_wangli 已提交
1112
**Return value**
A
Annie_wang 已提交
1113

A
annie_wangli 已提交
1114 1115
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
1116
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
1117

A
annie_wangli 已提交
1118
**Example**
A
Annie_wang 已提交
1119

A
Annie_wang 已提交
1120 1121 1122 1123
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.in("AGE", [18, 20])
```
A
annie_wangli 已提交
1124 1125 1126 1127 1128

### notIn

notIn(field: string, value: Array&lt;ValueType&gt;): RdbPredicates

A
Annie_wang 已提交
1129
Sets an **RdbPredicates** to match the field with data type **Array&#60;ValueType&#62;** and value out of the specified range.
A
annie_wangli 已提交
1130

A
annie_wangli 已提交
1131
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1132

A
annie_wangli 已提交
1133
**Parameters**
A
Annie_wang 已提交
1134

A
annie_wangli 已提交
1135 1136 1137 1138
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| field | string | Yes| Column name in the database table.|
| value | Array&lt;[ValueType](#valuetype)&gt; | Yes| Array of **ValueType**s to match.|
A
annie_wangli 已提交
1139

A
annie_wangli 已提交
1140
**Return value**
A
Annie_wang 已提交
1141

A
annie_wangli 已提交
1142 1143
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
1144
| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object created.|
A
annie_wangli 已提交
1145

A
annie_wangli 已提交
1146
**Example**
A
Annie_wang 已提交
1147

A
Annie_wang 已提交
1148 1149 1150 1151
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.notIn("NAME", ["Lisa", "Rose"])
```
A
annie_wangli 已提交
1152 1153

## RdbStore
Z
zengyawen 已提交
1154 1155 1156

Provides methods to manage an RDB store.

A
Annie_wang 已提交
1157
Before using the following APIs, use [executeSql](#executesql8) to initialize the database table structure and related data. For details, see [RDB Development](../../database/database-relational-guidelines.md).
A
Annie_wang 已提交
1158

A
annie_wangli 已提交
1159 1160
### insert

A
Annie_wang 已提交
1161
insert(table: string, values: ValuesBucket, callback: AsyncCallback&lt;number&gt;):void
Z
zengyawen 已提交
1162

A
Annie_wang 已提交
1163
Inserts a row of data into a table. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
1164

A
annie_wangli 已提交
1165
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1166

A
annie_wangli 已提交
1167
**Parameters**
A
Annie_wang 已提交
1168

A
annie_wangli 已提交
1169 1170
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
1171
| table | string | Yes| Name of the target table.|
A
annie_wangli 已提交
1172 1173 1174 1175
| values | [ValuesBucket](#valuesbucket) | Yes| Row of data to insert.|
| callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked to return the result. If the operation is successful, the row ID will be returned. Otherwise, **-1** will be returned.|

**Example**
A
Annie_wang 已提交
1176

A
Annie_wang 已提交
1177 1178 1179 1180 1181 1182 1183
```js
const valueBucket = {
    "NAME": "Lisa",
    "AGE": 18,
    "SALARY": 100.5,
    "CODES": new Uint8Array([1, 2, 3, 4, 5]),
}
A
Annie_wang 已提交
1184 1185 1186 1187
rdbStore.insert("EMPLOYEE", valueBucket, function (status, rowId) {
    if (status) {
        console.log("Failed to insert data");
        return;
A
Annie_wang 已提交
1188
    }
A
Annie_wang 已提交
1189
    console.log("Inserted data successfully, rowId = " + rowId);
A
Annie_wang 已提交
1190 1191
})
```
A
annie_wangli 已提交
1192 1193 1194

### insert

A
Annie_wang 已提交
1195
insert(table: string, values: ValuesBucket):Promise&lt;number&gt;
Z
zengyawen 已提交
1196

A
Annie_wang 已提交
1197
Inserts a row of data into a table. This API uses a promise to return the result.
Z
zengyawen 已提交
1198

A
annie_wangli 已提交
1199
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1200

A
annie_wangli 已提交
1201
**Parameters**
A
Annie_wang 已提交
1202

A
annie_wangli 已提交
1203 1204
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
1205
| table | string | Yes| Name of the target table.|
A
annie_wangli 已提交
1206
| values | [ValuesBucket](#valuesbucket) | Yes| Row of data to insert.|
A
annie_wangli 已提交
1207

A
annie_wangli 已提交
1208
**Return value**
A
Annie_wang 已提交
1209

A
annie_wangli 已提交
1210 1211 1212 1213 1214
| Type| Description|
| -------- | -------- |
| Promise&lt;number&gt; | Promise used to return the result. If the operation is successful, the row ID will be returned. Otherwise, **-1** will be returned.|

**Example**
A
Annie_wang 已提交
1215

A
Annie_wang 已提交
1216 1217 1218 1219 1220 1221 1222 1223
```js
const valueBucket = {
    "NAME": "Lisa",
    "AGE": 18,
    "SALARY": 100.5,
    "CODES": new Uint8Array([1, 2, 3, 4, 5]),
}
let promise = rdbStore.insert("EMPLOYEE", valueBucket)
A
Annie_wang 已提交
1224
promise.then((rowId) => {
A
Annie_wang 已提交
1225
    console.log("Inserted data successfully, rowId = " + rowId);
A
Annie_wang 已提交
1226 1227
}).catch((status) => {
    console.log("Failed to insert data");
A
Annie_wang 已提交
1228 1229
})
```
A
annie_wangli 已提交
1230

A
Annie_wang 已提交
1231
### batchInsert
A
Annie_wang 已提交
1232 1233 1234 1235 1236 1237 1238 1239

batchInsert(table: string, values: Array&lt;ValuesBucket&gt;, callback: AsyncCallback&lt;number&gt;):void

Batch inserts data into a table. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

**Parameters**
A
Annie_wang 已提交
1240

A
Annie_wang 已提交
1241 1242 1243 1244 1245 1246 1247
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| table | string | Yes| Name of the target table.|
| values | Array&lt;[ValuesBucket](#valuesbucket)&gt; | Yes| An array of data to insert.|
| callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked to return the result. If the operation is successful, the number of inserted data records is returned. Otherwise, **-1** is returned.|

**Example**
A
Annie_wang 已提交
1248

A
Annie_wang 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
```js
const valueBucket1 = {
    "NAME": "Lisa",
    "AGE": 18,
    "SALARY": 100.5,
    "CODES": new Uint8Array([1, 2, 3, 4, 5])
}
const valueBucket2 = {
    "NAME": "Jack",
    "AGE": 19,
    "SALARY": 101.5,
    "CODES": new Uint8Array([6, 7, 8, 9, 10])
}
const valueBucket3 = {
    "NAME": "Tom",
    "AGE": 20,
    "SALARY": 102.5,
    "CODES": new Uint8Array([11, 12, 13, 14, 15])
}

A
Annie_wang 已提交
1269
let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3);
A
Annie_wang 已提交
1270 1271
rdbStore.batchInsert("EMPLOYEE", valueBuckets, function(status, insertNum) {
    if (status) {
A
Annie_wang 已提交
1272
        console.log("Failed to batch insert data, status = " + status);
A
Annie_wang 已提交
1273 1274
        return;
    }
A
Annie_wang 已提交
1275
    console.log("Batch inserted data successfully. The number of values that were inserted = " + insertNum);
A
Annie_wang 已提交
1276 1277 1278
})
```

A
Annie_wang 已提交
1279
### batchInsert
A
Annie_wang 已提交
1280 1281 1282 1283 1284 1285 1286 1287

batchInsert(table: string, values: Array&lt;ValuesBucket&gt;):Promise&lt;number&gt;

Batch inserts data into a table. This API uses a promise to return the result.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

**Parameters**
A
Annie_wang 已提交
1288

A
Annie_wang 已提交
1289 1290 1291 1292 1293 1294
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| table | string | Yes| Name of the target table.|
| values | Array&lt;[ValuesBucket](#valuesbucket)&gt; | Yes| An array of data to insert.|

**Return value**
A
Annie_wang 已提交
1295

A
Annie_wang 已提交
1296 1297 1298 1299 1300
| Type| Description|
| -------- | -------- |
| Promise&lt;number&gt; | Promise used to return the result. If the operation is successful, the number of inserted data records is returned. Otherwise, **-1** is returned.|

**Example**
A
Annie_wang 已提交
1301

A
Annie_wang 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
```js
const valueBucket1 = {
    "NAME": "Lisa",
    "AGE": 18,
    "SALARY": 100.5,
    "CODES": new Uint8Array([1, 2, 3, 4, 5])
}
const valueBucket2 = {
    "NAME": "Jack",
    "AGE": 19,
    "SALARY": 101.5,
    "CODES": new Uint8Array([6, 7, 8, 9, 10])
}
const valueBucket3 = {
    "NAME": "Tom",
    "AGE": 20,
    "SALARY": 102.5,
    "CODES": new Uint8Array([11, 12, 13, 14, 15])
}

A
Annie_wang 已提交
1322
let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3);
A
Annie_wang 已提交
1323 1324
let promise = rdbStore.batchInsert("EMPLOYEE", valueBuckets);
promise.then((insertNum) => {
A
Annie_wang 已提交
1325
    console.log("Batch inserted data successfully. The number of values that were inserted = " + insertNum);
A
Annie_wang 已提交
1326
}).catch((status) => {
A
Annie_wang 已提交
1327
    console.log("Failed to batch insert data, status = " + status);
A
Annie_wang 已提交
1328 1329
})
```
A
annie_wangli 已提交
1330 1331 1332

### update

A
Annie_wang 已提交
1333
update(values: ValuesBucket, predicates: RdbPredicates, callback: AsyncCallback&lt;number&gt;):void
A
annie_wangli 已提交
1334

A
Annie_wang 已提交
1335
Updates data in the RDB store based on the specified **RdbPredicates** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
1336

A
annie_wangli 已提交
1337
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1338

A
annie_wangli 已提交
1339
**Parameters**
A
Annie_wang 已提交
1340

A
annie_wangli 已提交
1341 1342
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
1343
| values | [ValuesBucket](#valuesbucket) | Yes| Data to update in the RDB store. The key-value pair is associated with the column name in the target table.|
A
Annie_wang 已提交
1344
| predicates | [RdbPredicates](#rdbpredicates) | Yes| Update conditions specified by the **RdbPredicates** object.|
A
Annie_wang 已提交
1345
| callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked to return the number of rows updated.|
A
annie_wangli 已提交
1346 1347

**Example**
A
Annie_wang 已提交
1348

A
Annie_wang 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357
```js
const valueBucket = {
    "NAME": "Rose",
    "AGE": 22,
    "SALARY": 200.5,
    "CODES": new Uint8Array([1, 2, 3, 4, 5]),
}
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
A
Annie_wang 已提交
1358
rdbStore.update(valueBucket, predicates, function (err, rows) {
A
Annie_wang 已提交
1359
    if (err) {
A
Annie_wang 已提交
1360
        console.info("Failed to update data, err: " + err)
A
Annie_wang 已提交
1361 1362
        return
    }
A
Annie_wang 已提交
1363
    console.log("Updated row count: " + rows)
A
Annie_wang 已提交
1364 1365
})
```
A
annie_wangli 已提交
1366 1367 1368

### update

A
Annie_wang 已提交
1369
update(values: ValuesBucket, predicates: RdbPredicates):Promise&lt;number&gt;
A
annie_wangli 已提交
1370

A
Annie_wang 已提交
1371
Updates data based on the specified **RdbPredicates** object. This API uses a promise to return the result.
A
annie_wangli 已提交
1372

A
annie_wangli 已提交
1373
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1374

A
annie_wangli 已提交
1375
**Parameters**
A
Annie_wang 已提交
1376

A
annie_wangli 已提交
1377 1378
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
1379
| values | [ValuesBucket](#valuesbucket) | Yes| Data to update in the RDB store. The key-value pair is associated with the column name in the target table.|
A
Annie_wang 已提交
1380
| predicates | [RdbPredicates](#rdbpredicates) | Yes| Update conditions specified by the **RdbPredicates** object.|
A
annie_wangli 已提交
1381

A
annie_wangli 已提交
1382
**Return value**
A
Annie_wang 已提交
1383

A
annie_wangli 已提交
1384 1385 1386 1387 1388
| Type| Description|
| -------- | -------- |
| Promise&lt;number&gt; | Promise used to return the number of rows updated.|

**Example**
A
Annie_wang 已提交
1389

A
Annie_wang 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
```js
const valueBucket = {
    "NAME": "Rose",
    "AGE": 22,
    "SALARY": 200.5,
    "CODES": new Uint8Array([1, 2, 3, 4, 5]),
}
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
let promise = rdbStore.update(valueBucket, predicates)
A
Annie_wang 已提交
1400 1401
promise.then(async (rows) => {
    console.log("Updated row count: " + rows)
A
Annie_wang 已提交
1402
}).catch((err) => {
A
Annie_wang 已提交
1403
    console.info("Failed to update data, err: " + err)
A
Annie_wang 已提交
1404 1405
})
```
A
annie_wangli 已提交
1406 1407 1408

### delete

A
Annie_wang 已提交
1409
delete(predicates: RdbPredicates, callback: AsyncCallback&lt;number&gt;):void
A
annie_wangli 已提交
1410

A
Annie_wang 已提交
1411
Deletes data from the RDB store based on the specified **RdbPredicates** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
1412

A
annie_wangli 已提交
1413
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1414

A
annie_wangli 已提交
1415
**Parameters**
A
Annie_wang 已提交
1416

A
annie_wangli 已提交
1417 1418
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
1419
| predicates | [RdbPredicates](#rdbpredicates) | Yes| Conditions specified by the **RdbPredicates** object for deleting data.|
A
annie_wangli 已提交
1420
| callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked to return the number of rows updated.|
A
annie_wangli 已提交
1421

A
annie_wangli 已提交
1422
**Example**
A
Annie_wang 已提交
1423

A
Annie_wang 已提交
1424 1425 1426 1427 1428
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
rdbStore.delete(predicates, function (err, rows) {
    if (err) {
A
Annie_wang 已提交
1429
        console.info("Failed to delete data, err: " + err)
A
Annie_wang 已提交
1430 1431
        return
    }
A
Annie_wang 已提交
1432
    console.log("Deleted rows: " + rows)
A
Annie_wang 已提交
1433 1434
})
```
A
annie_wangli 已提交
1435 1436 1437

### delete

A
Annie_wang 已提交
1438
delete(predicates: RdbPredicates):Promise&lt;number&gt;
A
annie_wangli 已提交
1439

A
Annie_wang 已提交
1440
Deletes data from the RDB store based on the specified **RdbPredicates** object. This API uses a promise to return the result.
A
annie_wangli 已提交
1441

A
annie_wangli 已提交
1442
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1443

A
annie_wangli 已提交
1444
**Parameters**
A
Annie_wang 已提交
1445

A
annie_wangli 已提交
1446 1447
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
1448
| predicates | [RdbPredicates](#rdbpredicates) | Yes| Conditions specified by the **RdbPredicates** object for deleting data.|
A
annie_wangli 已提交
1449

A
annie_wangli 已提交
1450
**Return value**
A
Annie_wang 已提交
1451

A
annie_wangli 已提交
1452 1453
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
1454
| Promise&lt;number&gt; | Promise used to return the number of rows updated.|
A
annie_wangli 已提交
1455 1456

**Example**
A
Annie_wang 已提交
1457

A
Annie_wang 已提交
1458 1459 1460 1461 1462
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
let promise = rdbStore.delete(predicates)
promise.then((rows) => {
A
Annie_wang 已提交
1463
    console.log("Deleted rows: " + rows)
A
Annie_wang 已提交
1464
}).catch((err) => {
A
Annie_wang 已提交
1465
    console.info("Failed to delete data, err: " + err)
A
Annie_wang 已提交
1466 1467
})
```
A
annie_wangli 已提交
1468 1469 1470

### query

A
Annie_wang 已提交
1471
query(predicates: RdbPredicates, columns: Array&lt;string&gt;, callback: AsyncCallback&lt;ResultSet&gt;):void
Z
zengyawen 已提交
1472

A
Annie_wang 已提交
1473
Queries data from the RDB store based on specified conditions. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
1474

A
annie_wangli 已提交
1475
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1476

A
annie_wangli 已提交
1477
**Parameters**
A
Annie_wang 已提交
1478

A
annie_wangli 已提交
1479 1480
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
1481
| predicates | [RdbPredicates](#rdbpredicates) | Yes| Query conditions specified by the **RdbPredicates** object.|
A
annie_wangli 已提交
1482 1483 1484 1485
| columns | Array&lt;string&gt; | Yes| Columns to query. If this parameter is not specified, the query applies to all columns.|
| callback | AsyncCallback&lt;[ResultSet](js-apis-data-resultset.md)&gt; | Yes| Callback invoked to return the result. If the operation is successful, a **ResultSet** object will be returned.|

**Example**
A
Annie_wang 已提交
1486

A
Annie_wang 已提交
1487 1488 1489 1490 1491
```js
let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Rose")
rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
    if (err) {
A
Annie_wang 已提交
1492
        console.info("Failed to query data, err: " + err)
A
Annie_wang 已提交
1493 1494
        return
    }
A
Annie_wang 已提交
1495 1496
    console.log("ResultSet column names: " + resultSet.columnNames)
    console.log("ResultSet column count: " + resultSet.columnCount)
A
Annie_wang 已提交
1497 1498
})
```
A
annie_wangli 已提交
1499 1500 1501

### query

A
Annie_wang 已提交
1502
query(predicates: RdbPredicates, columns?: Array&lt;string&gt;):Promise&lt;ResultSet&gt;
Z
zengyawen 已提交
1503

A
Annie_wang 已提交
1504
Queries data from the RDB store based on specified conditions. This API uses a promise to return the result.
Z
zengyawen 已提交
1505

A
annie_wangli 已提交
1506
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1507

A
annie_wangli 已提交
1508
**Parameters**
A
Annie_wang 已提交
1509

A
annie_wangli 已提交
1510 1511
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
1512
| predicates | [RdbPredicates](#rdbpredicates) | Yes| Query conditions specified by the **RdbPredicates** object.|
A
annie_wangli 已提交
1513
| columns | Array&lt;string&gt; | No| Columns to query. If this parameter is not specified, the query applies to all columns.|
A
annie_wangli 已提交
1514

A
annie_wangli 已提交
1515
**Return value**
A
Annie_wang 已提交
1516

A
annie_wangli 已提交
1517 1518
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
1519
| Promise&lt;[ResultSet](js-apis-data-resultset.md)&gt; | Promise used to return the result. If the operation is successful, a **ResultSet** object will be returned.|
A
annie_wangli 已提交
1520 1521

**Example**
A
Annie_wang 已提交
1522

A
Annie_wang 已提交
1523
  ```js
A
annie_wangli 已提交
1524 1525
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "Rose")
A
Annie_wang 已提交
1526 1527
  let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promise.then((resultSet) => {
A
Annie_wang 已提交
1528 1529
      console.log("ResultSet column names: " + resultSet.columnNames)
      console.log("ResultSet column count: " + resultSet.columnCount)
A
annie_wangli 已提交
1530
  }).catch((err) => {
A
Annie_wang 已提交
1531
      console.info("Failed to query data, err: " + err)
A
annie_wangli 已提交
1532
  })
A
annie_wangli 已提交
1533 1534 1535 1536 1537 1538
  ```

### querySql<sup>8+</sup>

querySql(sql: string, bindArgs: Array&lt;ValueType&gt;, callback: AsyncCallback&lt;ResultSet&gt;):void

A
Annie_wang 已提交
1539
Queries data in the RDB store using the specified SQL statement. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
1540

A
annie_wangli 已提交
1541
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1542

A
annie_wangli 已提交
1543
**Parameters**
A
Annie_wang 已提交
1544

A
annie_wangli 已提交
1545 1546 1547
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| sql | string | Yes| SQL statement to run.|
A
Annie_wang 已提交
1548
| bindArgs | Array&lt;[ValueType](#valuetype)&gt; | Yes| Arguments in the SQL statement.|
A
annie_wangli 已提交
1549 1550 1551
| callback | AsyncCallback&lt;[ResultSet](js-apis-data-resultset.md)&gt; | Yes| Callback invoked to return the result. If the operation is successful, a **ResultSet** object will be returned.|

**Example**
A
Annie_wang 已提交
1552

A
Annie_wang 已提交
1553 1554 1555
```js
rdbStore.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], function (err, resultSet) {
    if (err) {
A
Annie_wang 已提交
1556
        console.info("Failed to query data, err: " + err)
A
Annie_wang 已提交
1557 1558
        return
    }
A
Annie_wang 已提交
1559 1560
    console.log("ResultSet column names: " + resultSet.columnNames)
    console.log("ResultSet column count: " + resultSet.columnCount)
A
Annie_wang 已提交
1561 1562
})
```
A
annie_wangli 已提交
1563 1564 1565 1566 1567

### querySql<sup>8+</sup>

querySql(sql: string, bindArgs?: Array&lt;ValueType&gt;):Promise&lt;ResultSet&gt;

A
Annie_wang 已提交
1568
Queries data in the RDB store using the specified SQL statement. This API uses a promise to return the result.
A
annie_wangli 已提交
1569

A
annie_wangli 已提交
1570
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1571

A
annie_wangli 已提交
1572
**Parameters**
A
Annie_wang 已提交
1573

A
annie_wangli 已提交
1574 1575 1576
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| sql | string | Yes| SQL statement to run.|
A
Annie_wang 已提交
1577
| bindArgs | Array&lt;[ValueType](#valuetype)&gt; | No| Arguments in the SQL statement.|
A
annie_wangli 已提交
1578

A
annie_wangli 已提交
1579
**Return value**
A
Annie_wang 已提交
1580

A
annie_wangli 已提交
1581 1582
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
1583
| Promise&lt;[ResultSet](js-apis-data-resultset.md)&gt; | Promise used to return the result. If the operation is successful, a **ResultSet** object will be returned.|
A
annie_wangli 已提交
1584 1585

**Example**
A
Annie_wang 已提交
1586

A
Annie_wang 已提交
1587 1588 1589
```js
let promise = rdbStore.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'])
promise.then((resultSet) => {
A
Annie_wang 已提交
1590 1591
    console.log("ResultSet column names: " + resultSet.columnNames)
    console.log("ResultSet column count: " + resultSet.columnCount)
A
Annie_wang 已提交
1592
}).catch((err) => {
A
Annie_wang 已提交
1593
    console.info("Failed to query data, err: " + err)
A
Annie_wang 已提交
1594 1595
})
```
A
annie_wangli 已提交
1596

A
Annie_wang 已提交
1597
### executeSql<sup>8+</sup>
A
annie_wangli 已提交
1598 1599

executeSql(sql: string, bindArgs: Array&lt;ValueType&gt;, callback: AsyncCallback&lt;void&gt;):void
Z
zengyawen 已提交
1600

A
Annie_wang 已提交
1601
Executes an SQL statement that contains specified arguments but returns no value. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
1602

A
annie_wangli 已提交
1603
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1604

A
annie_wangli 已提交
1605
**Parameters**
A
Annie_wang 已提交
1606

A
annie_wangli 已提交
1607 1608 1609
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| sql | string | Yes| SQL statement to run.|
A
Annie_wang 已提交
1610
| bindArgs | Array&lt;[ValueType](#valuetype)&gt; | Yes| Arguments in the SQL statement.|
A
Annie_wang 已提交
1611
| callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked to return the result.|
A
annie_wangli 已提交
1612 1613

**Example**
A
Annie_wang 已提交
1614

A
Annie_wang 已提交
1615 1616 1617 1618
```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)"
rdbStore.executeSql(SQL_CREATE_TABLE, null, function(err) {
    if (err) {
A
Annie_wang 已提交
1619
        console.info("Failed to execute SQL, err: " + err)
A
Annie_wang 已提交
1620 1621
        return
    }
A
Annie_wang 已提交
1622
    console.info('Created table successfully.')
A
Annie_wang 已提交
1623 1624
})
```
A
annie_wangli 已提交
1625

A
Annie_wang 已提交
1626
### executeSql<sup>8+</sup>
A
annie_wangli 已提交
1627 1628

executeSql(sql: string, bindArgs?: Array&lt;ValueType&gt;):Promise&lt;void&gt;
Z
zengyawen 已提交
1629

A
Annie_wang 已提交
1630
Executes an SQL statement that contains specified arguments but returns no value. This API uses a promise to return the result.
Z
zengyawen 已提交
1631

A
annie_wangli 已提交
1632
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1633

A
annie_wangli 已提交
1634
**Parameters**
A
Annie_wang 已提交
1635

A
annie_wangli 已提交
1636 1637 1638
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| sql | string | Yes| SQL statement to run.|
A
Annie_wang 已提交
1639
| bindArgs | Array&lt;[ValueType](#valuetype)&gt; | No| Arguments in the SQL statement.|
A
annie_wangli 已提交
1640

A
annie_wangli 已提交
1641
**Return value**
A
Annie_wang 已提交
1642

A
annie_wangli 已提交
1643 1644
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
1645
| Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1646 1647

**Example**
A
Annie_wang 已提交
1648

A
Annie_wang 已提交
1649 1650 1651 1652
```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)"
let promise = rdbStore.executeSql(SQL_CREATE_TABLE)
promise.then(() => {
A
Annie_wang 已提交
1653
    console.info('Created table successfully.')
A
Annie_wang 已提交
1654
}).catch((err) => {
A
Annie_wang 已提交
1655
    console.info("Failed to execute SQL, err: " + err)
A
Annie_wang 已提交
1656 1657
})
```
A
annie_wangli 已提交
1658

A
annie_wangli 已提交
1659 1660 1661 1662 1663
### beginTransaction<sup>8+</sup>

beginTransaction():void

Starts the transaction before executing an SQL statement.
A
annie_wangli 已提交
1664

A
annie_wangli 已提交
1665 1666 1667
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

**Example**
A
Annie_wang 已提交
1668

A
Annie_wang 已提交
1669
```js
A
Annie_wang 已提交
1670
import featureAbility from '@ohos.ability.featureAbility'
A
Annie_wang 已提交
1671
let context = featureAbility.getContext()
A
Annie_wang 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
const STORE_CONFIG = { name: "RdbTest.db"}
data_rdb.getRdbStore(context, STORE_CONFIG, 1, async function (err, rdbStore) {
    rdbStore.beginTransaction()
	const valueBucket = {
		"name": "lisi",
		"age": 18,
		"salary": 100.5,
		"blobType": new Uint8Array([1, 2, 3]),
	}
	await rdbStore.insert("test", valueBucket)
	rdbStore.commit()
})
A
annie_wangli 已提交
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
```

### commit<sup>8+</sup>

commit():void

Commits the executed SQL statements.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

**Example**
A
Annie_wang 已提交
1695

A
Annie_wang 已提交
1696 1697
```js
import featureAbility from '@ohos.ability.featureAbility'
A
Annie_wang 已提交
1698
let context = featureAbility.getContext()
A
Annie_wang 已提交
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
const STORE_CONFIG = { name: "RdbTest.db"}
data_rdb.getRdbStore(context, STORE_CONFIG, 1, async function (err, rdbStore) {
    rdbStore.beginTransaction()
	const valueBucket = {
		"name": "lisi",
		"age": 18,
		"salary": 100.5,
		"blobType": new Uint8Array([1, 2, 3]),
	}
	await rdbStore.insert("test", valueBucket)
	rdbStore.commit()
})
A
annie_wangli 已提交
1711 1712 1713 1714
```

### rollBack<sup>8+</sup>

A
Annie_wang 已提交
1715
rollBack():void
A
annie_wangli 已提交
1716 1717 1718 1719 1720 1721

Rolls back the SQL statements that have been executed.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

**Example**
A
Annie_wang 已提交
1722

A
Annie_wang 已提交
1723
```js
A
Annie_wang 已提交
1724
import featureAbility from '@ohos.ability.featureAbility'
A
Annie_wang 已提交
1725
let context = featureAbility.getContext()
A
Annie_wang 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
const STORE_CONFIG = { name: "RdbTest.db"}
data_rdb.getRdbStore(context, STORE_CONFIG, 1, async function (err, rdbStore) {
    try {
		rdbStore.beginTransaction()
		const valueBucket = {
			"id": 1,
			"name": "lisi",
			"age": 18,
			"salary": 100.5,
			"blobType": new Uint8Array([1, 2, 3]),
		}
		await rdbStore.insert("test", valueBucket)
		rdbStore.commit()
	} catch (e) {
		rdbStore.rollBack()
	}
})
A
annie_wangli 已提交
1743 1744 1745 1746 1747
```

### setDistributedTables<sup>8+</sup>

setDistributedTables(tables: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
1748

A
Annie_wang 已提交
1749
Sets distributed tables. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
1750 1751

**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
A
annie_wangli 已提交
1752

A
annie_wangli 已提交
1753
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1754

A
annie_wangli 已提交
1755
**Parameters**
A
Annie_wang 已提交
1756

A
annie_wangli 已提交
1757 1758 1759 1760 1761 1762
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| tables | Array&lt;string&gt; | Yes| Names of the distributed tables to set.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked to return the result.|

**Example**
A
Annie_wang 已提交
1763

A
Annie_wang 已提交
1764 1765 1766
```js
rdbStore.setDistributedTables(["EMPLOYEE"], function (err) {
    if (err) {
A
Annie_wang 已提交
1767
        console.info('Failed to set distributed tables, err: ' + err)
A
Annie_wang 已提交
1768 1769
        return
    }
A
Annie_wang 已提交
1770
    console.info('Set distributed tables successfully.')
A
Annie_wang 已提交
1771
})
A
Annie_wang 已提交
1772
```
A
annie_wangli 已提交
1773

A
annie_wangli 已提交
1774
### setDistributedTables<sup>8+</sup>
A
annie_wangli 已提交
1775

A
annie_wangli 已提交
1776
 setDistributedTables(tables: Array&lt;string&gt;): Promise&lt;void&gt;
A
annie_wangli 已提交
1777

A
Annie_wang 已提交
1778
Sets distributed tables. This API uses a promise to return the result.
A
annie_wangli 已提交
1779

A
Annie_wang 已提交
1780 1781
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

A
annie_wangli 已提交
1782
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1783

A
annie_wangli 已提交
1784
**Parameters**
A
Annie_wang 已提交
1785

A
annie_wangli 已提交
1786 1787 1788
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| tables | Array&lt;string&gt; | Yes| Names of the distributed tables to set.|
A
annie_wangli 已提交
1789

A
annie_wangli 已提交
1790
**Return value**
A
Annie_wang 已提交
1791

A
annie_wangli 已提交
1792 1793
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
1794
| Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
1795 1796

**Example**
A
Annie_wang 已提交
1797

A
Annie_wang 已提交
1798 1799 1800
```js
let promise = rdbStore.setDistributedTables(["EMPLOYEE"])
promise.then(() => {
A
Annie_wang 已提交
1801
    console.info("Set distributed tables successfully.")
A
Annie_wang 已提交
1802
}).catch((err) => {
A
Annie_wang 已提交
1803
    console.info("Failed to set distributed tables, err: " + err)
A
Annie_wang 已提交
1804 1805
})
```
A
annie_wangli 已提交
1806

A
annie_wangli 已提交
1807
### obtainDistributedTableName<sup>8+</sup>
A
annie_wangli 已提交
1808

A
annie_wangli 已提交
1809
obtainDistributedTableName(device: string, table: string, callback: AsyncCallback&lt;string&gt;): void
A
annie_wangli 已提交
1810

A
Annie_wang 已提交
1811
Obtains the distributed table name for a remote device based on the local table name. This API uses an asynchronous callback to return the result. The distributed table name is required when the RDB store of a remote device is queried.
A
Annie_wang 已提交
1812 1813

**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
A
annie_wangli 已提交
1814

A
annie_wangli 已提交
1815
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1816

A
annie_wangli 已提交
1817
**Parameters**
A
Annie_wang 已提交
1818

A
annie_wangli 已提交
1819 1820 1821 1822 1823 1824 1825
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| device | string | Yes| Remote device.|
| table | string | Yes| Local table name.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the result. If the operation succeeds, the distributed table name of the remote device is returned.|

**Example**
A
Annie_wang 已提交
1826

A
Annie_wang 已提交
1827
```js
A
Annie_wang 已提交
1828
rdbStore.obtainDistributedTableName("12345678abcde", "EMPLOYEE", function (err, tableName) {
A
Annie_wang 已提交
1829
    if (err) {
A
Annie_wang 已提交
1830
        console.info('Failed to obtain DistributedTableName, err: ' + err)
A
Annie_wang 已提交
1831 1832
        return
    }
A
Annie_wang 已提交
1833
    console.info('Obtained distributed table name successfully, tableName=.' + tableName)
A
Annie_wang 已提交
1834 1835
})
```
A
annie_wangli 已提交
1836

A
annie_wangli 已提交
1837
### obtainDistributedTableName<sup>8+</sup>
A
annie_wangli 已提交
1838

A
annie_wangli 已提交
1839
 obtainDistributedTableName(device: string, table: string): Promise&lt;string&gt;
A
annie_wangli 已提交
1840

A
Annie_wang 已提交
1841
Obtains the distributed table name for a remote device based on the local table name. This API uses a promise to return the result. The distributed table name is required when the RDB store of a remote device is queried.
A
annie_wangli 已提交
1842

A
Annie_wang 已提交
1843 1844
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

A
annie_wangli 已提交
1845 1846 1847
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

**Parameters**
A
Annie_wang 已提交
1848

A
annie_wangli 已提交
1849 1850 1851 1852
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| device | string | Yes| Remote device.|
| table | string | Yes| Local table name.|
A
annie_wangli 已提交
1853

A
annie_wangli 已提交
1854
**Return value**
A
Annie_wang 已提交
1855

A
annie_wangli 已提交
1856 1857 1858
| Type| Description|
| -------- | -------- |
| Promise&lt;string&gt; | Promise used to return the result. If the operation succeeds, the distributed table name of the remote device is returned.|
A
annie_wangli 已提交
1859

A
annie_wangli 已提交
1860
**Example**
A
Annie_wang 已提交
1861

A
Annie_wang 已提交
1862
```js
A
Annie_wang 已提交
1863
let promise = rdbStore.obtainDistributedTableName("12345678abcde", "EMPLOYEE")
A
Annie_wang 已提交
1864
promise.then((tableName) => {
A
Annie_wang 已提交
1865
    console.info('Obtained distributed table name successfully, tableName= ' + tableName)
A
Annie_wang 已提交
1866
}).catch((err) => {
A
Annie_wang 已提交
1867
    console.info('Failed to obtain DistributedTableName, err: ' + err)
A
Annie_wang 已提交
1868 1869
})
```
A
annie_wangli 已提交
1870

A
annie_wangli 已提交
1871
### sync<sup>8+</sup>
A
annie_wangli 已提交
1872

A
annie_wangli 已提交
1873
sync(mode: SyncMode, predicates: RdbPredicates, callback: AsyncCallback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
1874

A
Annie_wang 已提交
1875 1876 1877
Synchronizes data between devices. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
A
annie_wangli 已提交
1878

A
annie_wangli 已提交
1879
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1880

A
annie_wangli 已提交
1881
**Parameters**
A
Annie_wang 已提交
1882

A
annie_wangli 已提交
1883 1884 1885 1886
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| mode | [SyncMode](#syncmode8) | Yes| Data synchronization mode. The value can be **push** or **pull**.|
| predicates | [RdbPredicates](#rdbpredicates) | Yes| **RdbPredicates** object that specifies the data and devices to synchronize.|
A
Annie_wang 已提交
1887
| callback | AsyncCallback&lt;Array&lt;[string, number]&gt;&gt; | Yes| Callback invoked to send the synchronization result to the caller. <br>**string** indicates the device ID. <br>**number** indicates the synchronization status of that device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure. |
A
annie_wangli 已提交
1888 1889

**Example**
A
Annie_wang 已提交
1890

A
Annie_wang 已提交
1891
```js
A
Annie_wang 已提交
1892
let predicates = new data_rdb.RdbPredicates('EMPLOYEE')
A
Annie_wang 已提交
1893
predicates.inDevices(['12345678abcde'])
A
Annie_wang 已提交
1894
rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicates, function (err, result) {
A
Annie_wang 已提交
1895
    if (err) {
A
Annie_wang 已提交
1896
        console.log('Sync failed, err: ' + err)
A
Annie_wang 已提交
1897 1898
        return
    }
A
Annie_wang 已提交
1899
    console.log('Sync done.')
A
Annie_wang 已提交
1900 1901 1902 1903 1904
    for (let i = 0; i < result.length; i++) {
        console.log('device=' + result[i][0] + ' status=' + result[i][1])
    }
})
```
A
annie_wangli 已提交
1905

A
annie_wangli 已提交
1906
### sync<sup>8+</sup>
A
annie_wangli 已提交
1907

A
annie_wangli 已提交
1908
 sync(mode: SyncMode, predicates: RdbPredicates): Promise&lt;Array&lt;[string, number]&gt;&gt;
A
annie_wangli 已提交
1909

A
Annie_wang 已提交
1910
Synchronizes data between devices. This API uses a promise to return the result.
A
annie_wangli 已提交
1911

A
Annie_wang 已提交
1912 1913
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

A
annie_wangli 已提交
1914
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1915

A
annie_wangli 已提交
1916
**Parameters**
A
Annie_wang 已提交
1917

A
annie_wangli 已提交
1918 1919 1920 1921
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| mode | [SyncMode](#syncmode8) | Yes| Data synchronization mode. The value can be **push** or **pull**.|
| predicates | [RdbPredicates](#rdbpredicates) | Yes| **RdbPredicates** object that specifies the data and devices to synchronize.|
A
annie_wangli 已提交
1922

A
annie_wangli 已提交
1923 1924 1925 1926
**Return value**

| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
1927
| Promise&lt;Array&lt;[string, number]&gt;&gt; | Promise used to return the synchronization result to the caller. <br>**string** indicates the device ID. <br>**number** indicates the synchronization status of that device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure. |
A
annie_wangli 已提交
1928 1929

**Example**
A
Annie_wang 已提交
1930

A
Annie_wang 已提交
1931 1932 1933 1934 1935
```js
let predicates = new data_rdb.RdbPredicates('EMPLOYEE')
predicates.inDevices(['12345678abcde'])
let promise = rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicates)
promise.then((result) =>{
A
Annie_wang 已提交
1936
    console.log('Sync done.')
A
Annie_wang 已提交
1937 1938 1939 1940
    for (let i = 0; i < result.length; i++) {
        console.log('device=' + result[i][0] + ' status=' + result[i][1])
    }
}).catch((err) => {
A
Annie_wang 已提交
1941
    console.log('Sync failed')
A
Annie_wang 已提交
1942 1943
})
```
A
annie_wangli 已提交
1944

A
annie_wangli 已提交
1945
### on('dataChange')<sup>8+</sup>
A
annie_wangli 已提交
1946

A
annie_wangli 已提交
1947
on(event: 'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;): void
A
annie_wangli 已提交
1948 1949 1950

Registers an observer for this RDB store. When the data in the RDB store changes, a callback is invoked to return the data changes.

A
annie_wangli 已提交
1951
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
A
annie_wangli 已提交
1952

A
annie_wangli 已提交
1953 1954 1955 1956 1957
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| event | string | Yes| The value is'dataChange', which indicates a data change event.|
A
Annie_wang 已提交
1958
| type | [SubscribeType](#subscribetype8) | Yes| Subscription type to register.|
A
annie_wangli 已提交
1959 1960 1961
| observer | Callback&lt;Array&lt;string&gt;&gt; | Yes| Observer that listens for the data changes in the RDB store.|

**Example**
A
Annie_wang 已提交
1962

A
Annie_wang 已提交
1963 1964 1965 1966 1967 1968 1969 1970 1971
```js
function storeObserver(devices) {
    for (let i = 0; i < devices.length; i++) {
        console.log('device=' + devices[i] + ' data changed')
    }
}
try {
    rdbStore.on('dataChange', data_rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver)
} catch (err) {
A
Annie_wang 已提交
1972
    console.log('Failed to register observer')
A
Annie_wang 已提交
1973 1974
}
```
A
annie_wangli 已提交
1975

A
annie_wangli 已提交
1976
### off('dataChange')<sup>8+</sup>
A
annie_wangli 已提交
1977

A
annie_wangli 已提交
1978
off(event:'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;): void
A
annie_wangli 已提交
1979

A
Annie_wang 已提交
1980
Unregisters the observer of the specified type from the RDB store. This API uses a callback to return the result.
A
annie_wangli 已提交
1981

A
annie_wangli 已提交
1982 1983 1984 1985 1986 1987 1988
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| event | string | Yes| The value is'dataChange', which indicates a data change event.|
A
Annie_wang 已提交
1989
| type | [SubscribeType](#subscribetype8)    | Yes| Subscription type to unregister.|
A
annie_wangli 已提交
1990 1991 1992
| observer | Callback&lt;Array&lt;string&gt;&gt; | Yes| Data change observer registered.|

**Example**
A
Annie_wang 已提交
1993

A
Annie_wang 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002
```js
function storeObserver(devices) {
    for (let i = 0; i < devices.length; i++) {
        console.log('device=' + devices[i] + ' data changed')
    }
}
try {
    rdbStore.off('dataChange', data_rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver)
} catch (err) {
A
Annie_wang 已提交
2003
    console.log('Failed to unregister observer')
A
Annie_wang 已提交
2004 2005
}
```