js-apis-data-rdb.md 43.8 KB
Newer Older
1
# Relational Database
Z
zengyawen 已提交
2

A
annie_wangli 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
A
annie_wangli 已提交
4
> 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.
Z
zengyawen 已提交
5

A
annie_wangli 已提交
6 7 8


## Modules to Import
Z
zengyawen 已提交
9 10

```
A
annie_wangli 已提交
11
import data_rdb from '@ohos.data.rdb'
Z
zengyawen 已提交
12 13
```

A
annie_wangli 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
## System Capabilities
SystemCapability.DistributedDataManager.RelationalStore.Core


## data_rdb.getRdbStore

getRdbStore(context: Context, config: StoreConfig, version: number, callback: AsyncCallback<RdbStore>): void

Obtains a relational database (RDB) store. You can set parameters for the RDB store based on service requirements, call APIs to perform data operations, and use a callback to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | context | Context | Yes| Context of the app or functionality.|
  | config | [StoreConfig](#storeconfig) | Yes| Configuration of the RDB store.|
  | version | number | Yes| RDB store version.|
  | callback | AsyncCallback<[RdbStore](#rdbstore)> | Yes| Callback invoked to return the RDB store obtained.|

- Example
  ```
  import Ability from '@ohos.application.Ability'
  import data_rdb from '@ohos.data.rdb'
  export default class MainAbility extends Ability {
      const STORE_CONFIG = { name: "RdbTest.db"}
      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)"
      data_rdb.getRdbStore(this.context, STORE_CONFIG, 1, function (err, rdbStore) {
          rdbStore.executeSql(SQL_CREATE_TABLE)
          console.info(TAG + 'create table done.')
      })
  }
  ```

## data_rdb.getRdbStore

getRdbStore(context: Context, config: StoreConfig, version: number): Promise<RdbStore>
Z
zengyawen 已提交
49 50 51

Obtains an RDB store. You can set parameters for the RDB store based on service requirements, call APIs to perform data operations, and use a promise to return the result.

A
annie_wangli 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | context | Context | Yes| Context of the app or functionality.|
  | config | [StoreConfig](#storeconfig) | Yes| Configuration of the RDB store.|
  | version | number | Yes| RDB store version.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise<[RdbStore](#rdbstore)> | Promise used to return the RDB store obtained.|

- Example
  ```
  import Ability from '@ohos.application.Ability'
  import data_rdb from '@ohos.data.rdb'
  export default class MainAbility extends Ability {
      const STORE_CONFIG = { name: "RdbTest.db" }
      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 = data_rdb.getRdbStore(this.context, STORE_CONFIG, 1);
      promise.then(async (rdbStore) => {
          await rdbStore.executeSql(SQL_CREATE_TABLE, null)
      }).catch((err) => {
          expect(null).assertFail();
      })
  }
  ```

## data_rdb.deleteRdbStore

deleteRdbStore(context: Context, name: string, callback: AsyncCallback<void>): void
Z
zengyawen 已提交
83 84 85

Deletes an RDB store. This method uses a callback to return the result. 

A
annie_wangli 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | context | Context | Yes| Context of the app or functionality.|
  | name | string | Yes| Name of the RDB store to delete.|
  | callback | AsyncCallback<void> | Yes| Callback invoked to return the result. If the RDB store is deleted, **true** will be returned. Otherwise, **false** will be returned.|

- Example
  ```
  import Ability from '@ohos.application.Ability'
  import data_rdb from '@ohos.data.rdb'
  export default class MainAbility extends Ability {
      data_rdb.deleteRdbStore(this.context, "RdbTest.db", function (err, rdbStore) {
          console.info(TAG + 'delete store done.')})
  }
  ```

## data_rdb.deleteRdbStore

deleteRdbStore(context: Context, name: string): Promise<void>
Z
zengyawen 已提交
106 107 108

Deletes an RDB store. This method uses a promise to return the result.

A
annie_wangli 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | context | Context | Yes| Context of the app or functionality.|
  | name | string | Yes| Name of the RDB store to delete.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise<void> | Promise used to return the result. If the RDB store is deleted, **true** will be returned. Otherwise, **false** will be returned.|

- Example
  ```
  import Ability from '@ohos.application.Ability'
  import data_rdb from '@ohos.data.rdb'
  export default class MainAbility extends Ability {
      let promise = data_rdb.deleteRdbStore(this.context, "RdbTest.db")
      promise.then(()=>{
          console.info(TAG + 'delete store done.')
      })
  }
  ```

## RdbPredicates
Z
zengyawen 已提交
133 134 135

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

A
annie_wangli 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752

### constructor

constructor(name: string)


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


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | name | string | Yes| Database table name.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  ```

### inDevices

inDevices(devices: Array<string>): RdbPredicates;


Specifies a remote device on the network during distributed database synchronization.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | devices | Array<string> | Yes| ID of the remote device to specify.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicate.inDevices(['12345678abcde'])
  ```

### inAllDevices

inAllDevices(): RdbPredicates;


Connects to all remote devices on the network during distributed database synchronization.


- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.inAllDevices()
  ```

### equalTo

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


Sets the **RdbPredicates** to match the field with data type **ValueType** and value equal to the specified value.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "lisi")
  ```


### notEqualTo

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


Sets the **RdbPredicates** to match the field with data type **ValueType** and value not equal to the specified value.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.notEqualTo("NAME", "lisi")
  ```


### beginWrap

beginWrap(): RdbPredicates


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


- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** with a left parenthesis.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "lisi")
      .beginWrap()
      .equalTo("AGE", 18)
      .or()
      .equalTo("SALARY", 200.5)
      .endWrap()
  ```


### endWrap

endWrap(): RdbPredicates


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


- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** with a right parenthesis.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "lisi")
      .beginWrap()
      .equalTo("AGE", 18)
      .or()
      .equalTo("SALARY", 200.5)
      .endWrap()
  ```


### or

or(): RdbPredicates


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


- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** with the OR condition.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "Lisa")
      .or()
      .equalTo("NAME", "Rose")
  ```


### and

and(): RdbPredicates


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


- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** with the AND condition.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "Lisa")
      .and()
      .equalTo("SALARY", 200.5)
  ```


### contains

contains(field: string, value: string): RdbPredicat


Sets the **RdbPredicates** to match a string containing the specified value.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | string | Yes| Value to match the **RdbPredicates**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.contains("NAME", "os")
  ```


### beginsWith

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


Sets the **RdbPredicates** to match a string that starts with the specified value.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | string | Yes| Value to match the **RdbPredicates**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.beginsWith("NAME", "os")
  ```


### endsWith

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


Sets the **RdbPredicates** to match a string that ends with the specified value.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | string | Yes| Value to match the **RdbPredicates**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.endsWith("NAME", "se")
  ```


### isNull

isNull(field: string): RdbPredicates


Sets the **RdbPredicates** to match the field whose value is null.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.isNull("NAME")
  ```


### isNotNull

isNotNull(field: string): RdbPredicates


Sets the **RdbPredicates** to match the field whose value is not null.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.isNotNull("NAME")
  ```


### like

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


Sets the **RdbPredicates** to match a string that is similar to the specified value.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | string | Yes| Value to match the **RdbPredicates**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.like("NAME", "%os%")
  ```


### glob

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


Sets the **RdbPredicates** to match the specified string.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | string | Yes| Value to match the **RdbPredicates**. <br>Wildcards are supported. ***** indicates zero, one, or multiple digits or characters. **?** indicates a single digit or character.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.glob("NAME", "?h*g")
  ```


### between

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


Sets the **RdbPredicates** to match the field with data type **ValueType** and value within the specified range.


- Parameters
  | 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**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.between("AGE", 10, 50)
  ```


### notBetween

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


Sets the **RdbPredicates** to match the field with data type **ValueType** and value out of the specified range.


- Parameters
  | 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**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.notBetween("AGE", 10, 50)
  ```


### greaterThan

greaterThan(field: string, value: ValueType): RdbPredicatesgr


Sets the **RdbPredicates** to match the field with data type **ValueType** and value greater than the specified value.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.greaterThan("AGE", 18)
  ```


### lessThan

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


Sets the **RdbPredicates** to match the field with data type **ValueType** and value less than the specified value.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.lessThan("AGE", 20)
  ```


### greaterThanOrEqualTo


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


Sets the **RdbPredicates** to match the field with data type **ValueType** and value greater than or equal to the specified value.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.greaterThanOrEqualTo("AGE", 18)
  ```


### lessThanOrEqualTo


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


Sets the **RdbPredicates** to match the field with data type **ValueType** and value less than or equal to the specified value.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|
  | value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.lessThanOrEqualTo("AGE", 20)
  ```


### orderByAsc


orderByAsc(field: string): RdbPredicates


Sets the **RdbPredicates** to match the column with values sorted in ascending order.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.orderByAsc("NAME")
  ```


### orderByDesc


orderByDesc(field: string): RdbPredicates


Sets the **RdbPredicates** to match the column with values sorted in descending order.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | field | string | Yes| Column name in the database table.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.orderByDesc("AGE")
  ```


### distinct

distinct(): RdbPredicates


Sets the **RdbPredicates** to filter out duplicate records.


- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that can filter out duplicate records.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "Rose").distinct("NAME")
A
annie_wangli 已提交
753
  rdbStore.query(predicates, ["NAME"])
A
annie_wangli 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
  ```


### limitAs

limitAs(value: number): RdbPredicates


Sets the **RdbPredicates** to specify the maximum number of records.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | value | number | Yes| Maximum number of records.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that specifies the maximum number of records.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "Rose").limitAs(3)
  ```


### offsetAs

offsetAs(rowOffset: number): RdbPredicates


Sets the **RdbPredicates** to specify the start position of the returned result.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | rowOffset | number | Yes| Number of rows to offset from the beginning. The value is a positive integer.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that specifies the start position of the returned result.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "Rose").offsetAs(3)
  ```


### groupBy

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


Sets the **RdbPredicates** to group rows that have the same value into summary rows.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | fields | Array&lt;string&gt; | Yes| Names of columns to group.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that groups rows with the same value.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.groupBy(["AGE", "NAME"])
  ```


