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