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