### indexedBy

indexedBy(indexName: string): RdbPredicates


Sets the **RdbPredicates** object to specify the index column.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | indexName | string | Yes| Name of the index column.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that specifies the index column.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.indexedBy("SALARY_INDEX")
  ```


### in

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


Sets the **RdbPredicates** to match the field with data type **Array&#60;ValueType&#62;** and value within the specified range.


- Parameters
  | 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.|


- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.in("AGE", [18, 20])
  ```


### notIn

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


Sets the **RdbPredicates** to match the field with data type **Array&#60;ValueType&#62;** and value out of the specified range.


- Parameters
  | 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.|


- Return value
  | Type| Description|
  | -------- | -------- |
  | [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.notIn("NAME", ["Lisa", "Rose"])
  ```


## RdbStore
Z
zengyawen 已提交
912 913 914 915

Provides methods to manage an RDB store.


A
annie_wangli 已提交
916 917 918
### insert

insert(name: string, values: ValuesBucket, callback: AsyncCallback&lt;number&gt;):void
Z
zengyawen 已提交
919 920 921

Inserts a row of data into a table. This method uses a callback to return the result.

A
annie_wangli 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | name | string | Yes| Name of the target table.|
  | 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
  ```
  const valueBucket = {
      "NAME": "Lisa",
      "AGE": 18,
      "SALARY": 100.5,
      "CODES": new Uint8Array([1, 2, 3, 4, 5]),
  }
  rdbStore.insert("EMPLOYEE", valueBucket, function (err, ret) {
      expect(1).assertEqual(ret)
      console.log(TAG + "insert first done: " + ret)})
  ```


### insert

insert(name: string, values: ValuesBucket):Promise&lt;number&gt;
Z
zengyawen 已提交
946 947 948

Inserts a row of data into a table. This method uses a promise to return the result.

A
annie_wangli 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | name | string | Yes| Name of the target table.|
  | values | [ValuesBucket](#valuesbucket) | Yes| Row of data to insert.|

- Return value
  | 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
  ```
  const valueBucket = {
      "NAME": "Lisa",
      "AGE": 18,
      "SALARY": 100.5,
      "CODES": new Uint8Array([1, 2, 3, 4, 5]),
  }
  let promise = rdbStore.insert("EMPLOYEE", valueBucket)
  promise.then(async (ret) => {
      await console.log(TAG + "insert first done: " + ret)
  }).catch((err) => {})
  ```


### update

update(values: ValuesBucket, rdbPredicates: RdbPredicates, callback: AsyncCallback&lt;number&gt;):void

Updates data in the database based on the specified RdbPredicates object. This method uses a callback to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | values | [ValuesBucket](#valuesbucket) | Yes| Data to update. The value specifies the row of data to be updated in the database. The key-value pair is associated with the column name in the target table.|
  | rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Row of data to insert.|
  | callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked to return the number of rows updated.|

- Example
  ```
  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")
  rdbStore.update(valueBucket, predicates, function (err, ret) {
      console.log(TAG + "updated row count: " + changedRows)})
  ```


### update

update(values: ValuesBucket, rdbPredicates: RdbPredicates):Promise&lt;number&gt;

Updates data in the database based on the specified RdbPredicates object. This method uses a promise to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | values | [ValuesBucket](#valuesbucket) | Yes| Data to update. The value specifies the row of data to be updated in the database. The key-value pair is associated with the column name in the target table.|
  | rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Row of data to insert.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;number&gt; | Promise used to return the number of rows updated.|

- Example
  ```
  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)
  promise.then(async (ret) => {
      await console.log(TAG + "updated row count: " + changedRows)
  }).catch((err) => {})
  ```


### delete

delete(rdbPredicates: RdbPredicates, callback: AsyncCallback&lt;number&gt;):void


Deletes data from the database based on the specified RdbPredicates object. This method uses a callback to return the result.


- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Conditions specified for deleting data.|
  | callback | AsyncCallback&lt;number&gt; | Yes| Callback invoked to return the number of rows deleted.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "Lisa")
  rdbStore.delete(predicates, function (err, rows) {
      console.log(TAG + "delete rows: " + rows)})
  ```


