1# Relational Database 2 3The 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. 4 5This module provides the following RDB-related functions: 6 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. 8- [RdbStore](#rdbstore): provides APIs for managing an RDB store. 9 10> **NOTE**<br/> 11> 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. 13 14## Modules to Import 15 16```js 17import data_rdb from '@ohos.data.rdb'; 18``` 19 20## data_rdb.getRdbStore 21 22getRdbStore(context: Context, config: StoreConfig, version: number, callback: AsyncCallback<RdbStore>): void 23 24Obtains 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. 25 26**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 27 28**Parameters** 29 30| Name| Type| Mandatory| Description| 31| -------- | -------- | -------- | -------- | 32| context | [Context](js-apis-Context.md) | Yes| Application context.| 33| config | [StoreConfig](#storeconfig) | Yes| Configuration of the RDB store.| 34| version | number | Yes| RDB store version.| 35| callback | AsyncCallback<[RdbStore](#rdbstore)> | Yes| Callback invoked to return the RDB store obtained.| 36 37**Example** 38 39```js 40// Obtain the context. 41import featureAbility from '@ohos.ability.featureAbility' 42var context = featureAbility.getContext() 43 44// Call getRdbStore. 45const STORE_CONFIG = { name: "RdbTest.db"} 46data_rdb.getRdbStore(context, STORE_CONFIG, 1, function (err, rdbStore) { 47 if (err) { 48 console.info("Failed to get RdbStore, err: " + err) 49 return 50 } 51 console.log("Got RdbStore successfully.") 52}) 53``` 54 55## data_rdb.getRdbStore 56 57getRdbStore(context: Context, config: StoreConfig, version: number): Promise<RdbStore> 58 59Obtains 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 then call APIs to perform data operations. 60 61**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 62 63**Parameters** 64 65| Name| Type| Mandatory| Description| 66| -------- | -------- | -------- | -------- | 67| context | [Context](js-apis-Context.md) | Yes| Application context.| 68| config | [StoreConfig](#storeconfig) | Yes| Configuration of the RDB store.| 69| version | number | Yes| RDB store version.| 70 71**Return value** 72 73| Type| Description| 74| -------- | -------- | 75| Promise<[RdbStore](#rdbstore)> | Promise used to return the RDB store obtained.| 76 77**Example** 78 79```js 80// Obtain the context. 81import featureAbility from '@ohos.ability.featureAbility' 82var context = featureAbility.getContext() 83 84// Call getRdbStore. 85const STORE_CONFIG = { name: "RdbTest.db" } 86let promise = data_rdb.getRdbStore(context, STORE_CONFIG, 1); 87promise.then(async (rdbStore) => { 88 console.log("Got RdbStore successfully.") 89}).catch((err) => { 90 console.log("Failed to get RdbStore, err: " + err) 91}) 92``` 93 94## data_rdb.deleteRdbStore 95 96deleteRdbStore(context: Context, name: string, callback: AsyncCallback<void>): void 97 98Deletes an RDB store. This API uses an asynchronous callback to return the result. 99 100**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 101 102**Parameters** 103| Name| Type| Mandatory| Description| 104| -------- | -------- | -------- | -------- | 105| context | [Context](js-apis-Context.md) | Yes| Application context.| 106| name | string | Yes| Name of the RDB store to delete.| 107| callback | AsyncCallback<void> | Yes| Callback invoked to return the result.| 108 109**Example** 110```js 111// Obtain the context. 112import featureAbility from '@ohos.ability.featureAbility' 113var context = featureAbility.getContext() 114 115// Call deleteRdbStore. 116data_rdb.deleteRdbStore(context, "RdbTest.db", function (err) { 117 if (err) { 118 console.info("Failed to delete RdbStore, err: " + err) 119 return 120 } 121 console.log("Deleted RdbStore successfully.") 122}) 123``` 124 125## data_rdb.deleteRdbStore 126 127deleteRdbStore(context: Context, name: string): Promise<void> 128 129Deletes an RDB store. This API uses a promise to return the result. 130 131**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 132 133**Parameters** 134| Name| Type| Mandatory| Description| 135| -------- | -------- | -------- | -------- | 136| context | [Context](js-apis-Context.md) | Yes| Application context.| 137| name | string | Yes| Name of the RDB store to delete.| 138 139**Return value** 140| Type| Description| 141| -------- | -------- | 142| Promise<void> | Promise used to return the result.| 143 144**Example** 145```js 146// Obtain the context. 147import featureAbility from '@ohos.ability.featureAbility' 148var context = featureAbility.getContext() 149 150// Call deleteRdbStore. 151let promise = data_rdb.deleteRdbStore(context, "RdbTest.db") 152promise.then(()=>{ 153 console.log("Deleted RdbStore successfully.") 154}).catch((err) => { 155 console.info("Failed to delete RdbStore, err: " + err) 156}) 157``` 158 159 160## RdbPredicates 161 162Defines predicates for an RDB store. This class determines whether the conditional expression for the RDB store is true or false. 163 164 165### constructor 166 167constructor(name: string) 168 169 170A constructor used to create an **RdbPredicates** object. 171 172**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 173 174**Parameters** 175| Name| Type| Mandatory| Description| 176| -------- | -------- | -------- | -------- | 177| name | string | Yes| Database table name.| 178 179**Example** 180```js 181let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 182``` 183 184### inDevices<sup>8+</sup> 185 186inDevices(devices: Array<string>): RdbPredicates 187 188 189Sets an **RdbPredicates** to specify the remote devices to connect on the network during distributed database synchronization. 190 191**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 192 193**Parameters** 194| Name| Type| Mandatory| Description| 195| -------- | -------- | -------- | -------- | 196| devices | Array<string> | Yes| IDs of the remote devices in the same network.| 197 198**Return value** 199| Type| Description| 200| -------- | -------- | 201| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 202 203**Example** 204```js 205let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 206predicates.inDevices(['12345678abcde']) 207``` 208 209### inAllDevices<sup>8+</sup> 210 211inAllDevices(): RdbPredicates 212 213 214Sets an **RdbPredicates** to specify all remote devices on the network to connect during distributed database synchronization. 215 216**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 217 218**Return value** 219| Type| Description| 220| -------- | -------- | 221| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 222 223**Example** 224```js 225let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 226predicates.inAllDevices() 227``` 228 229### equalTo 230 231equalTo(field: string, value: ValueType): RdbPredicates 232 233 234Sets an **RdbPredicates** to match the field with data type **ValueType** and value equal to the specified value. 235 236**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 237 238**Parameters** 239| Name| Type| Mandatory| Description| 240| -------- | -------- | -------- | -------- | 241| field | string | Yes| Column name in the database table.| 242| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.| 243 244**Return value** 245| Type| Description| 246| -------- | -------- | 247| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 248 249**Example** 250```js 251let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 252predicates.equalTo("NAME", "lisi") 253``` 254 255 256### notEqualTo 257 258notEqualTo(field: string, value: ValueType): RdbPredicates 259 260 261Sets an **RdbPredicates** to match the field with data type **ValueType** and value not equal to the specified value. 262 263**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 264 265**Parameters** 266| Name| Type| Mandatory| Description| 267| -------- | -------- | -------- | -------- | 268| field | string | Yes| Column name in the database table.| 269| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.| 270 271**Return value** 272| Type| Description| 273| -------- | -------- | 274| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 275 276**Example** 277```js 278let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 279predicates.notEqualTo("NAME", "lisi") 280``` 281 282 283### beginWrap 284 285beginWrap(): RdbPredicates 286 287 288Adds a left parenthesis to the **RdbPredicates**. 289 290**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 291 292**Return value** 293| Type| Description| 294| -------- | -------- | 295| [RdbPredicates](#rdbpredicates) | **RdbPredicates** with a left parenthesis.| 296 297**Example** 298```js 299let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 300predicates.equalTo("NAME", "lisi") 301 .beginWrap() 302 .equalTo("AGE", 18) 303 .or() 304 .equalTo("SALARY", 200.5) 305 .endWrap() 306``` 307 308 309### endWrap 310 311endWrap(): RdbPredicates 312 313 314Adds a right parenthesis to the **RdbPredicates**. 315 316**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 317 318**Return value** 319| Type| Description| 320| -------- | -------- | 321| [RdbPredicates](#rdbpredicates) | **RdbPredicates** with a right parenthesis.| 322 323**Example** 324```js 325let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 326predicates.equalTo("NAME", "lisi") 327 .beginWrap() 328 .equalTo("AGE", 18) 329 .or() 330 .equalTo("SALARY", 200.5) 331 .endWrap() 332``` 333 334 335### or 336 337or(): RdbPredicates 338 339 340Adds the OR condition to the **RdbPredicates**. 341 342**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 343 344**Return value** 345| Type| Description| 346| -------- | -------- | 347| [RdbPredicates](#rdbpredicates) | **RdbPredicates** with the OR condition.| 348 349**Example** 350```js 351let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 352predicates.equalTo("NAME", "Lisa") 353 .or() 354 .equalTo("NAME", "Rose") 355``` 356 357 358### and 359 360and(): RdbPredicates 361 362 363Adds the AND condition to the **RdbPredicates**. 364 365**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 366 367**Return value** 368| Type| Description| 369| -------- | -------- | 370| [RdbPredicates](#rdbpredicates) | **RdbPredicates** with the AND condition.| 371 372**Example** 373```js 374let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 375predicates.equalTo("NAME", "Lisa") 376 .and() 377 .equalTo("SALARY", 200.5) 378``` 379 380 381### contains 382 383contains(field: string, value: string): RdbPredicates 384 385Sets an **RdbPredicates** to match a string containing the specified value. 386 387**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 388 389**Parameters** 390| Name| Type| Mandatory| Description| 391| -------- | -------- | -------- | -------- | 392| field | string | Yes| Column name in the database table.| 393| value | string | Yes| Value to match the **RdbPredicates**.| 394 395**Return value** 396| Type| Description| 397| -------- | -------- | 398| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 399 400**Example** 401```js 402let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 403predicates.contains("NAME", "os") 404``` 405 406 407### beginsWith 408 409beginsWith(field: string, value: string): RdbPredicates 410 411 412Sets an **RdbPredicates** to match a string that starts with the specified value. 413 414**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 415 416**Parameters** 417| Name| Type| Mandatory| Description| 418| -------- | -------- | -------- | -------- | 419| field | string | Yes| Column name in the database table.| 420| value | string | Yes| Value to match the **RdbPredicates**.| 421 422**Return value** 423| Type| Description| 424| -------- | -------- | 425| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 426 427**Example** 428```js 429let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 430predicates.beginsWith("NAME", "os") 431``` 432 433 434### endsWith 435 436endsWith(field: string, value: string): RdbPredicates 437 438 439Sets an **RdbPredicates** to match a string that ends with the specified value. 440 441**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 442 443**Parameters** 444| Name| Type| Mandatory| Description| 445| -------- | -------- | -------- | -------- | 446| field | string | Yes| Column name in the database table.| 447| value | string | Yes| Value to match the **RdbPredicates**.| 448 449**Return value** 450| Type| Description| 451| -------- | -------- | 452| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 453 454**Example** 455```js 456let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 457predicates.endsWith("NAME", "se") 458``` 459 460 461### isNull 462 463isNull(field: string): RdbPredicates 464 465 466Sets an **RdbPredicates** to match the field whose value is null. 467 468**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 469 470**Parameters** 471| Name| Type| Mandatory| Description| 472| -------- | -------- | -------- | -------- | 473| field | string | Yes| Column name in the database table.| 474 475**Return value** 476| Type| Description| 477| -------- | -------- | 478| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 479 480- Example 481```js 482let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 483predicates.isNull("NAME") 484``` 485 486 487### isNotNull 488 489isNotNull(field: string): RdbPredicates 490 491 492Sets an **RdbPredicates** to match the field whose value is not null. 493 494**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 495 496**Parameters** 497| Name| Type| Mandatory| Description| 498| -------- | -------- | -------- | -------- | 499| field | string | Yes| Column name in the database table.| 500 501**Return value** 502| Type| Description| 503| -------- | -------- | 504| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 505 506**Example** 507```js 508let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 509predicates.isNotNull("NAME") 510``` 511 512 513### like 514 515like(field: string, value: string): RdbPredicates 516 517 518Sets an **RdbPredicates** to match a string that is similar to the specified value. 519 520**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 521 522**Parameters** 523| Name| Type| Mandatory| Description| 524| -------- | -------- | -------- | -------- | 525| field | string | Yes| Column name in the database table.| 526| value | string | Yes| Value to match the **RdbPredicates**.| 527 528**Return value** 529| Type| Description| 530| -------- | -------- | 531| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 532 533**Example** 534```js 535let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 536predicates.like("NAME", "%os%") 537``` 538 539 540### glob 541 542glob(field: string, value: string): RdbPredicates 543 544 545Sets an **RdbPredicates** to match the specified string. 546 547**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 548 549**Parameters** 550| Name| Type| Mandatory| Description| 551| -------- | -------- | -------- | -------- | 552| field | string | Yes| Column name in the database table.| 553| 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.| 554 555**Return value** 556| Type| Description| 557| -------- | -------- | 558| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 559 560**Example** 561```js 562let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 563predicates.glob("NAME", "?h*g") 564``` 565 566 567### between 568 569between(field: string, low: ValueType, high: ValueType): RdbPredicates 570 571 572Sets an **RdbPredicates** to match the field with data type **ValueType** and value within the specified range. 573 574**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 575 576**Parameters** 577| Name| Type| Mandatory| Description| 578| -------- | -------- | -------- | -------- | 579| field | string | Yes| Column name in the database table.| 580| low | [ValueType](#valuetype) | Yes| Minimum value to match the **RdbPredicates**.| 581| high | [ValueType](#valuetype) | Yes| Maximum value to match the **RdbPredicates**.| 582 583**Return value** 584| Type| Description| 585| -------- | -------- | 586| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 587 588**Example** 589```js 590let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 591predicates.between("AGE", 10, 50) 592``` 593 594 595### notBetween 596 597notBetween(field: string, low: ValueType, high: ValueType): RdbPredicates 598 599 600Sets an **RdbPredicates** to match the field with data type **ValueType** and value out of the specified range. 601 602**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 603 604**Parameters** 605| Name| Type| Mandatory| Description| 606| -------- | -------- | -------- | -------- | 607| field | string | Yes| Column name in the database table.| 608| low | [ValueType](#valuetype) | Yes| Minimum value to match the **RdbPredicates**.| 609| high | [ValueType](#valuetype) | Yes| Maximum value to match the **RdbPredicates**.| 610 611**Return value** 612| Type| Description| 613| -------- | -------- | 614| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 615 616**Example** 617```js 618let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 619predicates.notBetween("AGE", 10, 50) 620``` 621 622 623### greaterThan 624 625greaterThan(field: string, value: ValueType): RdbPredicates 626 627Sets an **RdbPredicates** to match the field with data type **ValueType** and value greater than the specified value. 628 629**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 630 631**Parameters** 632| Name| Type| Mandatory| Description| 633| -------- | -------- | -------- | -------- | 634| field | string | Yes| Column name in the database table.| 635| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.| 636 637**Return value** 638| Type| Description| 639| -------- | -------- | 640| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 641 642**Example** 643```js 644let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 645predicates.greaterThan("AGE", 18) 646``` 647 648 649### lessThan 650 651lessThan(field: string, value: ValueType): RdbPredicates 652 653 654Sets an **RdbPredicates** to match the field with data type **ValueType** and value less than the specified value. 655 656**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 657 658**Parameters** 659| Name| Type| Mandatory| Description| 660| -------- | -------- | -------- | -------- | 661| field | string | Yes| Column name in the database table.| 662| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.| 663 664**Return value** 665| Type| Description| 666| -------- | -------- | 667| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 668 669**Example** 670```js 671let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 672predicates.lessThan("AGE", 20) 673``` 674 675 676### greaterThanOrEqualTo 677 678 679greaterThanOrEqualTo(field: string, value: ValueType): RdbPredicates 680 681 682Sets an **RdbPredicates** to match the field with data type **ValueType** and value greater than or equal to the specified value. 683 684**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 685 686**Parameters** 687| Name| Type| Mandatory| Description| 688| -------- | -------- | -------- | -------- | 689| field | string | Yes| Column name in the database table.| 690| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.| 691 692**Return value** 693| Type| Description| 694| -------- | -------- | 695| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 696 697**Example** 698```js 699let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 700predicates.greaterThanOrEqualTo("AGE", 18) 701``` 702 703 704### lessThanOrEqualTo 705 706 707lessThanOrEqualTo(field: string, value: ValueType): RdbPredicates 708 709 710Sets an **RdbPredicates** to match the field with data type **ValueType** and value less than or equal to the specified value. 711 712**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 713 714**Parameters** 715| Name| Type| Mandatory| Description| 716| -------- | -------- | -------- | -------- | 717| field | string | Yes| Column name in the database table.| 718| value | [ValueType](#valuetype) | Yes| Value to match the **RdbPredicates**.| 719 720**Return value** 721| Type| Description| 722| -------- | -------- | 723| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 724 725**Example** 726```js 727let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 728predicates.lessThanOrEqualTo("AGE", 20) 729``` 730 731 732### orderByAsc 733 734 735orderByAsc(field: string): RdbPredicates 736 737 738Sets an **RdbPredicates** to match the column with values sorted in ascending order. 739 740**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 741 742**Parameters** 743| Name| Type| Mandatory| Description| 744| -------- | -------- | -------- | -------- | 745| field | string | Yes| Column name in the database table.| 746 747**Return value** 748| Type| Description| 749| -------- | -------- | 750| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 751 752**Example** 753```js 754let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 755predicates.orderByAsc("NAME") 756``` 757 758 759### orderByDesc 760 761 762orderByDesc(field: string): RdbPredicates 763 764 765Sets an **RdbPredicates** to match the column with values sorted in descending order. 766 767**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 768 769**Parameters** 770| Name| Type| Mandatory| Description| 771| -------- | -------- | -------- | -------- | 772| field | string | Yes| Column name in the database table.| 773 774**Return value** 775| Type| Description| 776| -------- | -------- | 777| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 778 779**Example** 780```js 781let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 782predicates.orderByDesc("AGE") 783``` 784 785 786### distinct 787 788distinct(): RdbPredicates 789 790 791Sets an **RdbPredicates** to filter out duplicate records. 792 793**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 794 795**Return value** 796| Type| Description| 797| -------- | -------- | 798| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that can filter out duplicate records.| 799 800**Example** 801```js 802let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 803predicates.equalTo("NAME", "Rose").distinct() 804``` 805 806 807### limitAs 808 809limitAs(value: number): RdbPredicates 810 811 812Sets an **RdbPredicates** to specify the maximum number of records. 813 814**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 815 816**Parameters** 817| Name| Type| Mandatory| Description| 818| -------- | -------- | -------- | -------- | 819| value | number | Yes| Maximum number of records.| 820 821**Return value** 822| Type| Description| 823| -------- | -------- | 824| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that specifies the maximum number of records.| 825 826**Example** 827```js 828let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 829predicates.equalTo("NAME", "Rose").limitAs(3) 830``` 831 832 833### offsetAs 834 835offsetAs(rowOffset: number): RdbPredicates 836 837 838Sets an **RdbPredicates** to specify the start position of the returned result. 839 840**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 841 842**Parameters** 843| Name| Type| Mandatory| Description| 844| -------- | -------- | -------- | -------- | 845| rowOffset | number | Yes| Number of rows to offset from the beginning. The value is a positive integer.| 846 847**Return value** 848| Type| Description| 849| -------- | -------- | 850| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that specifies the start position of the returned result.| 851 852**Example** 853```js 854let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 855predicates.equalTo("NAME", "Rose").offsetAs(3) 856``` 857 858 859### groupBy 860 861groupBy(fields: Array<string>): RdbPredicates 862 863 864Sets an **RdbPredicates** to group rows that have the same value into summary rows. 865 866**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 867 868**Parameters** 869| Name| Type| Mandatory| Description| 870| -------- | -------- | -------- | -------- | 871| fields | Array<string> | Yes| Names of columns to group.| 872 873**Return value** 874| Type| Description| 875| -------- | -------- | 876| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that groups rows with the same value.| 877 878**Example** 879```js 880let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 881predicates.groupBy(["AGE", "NAME"]) 882``` 883 884 885### indexedBy 886 887indexedBy(field: string): RdbPredicates 888 889Sets an **RdbPredicates** object to specify the index column. 890 891**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 892 893**Parameters** 894| Name| Type| Mandatory| Description| 895| -------- | -------- | -------- | -------- | 896| field | string | Yes| Name of the index column.| 897 898**Return value** 899 900| Type| Description| 901| -------- | -------- | 902| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that specifies the index column.| 903 904**Example** 905```js 906let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 907predicates.indexedBy("SALARY_INDEX") 908``` 909 910 911### in 912 913in(field: string, value: Array<ValueType>): RdbPredicates 914 915 916Sets an **RdbPredicates** to match the field with data type **Array<ValueType>** and value within the specified range. 917 918**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 919 920**Parameters** 921| Name| Type| Mandatory| Description| 922| -------- | -------- | -------- | -------- | 923| field | string | Yes| Column name in the database table.| 924| value | Array<[ValueType](#valuetype)> | Yes| Array of **ValueType**s to match.| 925 926 927**Return value** 928| Type| Description| 929| -------- | -------- | 930| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 931 932**Example** 933```js 934let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 935predicates.in("AGE", [18, 20]) 936``` 937 938 939### notIn 940 941notIn(field: string, value: Array<ValueType>): RdbPredicates 942 943 944Sets an **RdbPredicates** to match the field with data type **Array<ValueType>** and value out of the specified range. 945 946**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 947 948**Parameters** 949| Name| Type| Mandatory| Description| 950| -------- | -------- | -------- | -------- | 951| field | string | Yes| Column name in the database table.| 952| value | Array<[ValueType](#valuetype)> | Yes| Array of **ValueType**s to match.| 953 954 955**Return value** 956| Type| Description| 957| -------- | -------- | 958| [RdbPredicates](#rdbpredicates) | **RdbPredicates** object that matches the specified field.| 959 960**Example** 961```js 962let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 963predicates.notIn("NAME", ["Lisa", "Rose"]) 964``` 965 966 967## RdbStore 968 969Provides methods to manage an RDB store. 970 971Before using the following APIs, use [executeSql](#executesql) to initialize the database table structure and related data. For details, see [RDB Development](../../database/database-relational-guidelines.md). 972 973 974### insert 975 976insert(name: string, values: ValuesBucket, callback: AsyncCallback<number>):void 977 978Inserts a row of data into a table. This API uses an asynchronous callback to return the result. 979 980**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 981 982**Parameters** 983| Name| Type| Mandatory| Description| 984| -------- | -------- | -------- | -------- | 985| name | string | Yes| Name of the target table.| 986| values | [ValuesBucket](#valuesbucket) | Yes| Row of data to insert.| 987| callback | AsyncCallback<number> | Yes| Callback invoked to return the result. If the operation is successful, the row ID will be returned. Otherwise, **-1** will be returned.| 988 989**Example** 990```js 991const valueBucket = { 992 "NAME": "Lisa", 993 "AGE": 18, 994 "SALARY": 100.5, 995 "CODES": new Uint8Array([1, 2, 3, 4, 5]), 996} 997rdbStore.insert("EMPLOYEE", valueBucket, function (err, ret) { 998 if (err) { 999 console.info("Failed to insert data, err: " + err) 1000 return 1001 } 1002 console.log("Inserted first row: " + ret) 1003}) 1004``` 1005 1006 1007### insert 1008 1009insert(name: string, values: ValuesBucket):Promise<number> 1010 1011Inserts a row of data into a table. This API uses a promise to return the result. 1012 1013**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1014 1015**Parameters** 1016| Name| Type| Mandatory| Description| 1017| -------- | -------- | -------- | -------- | 1018| name | string | Yes| Name of the target table.| 1019| values | [ValuesBucket](#valuesbucket) | Yes| Row of data to insert.| 1020 1021**Return value** 1022| Type| Description| 1023| -------- | -------- | 1024| Promise<number> | Promise used to return the result. If the operation is successful, the row ID will be returned. Otherwise, **-1** will be returned.| 1025 1026**Example** 1027```js 1028const valueBucket = { 1029 "NAME": "Lisa", 1030 "AGE": 18, 1031 "SALARY": 100.5, 1032 "CODES": new Uint8Array([1, 2, 3, 4, 5]), 1033} 1034let promise = rdbStore.insert("EMPLOYEE", valueBucket) 1035promise.then(async (ret) => { 1036 console.log("Inserted first row: " + ret) 1037}).catch((err) => { 1038 console.log("Failed to insert data, err: " + err) 1039}) 1040``` 1041 1042 1043### update 1044 1045update(values: ValuesBucket, rdbPredicates: RdbPredicates, callback: AsyncCallback<number>):void 1046 1047Updates data in the RDB store based on the specified **RdbPredicates** object. This API uses an asynchronous callback to return the result. 1048 1049**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1050 1051**Parameters** 1052| Name| Type| Mandatory| Description| 1053| -------- | -------- | -------- | -------- | 1054| 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.| 1055| rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Update conditions specified by the **RdbPredicates** object.| 1056| callback | AsyncCallback<number> | Yes| Callback invoked to return the number of rows updated.| 1057 1058**Example** 1059```js 1060const valueBucket = { 1061 "NAME": "Rose", 1062 "AGE": 22, 1063 "SALARY": 200.5, 1064 "CODES": new Uint8Array([1, 2, 3, 4, 5]), 1065} 1066let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 1067predicates.equalTo("NAME", "Lisa") 1068rdbStore.update(valueBucket, predicates, function (err, ret) { 1069 if (err) { 1070 console.info("Failed to update data, err: " + err) 1071 return 1072 } 1073 console.log("Updated row count: " + ret) 1074}) 1075``` 1076 1077 1078### update 1079 1080update(values: ValuesBucket, rdbPredicates: RdbPredicates):Promise<number> 1081 1082Updates data in the RDB store based on the specified **RdbPredicates** object. This API uses a promise to return the result. 1083 1084**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1085 1086**Parameters** 1087| Name| Type| Mandatory| Description| 1088| -------- | -------- | -------- | -------- | 1089| 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.| 1090| rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Update conditions specified by the **RdbPredicates** object.| 1091 1092**Return value** 1093| Type| Description| 1094| -------- | -------- | 1095| Promise<number> | Promise used to return the number of rows updated.| 1096 1097**Example** 1098```js 1099const valueBucket = { 1100 "NAME": "Rose", 1101 "AGE": 22, 1102 "SALARY": 200.5, 1103 "CODES": new Uint8Array([1, 2, 3, 4, 5]), 1104} 1105let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 1106predicates.equalTo("NAME", "Lisa") 1107let promise = rdbStore.update(valueBucket, predicates) 1108promise.then(async (ret) => { 1109 console.log("Updated row count: " + ret) 1110}).catch((err) => { 1111 console.info("Failed to update data, err: " + err) 1112}) 1113``` 1114 1115 1116### delete 1117 1118delete(rdbPredicates: RdbPredicates, callback: AsyncCallback<number>):void 1119 1120 1121Deletes data from the RDB store based on the specified **RdbPredicates** object. This API uses an asynchronous callback to return the result. 1122 1123**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1124 1125**Parameters** 1126| Name| Type| Mandatory| Description| 1127| -------- | -------- | -------- | -------- | 1128| rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Conditions specified by the **RdbPredicates** object for deleting data.| 1129| callback | AsyncCallback<number> | Yes| Callback invoked to return the number of rows updated.| 1130 1131**Example** 1132```js 1133let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 1134predicates.equalTo("NAME", "Lisa") 1135rdbStore.delete(predicates, function (err, rows) { 1136 if (err) { 1137 console.info("Failed to delete data, err: " + err) 1138 return 1139 } 1140 console.log("Deleted rows: " + rows) 1141}) 1142``` 1143 1144 1145### delete 1146 1147delete(rdbPredicates: RdbPredicates):Promise<number> 1148 1149Deletes data from the RDB store based on the specified **RdbPredicates** object. This API uses a promise to return the result. 1150 1151**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1152 1153**Parameters** 1154| Name| Type| Mandatory| Description| 1155| -------- | -------- | -------- | -------- | 1156| rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Conditions specified by the **RdbPredicates** object for deleting data.| 1157 1158**Return value** 1159| Type| Description| 1160| -------- | -------- | 1161| Promise<number> | Promise used to return the number of rows updated.| 1162 1163**Example** 1164```js 1165let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 1166predicates.equalTo("NAME", "Lisa") 1167let promise = rdbStore.delete(predicates) 1168promise.then((rows) => { 1169 console.log("Deleted rows: " + rows) 1170}).catch((err) => { 1171 console.info("Failed to delete data, err: " + err) 1172}) 1173``` 1174 1175 1176### query 1177 1178query(rdbPredicates: RdbPredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void 1179 1180Queries data in the RDB store based on specified conditions. This API uses an asynchronous callback to return the result. 1181 1182**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1183 1184**Parameters** 1185| Name| Type| Mandatory| Description| 1186| -------- | -------- | -------- | -------- | 1187| rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Query conditions specified by the **RdbPredicates** object.| 1188| columns | Array<string> | Yes| Columns to query. If this parameter is not specified, the query applies to all columns.| 1189| callback | AsyncCallback<[ResultSet](js-apis-data-resultset.md)> | Yes| Callback invoked to return the result. If the operation is successful, a **ResultSet** object will be returned.| 1190 1191**Example** 1192```js 1193let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 1194predicates.equalTo("NAME", "Rose") 1195rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) { 1196 if (err) { 1197 console.info("Failed to query data, err: " + err) 1198 return 1199 } 1200 console.log("resultSet column names:" + resultSet.columnNames) 1201 console.log("resultSet column count:" + resultSet.columnCount) 1202}) 1203``` 1204 1205 1206### query 1207 1208query(rdbPredicates: RdbPredicates, columns?: Array<string>):Promise<ResultSet> 1209 1210Queries data in the RDB store based on specified conditions. This API uses a promise to return the result. 1211 1212**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1213 1214**Parameters** 1215| Name| Type| Mandatory| Description| 1216| -------- | -------- | -------- | -------- | 1217| rdbPredicates | [RdbPredicates](#rdbpredicates) | Yes| Query conditions specified by the **RdbPredicates** object.| 1218| columns | Array<string> | No| Columns to query. If this parameter is not specified, the query applies to all columns.| 1219 1220**Return value** 1221| Type| Description| 1222| -------- | -------- | 1223| Promise<[ResultSet](js-apis-data-resultset.md)> | Promise used to return the result. If the operation is successful, a **ResultSet** object will be returned.| 1224 1225**Example** 1226 ```js 1227 let predicates = new data_rdb.RdbPredicates("EMPLOYEE") 1228 predicates.equalTo("NAME", "Rose") 1229 let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]) 1230 promise.then((resultSet) => { 1231 console.log("resultSet column names:" + resultSet.columnNames) 1232 console.log("resultSet column count:" + resultSet.columnCount) 1233 }).catch((err) => { 1234 console.info("Failed to query data, err: " + err) 1235 }) 1236 ``` 1237 1238 1239### querySql<sup>8+</sup> 1240 1241querySql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<ResultSet>):void 1242 1243Queries data in the RDB store using the specified SQL statement. This API uses an asynchronous callback to return the result. 1244 1245**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1246 1247**Parameters** 1248| Name| Type| Mandatory| Description| 1249| -------- | -------- | -------- | -------- | 1250| sql | string | Yes| SQL statement to run.| 1251| bindArgs | Array<[ValueType](#valuetype)> | Yes| Arguments in the SQL statement.| 1252| callback | AsyncCallback<[ResultSet](js-apis-data-resultset.md)> | Yes| Callback invoked to return the result. If the operation is successful, a **ResultSet** object will be returned.| 1253 1254**Example** 1255```js 1256rdbStore.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], function (err, resultSet) { 1257 if (err) { 1258 console.info("Failed to query data, err: " + err) 1259 return 1260 } 1261 console.log("resultSet column names:" + resultSet.columnNames) 1262 console.log("resultSet column count:" + resultSet.columnCount) 1263}) 1264``` 1265 1266 1267### querySql<sup>8+</sup> 1268 1269querySql(sql: string, bindArgs?: Array<ValueType>):Promise<ResultSet> 1270 1271Queries data in the RDB store using the specified SQL statement. This API uses a promise to return the result. 1272 1273**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1274 1275**Parameters** 1276| Name| Type| Mandatory| Description| 1277| -------- | -------- | -------- | -------- | 1278| sql | string | Yes| SQL statement to run.| 1279| bindArgs | Array<[ValueType](#valuetype)> | No| Arguments in the SQL statement.| 1280 1281**Return value** 1282| Type| Description| 1283| -------- | -------- | 1284| Promise<[ResultSet](js-apis-data-resultset.md)> | Promise used to return the result. If the operation is successful, a **ResultSet** object will be returned.| 1285 1286**Example** 1287```js 1288let promise = rdbStore.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo']) 1289promise.then((resultSet) => { 1290 console.log("resultSet column names:" + resultSet.columnNames) 1291 console.log("resultSet column count:" + resultSet.columnCount) 1292}).catch((err) => { 1293 console.info("Failed to query data, err: " + err) 1294}) 1295``` 1296 1297 1298### executeSql 1299 1300executeSql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<void>):void 1301 1302Runs the SQL statement that contains the specified parameters but does not return a value. This API uses an asynchronous callback to return the result. 1303 1304**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1305 1306**Parameters** 1307| Name| Type| Mandatory| Description| 1308| -------- | -------- | -------- | -------- | 1309| sql | string | Yes| SQL statement to run.| 1310| bindArgs | Array<[ValueType](#valuetype)> | Yes| Arguments in the SQL statement.| 1311| callback | AsyncCallback<void> | Yes| Callback invoked to return the result.| 1312 1313**Example** 1314```js 1315const SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)" 1316rdbStore.executeSql(SQL_CREATE_TABLE, null, function(err) { 1317 if (err) { 1318 console.info("Failed to execute SQL, err: " + err) 1319 return 1320 } 1321 console.info('Created table successfully.') 1322}) 1323``` 1324 1325 1326### executeSql 1327 1328executeSql(sql: string, bindArgs?: Array<ValueType>):Promise<void> 1329 1330Runs the SQL statement that contains the specified parameters but does not return a value. This API uses a promise to return the execution result. 1331 1332**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1333 1334**Parameters** 1335| Name| Type| Mandatory| Description| 1336| -------- | -------- | -------- | -------- | 1337| sql | string | Yes| SQL statement to run.| 1338| bindArgs | Array<[ValueType](#valuetype)> | No| Arguments in the SQL statement.| 1339 1340**Return value** 1341| Type| Description| 1342| -------- | -------- | 1343| Promise<void> | Promise used to return the result.| 1344 1345**Example** 1346```js 1347const SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)" 1348let promise = rdbStore.executeSql(SQL_CREATE_TABLE) 1349promise.then(() => { 1350 console.info('Created table successfully.') 1351}).catch((err) => { 1352 console.info("Failed to execute SQL, err: " + err) 1353}) 1354``` 1355 1356### beginTransaction<sup>8+</sup> 1357 1358beginTransaction():void 1359 1360Starts the transaction before executing an SQL statement. 1361 1362**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1363 1364**Example** 1365```js 1366rdbStore.beginTransaction() 1367const valueBucket = { 1368 "name": "lisi", 1369 "age": 18, 1370 "salary": 100.5, 1371 "blobType": new Uint8Array([1, 2, 3]), 1372} 1373rdbStore.insert("test", valueBucket, function (err, ret) { 1374 if (err) { 1375 console.info("Failed to insert data, err: " + err) 1376 return 1377 } 1378 console.log("Inserted data successfully: " + ret) 1379}) 1380rdbStore.commit() 1381``` 1382 1383 1384### commit<sup>8+</sup> 1385 1386commit():void 1387 1388Commits the executed SQL statements. 1389 1390**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1391 1392**Example** 1393```js 1394rdbStore.beginTransaction() 1395const valueBucket = { 1396 "name": "lisi", 1397 "age": 18, 1398 "salary": 100.5, 1399 "blobType": new Uint8Array([1, 2, 3]), 1400} 1401 1402rdbStore.insert("test", valueBucket, function (err, ret) { 1403 if (err) { 1404 console.info("Failed to insert data, err: " + err) 1405 return 1406 } 1407 console.log("Inserted data successfully: " + ret) 1408}) 1409rdbStore.commit() 1410``` 1411 1412 1413### rollBack<sup>8+</sup> 1414 1415rollBack():void; 1416 1417Rolls back the SQL statements that have been executed. 1418 1419**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1420 1421**Example** 1422```js 1423try { 1424 rdbStore.beginTransaction() 1425 const valueBucket = { 1426 "id": 1, 1427 "name": "lisi", 1428 "age": 18, 1429 "salary": 100.5, 1430 "blobType": new Uint8Array([1, 2, 3]), 1431 } 1432 rdbStore.insert("test", valueBucket, function (err, ret) { 1433 if (err) { 1434 console.info("Failed to insert data, err: " + err) 1435 return 1436 } 1437 console.log("Inserted data successfully: " + ret) 1438 }) 1439 rdbStore.commit() 1440} catch (e) { 1441 rdbStore.rollBack() 1442} 1443``` 1444 1445 1446### setDistributedTables<sup>8+</sup> 1447 1448setDistributedTables(tables: Array<string>, callback: AsyncCallback<void>): void 1449 1450Sets distributed tables. This API uses an asynchronous callback to return the result. 1451 1452**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1453 1454**Parameters** 1455| Name| Type| Mandatory| Description| 1456| -------- | -------- | -------- | -------- | 1457| tables | Array<string> | Yes| Names of the distributed tables to set.| 1458| callback | AsyncCallback<void> | Yes| Callback invoked to return the result.| 1459 1460**Example** 1461```js 1462rdbStore.setDistributedTables(["EMPLOYEE"], function (err) { 1463 if (err) { 1464 console.info('Failed to set distributed tables, err: ' + err) 1465 return 1466 } 1467 console.info('Set distributed tables successfully.') 1468}) 1469``` 1470 1471 1472### setDistributedTables<sup>8+</sup> 1473 1474 setDistributedTables(tables: Array<string>): Promise<void> 1475 1476Sets distributed tables. This API uses a promise to return the result. 1477 1478**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1479 1480**Parameters** 1481| Name| Type| Mandatory| Description| 1482| -------- | -------- | -------- | -------- | 1483| tables | Array<string> | Yes| Names of the distributed tables to set.| 1484 1485**Return value** 1486| Type| Description| 1487| -------- | -------- | 1488| Promise<void> | Promise used to return the result.| 1489 1490**Example** 1491```js 1492let promise = rdbStore.setDistributedTables(["EMPLOYEE"]) 1493promise.then(() => { 1494 console.info("Set distributed tables successfully.") 1495}).catch((err) => { 1496 console.info('Failed to set distributed tables, err: ' + err) 1497}) 1498``` 1499 1500### obtainDistributedTableName<sup>8+</sup> 1501 1502obtainDistributedTableName(device: string, table: string, callback: AsyncCallback<string>): void 1503 1504Obtains 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 database of a remote device is queried. 1505 1506**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1507 1508**Parameters** 1509| Name| Type| Mandatory| Description| 1510| -------- | -------- | -------- | -------- | 1511| device | string | Yes| Remote device.| 1512| table | string | Yes| Local table name.| 1513| callback | AsyncCallback<string> | Yes| Callback invoked to return the result. If the operation succeeds, the distributed table name of the remote device is returned.| 1514 1515**Example** 1516```js 1517rdbStore.obtainDistributedTableName("12345678abcde", "EMPLOYEE", function (err, tableName) { 1518 if (err) { 1519 console.info('Failed to obtain DistributedTableName, err: ' + err) 1520 return 1521 } 1522 console.info('Obtained DistributedTableName successfully, tableName=.' + tableName) 1523}) 1524``` 1525 1526 1527### obtainDistributedTableName<sup>8+</sup> 1528 1529 obtainDistributedTableName(device: string, table: string): Promise<string> 1530 1531Obtains 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 used to query the RDB store of the remote device. 1532 1533**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1534 1535**Parameters** 1536| Name| Type| Mandatory| Description| 1537| -------- | -------- | -------- | -------- | 1538| device | string | Yes| Remote device.| 1539| table | string | Yes| Local table name.| 1540 1541**Return value** 1542| Type| Description| 1543| -------- | -------- | 1544| Promise<string> | Promise used to return the result. If the operation succeeds, the distributed table name of the remote device is returned.| 1545 1546**Example** 1547```js 1548let promise = rdbStore.obtainDistributedTableName("12345678abcde", "EMPLOYEE") 1549promise.then((tableName) => { 1550 console.info('Obtained DistributedTableName successfully, tableName=' + tableName) 1551}).catch((err) => { 1552 console.info('Failed to obtain DistributedTableName, err: ' + err) 1553}) 1554``` 1555 1556### sync<sup>8+</sup> 1557 1558sync(mode: SyncMode, predicates: RdbPredicates, callback: AsyncCallback<Array<[string, number]>>): void 1559 1560Synchronizes data between devices. This API uses an asynchronous callback to return the result. 1561 1562**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1563 1564**Parameters** 1565| Name| Type| Mandatory| Description| 1566| -------- | -------- | -------- | -------- | 1567| mode | [SyncMode](#syncmode8) | Yes| Data synchronization mode. The value can be **push** or **pull**.| 1568| predicates | [RdbPredicates](#rdbpredicates) | Yes| **RdbPredicates** object that specifies the data and devices to synchronize.| 1569| callback | AsyncCallback<Array<[string, number]>> | 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. | 1570 1571**Example** 1572```js 1573let predicates = new data_rdb.RdbPredicates('EMPLOYEE') 1574predicates.inDevices(['12345678abcde']) 1575rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicates, function (err, result) { 1576 if (err) { 1577 console.log('sync failed, err: ' + err) 1578 return 1579 } 1580 console.log('sync done.') 1581 for (let i = 0; i < result.length; i++) { 1582 console.log('device=' + result[i][0] + ' status=' + result[i][1]) 1583 } 1584}) 1585``` 1586 1587 1588### sync<sup>8+</sup> 1589 1590 sync(mode: SyncMode, predicates: RdbPredicates): Promise<Array<[string, number]>> 1591 1592Synchronizes data between devices. This API uses a promise to return the result. 1593 1594**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1595 1596**Parameters** 1597| Name| Type| Mandatory| Description| 1598| -------- | -------- | -------- | -------- | 1599| mode | [SyncMode](#syncmode8) | Yes| Data synchronization mode. The value can be **push** or **pull**.| 1600| predicates | [RdbPredicates](#rdbpredicates) | Yes| **RdbPredicates** object that specifies the data and devices to synchronize.| 1601 1602**Return value** 1603 1604| Type| Description| 1605| -------- | -------- | 1606| Promise<Array<[string, number]>> | 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. | 1607 1608**Example** 1609```js 1610let predicates = new data_rdb.RdbPredicates('EMPLOYEE') 1611predicates.inDevices(['12345678abcde']) 1612let promise = rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicates) 1613promise.then((result) =>{ 1614 console.log('sync done.') 1615 for (let i = 0; i < result.length; i++) { 1616 console.log('device=' + result[i][0] + ' status=' + result[i][1]) 1617 } 1618}).catch((err) => { 1619 console.log('sync failed') 1620}) 1621``` 1622 1623### on('dataChange')<sup>8+</sup> 1624 1625on(event: 'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void 1626 1627Registers an observer for this RDB store. When the data in the RDB store changes, a callback is invoked to return the data changes. 1628 1629**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1630 1631**Parameters** 1632 1633| Name| Type| Mandatory| Description| 1634| -------- | -------- | -------- | -------- | 1635| event | string | Yes| The value is'dataChange', which indicates a data change event.| 1636| type | [SubscribeType](#subscribetype8) | Yes| Type defined in **SubscribeType**.| 1637| observer | Callback<Array<string>> | Yes| Observer that listens for the data changes in the RDB store.| 1638 1639**Example** 1640```js 1641function storeObserver(devices) { 1642 for (let i = 0; i < devices.length; i++) { 1643 console.log('device=' + devices[i] + ' data changed') 1644 } 1645} 1646try { 1647 rdbStore.on('dataChange', data_rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver) 1648} catch (err) { 1649 console.log('Failed to register observer') 1650} 1651``` 1652 1653### off('dataChange')<sup>8+</sup> 1654 1655off(event:'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void 1656 1657Unregisters the specified observer of the RDB store. This API uses a callback to return the result. 1658 1659**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1660 1661**Parameters** 1662 1663| Name| Type| Mandatory| Description| 1664| -------- | -------- | -------- | -------- | 1665| event | string | Yes| The value is'dataChange', which indicates a data change event.| 1666| type | [SubscribeType](#subscribetype8) | Yes| Type defined in **SubscribeType**.| 1667| observer | Callback<Array<string>> | Yes| Data change observer registered.| 1668 1669**Example** 1670```js 1671function storeObserver(devices) { 1672 for (let i = 0; i < devices.length; i++) { 1673 console.log('device=' + devices[i] + ' data changed') 1674 } 1675} 1676try { 1677 rdbStore.off('dataChange', data_rdb.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver) 1678} catch (err) { 1679 console.log('Failed to unregister observer') 1680} 1681``` 1682 1683## StoreConfig 1684 1685Manages the configuration of an RDB store. 1686 1687**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1688 1689| Name| Type| Mandatory| Description| 1690| -------- | -------- | -------- | -------- | 1691| name | string | Yes| Database file name.| 1692 1693 1694## ValueType 1695 1696Defines the data types allowed. 1697 1698**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1699 1700| Type| Description| 1701| -------- | -------- | 1702| number | Number.| 1703| string | String.| 1704| boolean | Boolean.| 1705 1706 1707## ValuesBucket 1708 1709Defines the types of the key and value in a KV pair. 1710 1711**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1712 1713| Key Type| Value Type| 1714| -------- | -------- | 1715| string | [ValueType](#valuetype)\| Uint8Array \| null | 1716 1717 1718## SyncMode<sup>8+</sup> 1719 1720Defines the database synchronization mode. 1721 1722**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1723 1724| Name | Default Value| Description| 1725| -------- | ----- |----- | 1726| SYNC_MODE_PUSH | 0 | Data is pushed from a local device to a remote device.| 1727| SYNC_MODE_PULL | 1 | Data is pulled from a remote device to a local device.| 1728 1729## SubscribeType<sup>8+</sup> 1730 1731Defines the subscription type. 1732 1733**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 1734 1735| Name | Default Value| Description| 1736| -------- | ----- |---- | 1737| SUBSCRIBE_TYPE_REMOTE | 0 | Subscribe to remote data changes.| 1738