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

W
wusongqing 已提交
3 4 5 6
> **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 已提交
7

W
wusongqing 已提交
8
## Modules to Import
W
wusongqing 已提交
9

W
wusongqing 已提交
10 11 12 13 14 15 16
```js
import featureAbility from "@ohos.ability.featureAbility";
```

## Usage

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

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

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

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

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

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

W
wusongqing 已提交
33
| Name    | Type                  | Mandatory| Description                              |
W
wusongqing 已提交
34
| -------- | ---------------------- | ---- | ---------------------------------- |
W
wusongqing 已提交
35 36 37
| uri      | string                 | Yes  | URI of the file to open.          |
| mode     | string                 | Yes  | Mode for opening the file. The value can be **rwt**.           |
| callback | AsyncCallback\<number> | Yes  | Callback used to return the file descriptor.|
W
wusongqing 已提交
38

W
wusongqing 已提交
39
**Example**
W
wusongqing 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

```javascript
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
var mode = "rwt";
DAHelper.openFile(
    "dataability:///com.example.DataAbility",
    mode,
    (err) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
55
## DataAbilityHelper.openFile
W
wusongqing 已提交
56

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

W
wusongqing 已提交
59
Opens a file with a specified URI. This API uses a promise to return the result.
W
wusongqing 已提交
60

W
wusongqing 已提交
61
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
62

W
wusongqing 已提交
63
**Parameters**
W
wusongqing 已提交
64

W
wusongqing 已提交
65
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
66
| ---- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
67 68
| uri  | string | Yes  | URI of the file to open.|
| mode | string | Yes  | Mode for opening the file. The value can be **rwt**. |
W
wusongqing 已提交
69

W
wusongqing 已提交
70
**Return value**
W
wusongqing 已提交
71

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

W
wusongqing 已提交
76
**Example**
W
wusongqing 已提交
77 78 79 80 81 82 83 84 85

```javascript
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
var mode = "rwt";
DAHelper.openFile(
    "dataability:///com.example.DataAbility",
W
wusongqing 已提交
86
    mode).then((data) => {
W
wusongqing 已提交
87 88 89 90
		console.info("==========================>openFileCallback=======================>");
});
```

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

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

W
wusongqing 已提交
95
Registers an observer to observe data specified by a given URI. This API uses an asynchronous callback to return the result.
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
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
103 104 105
| type     | string               | Yes  | Type of the event to observe. The value is **dataChange**.              |
| 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123

```js
import featureAbility from '@ohos.ability.featureAbility'
var helper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
function onChangeNotify() {
    console.info("==========================>onChangeNotify=======================>");
};
helper.on(
    "dataChange",
    "dataability:///com.example.DataAbility",
    onChangeNotify
)
```

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

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

W
wusongqing 已提交
128
Unregisters the observer used to observe data specified by a given URI. This API uses an asynchronous callback to return the result.
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
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
136 137 138
| type     | string               | Yes  | Type of the event to observe. The value is **dataChange**.              |
| uri      | string               | Yes  | URI of the data.|
| callback | AsyncCallback\<void> | No  | Callback used to return the result.      |
W
wusongqing 已提交
139

W
wusongqing 已提交
140
**Example**
W
wusongqing 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

```js
import featureAbility from '@ohos.ability.featureAbility'
var helper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
function onChangeNotify() {
    console.info("==========================>onChangeNotify=======================>");
};
helper.off(
    "dataChange",
    "dataability:///com.example.DataAbility",
)
helper.off(
    "dataChange",
    "dataability:///com.example.DataAbility",
    onChangeNotify
)
```

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

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

W
wusongqing 已提交
165
Obtains the MIME 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 174
| uri      | string                 | Yes  | URI of the data.                     |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the MIME type.|
W
wusongqing 已提交
175

W
wusongqing 已提交
176
**Example**
W
wusongqing 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.getType(
    "dataability:///com.example.DataAbility",
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
190
## DataAbilityHelper.getType
W
wusongqing 已提交
191

W
wusongqing 已提交
192
getType(uri: string): Promise\<string>
W
wusongqing 已提交
193

W
wusongqing 已提交
194
Obtains the MIME type of the data specified by a given URI. This API uses a promise to return the result.
W
wusongqing 已提交
195

W
wusongqing 已提交
196
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
197

W
wusongqing 已提交
198
**Parameters**
W
wusongqing 已提交
199

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

W
wusongqing 已提交
204
**Return value**
W
wusongqing 已提交
205

W
wusongqing 已提交
206
| Type            | Description                               |
W
wusongqing 已提交
207 208
| ---------------- | ----------------------------------- |
| Promise\<string> | Promise used to return the MIME type.|
W
wusongqing 已提交
209

W
wusongqing 已提交
210
**Example**
W
wusongqing 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.getType(
    "dataability:///com.example.DataAbility"
	).then((data) => {
		console.info("==========================>getTypeCallback=======================>");
});
```

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

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