### delete

delete(rdbPredicates: RdbPredicates):Promise&lt;number&gt;

Deletes data from the database based on the specified RdbPredicates object. This method uses a promise to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Conditions specified for deleting data.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;number&gt; | Promise used to return the number of rows deleted.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "Lisa")
  let promise = rdbStore.delete(predicates)
  promise.then((rows) => {
      console.log(TAG + "delete rows: " + rows)
  }).catch((err) => {})
  ```


### query

query(rdbPredicates: RdbPredicates, columns: Array&lt;string&gt;, callback: AsyncCallback&lt;ResultSet&gt;):void
Z
zengyawen 已提交
1090 1091 1092

Queries data in the database based on specified conditions. This method uses a callback to return the result.

A
annie_wangli 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Query conditions specified by the **RdbPredicates** object.|
  | 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
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "Rose")
  rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
      console.log(TAG + "resultSet column names:" + resultSet.columnNames)
      console.log(TAG + "resultSet column count:" + resultSet.columnCount)})
  ```


### query

query(rdbPredicates: RdbPredicates, columns?: Array&lt;string&gt;):Promise&lt;ResultSet&gt;
Z
zengyawen 已提交
1113 1114 1115

Queries data in the database based on specified conditions. This method uses a promise to return the result.

A
annie_wangli 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Query conditions specified by the **RdbPredicates** object.|
  | columns | Array&lt;string&gt; | No| Columns to query. If this parameter is not specified, the query applies to all columns.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;[ResultSet](../apis/js-apis-data-resultset.md)&gt; | Promise used to return the result. If the operation is successful, a **ResultSet** object will be returned.|

