js-apis-inner-ability-dataAbilityHelper.md 39.0 KB
Newer Older
W
wusongqing 已提交
1
# DataAbilityHelper
W
wusongqing 已提交
2

3 4
The **DataAbilityHelper** object is obtained through [acquireDataAbilityHelper](js-apis-ability-featureAbility.md#featureabilityacquiredataabilityhelper7).

W
wusongqing 已提交
5 6 7 8
> **NOTE**
> 
> 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. 
> The APIs of this module can be used only in the FA model.
W
wusongqing 已提交
9

G
Gloria 已提交
10 11 12 13 14 15
## Modules to Import

```ts
import ability from '@ohos.ability.ability';
```

W
wusongqing 已提交
16 17 18
## Usage

Import the following modules based on the actual situation before using the current module:
19
```ts
20 21
import ohos_data_ability from '@ohos.data.dataAbility';
import ohos_data_rdb from '@ohos.data.rdb';
W
wusongqing 已提交
22 23
```

W
wusongqing 已提交
24
## DataAbilityHelper.openFile
W
wusongqing 已提交
25

W
wusongqing 已提交
26
openFile(uri: string, mode: string, callback: AsyncCallback\<number>): void
W
wusongqing 已提交
27

28
Opens a file with a specified URI. This API uses an asynchronous callback to return a file descriptor.
W
wusongqing 已提交
29

W
wusongqing 已提交
30
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
31

W
wusongqing 已提交
32
**Parameters**
W
wusongqing 已提交
33

W
wusongqing 已提交
34
| Name    | Type                  | Mandatory| Description                              |
W
wusongqing 已提交
35
| -------- | ---------------------- | ---- | ---------------------------------- |
W
wusongqing 已提交
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.           |
W
wusongqing 已提交
38
| callback | AsyncCallback\<number> | Yes  | Callback used to return the file descriptor.|
W
wusongqing 已提交
39

W
wusongqing 已提交
40
**Example**
W
wusongqing 已提交
41

42
```ts
43
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
44
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
45
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
46
);
47
let mode = 'rw';
G
Gloria 已提交
48 49 50 51 52 53
DAHelper.openFile('dataability:///com.example.DataAbility', mode, (error, data) => {
    if (error && error.code !== 0) {
        console.error('openFile fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('openFile success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
54 55 56
});
```

W
wusongqing 已提交
57
## DataAbilityHelper.openFile
W
wusongqing 已提交
58

W
wusongqing 已提交
59
openFile(uri: string, mode: string): Promise\<number>
W
wusongqing 已提交
60

61
Opens a file with a specified URI. This API uses a promise to return a file descriptor.
W
wusongqing 已提交
62

W
wusongqing 已提交
63
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
64

W
wusongqing 已提交
65
**Parameters**
W
wusongqing 已提交
66

W
wusongqing 已提交
67
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
68
| ---- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
69
| uri  | string | Yes  | URI of the file to open.|
70
| 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. |
W
wusongqing 已提交
71

W
wusongqing 已提交
72
**Return value**
W
wusongqing 已提交
73

W
wusongqing 已提交
74
| Type            | Description            |
W
wusongqing 已提交
75 76
| ---------------- | ---------------- |
| Promise\<number> | Promise used to return the file descriptor.|
W
wusongqing 已提交
77

W
wusongqing 已提交
78
**Example**
W
wusongqing 已提交
79

80
```ts
81
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
82
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
83
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
84
);
85 86 87
let mode = 'rw';
DAHelper.openFile('dataability:///com.example.DataAbility', mode).then((data) => {
    console.info('openFile data: ${JSON.stringify(data)}');
W
wusongqing 已提交
88 89 90
});
```

W
wusongqing 已提交
91
## DataAbilityHelper.on
W
wusongqing 已提交
92

W
wusongqing 已提交
93
on(type: 'dataChange', uri: string, callback: AsyncCallback\<void>): void
W
wusongqing 已提交
94

95
Registers an observer to listen for changes in the data specified by a given URI.
W
wusongqing 已提交
96

W
wusongqing 已提交
97
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
98

W
wusongqing 已提交
99
**Parameters**
W
wusongqing 已提交
100

W
wusongqing 已提交
101
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
102
| -------- | -------------------- | ---- | ------------------------ |
103
| type     | string               | Yes  | The value **'dataChange'** means data changes.              |
W
wusongqing 已提交
104 105
| uri      | string               | Yes  | URI of the data.|
| callback | AsyncCallback\<void> | Yes  | Callback invoked when the data is changed.  |
W
wusongqing 已提交
106

W
wusongqing 已提交
107
**Example**
W
wusongqing 已提交
108

109
```ts
110
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
111
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
112
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
113 114
);
function onChangeNotify() {
115
    console.info('onChangeNotify call back');
W
wusongqing 已提交
116
};
117
DAHelper.on(
118 119
    'dataChange',
    'dataability:///com.example.DataAbility',
W
wusongqing 已提交
120
    onChangeNotify
121
);
W
wusongqing 已提交
122 123
```

W
wusongqing 已提交
124
## DataAbilityHelper.off
W
wusongqing 已提交
125

W
wusongqing 已提交
126
off(type: 'dataChange', uri: string, callback?: AsyncCallback\<void>): void
W
wusongqing 已提交
127

128
Deregisters the observer that listens for changes in the data specified by a given URI.
W
wusongqing 已提交
129

W
wusongqing 已提交
130
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
131

W
wusongqing 已提交
132
**Parameters**
W
wusongqing 已提交
133

W
wusongqing 已提交
134
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
135
| -------- | -------------------- | ---- | ------------------------ |
136
| type     | string               | Yes  | The value **'dataChange'** means data changes.              |
W
wusongqing 已提交
137
| uri      | string               | Yes  | URI of the data.|
138
| callback | AsyncCallback\<void> | No  | Callback of the listener to deregister. If the callback is set to **null**, all data change listeners are canceled.      |
W
wusongqing 已提交
139

W
wusongqing 已提交
140
**Example**
W
wusongqing 已提交
141

142
```ts
143
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
144
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
145
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
146 147
);
function onChangeNotify() {
148
    console.info('onChangeNotify call back');
W
wusongqing 已提交
149
};
150
DAHelper.off(
151 152
    'dataChange',
    'dataability:///com.example.DataAbility',
153 154 155
    onChangeNotify
);
DAHelper.off(
156 157
    'dataChange',
    'dataability:///com.example.DataAbility',
158
);
W
wusongqing 已提交
159 160
```

W
wusongqing 已提交
161
## DataAbilityHelper.getType
W
wusongqing 已提交
162

W
wusongqing 已提交
163
getType(uri: string, callback: AsyncCallback\<string>): void
W
wusongqing 已提交
164

165
Obtains the media resource type of the data specified by a given URI. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
166

W
wusongqing 已提交
167
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
168

W
wusongqing 已提交
169
**Parameters**
W
wusongqing 已提交
170

W
wusongqing 已提交
171
| Name    | Type                  | Mandatory| Description                                         |
W
wusongqing 已提交
172
| -------- | ---------------------- | ---- | --------------------------------------------- |
W
wusongqing 已提交
173
| uri      | string                 | Yes  | URI of the data.                     |
174
| callback | AsyncCallback\<string> | Yes  | Callback used to return the media resource type.|
W
wusongqing 已提交
175

W
wusongqing 已提交
176
**Example**
W
wusongqing 已提交
177

178
```ts
179
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
180
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
181
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
182
);
G
Gloria 已提交
183 184 185 186 187 188
DAHelper.getType('dataability:///com.example.DataAbility', (error, data) => {
    if (error && error.code !== 0) {
        console.error('getType fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('getType success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
189 190 191
});
```

W
wusongqing 已提交
192
## DataAbilityHelper.getType
W
wusongqing 已提交
193

W
wusongqing 已提交
194
getType(uri: string): Promise\<string>
W
wusongqing 已提交
195

196
Obtains the media resource type of the data specified by a given URI. This API uses a promise to return the result.
W
wusongqing 已提交
197

W
wusongqing 已提交
198
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
199

W
wusongqing 已提交
200
**Parameters**
W
wusongqing 已提交
201

W
wusongqing 已提交
202
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
203
| ---- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
204
| uri  | string | Yes  | URI of the data.|
W
wusongqing 已提交
205

W
wusongqing 已提交
206
**Return value**
W
wusongqing 已提交
207

W
wusongqing 已提交
208
| Type            | Description                               |
W
wusongqing 已提交
209
| ---------------- | ----------------------------------- |
210
| Promise\<string> | Promise used to return the media resource type.|
W
wusongqing 已提交
211

W
wusongqing 已提交
212
**Example**
W
wusongqing 已提交
213

214
```ts
215
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
216
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
217
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
218
);
219 220
DAHelper.getType('dataability:///com.example.DataAbility').then((data) => {
    console.info('getType data: ${JSON.stringify(data)}');
W
wusongqing 已提交
221 222 223
});
```

W
wusongqing 已提交
224
## DataAbilityHelper.getFileTypes
W
wusongqing 已提交
225

W
wusongqing 已提交
226
getFileTypes(uri: string, mimeTypeFilter: string, callback: AsyncCallback<Array\<string>>): void
W
wusongqing 已提交
227

228
Obtains the supported media resource types of a specified file. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
229

W
wusongqing 已提交
230
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
231

W
wusongqing 已提交
232
**Parameters**
W
wusongqing 已提交
233

W
wusongqing 已提交
234
| Name          | Type                          | Mandatory| Description                              |
W
wusongqing 已提交
235
| -------------- | ------------------------------ | ---- | ---------------------------------- |
W
wusongqing 已提交
236
| uri            | string                         | Yes  | URI of the file.          |
237 238
| mimeTypeFilter | string                         | Yes  | Media resource type of the file to obtain.      |
| callback       | AsyncCallback\<Array\<string>> | Yes  | Callback used to return an array holding the media resource type.|
W
wusongqing 已提交
239

W
wusongqing 已提交
240
**Example**
W
wusongqing 已提交
241

242
```ts
243
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
244
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
245
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
246
);
G
Gloria 已提交
247 248 249 250 251 252
DAHelper.getFileTypes( 'dataability:///com.example.DataAbility', 'image/*', (error, data) => {
    if (error && error.code !== 0) {
        console.error('getFileTypes fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('getFileTypes success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
253 254 255
});
```

W
wusongqing 已提交
256
## DataAbilityHelper.getFileTypes
W
wusongqing 已提交
257 258

getFileTypes(uri: string, mimeTypeFilter: string): Promise\<Array\<string>>
W
wusongqing 已提交
259

260
Obtains the supported media resource types of a specified file. This API uses a promise to return the result.
W
wusongqing 已提交
261

W
wusongqing 已提交
262
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
263

W
wusongqing 已提交
264
**Parameters**
W
wusongqing 已提交
265

W
wusongqing 已提交
266
| Name          | Type  | Mandatory| Description                        |
W
wusongqing 已提交
267
| -------------- | ------ | ---- | ---------------------------- |
W
wusongqing 已提交
268
| uri            | string | Yes  | URI of the file.    |
269
| mimeTypeFilter | string | Yes  | Media resource type of the file to obtain.|
W
wusongqing 已提交
270

W
wusongqing 已提交
271
**Return value**
W
wusongqing 已提交
272

W
wusongqing 已提交
273
| Type                    | Description                    |
W
wusongqing 已提交
274
| ------------------------ | ------------------------ |
275
| Promise\<Array\<string>> | Promise used to return an array holding the media resource type.|
W
wusongqing 已提交
276

W
wusongqing 已提交
277
**Example**
W
wusongqing 已提交
278

279
```ts
280
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
281
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
282
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
283
);
284 285
DAHelper.getFileTypes('dataability:///com.example.DataAbility', 'image/*').then((data) => {
    console.info('getFileTypes data: ${JSON.stringify(data)}');
W
wusongqing 已提交
286 287 288
});
```

W
wusongqing 已提交
289
## DataAbilityHelper.normalizeUri
W
wusongqing 已提交
290

W
wusongqing 已提交
291
normalizeUri(uri: string, callback: AsyncCallback\<string>): void
W
wusongqing 已提交
292

W
wusongqing 已提交
293
Converts the URI that refers to a Data ability into a normalized URI. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
294

W
wusongqing 已提交
295
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
296

W
wusongqing 已提交
297
**Parameters**
W
wusongqing 已提交
298

W
wusongqing 已提交
299
| Name    | Type                  | Mandatory| Description                                                        |
W
wusongqing 已提交
300
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
301 302
| uri      | string                 | Yes  | URI object to normalize.                                     |
| 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.|
W
wusongqing 已提交
303

W
wusongqing 已提交
304
**Example**
W
wusongqing 已提交
305

306
```ts
307
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
308
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
309
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
310
);
G
Gloria 已提交
311 312 313 314 315 316
DAHelper.normalizeUri('dataability:///com.example.DataAbility', (error, data) => {
    if (error && error.code !== 0) {
        console.error('normalizeUri fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('normalizeUri success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
317 318 319
});
```

W
wusongqing 已提交
320
## DataAbilityHelper.normalizeUri
W
wusongqing 已提交
321

W
wusongqing 已提交
322
normalizeUri(uri: string): Promise\<string>
W
wusongqing 已提交
323

W
wusongqing 已提交
324
Converts the URI that refers to a Data ability into a normalized URI. This API uses a promise to return the result.
W
wusongqing 已提交
325

W
wusongqing 已提交
326
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
327

W
wusongqing 已提交
328
**Parameters**
W
wusongqing 已提交
329

W
wusongqing 已提交
330
| Name| Type  | Mandatory| Description                   |
W
wusongqing 已提交
331
| ---- | ------ | ---- | ----------------------- |
W
wusongqing 已提交
332
| uri  | string | Yes  | URI object to normalize.|
W
wusongqing 已提交
333

W
wusongqing 已提交
334
**Return value**
W
wusongqing 已提交
335

W
wusongqing 已提交
336
| Type            | Description                                                  |
W
wusongqing 已提交
337 338
| ---------------- | ------------------------------------------------------ |
| 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.|
W
wusongqing 已提交
339

W
wusongqing 已提交
340
**Example**
W
wusongqing 已提交
341

342
```ts
343
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
344
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
345
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
346
);
347 348
DAHelper.normalizeUri('dataability:///com.example.DataAbility',).then((data) => {
    console.info('normalizeUri data: ${JSON.stringify(data)}');
W
wusongqing 已提交
349 350 351
});
```

W
wusongqing 已提交
352
## DataAbilityHelper.denormalizeUri
W
wusongqing 已提交
353

W
wusongqing 已提交
354
denormalizeUri(uri: string, callback: AsyncCallback\<string>): void
W
wusongqing 已提交
355

W
wusongqing 已提交
356
Converts 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.
W
wusongqing 已提交
357

W
wusongqing 已提交
358
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
359

W
wusongqing 已提交
360
**Parameters**
W
wusongqing 已提交
361

W
wusongqing 已提交
362
| Name    | Type                  | Mandatory| Description                                               |
W
wusongqing 已提交
363
| -------- | ---------------------- | ---- | --------------------------------------------------- |
364
| uri      | string                 | Yes  | URI object to denormalize.                            |
W
wusongqing 已提交
365
| callback | AsyncCallback\<string> | Yes  | Callback used to return the denormalized URI object.|
W
wusongqing 已提交
366

W
wusongqing 已提交
367
**Example**
W
wusongqing 已提交
368

369
```ts
370
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
371
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
372
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
373
);
G
Gloria 已提交
374 375 376 377 378 379
DAHelper.denormalizeUri('dataability:///com.example.DataAbility', (error, data) => {
    if (error && error.code !== 0) {
        console.error('denormalizeUri fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('denormalizeUri success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
380 381 382
});
```

W
wusongqing 已提交
383
## DataAbilityHelper.denormalizeUri
W
wusongqing 已提交
384 385

denormalizeUri(uri: string): Promise\<string>
W
wusongqing 已提交
386

W
wusongqing 已提交
387
Converts a normalized URI generated by **DataAbilityHelper.normalizeUri(uri: string)** to a denormalized one. This API uses a promise to return the result.
W
wusongqing 已提交
388

W
wusongqing 已提交
389
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
390

W
wusongqing 已提交
391
**Parameters**
W
wusongqing 已提交
392

W
wusongqing 已提交
393
| Name| Type  | Mandatory| Description                   |
W
wusongqing 已提交
394
| ---- | ------ | ---- | ----------------------- |
W
wusongqing 已提交
395
| uri  | string | Yes  | URI object to normalize.|
W
wusongqing 已提交
396

W
wusongqing 已提交
397
**Return value**
W
wusongqing 已提交
398

W
wusongqing 已提交
399
| Type            | Description                                     |
W
wusongqing 已提交
400 401
| ---------------- | ----------------------------------------- |
| Promise\<string> | Promise used to return the denormalized URI object.|
W
wusongqing 已提交
402

W
wusongqing 已提交
403
**Example**
W
wusongqing 已提交
404

405
```ts
406
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
407
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
408
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
409
);
410 411
DAHelper.denormalizeUri('dataability:///com.example.DataAbility',).then((data) => {
    console.info('denormalizeUri data: ${JSON.stringify(data)}');
W
wusongqing 已提交
412 413 414
});
```

W
wusongqing 已提交
415
## DataAbilityHelper.notifyChange
W
wusongqing 已提交
416

W
wusongqing 已提交
417
notifyChange(uri: string, callback: AsyncCallback\<void>): void
W
wusongqing 已提交
418

W
wusongqing 已提交
419
Notifies the registered observer of a change to the data specified by the URI. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
420

W
wusongqing 已提交
421
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
422

W
wusongqing 已提交
423
**Parameters**
W
wusongqing 已提交
424

W
wusongqing 已提交
425
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
426
| -------- | -------------------- | ---- | ------------------------ |
427
| uri      | string               | Yes  | URI of the data that changes.|
W
wusongqing 已提交
428
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.              |
W
wusongqing 已提交
429

W
wusongqing 已提交
430
**Example**
W
wusongqing 已提交
431

432
```ts
433
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
434
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
435
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
436
);
G
Gloria 已提交
437 438 439 440 441 442
DAHelper.notifyChange('dataability:///com.example.DataAbility', (error) => {
    if (error && error.code !== 0) {
        console.error('notifyChange fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('notifyChange success');
    }
W
wusongqing 已提交
443 444 445
});
```

W
wusongqing 已提交
446
## DataAbilityHelper.notifyChange
W
wusongqing 已提交
447

W
wusongqing 已提交
448
notifyChange(uri: string): Promise\<void>
W
wusongqing 已提交
449

W
wusongqing 已提交
450
Notifies the registered observer of a change to the data specified by the URI. This API uses a promise to return the result.
W
wusongqing 已提交
451

W
wusongqing 已提交
452
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
453

W
wusongqing 已提交
454
**Parameters**
W
wusongqing 已提交
455

W
wusongqing 已提交
456
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
457
| ---- | ------ | ---- | ------------------------ |
458
| uri  | string | Yes  | URI of the data that changes.|
W
wusongqing 已提交
459

W
wusongqing 已提交
460
**Return value**
W
wusongqing 已提交
461

W
wusongqing 已提交
462
| Type          | Description                 |
W
wusongqing 已提交
463 464
| -------------- | --------------------- |
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
465

W
wusongqing 已提交
466
**Example**
W
wusongqing 已提交
467

468
```ts
469
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
470
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
471
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
472
);
473 474
DAHelper.notifyChange('dataability:///com.example.DataAbility').then(() => {
    console.info('================>notifyChangeCallback================>');
W
wusongqing 已提交
475 476 477
});
```

W
wusongqing 已提交
478
## DataAbilityHelper.insert
W
wusongqing 已提交
479

W
wusongqing 已提交
480
insert(uri: string, valuesBucket: rdb.ValuesBucket, callback: AsyncCallback\<number>): void
W
wusongqing 已提交
481

W
wusongqing 已提交
482
Inserts a single data record into the database. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
483

W
wusongqing 已提交
484
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
485

W
wusongqing 已提交
486
**Parameters**
W
wusongqing 已提交
487

W
wusongqing 已提交
488
| Name        | Type                  | Mandatory| Description                                                  |
W
wusongqing 已提交
489
| ------------ | ---------------------- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
490 491 492
| uri          | string                 | Yes  | URI of the data to insert.                              |
| valuesBucket | rdb.ValuesBucket       | Yes  | Data record to insert. If this parameter is **null**, a blank row will be inserted.|
| callback     | AsyncCallback\<number> | Yes  | Callback used to return the index of the inserted data record.                    |
W
wusongqing 已提交
493

W
wusongqing 已提交
494
**Example**
W
wusongqing 已提交
495

496
```ts
497
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
498
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
499
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
500 501
);
const valueBucket = {
502 503 504 505
    'name': 'rose',
    'age': 22,
    'salary': 200.5,
    'blobType': 'u8',
506
};
G
Gloria 已提交
507 508 509 510 511 512
DAHelper.insert('dataability:///com.example.DataAbility', valueBucket, (error, data) => {
    if (error && error.code !== 0) {
        console.error('insert fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('insert success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
513 514 515
});
```

W
wusongqing 已提交
516
## DataAbilityHelper.insert
W
wusongqing 已提交
517

W
wusongqing 已提交
518
insert(uri: string, valuesBucket: rdb.ValuesBucket): Promise\<number>
W
wusongqing 已提交
519

W
wusongqing 已提交
520
Inserts a single data record into the database. This API uses a promise to return the result.
W
wusongqing 已提交
521

W
wusongqing 已提交
522
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
523

W
wusongqing 已提交
524
**Parameters**
W
wusongqing 已提交
525

W
wusongqing 已提交
526
| Name        | Type            | Mandatory| Description                                                  |
W
wusongqing 已提交
527
| ------------ | ---------------- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
528 529
| uri          | string           | Yes  | URI of the data to insert.                              |
| valuesBucket | rdb.ValuesBucket | Yes  | Data record to insert. If this parameter is **null**, a blank row will be inserted.|
W
wusongqing 已提交
530

W
wusongqing 已提交
531
**Return value**
W
wusongqing 已提交
532

W
wusongqing 已提交
533
| Type            | Description                    |
W
wusongqing 已提交
534 535
| ---------------- | ------------------------ |
| Promise\<number> | Promise used to return the index of the inserted data record.|
W
wusongqing 已提交
536

W
wusongqing 已提交
537
**Example**
W
wusongqing 已提交
538

539
```ts
540
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
541
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
542
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
543 544
);
const valueBucket = {
545 546 547 548
    'name': 'rose1',
    'age': 221,
    'salary': 20.5,
    'blobType': 'u8',
549
};
550 551
DAHelper.insert('dataability:///com.example.DataAbility', valueBucket).then((data) => {
    console.info('insert data: ${JSON.stringify(data)}');
W
wusongqing 已提交
552 553 554
});
```

W
wusongqing 已提交
555
## DataAbilityHelper.batchInsert
W
wusongqing 已提交
556

557
batchInsert(uri: string, valuesBuckets: Array\<rdb.ValuesBucket>, callback: AsyncCallback\<number>): void
W
wusongqing 已提交
558

W
wusongqing 已提交
559
Inserts multiple data records into the database. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
560

W
wusongqing 已提交
561
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
562

W
wusongqing 已提交
563
**Parameters**
W
wusongqing 已提交
564

W
wusongqing 已提交
565
| Name        | Type                   | Mandatory| Description                            |
W
wusongqing 已提交
566
| ------------ | ----------------------- | ---- | -------------------------------- |
W
wusongqing 已提交
567
| uri          | string                  | Yes  | URI of the data to insert.        |
568
| valuesBucket | Array\<rdb.ValuesBucket> | Yes  | Data records to insert.          |
W
wusongqing 已提交
569
| callback     | AsyncCallback\<number>  | Yes  | Callback used to return the number of inserted data records.|
W
wusongqing 已提交
570

W
wusongqing 已提交
571
**Example**
W
wusongqing 已提交
572

573
```ts
574
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
575
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
576
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
577
);
578 579 580
let cars = new Array({'name': 'roe11', 'age': 21, 'salary': 20.5, 'blobType': 'u8',},
                     {'name': 'roe12', 'age': 21, 'salary': 20.5, 'blobType': 'u8',},
                     {'name': 'roe13', 'age': 21, 'salary': 20.5, 'blobType': 'u8',});
G
Gloria 已提交
581 582 583 584 585 586
DAHelper.batchInsert('dataability:///com.example.DataAbility', cars, (error, data) => {
    if (error && error.code !== 0) {
        console.error('batchInsert fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('batchInsert success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
587 588 589
});
```

W
wusongqing 已提交
590
## DataAbilityHelper.batchInsert
W
wusongqing 已提交
591

W
wusongqing 已提交
592
batchInsert(uri: string, valuesBuckets: Array<rdb.ValuesBucket>): Promise\<number>
W
wusongqing 已提交
593

W
wusongqing 已提交
594
Inserts multiple data records into the database. This API uses a promise to return the result.
W
wusongqing 已提交
595

W
wusongqing 已提交
596
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
597

W
wusongqing 已提交
598
**Parameters**
W
wusongqing 已提交
599

W
wusongqing 已提交
600
| Name        | Type                   | Mandatory| Description                    |
W
wusongqing 已提交
601
| ------------ | ----------------------- | ---- | ------------------------ |
W
wusongqing 已提交
602
| uri          | string                  | Yes  | URI of the data to insert.|
603
| valuesBucket | Array<rdb.ValuesBucket> | Yes  | Data records to insert.  |
W
wusongqing 已提交
604

W
wusongqing 已提交
605
**Return value**
W
wusongqing 已提交
606

W
wusongqing 已提交
607
| Type            | Description                  |
W
wusongqing 已提交
608 609
| ---------------- | ---------------------- |
| Promise\<number> | Promise used to return the number of inserted data records.|
W
wusongqing 已提交
610

W
wusongqing 已提交
611
**Example**
W
wusongqing 已提交
612

613
```ts
614
import featureAbility from '@ohos.ability.featureAbility';
G
Gloria 已提交
615
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
616
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
617
);
618 619 620 621 622
let cars = new Array({'name': 'roe11', 'age': 21, 'salary': 20.5, 'blobType': 'u8',},
                     {'name': 'roe12', 'age': 21, 'salary': 20.5, 'blobType': 'u8',},
                     {'name': 'roe13', 'age': 21, 'salary': 20.5, 'blobType': 'u8',});
DAHelper.batchInsert('dataability:///com.example.DataAbility', cars).then((data) => {
    console.info('batchInsert data: ${JSON.stringify(data)}');
W
wusongqing 已提交
623 624 625
});
```

W
wusongqing 已提交
626
## DataAbilityHelper.delete
W
wusongqing 已提交
627

W
wusongqing 已提交
628
delete(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<number>): void
W
wusongqing 已提交
629

W
wusongqing 已提交
630
Deletes one or more data records from the database. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
631

W
wusongqing 已提交
632
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
633

W
wusongqing 已提交
634
**Parameters**
W
wusongqing 已提交
635

W
wusongqing 已提交
636
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
637
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
638
| uri          | string                            | Yes  | URI of the data to delete.                        |
639
| predicates | dataAbility.DataAbilityPredicates | Yes  | Filter criteria. You should define the processing logic when this parameter is **null**.|
W
wusongqing 已提交
640
| callback     | AsyncCallback\<number>            | Yes  | Callback used to return the number of deleted data records.              |
W
wusongqing 已提交
641

W
wusongqing 已提交
642
**Example**
W
wusongqing 已提交
643

644
```ts
645 646
import featureAbility from '@ohos.ability.featureAbility';
import ohos_data_ability from '@ohos.data.dataAbility';
G
Gloria 已提交
647
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
648
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
649
);
650
let da = new ohos_data_ability.DataAbilityPredicates();
G
Gloria 已提交
651 652 653 654 655 656
DAHelper.delete('dataability:///com.example.DataAbility', da, (error, data) => {
    if (error && error.code !== 0) {
        console.error('delete fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('delete success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
657 658 659
});
```

W
wusongqing 已提交
660
## DataAbilityHelper.delete
W
wusongqing 已提交
661

662
delete(uri: string, predicates?: dataAbility.DataAbilityPredicates): Promise\<number>;
W
wusongqing 已提交
663

W
wusongqing 已提交
664
Deletes one or more data records from the database. This API uses a promise to return the result.
W
wusongqing 已提交
665

W
wusongqing 已提交
666
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
667

W
wusongqing 已提交
668
**Parameters**
W
wusongqing 已提交
669

W
wusongqing 已提交
670
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
671
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
672
| uri          | string                            | Yes  | URI of the data to delete.                        |
673
| predicates | dataAbility.DataAbilityPredicates | No  | Filter criteria. You should define the processing logic when this parameter is **null**.|
W
wusongqing 已提交
674

W
wusongqing 已提交
675
**Return value**
W
wusongqing 已提交
676

W
wusongqing 已提交
677
| Type            | Description                    |
W
wusongqing 已提交
678 679
| ---------------- | ------------------------ |
| Promise\<number> | Promise used to return the number of deleted data records.|
W
wusongqing 已提交
680

W
wusongqing 已提交
681
**Example**
W
wusongqing 已提交
682

683
```ts
684 685
import featureAbility from '@ohos.ability.featureAbility';
import ohos_data_ability from '@ohos.data.dataAbility';
G
Gloria 已提交
686
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
687
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
688
);
689
let da = new ohos_data_ability.DataAbilityPredicates();
690 691
DAHelper.delete('dataability:///com.example.DataAbility', da).then((data) => {
    console.info('delete data: ${JSON.stringify(data)}');
W
wusongqing 已提交
692 693 694
});
```

W
wusongqing 已提交
695
## DataAbilityHelper.update
W
wusongqing 已提交
696

W
wusongqing 已提交
697
update(uri: string, valuesBucket: rdb.ValuesBucket, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<number>): void
W
wusongqing 已提交
698

W
wusongqing 已提交
699
Updates data records in the database. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
700

W
wusongqing 已提交
701
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
702

W
wusongqing 已提交
703
**Parameters**
W
wusongqing 已提交
704

W
wusongqing 已提交
705
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
706
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
707 708 709 710
| uri          | string                            | Yes  | URI of the data to update.                        |
| valuesBucket | rdb.ValuesBucket                  | Yes  | New values.                              |
| predicates   | dataAbility.DataAbilityPredicates | Yes  | Filter criteria. You should define the processing logic when this parameter is **null**.|
| callback     | AsyncCallback\<number>            | Yes  | Callback used to return the number of updated data records.                |
W
wusongqing 已提交
711

W
wusongqing 已提交
712
**Example**
W
wusongqing 已提交
713

714
```ts
715 716
import featureAbility from '@ohos.ability.featureAbility';
import ohos_data_ability from '@ohos.data.dataAbility';
G
Gloria 已提交
717
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
718
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
719 720
);
const va = {
721 722 723 724
    'name': 'roe1',
    'age': 21,
    'salary': 20.5,
    'blobType': 'u8',
725 726
};
let da = new ohos_data_ability.DataAbilityPredicates();
G
Gloria 已提交
727 728 729 730 731 732
DAHelper.update('dataability:///com.example.DataAbility', va, da, (error, data) => {
    if (error && error.code !== 0) {
        console.error('update fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('update success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
733 734 735
});
```

W
wusongqing 已提交
736
## DataAbilityHelper.update
W
wusongqing 已提交
737

738
update(uri: string, valuesBucket: rdb.ValuesBucket, predicates?: dataAbility.DataAbilityPredicates): Promise\<number>;
W
wusongqing 已提交
739

W
wusongqing 已提交
740
Updates data records in the database. This API uses a promise to return the result.
W
wusongqing 已提交
741

W
wusongqing 已提交
742
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
743

W
wusongqing 已提交
744
**Parameters**
W
wusongqing 已提交
745

W
wusongqing 已提交
746
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
747
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
748 749
| uri          | string                            | Yes  | URI of the data to update.                        |
| valuesBucket | rdb.ValuesBucket                  | Yes  | New values.                              |
750
| predicates   | dataAbility.DataAbilityPredicates | No  | Filter criteria. You should define the processing logic when this parameter is **null**.|
W
wusongqing 已提交
751

W
wusongqing 已提交
752
**Return value**
W
wusongqing 已提交
753

W
wusongqing 已提交
754
| Type            | Description                                        |
W
wusongqing 已提交
755
| ---------------- | -------------------------------------------- |
W
wusongqing 已提交
756
| Promise\<number> | Promise used to return the number of updated data records.  |
W
wusongqing 已提交
757

W
wusongqing 已提交
758
**Example**
W
wusongqing 已提交
759

760
```ts
761 762
import featureAbility from '@ohos.ability.featureAbility';
import ohos_data_ability from '@ohos.data.dataAbility';
G
Gloria 已提交
763
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
764
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
765 766
);
const va = {
767 768 769 770
    'name': 'roe1',
    'age': 21,
    'salary': 20.5,
    'blobType': 'u8',
771 772
};
let da = new ohos_data_ability.DataAbilityPredicates();
773 774
DAHelper.update('dataability:///com.example.DataAbility', va, da).then((data) => {
    console.info('update data: ${JSON.stringify(data)}');
W
wusongqing 已提交
775 776 777
});
```

W
wusongqing 已提交
778
## DataAbilityHelper.query
W
wusongqing 已提交
779

W
wusongqing 已提交
780
query(uri: string, columns: Array\<string>, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<ResultSet>): void
W
wusongqing 已提交
781

W
wusongqing 已提交
782
Queries data in the database. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
783

W
wusongqing 已提交
784
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
785

W
wusongqing 已提交
786
**Parameters**
W
wusongqing 已提交
787

W
wusongqing 已提交
788
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
789
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
790
| uri        | string                            | Yes  | URI of the data to query.                        |
791
| columns    | Array\<string>                | Yes  | Columns to query. If this parameter is **null**, all columns will be queried.  |
W
wusongqing 已提交
792 793
| predicates | dataAbility.DataAbilityPredicates | Yes  | Filter criteria. You should define the processing logic when this parameter is **null**.|
| callback   | AsyncCallback\<ResultSet>         | Yes  | Callback used to return the data queried.                        |
W
wusongqing 已提交
794

W
wusongqing 已提交
795
**Example**
W
wusongqing 已提交
796

797
```ts
798 799
import featureAbility from '@ohos.ability.featureAbility';
import ohos_data_ability from '@ohos.data.dataAbility';
G
Gloria 已提交
800
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
801
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
802
);
803
let cars=new Array('value1', 'value2', 'value3', 'value4');
804
let da = new ohos_data_ability.DataAbilityPredicates();
G
Gloria 已提交
805 806 807 808 809 810
DAHelper.query('dataability:///com.example.DataAbility', cars, da, (error, data) => {
    if (error && error.code !== 0) {
        console.error('query fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('query success, data: ${JSON.stringify(data)}');
    }
W
wusongqing 已提交
811 812 813 814 815
});
```



W
wusongqing 已提交
816
## DataAbilityHelper.query
W
wusongqing 已提交
817

818
query(uri: string, columns?: Array\<string>, predicates?: dataAbility.DataAbilityPredicates): Promise\<ResultSet>;
W
wusongqing 已提交
819

W
wusongqing 已提交
820
Queries data in the database. This API uses a promise to return the result.
W
wusongqing 已提交
821

W
wusongqing 已提交
822
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
823

W
wusongqing 已提交
824
**Parameters**
W
wusongqing 已提交
825

W
wusongqing 已提交
826
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
827
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
828
| uri        | string                            | Yes  | URI of the data to query.                        |
829 830
| columns    | Array\<string>               | No  | Columns to query. If this parameter is **null**, all columns will be queried.  |
| predicates | dataAbility.DataAbilityPredicates | No  | Filter criteria. You should define the processing logic when this parameter is **null**.|
W
wusongqing 已提交
831

W
wusongqing 已提交
832
**Return value**
W
wusongqing 已提交
833

W
wusongqing 已提交
834
| Type               | Description          |
W
wusongqing 已提交
835 836
| ------------------- | -------------- |
| Promise\<ResultSet> | Promise used to return the data queried.|
W
wusongqing 已提交
837

W
wusongqing 已提交
838
**Example**
W
wusongqing 已提交
839

840
```ts
841 842
import featureAbility from '@ohos.ability.featureAbility';
import ohos_data_ability from '@ohos.data.dataAbility';
G
Gloria 已提交
843
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
844
    'dataability:///com.example.DataAbility'
W
wusongqing 已提交
845
);
846
let cars = new Array('value1', 'value2', 'value3', 'value4');
847
let da = new ohos_data_ability.DataAbilityPredicates();
848 849
DAHelper.query('dataability:///com.example.DataAbility', cars, da).then((data) => {
    console.info('query data: ${JSON.stringify(data)}');
W
wusongqing 已提交
850 851
});
```
W
wusongqing 已提交
852 853 854

## DataAbilityHelper.call

855
call(uri: string, method: string, arg: string, extras: PacMap, callback: AsyncCallback\<PacMap>): void
W
wusongqing 已提交
856

857
Calls an extended API of the DataAbility. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
858 859 860 861 862 863 864

**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel

**Parameters**

| Name      | Type                             | Mandatory| Description                                            |
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
865
| uri        | string                 | Yes  | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'.          |
W
wusongqing 已提交
866
| method    | string                  | Yes  | Name of the API to call.  |
867
| arg      | string                   | Yes  | Parameter to pass in.     |
G
Gloria 已提交
868 869
| extras   | [PacMap](js-apis-inner-application-pacMap.md)        | Yes  | Key-value pair parameter.      |
| callback | AsyncCallback\<[PacMap](js-apis-inner-application-pacMap.md)> | Yes| Callback used to return the result.    |
W
wusongqing 已提交
870 871 872

**Example**

873
```ts
W
wusongqing 已提交
874 875
import featureAbility from '@ohos.ability.featureAbility';

G
Gloria 已提交
876
let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
877
    'dataability:///com.example.jsapidemo.UserDataAbility'
878
);
879
dataAbilityHelper.call('dataability:///com.example.jsapidemo.UserDataAbility',
G
Gloria 已提交
880 881 882 883 884
    'method', 'arg', {'key1':'value1'}, (error, data) => {
    if (error && error.code !== 0) {
        console.error('call fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('call success, data: ${JSON.stringify(data)}');
885
    }
W
wusongqing 已提交
886 887 888 889 890
});
```

## DataAbilityHelper.call

891
call(uri: string, method: string, arg: string, extras: PacMap): Promise\<PacMap>
W
wusongqing 已提交
892

893
Calls an extended API of the DataAbility. This API uses a promise to return the result.
W
wusongqing 已提交
894 895 896 897 898 899 900

**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel

**Parameters**

| Name      | Type                             | Mandatory| Description                                            |
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
901
| uri        | string                 | Yes  | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'.          |
W
wusongqing 已提交
902
| method    | string                  | Yes  | Name of the API to call.  |
903
| arg      | string                   | Yes  | Parameter to pass in.     |
G
Gloria 已提交
904
| extras   | [PacMap](js-apis-inner-application-pacMap.md)        | Yes  | Key-value pair parameter.      |
905 906 907 908 909

**Return value**

| Type| Description|
|------ | ------- |
G
Gloria 已提交
910
|Promise\<[PacMap](js-apis-inner-application-pacMap.md)> | Promise used to return the result.|
W
wusongqing 已提交
911 912 913

**Example**

914
```ts
W
wusongqing 已提交
915 916
import featureAbility from '@ohos.ability.featureAbility';

G
Gloria 已提交
917
let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
918
    'dataability:///com.example.jsapidemo.UserDataAbility'
919
);
920 921
dataAbilityHelper.call('dataability:///com.example.jsapidemo.UserDataAbility',
    'method', 'arg', {'key1':'value1'}).then((data) => {
G
Gloria 已提交
922
    console.info('call success, data: ${data}');
923
}).catch((error) => {
G
Gloria 已提交
924
    console.error('call failed, error: ${error}');
W
wusongqing 已提交
925 926
});
```
927 928 929 930 931

## DataAbilityHelper.executeBatch

executeBatch(uri: string, operations: Array\<DataAbilityOperation>, callback: AsyncCallback\<Array\<DataAbilityResult>>): void;

932
Operates data in the database in batches. This API uses an asynchronous callback to return the result.
933 934 935 936 937

**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel

**Parameters**

938 939
| Name       | Type                         | Mandatory| Description                                            |
| ----------| ---------------------------------| ---- | ------------------------------------------------ |
940
| uri       | string                           | Yes  | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'.|
941 942
| operations    |  Array\<[DataAbilityOperation](js-apis-inner-ability-dataAbilityOperation.md)>        | Yes  | An array holding the data operations on the database.  |
| 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 945

**Example**

946
```ts
947 948 949 950
import featureAbility from '@ohos.ability.featureAbility';

// Select the operations to be performed on the database according to the DataAbilityOperation array.
let op=new Array();
G
Gloria 已提交
951
let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
952
    'dataability:///com.example.jsapidemo.UserDataAbility'
953
);
G
Gloria 已提交
954 955 956 957 958
dataAbilityHelper.executeBatch('dataability:///com.example.jsapidemo.UserDataAbility', op, (error, data) => {
    if (error && error.code !== 0) {
        console.error('executeBatch fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('executeBatch success, data: ${JSON.stringify(data)}');
959 960 961 962 963 964 965 966
    }
});
```

## DataAbilityHelper.executeBatch

executeBatch(uri: string, operations: Array\<DataAbilityOperation>): Promise\<Array\<DataAbilityResult>>;

967
Operates data in the database in batches. This API uses a promise to return the result.
968 969 970 971 972 973 974

**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel

**Parameters**

| Name         | Type                           | Mandatory| Description                                            |
| ----------    | -------------------------------| ---- | ------------------------------------------------ |
975
| uri           | string                         | Yes  | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'.|
976
| operations    |  Array\<[DataAbilityOperation](js-apis-inner-ability-dataAbilityOperation.md)>  | Yes  | An array holding the data operations on the database.  |
977 978 979 980 981

**Return value**

| Type| Description|
|------ | ------- |
982
|Promise\<Array\<[DataAbilityResult](js-apis-inner-ability-dataAbilityResult.md)>> | Promise used to return the result of each operation in the **DataAbilityResult** array.|
983 984 985

**Example**

986
```ts
987 988 989 990
import featureAbility from '@ohos.ability.featureAbility';

// Select the operations to be performed on the database according to the DataAbilityOperation array.
let op=new Array();
G
Gloria 已提交
991
let dataAbilityHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(
992
    'dataability:///com.example.jsapidemo.UserDataAbility'
993
);
994
dataAbilityHelper.executeBatch('dataability:///com.example.jsapidemo.UserDataAbility', op).then((data) => {
G
Gloria 已提交
995
    console.info('executeBatch success, data: ${data}');
996
}).catch((error) => {
G
Gloria 已提交
997
    console.error('executeBatch failed, error: ${error}');
998 999 1000
});

```