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