- Example
  ```
  let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
  predicates.equalTo("NAME", "Rose")
  let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promise.then((resultSet) => {
      console.log(TAG + "resultSet column names:" + resultSet.columnNames)
      console.log(TAG + "resultSet column count:" + resultSet.columnCount)})
  ```


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

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

Queries data in the RDB store using the specified SQL statement. This method uses a callback to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | sql | string | Yes| SQL statement to run.|
  | bindArgs | Array&lt;[ValueType](#valuetype)&gt; | Yes| Values of the parameters in the SQL statement.|
  | 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
  ```
  rdbStore.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], function (err, resultSet) {
      console.log(TAG + "resultSet column names:" + resultSet.columnNames)
      console.log(TAG + "resultSet column count:" + resultSet.columnCount)})
  ```


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

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

Queries data in the RDB store using the specified SQL statement. This method uses a promise to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | sql | string | Yes| SQL statement to run.|
  | bindArgs | Array&lt;[ValueType](#valuetype)&gt; | No| Values of the parameters in the SQL statement.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;[ResultSet](../apis/js-apis-data-resultset.md)&gt; | Promise used to return the result. If the operation is successful, a **ResultSet** object will be returned.|

- Example
  ```
  let promise = rdbStore.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'])
  promise.then((resultSet) => {
      console.log(TAG + "resultSet column names:" + resultSet.columnNames)
      console.log(TAG + "resultSet column count:" + resultSet.columnCount)})
  ```


### executeSql

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

Runs the SQL statement that contains the specified parameters but does not return a value. This method uses a callback to return the execution result.

A
annie_wangli 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | sql | string | Yes| SQL statement to run.|
  | bindArgs | Array&lt;[ValueType](#valuetype)&gt; | Yes| Values of the parameters in the SQL statement.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked to return the result.|

- Example
  ```
  rdbStore.executeSql("DELETE FROM EMPLOYEE", null, function () {
      console.info(TAG + 'delete done.')})
  ```


### executeSql

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

Runs the SQL statement that contains the specified parameters but does not return a value. This method uses a promise to return the execution result.

A
annie_wangli 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | sql | string | Yes| SQL statement to run.|
  | bindArgs | Array&lt;[ValueType](#valuetype)&gt; | No| Values of the parameters in the SQL statement.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example
  ```
  let promise = rdbStore.executeSql("DELETE FROM EMPLOYEE")
  promise.then(() => {
      console.info(TAG + 'delete done.')})
  ```

### setDistributedTables

setDistributedTables(tables: Array<string>, callback: AsyncCallback<void>): void;

Sets a list of distributed tables. This method uses a callback to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | tables | Array<string> | Yes| Names of the distributed tables to be set.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked to return the result.|

- Example
  ```
  rdbStore.setDistributedTables(["EMPLOYEE"], function (err) {
      if (err) {
          console.info('setDistributedTables failed.')
          return
      }
      console.info('setDistributedTables success.')
  })
  ```


### setDistributedTables

 setDistributedTables(tables: Array<string>): Promise<void>;

Sets a list of distributed tables. This method uses a promise to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | tables | Array<string> | Yes| Names of the distributed tables to be set.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

- Example
  ```
  let promise = rdbStore.setDistributedTables(["EMPLOYEE"])
  promise.then(() => {
      console.info("setDistributedTables success.")
  }).catch((err) => {
      console.info("setDistributedTables failed."")
  })
  ```

### obtainDistributedTableName

obtainDistributedTableName(device: string, table: string, callback: AsyncCallback<string>): void;

Obtains the distributed table name for a remote device based on the local table name. The distributed table name is required when the database of a remote device is queried. This method uses a callback to return the result.

- Parameters
  | 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
  ```
  rdbStore.obtainDistributedTableName(deviceId, "EMPLOYEE", function (err, tableName) {
      if (err) {
          console.info('obtainDistributedTableName failed.')
          return
      }
      console.info('obtainDistributedTableName success, tableName=.' + tableName)
   })
  ```


### obtainDistributedTableName

 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 database of a remote device is queried. This method uses a promise to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | device | string | Yes| Remote device.|
  | table | string | Yes| Local table name.|

- Return value
  | 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.|

- Example
  ```
  let promise = rdbStore.obtainDistributedTableName(deviceId, "EMPLOYEE")
  promise.then((tableName) => {
      console.info('obtainDistributedTableName success, tableName=' + tableName)
  }).catch((err) => {
      console.info('obtainDistributedTableName failed.')
  })
  ```

### sync

sync(mode: SyncMode, predicates: RdbPredicates, callback: AsyncCallback<Array<[string, number]>>): void;

Synchronizes data between devices. This method uses a callback to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | mode | SyncMode | Yes| Data synchronization mode, which can be **push** or **pull**.|
  | predicates | RdbPredicates | Yes| **RdbPredicates** object that specifies the data and devices to synchronize.|
  | callback | AsyncCallback&lt;Array<[string, number]>&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 each device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure. |

- Example
  ```
  let predicate = new rdb.RdbPredicates('EMPLOYEE')
  predicate.inDevices(['12345678abcde'])
  rdbStore.sync(rdb.SyncMode.SYNC_MODE_PUSH, predicate, function (err, result) {
      if (err) {
          console.log('sync failed')
          return
       }
      console.log('sync done.')
      for (let i = 0; i < result.length; i++) {
          console.log('device=' + result[i][0] + ' status=' + result[i][1])
       }
  })
  ```


### sync

 sync(mode: SyncMode, predicates: RdbPredicates): Promise<Array<[string, number]>>;

Synchronizes data between devices. This method uses a promise to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | mode | SyncMode | Yes| Data synchronization mode, which can be **push** or **pull**.|
  | predicates | RdbPredicates | Yes| **RdbPredicates** object that specifies the data and devices to synchronize.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;Array<[string, number]>&gt; | Promise used to return the synchronization result to the caller. <br>**string** indicates the device ID. <br>**number** indicates the synchronization status of each device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure. |

- Example
  ```
  let predicate = new rdb.RdbPredicates('EMPLOYEE')
  predicate.inDevices(['12345678abcde'])
  let promise = rdbStore.sync(rdb.SyncMode.SYNC_MODE_PUSH, predicate)
  promise.then(result) {
      console.log('sync done.')
      for (let i = 0; i < result.length; i++) {
          console.log('device=' + result[i][0] + ' status=' + result[i][1])
      }
   }).catch((err) => {
       console.log('sync failed')
   })
  ```

### on

on(event: 'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void;

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

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | type | SubscribeType | Yes| Type defined in **SubscribeType**.|
  | observer | Callback<Array<string>> | Yes| Observer that listens for the data changes in the RDB store.|

- Example
  ```
  function storeObserver(devices) {
      for (let i = 0; i < devices.length; i++) {
          console.log('device=' + device[i] + ' data changed')
       }
   }
   try {
       rdbStore.on('dataChange', rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver)
   } catch (err) {
       console.log('register observer failed')
   }
  ```

### off

off(event:'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void;

Deletes the specified observer of the RDB store. This method uses a callback to return the result.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | type | SubscribeType | Yes| Type defined in **SubscribeType**.|
  | observer | Callback<Array<string>> | Yes| Data change observer registered.|

- Example
  ```
  function storeObserver(devices) {
      for (let i = 0; i < devices.length; i++) {
          console.log('device=' + device[i] + ' data changed')
      }
  }
  try {
      rdbStore.off('dataChange', rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver)
  } catch (err) {
      console.log('unregister observer failed')
  }
  ```


## StoreConfig
Z
zengyawen 已提交
1447 1448 1449

Manages the configuration of an RDB store.

A
annie_wangli 已提交
1450 1451 1452 1453 1454 1455
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Database file name.|


## ValueType
Z
zengyawen 已提交
1456 1457 1458

Defines the data types allowed.

A
annie_wangli 已提交
1459 1460 1461 1462 1463 1464 1465 1466
| Name| Description|
| -------- | -------- |
| number | Number.|
| string | String.|
| boolean | Boolean.|


## ValuesBucket
Z
zengyawen 已提交
1467 1468 1469

Defines a bucket to store key-value pairs.

Z
zengyawen 已提交
1470

A
annie_wangli 已提交
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| [key:&nbsp;string] | [ValueType](#valuetype)\|&nbsp;Uint8Array&nbsp;\|&nbsp;null | Yes| Defines a bucket to store key-value pairs.|


## SyncMode

Defines the database synchronization mode.

| Name| Description|
| -------- | -------- |
| SYNC_MODE_PUSH | Data is pushed from a local device to a remote device.|
| SYNC_MODE_PULL | Data is loaded from a remote device to a local device.|

## SubscribeType

Defines the subscription type.

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