W
wusongqing 已提交
228
Obtains the supported MIME 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 237 238
| uri            | string                         | Yes  | URI of the file.          |
| mimeTypeFilter | string                         | Yes  | MIME type of the file.      |
| callback       | AsyncCallback\<Array\<string>> | Yes  | Callback used to return the supported MIME types.|
W
wusongqing 已提交
239

W
wusongqing 已提交
240
**Example**
W
wusongqing 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.getFileTypes(
    "dataability:///com.example.DataAbility",
    "image/*",
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```



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

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

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

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

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

W
wusongqing 已提交
267
| Name          | Type  | Mandatory| Description                        |
W
wusongqing 已提交
268
| -------------- | ------ | ---- | ---------------------------- |
W
wusongqing 已提交
269 270
| uri            | string | Yes  | URI of the file.    |
| mimeTypeFilter | string | Yes  | MIME type of the file.|
W
wusongqing 已提交
271

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

W
wusongqing 已提交
274
| Type                    | Description                    |
W
wusongqing 已提交
275 276
| ------------------------ | ------------------------ |
| Promise\<Array\<string>> | Promise used to return the supported MIME types.|
W
wusongqing 已提交
277

W
wusongqing 已提交
278
**Example**
W
wusongqing 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.getFileTypes(
    "dataability:///com.example.DataAbility",
    "image/*"
	).then((data) => {
		console.info("==========================>getFileTypesCallback=======================>");
});
```

W
wusongqing 已提交
293
## DataAbilityHelper.normalizeUri
W
wusongqing 已提交
294

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

W
wusongqing 已提交
297
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 已提交
298

W
wusongqing 已提交
299
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
300

W
wusongqing 已提交
301
**Parameters**
W
wusongqing 已提交
302

W
wusongqing 已提交
303
| Name    | Type                  | Mandatory| Description                                                        |
W
wusongqing 已提交
304
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
305 306
| 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 已提交
307

W
wusongqing 已提交
308
**Example**
W
wusongqing 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.normalizeUri(
    "dataability:///com.example.DataAbility",
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
322
## DataAbilityHelper.normalizeUri
W
wusongqing 已提交
323

W
wusongqing 已提交
324
normalizeUri(uri: string): Promise\<string>
W
wusongqing 已提交
325

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

W
wusongqing 已提交
328
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
329

W
wusongqing 已提交
330
**Parameters**
W
wusongqing 已提交
331

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

W
wusongqing 已提交
336
**Return value**
W
wusongqing 已提交
337

W
wusongqing 已提交
338
| Type            | Description                                                  |
W
wusongqing 已提交
339 340
| ---------------- | ------------------------------------------------------ |
| 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 已提交
341

W
wusongqing 已提交
342
**Example**
W
wusongqing 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.normalizeUri(
    "dataability:///com.example.DataAbility",
	).then((data) => {
		console.info("==========================>normalizeUriCallback=======================>");
});
```

W
wusongqing 已提交
356
## DataAbilityHelper.denormalizeUri
W
wusongqing 已提交
357

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

W
wusongqing 已提交
360
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 已提交
361

W
wusongqing 已提交
362
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
363

W
wusongqing 已提交
364
**Parameters**
W
wusongqing 已提交
365

W
wusongqing 已提交
366
| Name    | Type                  | Mandatory| Description                                               |
W
wusongqing 已提交
367
| -------- | ---------------------- | ---- | --------------------------------------------------- |
W
wusongqing 已提交
368 369
| uri      | string                 | Yes  | URI object to normalize.                            |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the denormalized URI object.|
W
wusongqing 已提交
370

W
wusongqing 已提交
371
**Example**
W
wusongqing 已提交
372 373 374 375 376 377 378

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.denormalizeUri(
W
wusongqing 已提交
379
    "dataability:///com.example.DataAbility",
W
wusongqing 已提交
380 381 382 383 384 385 386
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```



W
wusongqing 已提交
387
## DataAbilityHelper.denormalizeUri
W
wusongqing 已提交
388 389

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

W
wusongqing 已提交
391
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 已提交
392

W
wusongqing 已提交
393
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
394

W
wusongqing 已提交
395
**Parameters**
W
wusongqing 已提交
396

W
wusongqing 已提交
397
| Name| Type  | Mandatory| Description                   |
W
wusongqing 已提交
398
| ---- | ------ | ---- | ----------------------- |
W
wusongqing 已提交
399
| uri  | string | Yes  | URI object to normalize.|
W
wusongqing 已提交
400

W
wusongqing 已提交
401
**Return value**
W
wusongqing 已提交
402

W
wusongqing 已提交
403
| Type            | Description                                     |
W
wusongqing 已提交
404 405
| ---------------- | ----------------------------------------- |
| Promise\<string> | Promise used to return the denormalized URI object.|
W
wusongqing 已提交
406

W
wusongqing 已提交
407
**Example**
W
wusongqing 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.denormalizeUri(
    "dataability:///com.example.DataAbility",
	).then((data) => {
		console.info("==========================>denormalizeUriCallback=======================>");
});
```

W
wusongqing 已提交
421
## DataAbilityHelper.notifyChange
W
wusongqing 已提交
422

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

W
wusongqing 已提交
425
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 已提交
426

W
wusongqing 已提交
427
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
428

W
wusongqing 已提交
429
**Parameters**
W
wusongqing 已提交
430

W
wusongqing 已提交
431
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
432
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
433 434
| uri      | string               | Yes  | URI of the data.|
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.              |
W
wusongqing 已提交
435

W
wusongqing 已提交
436
**Example**
W
wusongqing 已提交
437 438 439 440 441 442 443

```js
import featureAbility from '@ohos.ability.featureAbility'
var helper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
helper.notifyChange(
W
wusongqing 已提交
444
    "dataability:///com.example.DataAbility",
W
wusongqing 已提交
445 446 447 448 449
    (err) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
450
## DataAbilityHelper.notifyChange
W
wusongqing 已提交
451

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

W
wusongqing 已提交
454
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 已提交
455

W
wusongqing 已提交
456
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
457

W
wusongqing 已提交
458
**Parameters**
W
wusongqing 已提交
459

W
wusongqing 已提交
460
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
461
| ---- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
462
| uri  | string | Yes  | URI of the data.|
W
wusongqing 已提交
463

W
wusongqing 已提交
464
**Return value**
W
wusongqing 已提交
465

W
wusongqing 已提交
466
| Type          | Description                 |
W
wusongqing 已提交
467 468
| -------------- | --------------------- |
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
469

W
wusongqing 已提交
470
**Example**
W
wusongqing 已提交
471 472 473 474 475 476 477 478

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.notifyChange(
    "dataability:///com.example.DataAbility",
W
wusongqing 已提交
479
	).then(() => {
W
wusongqing 已提交
480 481 482 483
		console.info("==========================>notifyChangeCallback=======================>");
});
```

W
wusongqing 已提交
484
## DataAbilityHelper.insert
W
wusongqing 已提交
485

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

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

W
wusongqing 已提交
490
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
491

W
wusongqing 已提交
492
**Parameters**
W
wusongqing 已提交
493

W
wusongqing 已提交
494
| Name        | Type                  | Mandatory| Description                                                  |
W
wusongqing 已提交
495
| ------------ | ---------------------- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
496 497 498
| 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 已提交
499

W
wusongqing 已提交
500
**Example**
W
wusongqing 已提交
501 502 503 504 505 506 507 508 509 510

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
const valueBucket = {
    "name": "rose",
    "age": 22,
    "salary": 200.5,
W
wusongqing 已提交
511
    "blobType": "u8",
W
wusongqing 已提交
512 513 514 515 516 517 518 519 520
}
DAHelper.insert(
    "dataability:///com.example.DataAbility",
    valueBucket,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
521
## DataAbilityHelper.insert
W
wusongqing 已提交
522

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

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

W
wusongqing 已提交
527
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
528

W
wusongqing 已提交
529
**Parameters**
W
wusongqing 已提交
530

W
wusongqing 已提交
531
| Name        | Type            | Mandatory| Description                                                  |
W
wusongqing 已提交
532
| ------------ | ---------------- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
533 534
| 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 已提交
535

W
wusongqing 已提交
536
**Return value**
W
wusongqing 已提交
537

W
wusongqing 已提交
538
| Type            | Description                    |
W
wusongqing 已提交
539 540
| ---------------- | ------------------------ |
| Promise\<number> | Promise used to return the index of the inserted data record.|
W
wusongqing 已提交
541

W
wusongqing 已提交
542
**Example**
W
wusongqing 已提交
543 544 545 546 547 548 549 550 551 552

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
const valueBucket = {
    "name": "rose1",
    "age": 221,
    "salary": 20.5,
W
wusongqing 已提交
553
    "blobType": "u8",
W
wusongqing 已提交
554 555 556 557 558 559 560 561 562
}
DAHelper.insert(
    "dataability:///com.example.DataAbility",
    valueBucket
	).then((data) => {
		console.info("==========================>insertCallback=======================>");
});
```

W
wusongqing 已提交
563
## DataAbilityHelper.batchInsert
W
wusongqing 已提交
564

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

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

W
wusongqing 已提交
569
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
570

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

W
wusongqing 已提交
573
| Name        | Type                   | Mandatory| Description                            |
W
wusongqing 已提交
574
| ------------ | ----------------------- | ---- | -------------------------------- |
W
wusongqing 已提交
575
| uri          | string                  | Yes  | URI of the data to insert.        |
W
wusongqing 已提交
576
| valuesBucket | Array\<rdb.ValuesBucket> | Yes  | Data records to insert.          |
W
wusongqing 已提交
577
| callback     | AsyncCallback\<number>  | Yes  | Callback used to return the number of inserted data records.|
W
wusongqing 已提交
578

W
wusongqing 已提交
579
**Example**
W
wusongqing 已提交
580 581 582 583 584 585

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
W
wusongqing 已提交
586 587 588
var 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",})
W
wusongqing 已提交
589 590 591 592 593 594 595 596
DAHelper.batchInsert(
    "dataability:///com.example.DataAbility",
    cars,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
597
## DataAbilityHelper.batchInsert
W
wusongqing 已提交
598

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

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

W
wusongqing 已提交
603
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
604

W
wusongqing 已提交
605
**Parameters**
W
wusongqing 已提交
606

W
wusongqing 已提交
607
| Name        | Type                   | Mandatory| Description                    |
W
wusongqing 已提交
608
| ------------ | ----------------------- | ---- | ------------------------ |
W
wusongqing 已提交
609 610
| uri          | string                  | Yes  | URI of the data to insert.|
| valuesBucket | Array<rdb.ValuesBucket> | Yes  | Data record to insert.  |
W
wusongqing 已提交
611

W
wusongqing 已提交
612
**Return value**
W
wusongqing 已提交
613

W
wusongqing 已提交
614
| Type            | Description                  |
W
wusongqing 已提交
615 616
| ---------------- | ---------------------- |
| Promise\<number> | Promise used to return the number of inserted data records.|
W
wusongqing 已提交
617

W
wusongqing 已提交
618
**Example**
W
wusongqing 已提交
619 620 621 622 623 624

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
W
wusongqing 已提交
625 626 627
var 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",})
W
wusongqing 已提交
628 629 630 631 632 633 634 635
DAHelper.batchInsert(
    "dataability:///com.example.DataAbility",
    cars
	).then((data) => {
		console.info("==========================>batchInsertCallback=======================>");
});
```

W
wusongqing 已提交
636
## DataAbilityHelper.delete
W
wusongqing 已提交
637

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

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

W
wusongqing 已提交
642
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
643

W
wusongqing 已提交
644
**Parameters**
W
wusongqing 已提交
645

W
wusongqing 已提交
646
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
647
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
648 649 650
| uri          | string                            | Yes  | URI of the data to delete.                        |
| valuesBucket | 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 deleted data records.              |
W
wusongqing 已提交
651

W
wusongqing 已提交
652
**Example**
W
wusongqing 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668

```js
import featureAbility from '@ohos.ability.featureAbility'
import ohos_data_ability from '@ohos.data.dataability'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.delete(
    "dataability:///com.example.DataAbility",
    da,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
669
## DataAbilityHelper.delete
W
wusongqing 已提交
670

W
wusongqing 已提交
671
delete(uri: string, predicates: dataAbility.DataAbilityPredicates): Promise\<number>
W
wusongqing 已提交
672

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

W
wusongqing 已提交
675
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
676

W
wusongqing 已提交
677
**Parameters**
W
wusongqing 已提交
678

W
wusongqing 已提交
679
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
680
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
681 682
| uri          | string                            | Yes  | URI of the data to delete.                        |
| valuesBucket | dataAbility.DataAbilityPredicates | Yes  | Filter criteria. You should define the processing logic when this parameter is **null**.|
W
wusongqing 已提交
683

W
wusongqing 已提交
684
**Return value**
W
wusongqing 已提交
685

W
wusongqing 已提交
686
| Type            | Description                    |
W
wusongqing 已提交
687 688
| ---------------- | ------------------------ |
| Promise\<number> | Promise used to return the number of deleted data records.|
W
wusongqing 已提交
689

W
wusongqing 已提交
690
**Example**
W
wusongqing 已提交
691 692 693

```js
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
694
import ohos_data_ability from '@ohos.data.dataability'
W
wusongqing 已提交
695 696 697 698 699 700 701 702 703 704 705 706
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.delete(
    "dataability:///com.example.DataAbility",
    da
	).then((data) => {
		console.info("==========================>deleteCallback=======================>");
});
```

W
wusongqing 已提交
707
## DataAbilityHelper.update
W
wusongqing 已提交
708

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

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

W
wusongqing 已提交
713
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
714

W
wusongqing 已提交
715
**Parameters**
W
wusongqing 已提交
716

W
wusongqing 已提交
717
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
718
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
719 720 721 722
| 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 已提交
723

W
wusongqing 已提交
724
**Example**
W
wusongqing 已提交
725 726 727 728 729 730 731 732 733 734 735

```js
import featureAbility from '@ohos.ability.featureAbility'
import ohos_data_ability from '@ohos.data.dataability'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
const va = {
    "name": "roe1",
    "age": 21,
    "salary": 20.5,
W
wusongqing 已提交
736
    "blobType": "u8",
W
wusongqing 已提交
737 738 739 740 741 742 743 744 745 746 747
}
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.update(
    "dataability:///com.example.DataAbility",
    va,
    da,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
748
## DataAbilityHelper.update
W
wusongqing 已提交
749

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

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

W
wusongqing 已提交
754
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
755

W
wusongqing 已提交
756
**Parameters**
W
wusongqing 已提交
757

W
wusongqing 已提交
758
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
759
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
760 761 762
| 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**.|
W
wusongqing 已提交
763

W
wusongqing 已提交
764
**Return value**
W
wusongqing 已提交
765

W
wusongqing 已提交
766
| Type            | Description                                        |
W
wusongqing 已提交
767
| ---------------- | -------------------------------------------- |
W
wusongqing 已提交
768
| Promise\<number> | Promise used to return the number of updated data records.  |
W
wusongqing 已提交
769

W
wusongqing 已提交
770
**Example**
W
wusongqing 已提交
771 772 773 774 775 776 777 778 779 780 781

```js
import featureAbility from '@ohos.ability.featureAbility'
import ohos_data_ability from '@ohos.data.dataability'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
const va = {
    "name": "roe1",
    "age": 21,
    "salary": 20.5,
W
wusongqing 已提交
782
    "blobType": "u8",
W
wusongqing 已提交
783 784 785 786 787 788 789 790 791 792 793
}
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.update(
    "dataability:///com.example.DataAbility",
    va,
    da
	).then((data) => {
		console.info("==========================>updateCallback=======================>");
});
```

W
wusongqing 已提交
794
## DataAbilityHelper.query
W
wusongqing 已提交
795

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

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

W
wusongqing 已提交
800
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
801

W
wusongqing 已提交
802
**Parameters**
W
wusongqing 已提交
803

W
wusongqing 已提交
804
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
805
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
806 807 808 809
| uri        | string                            | Yes  | URI of the data to query.                        |
| columns    | rdb.ValuesBucket                  | Yes  | Columns to query. If this parameter is **null**, all columns will be queried.  |
| 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 已提交
810

W
wusongqing 已提交
811
**Example**
W
wusongqing 已提交
812 813 814 815 816 817 818

```js
import featureAbility from '@ohos.ability.featureAbility'
import ohos_data_ability from '@ohos.data.dataability'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
W
wusongqing 已提交
819
var cars=new Array("value1", "value2", "value3", "value4");
W
wusongqing 已提交
820 821 822 823 824 825 826 827 828 829 830 831
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.query(
    "dataability:///com.example.DataAbility",
    cars,
    da,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```



W
wusongqing 已提交
832
## DataAbilityHelper.query
W
wusongqing 已提交
833

W
wusongqing 已提交
834
query(uri: string, columns: Array\<string>, predicates: dataAbility.DataAbilityPredicates): Promise\<ResultSet>
W
wusongqing 已提交
835

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

W
wusongqing 已提交
838
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
839

W
wusongqing 已提交
840
**Parameters**
W
wusongqing 已提交
841

W
wusongqing 已提交
842
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
843
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
844 845 846
| uri        | string                            | Yes  | URI of the data to query.                        |
| columns    | rdb.ValuesBucket                  | Yes  | Columns to query. If this parameter is **null**, all columns will be queried.  |
| predicates | dataAbility.DataAbilityPredicates | Yes  | Filter criteria. You should define the processing logic when this parameter is **null**.|
W
wusongqing 已提交
847

W
wusongqing 已提交
848
**Return value**
W
wusongqing 已提交
849

W
wusongqing 已提交
850
| Type               | Description          |
W
wusongqing 已提交
851 852
| ------------------- | -------------- |
| Promise\<ResultSet> | Promise used to return the data queried.|
W
wusongqing 已提交
853

W
wusongqing 已提交
854
**Example**
W
wusongqing 已提交
855 856 857 858 859 860 861

```js
import featureAbility from '@ohos.ability.featureAbility'
import ohos_data_ability from '@ohos.data.dataability'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
W
wusongqing 已提交
862
var cars=new Array("value1", "value2", "value3", "value4");
W
wusongqing 已提交
863 864 865 866 867 868 869 870 871
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.query(
    "dataability:///com.example.DataAbility",
    cars,
    da
	).then((data) => {
		console.info("==========================>queryCallback=======================>");
});
```
W
wusongqing 已提交
872 873 874

## DataAbilityHelper.call

W
wusongqing 已提交
875
call(uri: string, method: string, arg: string, extras: PacMap): Promise\<PacMap>
W
wusongqing 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893

Calls the extended API of the Data ability. This API uses a promise to return the result.

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

**Parameters**

| Name      | Type                             | Mandatory| Description                                            |
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
| uri        | string                 | Yes  | URI of the Data ability. Example: "dataability:///com.example.xxx.xxxx"          |
| method    | string                  | Yes  | Name of the API to call.  |
| arg      | string                   | Yes  |Parameter to pass.     |
| extras   | [PacMap](#pacmap)        | Yes  | Key-value pair parameter.      |

**Return value**

| Type| Description|
|------ | ------- |
W
wusongqing 已提交
894
|Promise\<[PacMap](#pacmap)> | Promise used to return the result.|
W
wusongqing 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility';

let dataAbilityHelper = featureAbility.acquireDataAbilityHelper("dataability:///com.example.jsapidemo.UserDataAbility");
dataAbilityHelper.call("dataability:///com.example.jsapidemo.UserDataAbility", "method", "arg", {"key1":"value1"}).then((data) => {
    console.info('Operation succeeded: ' + data);
}).catch((error) => {
    console.error('Operation failed. Cause: ' + error);
});
```

## DataAbilityHelper.call

W
wusongqing 已提交
911
call(uri: string, method: string, arg: string, extras: PacMap, callback: AsyncCallback\<PacMap>): void
W
wusongqing 已提交
912 913 914 915 916 917 918 919 920 921 922 923 924

Calls the extended API of the Data ability. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name      | Type                             | Mandatory| Description                                            |
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
| uri        | string                 | Yes  | URI of the Data ability. Example: "dataability:///com.example.xxx.xxxx"          |
| method    | string                  | Yes  | Name of the API to call.  |
| arg      | string                   | Yes  |Parameter to pass.     |
| extras   | [PacMap](#pacmap)        | Yes  | Key-value pair parameter.      |
W
wusongqing 已提交
925
| callback | AsyncCallback\<[PacMap](#pacmap)> | Yes| Callback used to return the result.    |
W
wusongqing 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility';

let dataAbilityHelper = featureAbility.acquireDataAbilityHelper("dataability:///com.example.jsapidemo.UserDataAbility");
dataAbilityHelper.call("dataability:///com.example.jsapidemo.UserDataAbility", "method", "arg", {"key1":"value1"}, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + err);
        return;
    }
    console.info('Operation succeeded: ' + data);
});
```
## PacMap

| Name| Type| Mandatory| Description|
| ------ | ------ | ------ | ------ |
| [key: string] | number \| string \| boolean \| Array\<string \| number \| boolean\> \| null | Yes| Data stored in key-value pairs.|