1# DataAbilityHelper 2 3The **DataAbilityHelper** object is obtained through [acquireDataAbilityHelper](js-apis-ability-featureAbility.md#featureabilityacquiredataabilityhelper7). 4 5> **NOTE** 6> 7> 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. 8> The APIs of this module can be used only in the FA model. 9 10## Modules to Import 11 12```ts 13import ability from '@ohos.ability.ability'; 14``` 15 16## Usage 17 18Import the following modules based on the actual situation before using the current module: 19```ts 20import ohos_data_ability from '@ohos.data.dataAbility'; 21import ohos_data_rdb from '@ohos.data.rdb'; 22``` 23 24## DataAbilityHelper.openFile 25 26openFile(uri: string, mode: string, callback: AsyncCallback\<number>): void 27 28Opens a file with a specified URI. This API uses an asynchronous callback to return the result. 29 30**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 31 32**Parameters** 33 34| Name | Type | Mandatory| Description | 35| -------- | ---------------------- | ---- | ---------------------------------- | 36| uri | string | Yes | URI of the file to open. | 37| mode | string | Yes | Mode for opening the file. The value can be **rwt**. | 38| callback | AsyncCallback\<number> | Yes | Callback used to return the file descriptor.| 39 40**Example** 41 42```ts 43import featureAbility from '@ohos.ability.featureAbility'; 44let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 45 'dataability:///com.example.DataAbility' 46); 47let mode = 'rwt'; 48DAHelper.openFile('dataability:///com.example.DataAbility', mode, (err) => { 49 console.info('==========================>Called=======================>'); 50}); 51``` 52 53## DataAbilityHelper.openFile 54 55openFile(uri: string, mode: string): Promise\<number> 56 57Opens a file with a specified URI. This API uses a promise to return the result. 58 59**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 60 61**Parameters** 62 63| Name| Type | Mandatory| Description | 64| ---- | ------ | ---- | ------------------------ | 65| uri | string | Yes | URI of the file to open.| 66| mode | string | Yes | Mode for opening the file. The value can be **rwt**. | 67 68**Return value** 69 70| Type | Description | 71| ---------------- | ---------------- | 72| Promise\<number> | Promise used to return the file descriptor.| 73 74**Example** 75 76```ts 77import featureAbility from '@ohos.ability.featureAbility'; 78let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 79 'dataability:///com.example.DataAbility' 80); 81let mode = 'rwt'; 82DAHelper.openFile('dataability:///com.example.DataAbility', mode).then((data) => { 83 console.info('==========================>openFileCallback=======================>'); 84}); 85``` 86 87## DataAbilityHelper.on 88 89on(type: 'dataChange', uri: string, callback: AsyncCallback\<void>): void 90 91Registers an observer to listen for changes in the data specified by a given URI. 92 93**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 94 95**Parameters** 96 97| Name | Type | Mandatory| Description | 98| -------- | -------------------- | ---- | ------------------------ | 99| type | string | Yes | Event type. The value is **dataChange**. | 100| uri | string | Yes | URI of the data.| 101| callback | AsyncCallback\<void> | Yes | Callback invoked when the data is changed. | 102 103**Example** 104 105```ts 106import featureAbility from '@ohos.ability.featureAbility'; 107let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 108 'dataability:///com.example.DataAbility' 109); 110function onChangeNotify() { 111 console.info('==========================>onChangeNotify=======================>'); 112}; 113DAHelper.on( 114 'dataChange', 115 'dataability:///com.example.DataAbility', 116 onChangeNotify 117); 118``` 119 120## DataAbilityHelper.off 121 122off(type: 'dataChange', uri: string, callback?: AsyncCallback\<void>): void 123 124Deregisters the observer that listens for changes in the data specified by a given URI. 125 126**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 127 128**Parameters** 129 130| Name | Type | Mandatory| Description | 131| -------- | -------------------- | ---- | ------------------------ | 132| type | string | Yes | Event type. The value is **dataChange**. | 133| uri | string | Yes | URI of the data.| 134| callback | AsyncCallback\<void> | No | Callback used to return the result. | 135 136**Example** 137 138```ts 139import featureAbility from '@ohos.ability.featureAbility'; 140let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 141 'dataability:///com.example.DataAbility' 142); 143function onChangeNotify() { 144 console.info('==========================>onChangeNotify=======================>'); 145}; 146DAHelper.off( 147 'dataChange', 148 'dataability:///com.example.DataAbility', 149); 150DAHelper.off( 151 'dataChange', 152 'dataability:///com.example.DataAbility', 153 onChangeNotify 154); 155``` 156 157## DataAbilityHelper.getType 158 159getType(uri: string, callback: AsyncCallback\<string>): void 160 161Obtains the MIME type of the data specified by a given URI. This API uses an asynchronous callback to return the result. 162 163**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 164 165**Parameters** 166 167| Name | Type | Mandatory| Description | 168| -------- | ---------------------- | ---- | --------------------------------------------- | 169| uri | string | Yes | URI of the data. | 170| callback | AsyncCallback\<string> | Yes | Callback used to return the MIME type.| 171 172**Example** 173 174```ts 175import featureAbility from '@ohos.ability.featureAbility'; 176let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 177 'dataability:///com.example.DataAbility' 178); 179DAHelper.getType('dataability:///com.example.DataAbility', (err, data) => { 180 console.info('==========================>Called=======================>'); 181}); 182``` 183 184## DataAbilityHelper.getType 185 186getType(uri: string): Promise\<string> 187 188Obtains the MIME type of the data specified by a given URI. This API uses a promise to return the result. 189 190**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 191 192**Parameters** 193 194| Name| Type | Mandatory| Description | 195| ---- | ------ | ---- | ------------------------ | 196| uri | string | Yes | URI of the data.| 197 198**Return value** 199 200| Type | Description | 201| ---------------- | ----------------------------------- | 202| Promise\<string> | Promise used to return the MIME type.| 203 204**Example** 205 206```ts 207import featureAbility from '@ohos.ability.featureAbility'; 208let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 209 'dataability:///com.example.DataAbility' 210); 211DAHelper.getType('dataability:///com.example.DataAbility').then((data) => { 212 console.info('==========================>getTypeCallback=======================>'); 213}); 214``` 215 216## DataAbilityHelper.getFileTypes 217 218getFileTypes(uri: string, mimeTypeFilter: string, callback: AsyncCallback<Array\<string>>): void 219 220Obtains the supported MIME types of a specified file. This API uses an asynchronous callback to return the result. 221 222**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 223 224**Parameters** 225 226| Name | Type | Mandatory| Description | 227| -------------- | ------------------------------ | ---- | ---------------------------------- | 228| uri | string | Yes | URI of the file. | 229| mimeTypeFilter | string | Yes | MIME types to match. | 230| callback | AsyncCallback\<Array\<string>> | Yes | Callback used to return the supported MIME types.| 231 232**Example** 233 234```ts 235import featureAbility from '@ohos.ability.featureAbility'; 236let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 237 'dataability:///com.example.DataAbility' 238); 239DAHelper.getFileTypes( 'dataability:///com.example.DataAbility', 240 'image/*', (err, data) => { 241 console.info('==========================>Called=======================>'); 242}); 243``` 244 245 246 247## DataAbilityHelper.getFileTypes 248 249getFileTypes(uri: string, mimeTypeFilter: string): Promise\<Array\<string>> 250 251Obtains the supported MIME types of a specified file. This API uses a promise to return the result. 252 253**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 254 255**Parameters** 256 257| Name | Type | Mandatory| Description | 258| -------------- | ------ | ---- | ---------------------------- | 259| uri | string | Yes | URI of the file. | 260| mimeTypeFilter | string | Yes | MIME types to match.| 261 262**Return value** 263 264| Type | Description | 265| ------------------------ | ------------------------ | 266| Promise\<Array\<string>> | Promise used to return the supported MIME types.| 267 268**Example** 269 270```ts 271import featureAbility from '@ohos.ability.featureAbility'; 272let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 273 'dataability:///com.example.DataAbility' 274); 275DAHelper.getFileTypes('dataability:///com.example.DataAbility', 276 'image/*').then((data) => { 277 console.info('===================>getFileTypesCallback================>'); 278}); 279``` 280 281## DataAbilityHelper.normalizeUri 282 283normalizeUri(uri: string, callback: AsyncCallback\<string>): void 284 285Converts the URI that refers to a DataAbility into a normalized URI. This API uses an asynchronous callback to return the result. 286 287**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 288 289**Parameters** 290 291| Name | Type | Mandatory| Description | 292| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 293| uri | string | Yes | URI object to normalize. | 294| callback | AsyncCallback\<string> | Yes | Callback used to return the normalized URI object if the DataAbility supports URI normalization. If the DataAbility does not support URI normalization, **null** is returned.| 295 296**Example** 297 298```ts 299import featureAbility from '@ohos.ability.featureAbility'; 300let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 301 'dataability:///com.example.DataAbility' 302); 303DAHelper.normalizeUri('dataability:///com.example.DataAbility', (err, data) => { 304 console.info('==========================>Called=======================>'); 305}); 306``` 307 308## DataAbilityHelper.normalizeUri 309 310normalizeUri(uri: string): Promise\<string> 311 312Converts the URI that refers to a DataAbility into a normalized URI. This API uses a promise to return the result. 313 314**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 315 316**Parameters** 317 318| Name| Type | Mandatory| Description | 319| ---- | ------ | ---- | ----------------------- | 320| uri | string | Yes | URI object to normalize.| 321 322**Return value** 323 324| Type | Description | 325| ---------------- | ------------------------------------------------------ | 326| Promise\<string> | Promise used to return the normalized URI object if the DataAbility supports URI normalization. If the DataAbility does not support URI normalization, **null** is returned.| 327 328**Example** 329 330```ts 331import featureAbility from '@ohos.ability.featureAbility'; 332let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 333 'dataability:///com.example.DataAbility' 334); 335DAHelper.normalizeUri('dataability:///com.example.DataAbility',).then((data) => { 336 console.info('=================>normalizeUriCallback=======================>'); 337}); 338``` 339 340## DataAbilityHelper.denormalizeUri 341 342denormalizeUri(uri: string, callback: AsyncCallback\<string>): void 343 344Converts a normalized URI generated by **DataAbilityHelper.normalizeUri(uri: string, callback: AsyncCallback\<string>)** to a denormalized one. This API uses an asynchronous callback to return the result. 345 346**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 347 348**Parameters** 349 350| Name | Type | Mandatory| Description | 351| -------- | ---------------------- | ---- | --------------------------------------------------- | 352| uri | string | Yes | URI object to normalize. | 353| callback | AsyncCallback\<string> | Yes | Callback used to return the denormalized URI object.| 354 355**Example** 356 357```ts 358import featureAbility from '@ohos.ability.featureAbility'; 359let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 360 'dataability:///com.example.DataAbility' 361); 362DAHelper.denormalizeUri('dataability:///com.example.DataAbility', (err, data) => { 363 console.info('==========================>Called=======================>'); 364}); 365``` 366 367 368 369## DataAbilityHelper.denormalizeUri 370 371denormalizeUri(uri: string): Promise\<string> 372 373Converts a normalized URI generated by **DataAbilityHelper.normalizeUri(uri: string)** to a denormalized one. This API uses a promise to return the result. 374 375**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 376 377**Parameters** 378 379| Name| Type | Mandatory| Description | 380| ---- | ------ | ---- | ----------------------- | 381| uri | string | Yes | URI object to normalize.| 382 383**Return value** 384 385| Type | Description | 386| ---------------- | ----------------------------------------- | 387| Promise\<string> | Promise used to return the denormalized URI object.| 388 389**Example** 390 391```ts 392import featureAbility from '@ohos.ability.featureAbility'; 393let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 394 'dataability:///com.example.DataAbility' 395); 396DAHelper.denormalizeUri('dataability:///com.example.DataAbility',).then((data) => { 397 console.info('===============>denormalizeUriCallback=======================>'); 398}); 399``` 400 401## DataAbilityHelper.notifyChange 402 403notifyChange(uri: string, callback: AsyncCallback\<void>): void 404 405Notifies the registered observer of a change to the data specified by the URI. This API uses an asynchronous callback to return the result. 406 407**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 408 409**Parameters** 410 411| Name | Type | Mandatory| Description | 412| -------- | -------------------- | ---- | ------------------------ | 413| uri | string | Yes | URI of the data.| 414| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 415 416**Example** 417 418```ts 419import featureAbility from '@ohos.ability.featureAbility'; 420let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 421 'dataability:///com.example.DataAbility' 422); 423DAHelper.notifyChange('dataability:///com.example.DataAbility', (err) => { 424 console.info('==========================>Called=======================>'); 425}); 426``` 427 428## DataAbilityHelper.notifyChange 429 430notifyChange(uri: string): Promise\<void> 431 432Notifies the registered observer of a change to the data specified by the URI. This API uses a promise to return the result. 433 434**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 435 436**Parameters** 437 438| Name| Type | Mandatory| Description | 439| ---- | ------ | ---- | ------------------------ | 440| uri | string | Yes | URI of the data.| 441 442**Return value** 443 444| Type | Description | 445| -------------- | --------------------- | 446| Promise\<void> | Promise used to return the result.| 447 448**Example** 449 450```ts 451import featureAbility from '@ohos.ability.featureAbility'; 452let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 453 'dataability:///com.example.DataAbility' 454); 455DAHelper.notifyChange('dataability:///com.example.DataAbility').then(() => { 456 console.info('================>notifyChangeCallback================>'); 457}); 458``` 459 460## DataAbilityHelper.insert 461 462insert(uri: string, valuesBucket: rdb.ValuesBucket, callback: AsyncCallback\<number>): void 463 464Inserts a single data record into the database. This API uses an asynchronous callback to return the result. 465 466**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 467 468**Parameters** 469 470| Name | Type | Mandatory| Description | 471| ------------ | ---------------------- | ---- | ------------------------------------------------------ | 472| uri | string | Yes | URI of the data to insert. | 473| valuesBucket | rdb.ValuesBucket | Yes | Data record to insert. If this parameter is **null**, a blank row will be inserted.| 474| callback | AsyncCallback\<number> | Yes | Callback used to return the index of the inserted data record. | 475 476**Example** 477 478```ts 479import featureAbility from '@ohos.ability.featureAbility'; 480let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 481 'dataability:///com.example.DataAbility' 482); 483const valueBucket = { 484 'name': 'rose', 485 'age': 22, 486 'salary': 200.5, 487 'blobType': 'u8', 488}; 489DAHelper.insert('dataability:///com.example.DataAbility', valueBucket, 490 (err, data) => { 491 console.info('==========================>Called=======================>'); 492}); 493``` 494 495## DataAbilityHelper.insert 496 497insert(uri: string, valuesBucket: rdb.ValuesBucket): Promise\<number> 498 499Inserts a single data record into the database. This API uses a promise to return the result. 500 501**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 502 503**Parameters** 504 505| Name | Type | Mandatory| Description | 506| ------------ | ---------------- | ---- | ------------------------------------------------------ | 507| uri | string | Yes | URI of the data to insert. | 508| valuesBucket | rdb.ValuesBucket | Yes | Data record to insert. If this parameter is **null**, a blank row will be inserted.| 509 510**Return value** 511 512| Type | Description | 513| ---------------- | ------------------------ | 514| Promise\<number> | Promise used to return the index of the inserted data record.| 515 516**Example** 517 518```ts 519import featureAbility from '@ohos.ability.featureAbility'; 520let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 521 'dataability:///com.example.DataAbility' 522); 523const valueBucket = { 524 'name': 'rose1', 525 'age': 221, 526 'salary': 20.5, 527 'blobType': 'u8', 528}; 529DAHelper.insert('dataability:///com.example.DataAbility', valueBucket).then((data) => { 530 console.info('====================>insertCallback=======================>'); 531}); 532``` 533 534## DataAbilityHelper.batchInsert 535 536batchInsert(uri: string, valuesBuckets: Array\<rdb.ValuesBucket>, callback: AsyncCallback\<number>): void 537 538Inserts multiple data records into the database. This API uses an asynchronous callback to return the result. 539 540**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 541 542**Parameters** 543 544| Name | Type | Mandatory| Description | 545| ------------ | ----------------------- | ---- | -------------------------------- | 546| uri | string | Yes | URI of the data to insert. | 547| valuesBucket | Array\<rdb.ValuesBucket> | Yes | Data record to insert. | 548| callback | AsyncCallback\<number> | Yes | Callback used to return the number of inserted data records.| 549 550**Example** 551 552```ts 553import featureAbility from '@ohos.ability.featureAbility'; 554let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 555 'dataability:///com.example.DataAbility' 556); 557let cars = new Array({'name': 'roe11', 'age': 21, 'salary': 20.5, 'blobType': 'u8',}, 558 {'name': 'roe12', 'age': 21, 'salary': 20.5, 'blobType': 'u8',}, 559 {'name': 'roe13', 'age': 21, 'salary': 20.5, 'blobType': 'u8',}); 560DAHelper.batchInsert('dataability:///com.example.DataAbility', cars, 561 (err, data) => { 562 console.info('==========================>Called=======================>'); 563}); 564``` 565 566## DataAbilityHelper.batchInsert 567 568batchInsert(uri: string, valuesBuckets: Array<rdb.ValuesBucket>): Promise\<number> 569 570Inserts multiple data records into the database. This API uses a promise to return the result. 571 572**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 573 574**Parameters** 575 576| Name | Type | Mandatory| Description | 577| ------------ | ----------------------- | ---- | ------------------------ | 578| uri | string | Yes | URI of the data to insert.| 579| valuesBucket | Array<rdb.ValuesBucket> | Yes | Data record to insert. | 580 581**Return value** 582 583| Type | Description | 584| ---------------- | ---------------------- | 585| Promise\<number> | Promise used to return the number of inserted data records.| 586 587**Example** 588 589```ts 590import featureAbility from '@ohos.ability.featureAbility'; 591let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 592 'dataability:///com.example.DataAbility' 593); 594let cars = new Array({'name': 'roe11', 'age': 21, 'salary': 20.5, 'blobType': 'u8',}, 595 {'name': 'roe12', 'age': 21, 'salary': 20.5, 'blobType': 'u8',}, 596 {'name': 'roe13', 'age': 21, 'salary': 20.5, 'blobType': 'u8',}); 597DAHelper.batchInsert('dataability:///com.example.DataAbility', cars).then((data) => { 598 console.info('==================>batchInsertCallback=======================>'); 599}); 600``` 601 602## DataAbilityHelper.delete 603 604delete(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<number>): void 605 606Deletes one or more data records from the database. This API uses an asynchronous callback to return the result. 607 608**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 609 610**Parameters** 611 612| Name | Type | Mandatory| Description | 613| ------------ | --------------------------------- | ---- | ------------------------------------------------ | 614| uri | string | Yes | URI of the data to delete. | 615| predicates | dataAbility.DataAbilityPredicates | Yes | Filter criteria. You should define the processing logic when this parameter is **null**.| 616| callback | AsyncCallback\<number> | Yes | Callback used to return the number of deleted data records. | 617 618**Example** 619 620```ts 621import featureAbility from '@ohos.ability.featureAbility'; 622import ohos_data_ability from '@ohos.data.dataAbility'; 623let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 624 'dataability:///com.example.DataAbility' 625); 626let da = new ohos_data_ability.DataAbilityPredicates(); 627DAHelper.delete('dataability:///com.example.DataAbility', da, 628 (err, data) => { 629 console.info('==========================>Called=======================>'); 630}); 631``` 632 633## DataAbilityHelper.delete 634 635delete(uri: string, predicates?: dataAbility.DataAbilityPredicates): Promise\<number>; 636 637Deletes one or more data records from the database. This API uses a promise to return the result. 638 639**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 640 641**Parameters** 642 643| Name | Type | Mandatory| Description | 644| ------------ | --------------------------------- | ---- | ------------------------------------------------ | 645| uri | string | Yes | URI of the data to delete. | 646| predicates | dataAbility.DataAbilityPredicates | No | Filter criteria. You should define the processing logic when this parameter is **null**.| 647 648**Return value** 649 650| Type | Description | 651| ---------------- | ------------------------ | 652| Promise\<number> | Promise used to return the number of deleted data records.| 653 654**Example** 655 656```ts 657import featureAbility from '@ohos.ability.featureAbility'; 658import ohos_data_ability from '@ohos.data.dataAbility'; 659let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 660 'dataability:///com.example.DataAbility' 661); 662let da = new ohos_data_ability.DataAbilityPredicates(); 663DAHelper.delete('dataability:///com.example.DataAbility', da).then((data) => { 664 console.info('==========================>deleteCallback=======================>'); 665}); 666``` 667 668## DataAbilityHelper.update 669 670update(uri: string, valuesBucket: rdb.ValuesBucket, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<number>): void 671 672Updates data records in the database. This API uses an asynchronous callback to return the result. 673 674**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 675 676**Parameters** 677 678| Name | Type | Mandatory| Description | 679| ------------ | --------------------------------- | ---- | ------------------------------------------------ | 680| uri | string | Yes | URI of the data to update. | 681| valuesBucket | rdb.ValuesBucket | Yes | New values. | 682| predicates | dataAbility.DataAbilityPredicates | Yes | Filter criteria. You should define the processing logic when this parameter is **null**.| 683| callback | AsyncCallback\<number> | Yes | Callback used to return the number of updated data records. | 684 685**Example** 686 687```ts 688import featureAbility from '@ohos.ability.featureAbility'; 689import ohos_data_ability from '@ohos.data.dataAbility'; 690let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 691 'dataability:///com.example.DataAbility' 692); 693const va = { 694 'name': 'roe1', 695 'age': 21, 696 'salary': 20.5, 697 'blobType': 'u8', 698}; 699let da = new ohos_data_ability.DataAbilityPredicates(); 700DAHelper.update('dataability:///com.example.DataAbility', va, da, (err, data) => { 701 console.info('==========================>Called=======================>'); 702}); 703``` 704 705## DataAbilityHelper.update 706 707update(uri: string, valuesBucket: rdb.ValuesBucket, predicates?: dataAbility.DataAbilityPredicates): Promise\<number>; 708 709Updates data records in the database. This API uses a promise to return the result. 710 711**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 712 713**Parameters** 714 715| Name | Type | Mandatory| Description | 716| ------------ | --------------------------------- | ---- | ------------------------------------------------ | 717| uri | string | Yes | URI of the data to update. | 718| valuesBucket | rdb.ValuesBucket | Yes | New values. | 719| predicates | dataAbility.DataAbilityPredicates | No | Filter criteria. You should define the processing logic when this parameter is **null**.| 720 721**Return value** 722 723| Type | Description | 724| ---------------- | -------------------------------------------- | 725| Promise\<number> | Promise used to return the number of updated data records. | 726 727**Example** 728 729```ts 730import featureAbility from '@ohos.ability.featureAbility'; 731import ohos_data_ability from '@ohos.data.dataAbility'; 732let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 733 'dataability:///com.example.DataAbility' 734); 735const va = { 736 'name': 'roe1', 737 'age': 21, 738 'salary': 20.5, 739 'blobType': 'u8', 740}; 741let da = new ohos_data_ability.DataAbilityPredicates(); 742DAHelper.update('dataability:///com.example.DataAbility', va, da).then((data) => { 743 console.info('==========================>updateCallback=======================>'); 744}); 745``` 746 747## DataAbilityHelper.query 748 749query(uri: string, columns: Array\<string>, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<ResultSet>): void 750 751Queries data in the database. This API uses an asynchronous callback to return the result. 752 753**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 754 755**Parameters** 756 757| Name | Type | Mandatory| Description | 758| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 759| uri | string | Yes | URI of the data to query. | 760| columns | Array\<string> | Yes | Columns to query. If this parameter is **null**, all columns will be queried. | 761| predicates | dataAbility.DataAbilityPredicates | Yes | Filter criteria. You should define the processing logic when this parameter is **null**.| 762| callback | AsyncCallback\<ResultSet> | Yes | Callback used to return the data queried. | 763 764**Example** 765 766```ts 767import featureAbility from '@ohos.ability.featureAbility'; 768import ohos_data_ability from '@ohos.data.dataAbility'; 769let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 770 'dataability:///com.example.DataAbility' 771); 772let cars=new Array('value1', 'value2', 'value3', 'value4'); 773let da = new ohos_data_ability.DataAbilityPredicates(); 774DAHelper.query('dataability:///com.example.DataAbility', cars, da, (err, data) => { 775 console.info('==========================>Called=======================>'); 776}); 777``` 778 779 780 781## DataAbilityHelper.query 782 783query(uri: string, columns?: Array\<string>, predicates?: dataAbility.DataAbilityPredicates): Promise\<ResultSet>; 784 785Queries data in the database. This API uses a promise to return the result. 786 787**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 788 789**Parameters** 790 791| Name | Type | Mandatory| Description | 792| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 793| uri | string | Yes | URI of the data to query. | 794| columns | Array\<string> | No | Columns to query. If this parameter is **null**, all columns will be queried. | 795| predicates | dataAbility.DataAbilityPredicates | No | Filter criteria. You should define the processing logic when this parameter is **null**.| 796 797**Return value** 798 799| Type | Description | 800| ------------------- | -------------- | 801| Promise\<ResultSet> | Promise used to return the data queried.| 802 803**Example** 804 805```ts 806import featureAbility from '@ohos.ability.featureAbility'; 807import ohos_data_ability from '@ohos.data.dataAbility'; 808let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 809 'dataability:///com.example.DataAbility' 810); 811let cars = new Array('value1', 'value2', 'value3', 'value4'); 812let da = new ohos_data_ability.DataAbilityPredicates(); 813DAHelper.query('dataability:///com.example.DataAbility', cars, da).then((data) => { 814 console.info('query data: ${JSON.stringify(data)}'); 815}); 816``` 817 818## DataAbilityHelper.call 819 820call(uri: string, method: string, arg: string, extras: PacMap, callback: AsyncCallback\<PacMap>): void 821 822Calls an extended API of the DataAbility. This API uses an asynchronous callback to return the result. 823 824**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 825 826**Parameters** 827 828| Name | Type | Mandatory| Description | 829| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 830| uri | string | Yes | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'. | 831| method | string | Yes | Name of the API to call. | 832| arg | string | Yes | Parameter to pass in. | 833| extras | [PacMap](#pacmap) | Yes | Key-value pair parameter. | 834| callback | AsyncCallback\<[PacMap](#pacmap)> | Yes| Callback used to return the result. | 835 836**Example** 837 838```ts 839import featureAbility from '@ohos.ability.featureAbility'; 840 841let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 842 'dataability:///com.example.jsapidemo.UserDataAbility' 843); 844dataAbilityHelper.call('dataability:///com.example.jsapidemo.UserDataAbility', 845 'method', 'arg', {'key1':'value1'}, (error, data) => { 846 if (error && error.code !== 0) { 847 console.error('call fail, error: ${JSON.stringify(error)}'); 848 } else { 849 console.log('call success, data: ${JSON.stringify(data)}'); 850 } 851}); 852``` 853 854## DataAbilityHelper.call 855 856call(uri: string, method: string, arg: string, extras: PacMap): Promise\<PacMap> 857 858Calls an extended API of the DataAbility. This API uses a promise to return the result. 859 860**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 861 862**Parameters** 863 864| Name | Type | Mandatory| Description | 865| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 866| uri | string | Yes | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'. | 867| method | string | Yes | Name of the API to call. | 868| arg | string | Yes |Parameter to pass in. | 869| extras | [PacMap](#pacmap) | Yes | Key-value pair parameter. | 870 871**Return value** 872 873| Type| Description| 874|------ | ------- | 875|Promise\<[PacMap](#pacmap)> | Promise used to return the result.| 876 877**Example** 878 879```ts 880import featureAbility from '@ohos.ability.featureAbility'; 881 882let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 883 'dataability:///com.example.jsapidemo.UserDataAbility' 884); 885dataAbilityHelper.call('dataability:///com.example.jsapidemo.UserDataAbility', 886 'method', 'arg', {'key1':'value1'}).then((data) => { 887 console.info('Operation succeeded: ' + data); 888}).catch((error) => { 889 console.error('Operation failed. Cause: ' + error); 890}); 891``` 892 893## DataAbilityHelper.call 894 895call(uri: string, method: string, arg: string, extras: PacMap, callback: AsyncCallback\<PacMap>): void 896 897Calls an extended API of the DataAbility. This API uses an asynchronous callback to return the result. 898 899**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 900 901**Parameters** 902 903| Name | Type | Mandatory| Description | 904| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 905| uri | string | Yes | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'. | 906| method | string | Yes | Name of the API to call. | 907| arg | string | Yes |Parameter to pass in. | 908| extras | [PacMap](#pacmap) | Yes | Key-value pair parameter. | 909| callback | AsyncCallback\<[PacMap](#pacmap)> | Yes| Callback used to return the result. | 910 911**Example** 912 913```ts 914import featureAbility from '@ohos.ability.featureAbility'; 915 916let dataAbilityHelper = featureAbility.acquireDataAbilityHelper( 917 'dataability:///com.example.jsapidemo.UserDataAbility'); 918dataAbilityHelper.call('dataability:///com.example.jsapidemo.UserDataAbility', 919 'method', 'arg', {'key1':'value1'}, (err, data) => { 920 if (err) { 921 console.error('Operation failed. Cause: ' + err); 922 return; 923 } 924 console.info('Operation succeeded: ' + data); 925}); 926``` 927 928## DataAbilityHelper.executeBatch 929 930executeBatch(uri: string, operations: Array\<DataAbilityOperation>, callback: AsyncCallback\<Array\<DataAbilityResult>>): void; 931 932Operates data in the database in batches. This API uses an asynchronous callback to return the result. 933 934**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 935 936**Parameters** 937 938| Name | Type | Mandatory| Description | 939| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 940| uri | string | Yes | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'.| 941| operations | Array\<[DataAbilityOperation](js-apis-inner-ability-dataAbilityOperation.md)> | Yes | An array holding the data operations on the database. | 942| callback | AsyncCallback\<Array\<[DataAbilityResult](js-apis-inner-ability-dataAbilityResult.md)>> | Yes | Callback used to return the result of each operation in the **DataAbilityResult** array. | 943 944**Example** 945 946```ts 947import featureAbility from '@ohos.ability.featureAbility'; 948 949// Select the operations to be performed on the database according to the DataAbilityOperation array. 950let op=new Array(); 951let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 952 'dataability:///com.example.jsapidemo.UserDataAbility' 953); 954dataAbilityHelper.executeBatch('dataability:///com.example.jsapidemo.UserDataAbility', op, (error, data) => { 955 if (error && error.code !== 0) { 956 console.error('executeBatch fail, error: ${JSON.stringify(error)}'); 957 } else { 958 console.log('executeBatch success, data: ${JSON.stringify(data)}'); 959 } 960 console.info('Operation succeeded: ' + data); 961}); 962``` 963 964## DataAbilityHelper.executeBatch 965 966executeBatch(uri: string, operations: Array\<DataAbilityOperation>): Promise\<Array\<DataAbilityResult>>; 967 968Operates data in the database in batches. This API uses a promise to return the result. 969 970**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 971 972**Parameters** 973 974| Name | Type | Mandatory| Description | 975| ---------- | -------------------------------| ---- | ------------------------------------------------ | 976| uri | string | Yes | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'.| 977| operations | Array\<[DataAbilityOperation](js-apis-inner-ability-dataAbilityOperation.md)> | Yes | An array holding the data operations on the database. | 978 979**Return value** 980 981| Type| Description| 982|------ | ------- | 983|Promise\<Array\<[DataAbilityResult](js-apis-inner-ability-dataAbilityResult.md)>> | Promise used to return the result of each operation in the **DataAbilityResult** array.| 984 985**Example** 986 987```ts 988import featureAbility from '@ohos.ability.featureAbility'; 989 990// Select the operations to be performed on the database according to the DataAbilityOperation array. 991let op=new Array(); 992let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 993 'dataability:///com.example.jsapidemo.UserDataAbility' 994); 995dataAbilityHelper.executeBatch('dataability:///com.example.jsapidemo.UserDataAbility', op).then((data) => { 996 console.info('executeBatch success, data: ${data}'); 997}).catch((error) => { 998 console.error('Operation failed. Cause: ' + error); 999}); 1000 1001``` 1002 1003## PacMap 1004 1005[key: string]: number | string | boolean | Array\<string | number | boolean> | null; 1006 1007**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 1008 1009| Name| Type| Mandatory| Description| 1010| ------ | ------ | ------ | ------ | 1011| [key: string] | number \| string \| boolean \| Array\<string \| number \| boolean\> \| null | Yes| Data stored in key-value pairs.| 1012