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