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 relationalStore from '@ohos.data.relationalStore' 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 a file descriptor. 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 **r** indicates read-only access, **w** indicates **write-only** access, and **rw** indicates read-write access. | 38| callback | AsyncCallback\<number> | Yes | Callback used to return the file descriptor.| 39 40**Example** 41 42```ts 43import ability from '@ohos.ability.ability'; 44import featureAbility from '@ohos.ability.featureAbility'; 45 46let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 47 'dataability:///com.example.DataAbility' 48); 49let mode = 'rw'; 50DAHelper.openFile('dataability:///com.example.DataAbility', mode, (error, data) => { 51 if (error && error.code !== 0) { 52 console.error(`openFile fail, error: ${JSON.stringify(error)}`); 53 } else { 54 console.log(`openFile success, data: ${JSON.stringify(data)}`); 55 } 56}); 57``` 58 59## DataAbilityHelper.openFile 60 61openFile(uri: string, mode: string): Promise\<number> 62 63Opens a file with a specified URI. This API uses a promise to return a file descriptor. 64 65**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 66 67**Parameters** 68 69| Name| Type | Mandatory| Description | 70| ---- | ------ | ---- | ------------------------ | 71| uri | string | Yes | URI of the file to open.| 72| mode | string | Yes | Mode for opening the file. The value **r** indicates read-only access, **w** indicates **write-only** access, and **rw** indicates read-write access. | 73 74**Return value** 75 76| Type | Description | 77| ---------------- | ---------------- | 78| Promise\<number> | Promise used to return the file descriptor.| 79 80**Example** 81 82```ts 83import ability from '@ohos.ability.ability'; 84import featureAbility from '@ohos.ability.featureAbility'; 85 86let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 87 'dataability:///com.example.DataAbility' 88); 89let mode = 'rw'; 90DAHelper.openFile('dataability:///com.example.DataAbility', mode).then((data) => { 91 console.info(`openFile data: ${JSON.stringify(data)}`); 92}); 93``` 94 95## DataAbilityHelper.on 96 97on(type: 'dataChange', uri: string, callback: AsyncCallback\<void>): void 98 99Registers an observer to listen for changes in the data specified by a given URI. 100 101**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 102 103**Parameters** 104 105| Name | Type | Mandatory| Description | 106| -------- | -------------------- | ---- | ------------------------ | 107| type | string | Yes | The value **'dataChange'** means data changes. | 108| uri | string | Yes | URI of the data.| 109| callback | AsyncCallback\<void> | Yes | Callback invoked when the data is changed. | 110 111**Example** 112 113```ts 114import ability from '@ohos.ability.ability'; 115import featureAbility from '@ohos.ability.featureAbility'; 116 117let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 118 'dataability:///com.example.DataAbility' 119); 120function onChangeNotify() { 121 console.info('onChangeNotify call back'); 122}; 123DAHelper.on( 124 'dataChange', 125 'dataability:///com.example.DataAbility', 126 onChangeNotify 127); 128``` 129 130## DataAbilityHelper.off 131 132off(type: 'dataChange', uri: string, callback?: AsyncCallback\<void>): void 133 134Deregisters the observer that listens for changes in the data specified by a given URI. 135 136**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 137 138**Parameters** 139 140| Name | Type | Mandatory| Description | 141| -------- | -------------------- | ---- | ------------------------ | 142| type | string | Yes | The value **'dataChange'** means data changes. | 143| uri | string | Yes | URI of the data.| 144| callback | AsyncCallback\<void> | No | Callback of the listener to deregister. If the callback is set to **null**, all data change listeners are canceled. | 145 146**Example** 147 148```ts 149import ability from '@ohos.ability.ability'; 150import featureAbility from '@ohos.ability.featureAbility'; 151 152let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 153 'dataability:///com.example.DataAbility' 154); 155function onChangeNotify() { 156 console.info('onChangeNotify call back'); 157}; 158DAHelper.off( 159 'dataChange', 160 'dataability:///com.example.DataAbility', 161 onChangeNotify 162); 163DAHelper.off( 164 'dataChange', 165 'dataability:///com.example.DataAbility', 166); 167``` 168 169## DataAbilityHelper.getType 170 171getType(uri: string, callback: AsyncCallback\<string>): void 172 173Obtains the media resource type of the data specified by a given URI. This API uses an asynchronous callback to return the result. 174 175**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 176 177**Parameters** 178 179| Name | Type | Mandatory| Description | 180| -------- | ---------------------- | ---- | --------------------------------------------- | 181| uri | string | Yes | URI of the data. | 182| callback | AsyncCallback\<string> | Yes | Callback used to return the media resource type.| 183 184**Example** 185 186```ts 187import ability from '@ohos.ability.ability'; 188import featureAbility from '@ohos.ability.featureAbility'; 189 190let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 191 'dataability:///com.example.DataAbility' 192); 193DAHelper.getType('dataability:///com.example.DataAbility', (error, data) => { 194 if (error && error.code !== 0) { 195 console.error(`getType fail, error: ${JSON.stringify(error)}`); 196 } else { 197 console.log(`getType success, data: ${JSON.stringify(data)}`); 198 } 199}); 200``` 201 202## DataAbilityHelper.getType 203 204getType(uri: string): Promise\<string> 205 206Obtains the media resource type of the data specified by a given URI. This API uses a promise to return the result. 207 208**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 209 210**Parameters** 211 212| Name| Type | Mandatory| Description | 213| ---- | ------ | ---- | ------------------------ | 214| uri | string | Yes | URI of the data.| 215 216**Return value** 217 218| Type | Description | 219| ---------------- | ----------------------------------- | 220| Promise\<string> | Promise used to return the media resource type.| 221 222**Example** 223 224```ts 225import ability from '@ohos.ability.ability'; 226import featureAbility from '@ohos.ability.featureAbility'; 227 228let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 229 'dataability:///com.example.DataAbility' 230); 231DAHelper.getType('dataability:///com.example.DataAbility').then((data) => { 232 console.info(`getType data: ${JSON.stringify(data)}`); 233}); 234``` 235 236## DataAbilityHelper.getFileTypes 237 238getFileTypes(uri: string, mimeTypeFilter: string, callback: AsyncCallback<Array\<string>>): void 239 240Obtains the supported media resource types of a specified file. This API uses an asynchronous callback to return the result. 241 242**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 243 244**Parameters** 245 246| Name | Type | Mandatory| Description | 247| -------------- | ------------------------------ | ---- | ---------------------------------- | 248| uri | string | Yes | URI of the file. | 249| mimeTypeFilter | string | Yes | Media resource type of the file to obtain. | 250| callback | AsyncCallback\<Array\<string>> | Yes | Callback used to return an array holding the media resource type.| 251 252**Example** 253 254```ts 255import ability from '@ohos.ability.ability'; 256import featureAbility from '@ohos.ability.featureAbility'; 257 258let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 259 'dataability:///com.example.DataAbility' 260); 261DAHelper.getFileTypes( 'dataability:///com.example.DataAbility', 'image/*', (error, data) => { 262 if (error && error.code !== 0) { 263 console.error(`getFileTypes fail, error: ${JSON.stringify(error)}`); 264 } else { 265 console.log(`getFileTypes success, data: ${JSON.stringify(data)}`); 266 } 267}); 268``` 269 270## DataAbilityHelper.getFileTypes 271 272getFileTypes(uri: string, mimeTypeFilter: string): Promise\<Array\<string>> 273 274Obtains the supported media resource types of a specified file. This API uses a promise to return the result. 275 276**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 277 278**Parameters** 279 280| Name | Type | Mandatory| Description | 281| -------------- | ------ | ---- | ---------------------------- | 282| uri | string | Yes | URI of the file. | 283| mimeTypeFilter | string | Yes | Media resource type of the file to obtain.| 284 285**Return value** 286 287| Type | Description | 288| ------------------------ | ------------------------ | 289| Promise\<Array\<string>> | Promise used to return an array holding the media resource type.| 290 291**Example** 292 293```ts 294import ability from '@ohos.ability.ability'; 295import featureAbility from '@ohos.ability.featureAbility'; 296 297let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 298 'dataability:///com.example.DataAbility' 299); 300DAHelper.getFileTypes('dataability:///com.example.DataAbility', 'image/*').then((data) => { 301 console.info(`getFileTypes data: ${JSON.stringify(data)}`); 302}); 303``` 304 305## DataAbilityHelper.normalizeUri 306 307normalizeUri(uri: string, callback: AsyncCallback\<string>): void 308 309Converts the URI that refers to a Data ability into a normalized URI. This API uses an asynchronous callback to return the result. 310 311**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 312 313**Parameters** 314 315| Name | Type | Mandatory| Description | 316| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 317| uri | string | Yes | URI object to normalize. | 318| callback | AsyncCallback\<string> | Yes | Callback used to return the normalized URI object if the Data ability supports URI normalization. If the Data ability does not support URI normalization, **null** is returned.| 319 320**Example** 321 322```ts 323import ability from '@ohos.ability.ability'; 324import featureAbility from '@ohos.ability.featureAbility'; 325 326let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 327 'dataability:///com.example.DataAbility' 328); 329DAHelper.normalizeUri('dataability:///com.example.DataAbility', (error, data) => { 330 if (error && error.code !== 0) { 331 console.error(`normalizeUri fail, error: ${JSON.stringify(error)}`); 332 } else { 333 console.log(`normalizeUri success, data: ${JSON.stringify(data)}`); 334 } 335}); 336``` 337 338## DataAbilityHelper.normalizeUri 339 340normalizeUri(uri: string): Promise\<string> 341 342Converts the URI that refers to a Data ability into a normalized URI. This API uses a promise to return the result. 343 344**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 345 346**Parameters** 347 348| Name| Type | Mandatory| Description | 349| ---- | ------ | ---- | ----------------------- | 350| uri | string | Yes | URI object to normalize.| 351 352**Return value** 353 354| Type | Description | 355| ---------------- | ------------------------------------------------------ | 356| Promise\<string> | Promise used to return the normalized URI object if the Data ability supports URI normalization. If the Data ability does not support URI normalization, **null** is returned.| 357 358**Example** 359 360```ts 361import ability from '@ohos.ability.ability'; 362import featureAbility from '@ohos.ability.featureAbility'; 363 364let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 365 'dataability:///com.example.DataAbility' 366); 367DAHelper.normalizeUri('dataability:///com.example.DataAbility',).then((data) => { 368 console.info(`normalizeUri data: ${JSON.stringify(data)}`); 369}); 370``` 371 372## DataAbilityHelper.denormalizeUri 373 374denormalizeUri(uri: string, callback: AsyncCallback\<string>): void 375 376Converts 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. 377 378**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 379 380**Parameters** 381 382| Name | Type | Mandatory| Description | 383| -------- | ---------------------- | ---- | --------------------------------------------------- | 384| uri | string | Yes | URI object to denormalize. | 385| callback | AsyncCallback\<string> | Yes | Callback used to return the denormalized URI object.| 386 387**Example** 388 389```ts 390import ability from '@ohos.ability.ability'; 391import featureAbility from '@ohos.ability.featureAbility'; 392 393let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 394 'dataability:///com.example.DataAbility' 395); 396DAHelper.denormalizeUri('dataability:///com.example.DataAbility', (error, data) => { 397 if (error && error.code !== 0) { 398 console.error(`denormalizeUri fail, error: ${JSON.stringify(error)}`); 399 } else { 400 console.log(`denormalizeUri success, data: ${JSON.stringify(data)}`); 401 } 402}); 403``` 404 405## DataAbilityHelper.denormalizeUri 406 407denormalizeUri(uri: string): Promise\<string> 408 409Converts a normalized URI generated by **DataAbilityHelper.normalizeUri(uri: string)** to a denormalized one. This API uses a promise to return the result. 410 411**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 412 413**Parameters** 414 415| Name| Type | Mandatory| Description | 416| ---- | ------ | ---- | ----------------------- | 417| uri | string | Yes | URI object to denormalize.| 418 419**Return value** 420 421| Type | Description | 422| ---------------- | ----------------------------------------- | 423| Promise\<string> | Promise used to return the denormalized URI object.| 424 425**Example** 426 427```ts 428import ability from '@ohos.ability.ability'; 429import featureAbility from '@ohos.ability.featureAbility'; 430 431let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 432 'dataability:///com.example.DataAbility' 433); 434DAHelper.denormalizeUri('dataability:///com.example.DataAbility',).then((data) => { 435 console.info(`denormalizeUri data: ${JSON.stringify(data)}`); 436}); 437``` 438 439## DataAbilityHelper.notifyChange 440 441notifyChange(uri: string, callback: AsyncCallback\<void>): void 442 443Notifies the registered observer of a change to the data specified by the URI. This API uses an asynchronous callback to return the result. 444 445**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 446 447**Parameters** 448 449| Name | Type | Mandatory| Description | 450| -------- | -------------------- | ---- | ------------------------ | 451| uri | string | Yes | URI of the data that changes.| 452| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 453 454**Example** 455 456```ts 457import ability from '@ohos.ability.ability'; 458import featureAbility from '@ohos.ability.featureAbility'; 459 460let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 461 'dataability:///com.example.DataAbility' 462); 463DAHelper.notifyChange('dataability:///com.example.DataAbility', (error) => { 464 if (error && error.code !== 0) { 465 console.error(`notifyChange fail, error: ${JSON.stringify(error)}`); 466 } else { 467 console.log('notifyChange success'); 468 } 469}); 470``` 471 472## DataAbilityHelper.notifyChange 473 474notifyChange(uri: string): Promise\<void> 475 476Notifies the registered observer of a change to the data specified by the URI. This API uses a promise to return the result. 477 478**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 479 480**Parameters** 481 482| Name| Type | Mandatory| Description | 483| ---- | ------ | ---- | ------------------------ | 484| uri | string | Yes | URI of the data that changes.| 485 486**Return value** 487 488| Type | Description | 489| -------------- | --------------------- | 490| Promise\<void> | Promise used to return the result.| 491 492**Example** 493 494```ts 495import ability from '@ohos.ability.ability'; 496import featureAbility from '@ohos.ability.featureAbility'; 497 498let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 499 'dataability:///com.example.DataAbility' 500); 501DAHelper.notifyChange('dataability:///com.example.DataAbility').then(() => { 502 console.info('================>notifyChangeCallback================>'); 503}); 504``` 505 506## DataAbilityHelper.insert 507 508insert(uri: string, valuesBucket: rdb.ValuesBucket, callback: AsyncCallback\<number>): void 509 510Inserts a single data record into the database. This API uses an asynchronous callback to return the result. 511 512**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 513 514**Parameters** 515 516| Name | Type | Mandatory| Description | 517| ------------ | ---------------------- | ---- | ------------------------------------------------------ | 518| uri | string | Yes | URI of the data to insert. | 519| valuesBucket | rdb.ValuesBucket | Yes | Data record to insert. If this parameter is **null**, a blank row will be inserted.| 520| callback | AsyncCallback\<number> | Yes | Callback used to return the index of the inserted data record. | 521 522**Example** 523 524```ts 525import ability from '@ohos.ability.ability'; 526import featureAbility from '@ohos.ability.featureAbility'; 527import rdb from '@ohos.data.rdb'; 528 529let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 530 'dataability:///com.example.DataAbility' 531); 532const valueBucket: rdb.ValuesBucket = { 533 'name': 'rose', 534 'age': 22, 535 'salary': 200.5, 536 'blobType': 'u8', 537}; 538DAHelper.insert('dataability:///com.example.DataAbility', valueBucket, (error, data) => { 539 if (error && error.code !== 0) { 540 console.error(`insert fail, error: ${JSON.stringify(error)}`); 541 } else { 542 console.log(`insert success, data: ${JSON.stringify(data)}`); 543 } 544}); 545``` 546 547## DataAbilityHelper.insert 548 549insert(uri: string, valuesBucket: rdb.ValuesBucket): Promise\<number> 550 551Inserts a single data record into the database. This API uses a promise to return the result. 552 553**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 554 555**Parameters** 556 557| Name | Type | Mandatory| Description | 558| ------------ | ---------------- | ---- | ------------------------------------------------------ | 559| uri | string | Yes | URI of the data to insert. | 560| valuesBucket | rdb.ValuesBucket | Yes | Data record to insert. If this parameter is **null**, a blank row will be inserted.| 561 562**Return value** 563 564| Type | Description | 565| ---------------- | ------------------------ | 566| Promise\<number> | Promise used to return the index of the inserted data record.| 567 568**Example** 569 570```ts 571import ability from '@ohos.ability.ability'; 572import featureAbility from '@ohos.ability.featureAbility'; 573import rdb from '@ohos.data.rdb'; 574 575let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 576 'dataability:///com.example.DataAbility' 577); 578const valueBucket: rdb.ValuesBucket = { 579 'name': 'rose1', 580 'age': 221, 581 'salary': 20.5, 582 'blobType': 'u8', 583}; 584DAHelper.insert('dataability:///com.example.DataAbility', valueBucket).then((data) => { 585 console.info(`insert data: ${JSON.stringify(data)}`); 586}); 587``` 588 589## DataAbilityHelper.batchInsert 590 591batchInsert(uri: string, valuesBuckets: Array\<rdb.ValuesBucket>, callback: AsyncCallback\<number>): void 592 593Inserts multiple data records into the database. This API uses an asynchronous callback to return the result. 594 595**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 596 597**Parameters** 598 599| Name | Type | Mandatory| Description | 600| ------------ | ----------------------- | ---- | -------------------------------- | 601| uri | string | Yes | URI of the data to insert. | 602| valuesBucket | Array\<rdb.ValuesBucket> | Yes | Data records to insert. | 603| callback | AsyncCallback\<number> | Yes | Callback used to return the number of inserted data records.| 604 605**Example** 606 607```ts 608import ability from '@ohos.ability.ability'; 609import featureAbility from '@ohos.ability.featureAbility'; 610import rdb from '@ohos.data.rdb'; 611 612let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 613 'dataability:///com.example.DataAbility' 614); 615let cars = new Array({'name': 'roe11', 'age': 21, 'salary': 20.5, 'blobType': 'u8',} as rdb.ValuesBucket, 616 {'name': 'roe12', 'age': 21, 'salary': 20.5, 'blobType': 'u8',} as rdb.ValuesBucket, 617 {'name': 'roe13', 'age': 21, 'salary': 20.5, 'blobType': 'u8',} as rdb.ValuesBucket); 618DAHelper.batchInsert('dataability:///com.example.DataAbility', cars, (error, data) => { 619 if (error && error.code !== 0) { 620 console.error(`batchInsert fail, error: ${JSON.stringify(error)}`); 621 } else { 622 console.log(`batchInsert success, data: ${JSON.stringify(data)}`); 623 } 624}); 625``` 626 627## DataAbilityHelper.batchInsert 628 629batchInsert(uri: string, valuesBuckets: Array<rdb.ValuesBucket>): Promise\<number> 630 631Inserts multiple data records into the database. This API uses a promise to return the result. 632 633**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 634 635**Parameters** 636 637| Name | Type | Mandatory| Description | 638| ------------ | ----------------------- | ---- | ------------------------ | 639| uri | string | Yes | URI of the data to insert.| 640| valuesBucket | Array<rdb.ValuesBucket> | Yes | Data records to insert. | 641 642**Return value** 643 644| Type | Description | 645| ---------------- | ---------------------- | 646| Promise\<number> | Promise used to return the number of inserted data records.| 647 648**Example** 649 650```ts 651import ability from '@ohos.ability.ability'; 652import featureAbility from '@ohos.ability.featureAbility'; 653import rdb from '@ohos.data.rdb'; 654 655let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 656 'dataability:///com.example.DataAbility' 657); 658let cars = new Array({'name': 'roe11', 'age': 21, 'salary': 20.5, 'blobType': 'u8',} as rdb.ValuesBucket, 659 {'name': 'roe12', 'age': 21, 'salary': 20.5, 'blobType': 'u8',} as rdb.ValuesBucket, 660 {'name': 'roe13', 'age': 21, 'salary': 20.5, 'blobType': 'u8',} as rdb.ValuesBucket); 661DAHelper.batchInsert('dataability:///com.example.DataAbility', cars).then((data) => { 662 console.info(`batchInsert data: ${JSON.stringify(data)}`); 663}); 664``` 665 666## DataAbilityHelper.delete 667 668delete(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<number>): void 669 670Deletes one or more data records from the database. This API uses an asynchronous callback to return the result. 671 672**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 673 674**Parameters** 675 676| Name | Type | Mandatory| Description | 677| ------------ | --------------------------------- | ---- | ------------------------------------------------ | 678| uri | string | Yes | URI of the data to delete. | 679| predicates | dataAbility.DataAbilityPredicates | Yes | Filter criteria. You should define the processing logic when this parameter is **null**.| 680| callback | AsyncCallback\<number> | Yes | Callback used to return the number of deleted data records. | 681 682**Example** 683 684```ts 685import ability from '@ohos.ability.ability'; 686import featureAbility from '@ohos.ability.featureAbility'; 687import ohos_data_ability from '@ohos.data.dataAbility'; 688 689let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 690 'dataability:///com.example.DataAbility' 691); 692let da = new ohos_data_ability.DataAbilityPredicates(); 693DAHelper.delete('dataability:///com.example.DataAbility', da, (error, data) => { 694 if (error && error.code !== 0) { 695 console.error(`delete fail, error: ${JSON.stringify(error)}`); 696 } else { 697 console.log(`delete success, data: ${JSON.stringify(data)}`); 698 } 699}); 700``` 701 702## DataAbilityHelper.delete 703 704delete(uri: string, predicates?: dataAbility.DataAbilityPredicates): Promise\<number>; 705 706Deletes one or more data records from the database. This API uses a promise to return the result. 707 708**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 709 710**Parameters** 711 712| Name | Type | Mandatory| Description | 713| ------------ | --------------------------------- | ---- | ------------------------------------------------ | 714| uri | string | Yes | URI of the data to delete. | 715| predicates | dataAbility.DataAbilityPredicates | No | Filter criteria. You should define the processing logic when this parameter is **null**.| 716 717**Return value** 718 719| Type | Description | 720| ---------------- | ------------------------ | 721| Promise\<number> | Promise used to return the number of deleted data records.| 722 723**Example** 724 725```ts 726import ability from '@ohos.ability.ability'; 727import featureAbility from '@ohos.ability.featureAbility'; 728import ohos_data_ability from '@ohos.data.dataAbility'; 729 730let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 731 'dataability:///com.example.DataAbility' 732); 733let da = new ohos_data_ability.DataAbilityPredicates(); 734DAHelper.delete('dataability:///com.example.DataAbility', da).then((data) => { 735 console.info(`delete data: ${JSON.stringify(data)}`); 736}); 737``` 738 739## DataAbilityHelper.delete 740 741delete(uri: string, callback: AsyncCallback\<number>): void 742 743Uses a custom processing logic to delete data records from the database. This API uses an asynchronous callback to return the result. 744 745**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 746 747**Parameters** 748 749| Name | Type | Mandatory| Description | 750| ------------ | --------------------------------- | ---- | ------------------------------------------------ | 751| uri | string | Yes | URI of the data to delete. | 752| callback | AsyncCallback\<number> | Yes | Callback used to return the number of deleted data records. | 753 754**Example** 755 756```ts 757import ability from '@ohos.ability.ability'; 758import featureAbility from '@ohos.ability.featureAbility'; 759import ohos_data_ability from '@ohos.data.dataAbility'; 760 761let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 762 'dataability:///com.example.DataAbility' 763); 764DAHelper.delete('dataability:///com.example.DataAbility', (error, data) => { 765 if (error && error.code !== 0) { 766 console.error(`delete fail, error: ${JSON.stringify(error)}`); 767 } else { 768 console.log(`delete success, data: ${JSON.stringify(data)}`); 769 } 770}); 771``` 772 773## DataAbilityHelper.update 774 775update(uri: string, valuesBucket: rdb.ValuesBucket, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<number>): void 776 777Updates data records in the database. This API uses an asynchronous callback to return the result. 778 779**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 780 781**Parameters** 782 783| Name | Type | Mandatory| Description | 784| ------------ | --------------------------------- | ---- | ------------------------------------------------ | 785| uri | string | Yes | URI of the data to update. | 786| valuesBucket | rdb.ValuesBucket | Yes | New values. | 787| predicates | dataAbility.DataAbilityPredicates | Yes | Filter criteria. You should define the processing logic when this parameter is **null**.| 788| callback | AsyncCallback\<number> | Yes | Callback used to return the number of updated data records. | 789 790**Example** 791 792```ts 793import ability from '@ohos.ability.ability'; 794import featureAbility from '@ohos.ability.featureAbility'; 795import ohos_data_ability from '@ohos.data.dataAbility'; 796import rdb from '@ohos.data.rdb'; 797 798let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 799 'dataability:///com.example.DataAbility' 800); 801const va: rdb.ValuesBucket = { 802 'name': 'roe1', 803 'age': 21, 804 'salary': 20.5, 805 'blobType': 'u8', 806}; 807let da = new ohos_data_ability.DataAbilityPredicates(); 808DAHelper.update('dataability:///com.example.DataAbility', va, da, (error, data) => { 809 if (error && error.code !== 0) { 810 console.error(`update fail, error: ${JSON.stringify(error)}`); 811 } else { 812 console.log(`update success, data: ${JSON.stringify(data)}`); 813 } 814}); 815``` 816 817## DataAbilityHelper.update 818 819update(uri: string, valuesBucket: rdb.ValuesBucket, predicates?: dataAbility.DataAbilityPredicates): Promise\<number>; 820 821Updates data records in the database. This API uses a promise to return the result. 822 823**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 824 825**Parameters** 826 827| Name | Type | Mandatory| Description | 828| ------------ | --------------------------------- | ---- | ------------------------------------------------ | 829| uri | string | Yes | URI of the data to update. | 830| valuesBucket | rdb.ValuesBucket | Yes | New values. | 831| predicates | dataAbility.DataAbilityPredicates | No | Filter criteria. You should define the processing logic when this parameter is **null**.| 832 833**Return value** 834 835| Type | Description | 836| ---------------- | -------------------------------------------- | 837| Promise\<number> | Promise used to return the number of updated data records. | 838 839**Example** 840 841```ts 842import ability from '@ohos.ability.ability'; 843import featureAbility from '@ohos.ability.featureAbility'; 844import ohos_data_ability from '@ohos.data.dataAbility'; 845import rdb from '@ohos.data.rdb'; 846 847let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 848 'dataability:///com.example.DataAbility' 849); 850const va: rdb.ValuesBucket = { 851 'name': 'roe1', 852 'age': 21, 853 'salary': 20.5, 854 'blobType': 'u8', 855}; 856let da = new ohos_data_ability.DataAbilityPredicates(); 857DAHelper.update('dataability:///com.example.DataAbility', va, da).then((data) => { 858 console.info(`update data: ${JSON.stringify(data)}`); 859}); 860``` 861 862## DataAbilityHelper.update 863 864update(uri: string, valuesBucket: rdb.ValuesBucket, callback: AsyncCallback\<number>): void 865 866Uses a custom processing logic to update data records in the database. This API uses an asynchronous callback to return the result. 867 868**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 869 870**Parameters** 871 872| Name | Type | Mandatory| Description | 873| ------------ | --------------------------------- | ---- | ------------------------------------------------ | 874| uri | string | Yes | URI of the data to update. | 875| valuesBucket | rdb.ValuesBucket | Yes | New values. | 876| callback | AsyncCallback\<number> | Yes | Callback used to return the number of updated data records. | 877 878**Example** 879 880```ts 881import ability from '@ohos.ability.ability'; 882import featureAbility from '@ohos.ability.featureAbility'; 883import ohos_data_ability from '@ohos.data.dataAbility'; 884import rdb from '@ohos.data.rdb'; 885 886let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 887 'dataability:///com.example.DataAbility' 888); 889const va: rdb.ValuesBucket = { 890 'name': 'roe1', 891 'age': 21, 892 'salary': 20.5, 893 'blobType': 'u8', 894}; 895DAHelper.update('dataability:///com.example.DataAbility', va, (error, data) => { 896 if (error && error.code !== 0) { 897 console.error(`update fail, error: ${JSON.stringify(error)}`); 898 } else { 899 console.log(`update success, data: ${JSON.stringify(data)}`); 900 } 901}); 902``` 903 904## DataAbilityHelper.query 905 906query(uri: string, columns: Array\<string>, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<ResultSet>): void 907 908Queries data in the database. This API uses an asynchronous callback to return the result. 909 910**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 911 912**Parameters** 913 914| Name | Type | Mandatory| Description | 915| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 916| uri | string | Yes | URI of the data to query. | 917| columns | Array\<string> | Yes | Columns to query. If this parameter is **null**, all columns will be queried. | 918| predicates | dataAbility.DataAbilityPredicates | Yes | Filter criteria. You should define the processing logic when this parameter is **null**.| 919| callback | AsyncCallback\<ResultSet> | Yes | Callback used to return the data queried. | 920 921**Example** 922 923```ts 924import ability from '@ohos.ability.ability'; 925import featureAbility from '@ohos.ability.featureAbility'; 926import ohos_data_ability from '@ohos.data.dataAbility'; 927 928let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 929 'dataability:///com.example.DataAbility' 930); 931let cars=new Array('value1', 'value2', 'value3', 'value4'); 932let da = new ohos_data_ability.DataAbilityPredicates(); 933DAHelper.query('dataability:///com.example.DataAbility', cars, da, (error, data) => { 934 if (error && error.code !== 0) { 935 console.error(`query fail, error: ${JSON.stringify(error)}`); 936 } else { 937 console.log(`query success, data: ${JSON.stringify(data)}`); 938 } 939}); 940``` 941 942## DataAbilityHelper.query 943 944query(uri: string, callback: AsyncCallback\<ResultSet>): void 945 946Uses a custom processing logic to query data records in the database. This API uses an asynchronous callback to return the result. 947 948**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 949 950**Parameters** 951 952| Name | Type | Mandatory| Description | 953| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 954| uri | string | Yes | URI of the data to query. | 955| callback | AsyncCallback\<ResultSet> | Yes | Callback used to return the data queried. | 956 957**Example** 958 959```ts 960import ability from '@ohos.ability.ability'; 961import featureAbility from '@ohos.ability.featureAbility'; 962import ohos_data_ability from '@ohos.data.dataAbility'; 963 964let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 965 'dataability:///com.example.DataAbility' 966); 967DAHelper.query('dataability:///com.example.DataAbility', (error, data) => { 968 if (error && error.code !== 0) { 969 console.error(`query fail, error: ${JSON.stringify(error)}`); 970 } else { 971 console.log(`query success, data: ${JSON.stringify(data)}`); 972 } 973}); 974``` 975 976## DataAbilityHelper.query 977 978query(uri: string, columns: Array\<string>, callback: AsyncCallback\<ResultSet>): void 979 980Uses a custom processing logic to query data records in the database. This API uses an asynchronous callback to return the result. 981 982**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 983 984**Parameters** 985 986| Name | Type | Mandatory| Description | 987| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 988| uri | string | Yes | URI of the data to query. | 989| columns | Array\<string> | Yes | Columns to query. If this parameter is **null**, all columns will be queried. | 990| callback | AsyncCallback\<ResultSet> | Yes | Callback used to return the data queried. | 991 992**Example** 993 994```ts 995import ability from '@ohos.ability.ability'; 996import featureAbility from '@ohos.ability.featureAbility'; 997import ohos_data_ability from '@ohos.data.dataAbility'; 998 999let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 1000 'dataability:///com.example.DataAbility' 1001); 1002let cars = new Array('value1', 'value2', 'value3', 'value4'); 1003DAHelper.query('dataability:///com.example.DataAbility', cars, (error, data) => { 1004 if (error && error.code !== 0) { 1005 console.error(`query fail, error: ${JSON.stringify(error)}`); 1006 } else { 1007 console.log(`query success, data: ${JSON.stringify(data)}`); 1008 } 1009}); 1010``` 1011 1012## DataAbilityHelper.query 1013 1014query(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<ResultSet>): void 1015 1016Uses a custom processing logic to query data records in the database. This API uses an asynchronous callback to return the result. 1017 1018**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 1019 1020**Parameters** 1021 1022| Name | Type | Mandatory| Description | 1023| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 1024| uri | string | Yes | URI of the data to query. | 1025| predicates | dataAbility.DataAbilityPredicates | Yes | Filter criteria. You should define the processing logic when this parameter is **null**.| 1026| callback | AsyncCallback\<ResultSet> | Yes | Callback used to return the data queried. | 1027 1028**Example** 1029 1030```ts 1031import ability from '@ohos.ability.ability'; 1032import featureAbility from '@ohos.ability.featureAbility'; 1033import ohos_data_ability from '@ohos.data.dataAbility'; 1034 1035let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 1036 'dataability:///com.example.DataAbility' 1037); 1038let da = new ohos_data_ability.DataAbilityPredicates(); 1039DAHelper.query('dataability:///com.example.DataAbility', da, (error, data) => { 1040 if (error && error.code !== 0) { 1041 console.error(`query fail, error: ${JSON.stringify(error)}`); 1042 } else { 1043 console.log(`query success, data: ${JSON.stringify(data)}`); 1044 } 1045}); 1046``` 1047 1048## DataAbilityHelper.query 1049 1050query(uri: string, columns?: Array\<string>, predicates?: dataAbility.DataAbilityPredicates): Promise\<ResultSet>; 1051 1052Queries data in the database. This API uses a promise to return the result. 1053 1054**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 1055 1056**Parameters** 1057 1058| Name | Type | Mandatory| Description | 1059| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 1060| uri | string | Yes | URI of the data to query. | 1061| columns | Array\<string> | No | Columns to query. If this parameter is **null**, all columns will be queried. | 1062| predicates | dataAbility.DataAbilityPredicates | No | Filter criteria. You should define the processing logic when this parameter is **null**.| 1063 1064**Return value** 1065 1066| Type | Description | 1067| ------------------- | -------------- | 1068| Promise\<ResultSet> | Promise used to return the data queried.| 1069 1070**Example** 1071 1072```ts 1073import ability from '@ohos.ability.ability'; 1074import featureAbility from '@ohos.ability.featureAbility'; 1075import ohos_data_ability from '@ohos.data.dataAbility'; 1076 1077let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 1078 'dataability:///com.example.DataAbility' 1079); 1080let cars = new Array('value1', 'value2', 'value3', 'value4'); 1081let da = new ohos_data_ability.DataAbilityPredicates(); 1082DAHelper.query('dataability:///com.example.DataAbility', cars, da).then((data) => { 1083 console.info(`query data: ${JSON.stringify(data)}`); 1084}); 1085``` 1086 1087## DataAbilityHelper.call 1088 1089call(uri: string, method: string, arg: string, extras: PacMap, callback: AsyncCallback\<PacMap>): void 1090 1091Calls an extended API of the DataAbility. This API uses an asynchronous callback to return the result. 1092 1093**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 1094 1095**Parameters** 1096 1097| Name | Type | Mandatory| Description | 1098| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 1099| uri | string | Yes | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'. | 1100| method | string | Yes | Name of the API to call. | 1101| arg | string | Yes | Parameter to pass in. | 1102| extras | [PacMap](js-apis-inner-ability-dataAbilityHelper.md#pacmap) | Yes | Key-value pair parameter. | 1103| callback | AsyncCallback\<[PacMap](js-apis-inner-ability-dataAbilityHelper.md#pacmap)> | Yes| Callback used to return the result. | 1104 1105**Example** 1106 1107```ts 1108import ability from '@ohos.ability.ability'; 1109import featureAbility from '@ohos.ability.featureAbility'; 1110 1111let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 1112 'dataability:///com.example.jsapidemo.UserDataAbility' 1113); 1114dataAbilityHelper.call('dataability:///com.example.jsapidemo.UserDataAbility', 1115 'method', 'arg', {'key1':'value1'}, (error, data) => { 1116 if (error && error.code !== 0) { 1117 console.error(`call fail, error: ${JSON.stringify(error)}`); 1118 } else { 1119 console.log(`call success, data: ${JSON.stringify(data)}`); 1120 } 1121}); 1122``` 1123 1124## DataAbilityHelper.call 1125 1126call(uri: string, method: string, arg: string, extras: PacMap): Promise\<PacMap> 1127 1128Calls an extended API of the DataAbility. This API uses a promise to return the result. 1129 1130**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 1131 1132**Parameters** 1133 1134| Name | Type | Mandatory| Description | 1135| ---------- | --------------------------------- | ---- | ------------------------------------------------ | 1136| uri | string | Yes | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'. | 1137| method | string | Yes | Name of the API to call. | 1138| arg | string | Yes | Parameter to pass in. | 1139| extras | [PacMap](js-apis-inner-ability-dataAbilityHelper.md#pacmap) | Yes | Key-value pair parameter. | 1140 1141**Return value** 1142 1143| Type| Description| 1144|------ | ------- | 1145|Promise\<[PacMap](js-apis-inner-ability-dataAbilityHelper.md#pacmap)> | Promise used to return the result.| 1146 1147**Example** 1148 1149```ts 1150import ability from '@ohos.ability.ability'; 1151import featureAbility from '@ohos.ability.featureAbility'; 1152import { BusinessError } from '@ohos.base'; 1153 1154let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 1155 'dataability:///com.example.jsapidemo.UserDataAbility' 1156); 1157dataAbilityHelper.call('dataability:///com.example.jsapidemo.UserDataAbility', 1158 'method', 'arg', {'key1':'value1'}).then((data) => { 1159 console.info('call success, data: ${data}'); 1160}).catch((error: BusinessError) => { 1161 console.error('call failed, error: ${error}'); 1162}); 1163``` 1164 1165## DataAbilityHelper.executeBatch 1166 1167executeBatch(uri: string, operations: Array\<DataAbilityOperation>, callback: AsyncCallback\<Array\<DataAbilityResult>>): void; 1168 1169Operates data in the database in batches. This API uses an asynchronous callback to return the result. 1170 1171**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 1172 1173**Parameters** 1174 1175| Name | Type | Mandatory| Description | 1176| ----------| ---------------------------------| ---- | ------------------------------------------------ | 1177| uri | string | Yes | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'.| 1178| operations | Array\<[DataAbilityOperation](js-apis-inner-ability-dataAbilityOperation.md)> | Yes | An array holding the data operations on the database. | 1179| callback | AsyncCallback\<Array\<[DataAbilityResult](js-apis-inner-ability-dataAbilityResult.md)>> | Yes | Callback used to return the result of each operation in the **DataAbilityResult** array. | 1180 1181**Example** 1182 1183```ts 1184import ability from '@ohos.ability.ability'; 1185import featureAbility from '@ohos.ability.featureAbility'; 1186 1187// Select the operations to be performed on the database according to the DataAbilityOperation array. 1188let op: Array<ability.DataAbilityOperation> = new Array(); 1189let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 1190 'dataability:///com.example.jsapidemo.UserDataAbility' 1191); 1192dataAbilityHelper.executeBatch('dataability:///com.example.jsapidemo.UserDataAbility', op, (error, data) => { 1193 if (error && error.code !== 0) { 1194 console.error(`executeBatch fail, error: ${JSON.stringify(error)}`); 1195 } else { 1196 console.log(`executeBatch success, data: ${JSON.stringify(data)}`); 1197 } 1198}); 1199``` 1200 1201## DataAbilityHelper.executeBatch 1202 1203executeBatch(uri: string, operations: Array\<DataAbilityOperation>): Promise\<Array\<DataAbilityResult>>; 1204 1205Operates data in the database in batches. This API uses a promise to return the result. 1206 1207**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 1208 1209**Parameters** 1210 1211| Name | Type | Mandatory| Description | 1212| ---------- | -------------------------------| ---- | ------------------------------------------------ | 1213| uri | string | Yes | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'.| 1214| operations | Array\<[DataAbilityOperation](js-apis-inner-ability-dataAbilityOperation.md)> | Yes | An array holding the data operations on the database. | 1215 1216**Return value** 1217 1218| Type| Description| 1219|------ | ------- | 1220|Promise\<Array\<[DataAbilityResult](js-apis-inner-ability-dataAbilityResult.md)>> | Promise used to return the result of each operation in the **DataAbilityResult** array.| 1221 1222**Example** 1223 1224```ts 1225import ability from '@ohos.ability.ability'; 1226import featureAbility from '@ohos.ability.featureAbility'; 1227import { BusinessError } from '@ohos.base'; 1228 1229// Select the operations to be performed on the database according to the DataAbilityOperation array. 1230let op: Array<ability.DataAbilityOperation> = new Array(); 1231let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper( 1232 'dataability:///com.example.jsapidemo.UserDataAbility' 1233); 1234dataAbilityHelper.executeBatch('dataability:///com.example.jsapidemo.UserDataAbility', op).then((data) => { 1235 console.info('executeBatch success, data: ${data}'); 1236}).catch((error: BusinessError) => { 1237 console.error('executeBatch failed, error: ${error}'); 1238}); 1239 1240``` 1241 1242## PacMap 1243 1244[key: string]: number | string | boolean | Array\<string | number | boolean> | null; 1245 1246**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 1247 1248| Name| Type| Mandatory| Description| 1249| ------ | ------ | ------ | ------ | 1250| [key: string] | number \| string \| boolean \| Array\<string \| number \| boolean\> \| null | Yes| Data stored in key-value pairs.| 1251