js-apis-dataAbilityHelper.md 33.7 KB
Newer Older
Z
zengyawen 已提交
1
# DataAbilityHelper Module (JavaScript SDK APIs)
W
wusongqing 已提交
2

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

W
wusongqing 已提交
6
## Modules to Import
W
wusongqing 已提交
7

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

## Usage

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

W
wusongqing 已提交
21
## DataAbilityHelper.openFile
W
wusongqing 已提交
22

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

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

W
wusongqing 已提交
27
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
28

W
wusongqing 已提交
29
**Parameters**
W
wusongqing 已提交
30

W
wusongqing 已提交
31
| Name    | Type                  | Mandatory| Description                              |
W
wusongqing 已提交
32
| -------- | ---------------------- | ---- | ---------------------------------- |
W
wusongqing 已提交
33 34 35
| 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 已提交
36

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

```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 已提交
53
## DataAbilityHelper.openFile
W
wusongqing 已提交
54

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

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

W
wusongqing 已提交
59
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
60

W
wusongqing 已提交
61
**Parameters**
W
wusongqing 已提交
62

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

W
wusongqing 已提交
68
**Return value**
W
wusongqing 已提交
69

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

W
wusongqing 已提交
74
**Example**
W
wusongqing 已提交
75 76 77 78 79 80 81 82 83

```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 已提交
84
    mode).then((data) => {
W
wusongqing 已提交
85 86 87 88
		console.info("==========================>openFileCallback=======================>");
});
```

W
wusongqing 已提交
89
## DataAbilityHelper.on
W
wusongqing 已提交
90

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

W
wusongqing 已提交
93
Registers an observer to observe data specified by a given URI. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
94

W
wusongqing 已提交
95
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
96

W
wusongqing 已提交
97
**Parameters**
W
wusongqing 已提交
98

W
wusongqing 已提交
99
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
100
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
101 102 103
| 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 已提交
104

W
wusongqing 已提交
105
**Example**
W
wusongqing 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

```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 已提交
122
## DataAbilityHelper.off
W
wusongqing 已提交
123

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

W
wusongqing 已提交
126
Unregisters the observer used to observe data specified by a given URI. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
127

W
wusongqing 已提交
128
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
129

W
wusongqing 已提交
130
**Parameters**
W
wusongqing 已提交
131

W
wusongqing 已提交
132
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
133
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
134 135 136
| 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 已提交
137

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

```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 已提交
159
## DataAbilityHelper.getType
W
wusongqing 已提交
160

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

W
wusongqing 已提交
163
Obtains the MIME type of the data specified by a given URI. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
164

W
wusongqing 已提交
165
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
166

W
wusongqing 已提交
167
**Parameters**
W
wusongqing 已提交
168

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

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

```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 已提交
188
## DataAbilityHelper.getType
W
wusongqing 已提交
189

W
wusongqing 已提交
190
getType(uri: string): Promise\<string>
W
wusongqing 已提交
191

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

W
wusongqing 已提交
194
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
195

W
wusongqing 已提交
196
**Parameters**
W
wusongqing 已提交
197

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

W
wusongqing 已提交
202
**Return value**
W
wusongqing 已提交
203

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

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

```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 已提交
222
## DataAbilityHelper.getFileTypes
W
wusongqing 已提交
223

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

W
wusongqing 已提交
226
Obtains the supported MIME types of a specified file. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
227

W
wusongqing 已提交
228
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
229

W
wusongqing 已提交
230
**Parameters**
W
wusongqing 已提交
231

W
wusongqing 已提交
232
| Name          | Type                          | Mandatory| Description                              |
W
wusongqing 已提交
233
| -------------- | ------------------------------ | ---- | ---------------------------------- |
W
wusongqing 已提交
234 235 236
| 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 已提交
237

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

```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 已提交
255
## DataAbilityHelper.getFileTypes
W
wusongqing 已提交
256 257

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

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

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

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

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

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

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

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

```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 已提交
291
## DataAbilityHelper.normalizeUri
W
wusongqing 已提交
292

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

W
wusongqing 已提交
295
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 已提交
296

W
wusongqing 已提交
297
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
298

W
wusongqing 已提交
299
**Parameters**
W
wusongqing 已提交
300

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

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

```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 已提交
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 343 344 345 346 347 348 349 350 351 352 353

```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 已提交
354
## DataAbilityHelper.denormalizeUri
W
wusongqing 已提交
355

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

W
wusongqing 已提交
358
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 已提交
359

W
wusongqing 已提交
360
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
361

W
wusongqing 已提交
362
**Parameters**
W
wusongqing 已提交
363

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

W
wusongqing 已提交
369
**Example**
W
wusongqing 已提交
370 371 372 373 374 375 376

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



W
wusongqing 已提交
385
## DataAbilityHelper.denormalizeUri
W
wusongqing 已提交
386 387

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

W
wusongqing 已提交
389
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 已提交
390

W
wusongqing 已提交
391
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
392

W
wusongqing 已提交
393
**Parameters**
W
wusongqing 已提交
394

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

W
wusongqing 已提交
399
**Return value**
W
wusongqing 已提交
400

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

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

```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 已提交
419
## DataAbilityHelper.notifyChange
W
wusongqing 已提交
420

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

W
wusongqing 已提交
423
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 已提交
424

W
wusongqing 已提交
425
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
426

W
wusongqing 已提交
427
**Parameters**
W
wusongqing 已提交
428

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

W
wusongqing 已提交
434
**Example**
W
wusongqing 已提交
435 436 437 438 439 440 441

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

W
wusongqing 已提交
448
## DataAbilityHelper.notifyChange
W
wusongqing 已提交
449

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

W
wusongqing 已提交
452
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 已提交
453

W
wusongqing 已提交
454
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
455

W
wusongqing 已提交
456
**Parameters**
W
wusongqing 已提交
457

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

W
wusongqing 已提交
462
**Return value**
W
wusongqing 已提交
463

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

W
wusongqing 已提交
468
**Example**
W
wusongqing 已提交
469 470 471 472 473 474 475 476

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

W
wusongqing 已提交
482
## DataAbilityHelper.insert
W
wusongqing 已提交
483

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

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

W
wusongqing 已提交
488
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
489

W
wusongqing 已提交
490
**Parameters**
W
wusongqing 已提交
491

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

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

```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 已提交
509
    "blobType": "u8",
W
wusongqing 已提交
510 511 512 513 514 515 516 517 518
}
DAHelper.insert(
    "dataability:///com.example.DataAbility",
    valueBucket,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
519
## DataAbilityHelper.insert
W
wusongqing 已提交
520

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

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

W
wusongqing 已提交
525
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
526

W
wusongqing 已提交
527
**Parameters**
W
wusongqing 已提交
528

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

W
wusongqing 已提交
534
**Return value**
W
wusongqing 已提交
535

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

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

```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 已提交
551
    "blobType": "u8",
W
wusongqing 已提交
552 553 554 555 556 557 558 559 560
}
DAHelper.insert(
    "dataability:///com.example.DataAbility",
    valueBucket
	).then((data) => {
		console.info("==========================>insertCallback=======================>");
});
```

W
wusongqing 已提交
561
## DataAbilityHelper.batchInsert
W
wusongqing 已提交
562

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

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

W
wusongqing 已提交
567
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
568

W
wusongqing 已提交
569
**Parameters**
W
wusongqing 已提交
570

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

W
wusongqing 已提交
577
**Example**
W
wusongqing 已提交
578 579 580 581 582 583

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

W
wusongqing 已提交
595
## DataAbilityHelper.batchInsert
W
wusongqing 已提交
596

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

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

W
wusongqing 已提交
601
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
602

W
wusongqing 已提交
603
**Parameters**
W
wusongqing 已提交
604

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

W
wusongqing 已提交
610
**Return value**
W
wusongqing 已提交
611

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

W
wusongqing 已提交
616
**Example**
W
wusongqing 已提交
617 618 619 620 621 622

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

W
wusongqing 已提交
634
## DataAbilityHelper.delete
W
wusongqing 已提交
635

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

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

W
wusongqing 已提交
640
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
641

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

W
wusongqing 已提交
644
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
645
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
646 647 648
| 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 已提交
649

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

```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 已提交
667
## DataAbilityHelper.delete
W
wusongqing 已提交
668

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

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

W
wusongqing 已提交
673
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
674

W
wusongqing 已提交
675
**Parameters**
W
wusongqing 已提交
676

W
wusongqing 已提交
677
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
678
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
679 680
| 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 已提交
681

W
wusongqing 已提交
682
**Return value**
W
wusongqing 已提交
683

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

W
wusongqing 已提交
688
**Example**
W
wusongqing 已提交
689 690 691

```js
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
692
import ohos_data_ability from '@ohos.data.dataability'
W
wusongqing 已提交
693 694 695 696 697 698 699 700 701 702 703 704
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 已提交
705
## DataAbilityHelper.update
W
wusongqing 已提交
706

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

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

W
wusongqing 已提交
711
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
712

W
wusongqing 已提交
713
**Parameters**
W
wusongqing 已提交
714

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

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

```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 已提交
734
    "blobType": "u8",
W
wusongqing 已提交
735 736 737 738 739 740 741 742 743 744 745
}
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.update(
    "dataability:///com.example.DataAbility",
    va,
    da,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
746
## DataAbilityHelper.update
W
wusongqing 已提交
747

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

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

W
wusongqing 已提交
752
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
753

W
wusongqing 已提交
754
**Parameters**
W
wusongqing 已提交
755

W
wusongqing 已提交
756
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
757
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
758 759 760
| 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 已提交
761

W
wusongqing 已提交
762
**Return value**
W
wusongqing 已提交
763

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

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

```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 已提交
780
    "blobType": "u8",
W
wusongqing 已提交
781 782 783 784 785 786 787 788 789 790 791
}
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.update(
    "dataability:///com.example.DataAbility",
    va,
    da
	).then((data) => {
		console.info("==========================>updateCallback=======================>");
});
```

W
wusongqing 已提交
792
## DataAbilityHelper.query
W
wusongqing 已提交
793

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

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

W
wusongqing 已提交
798
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
799

W
wusongqing 已提交
800
**Parameters**
W
wusongqing 已提交
801

W
wusongqing 已提交
802
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
803
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
804 805 806 807
| 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 已提交
808

W
wusongqing 已提交
809
**Example**
W
wusongqing 已提交
810 811 812 813 814 815 816

```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 已提交
817
var cars=new Array("value1", "value2", "value3", "value4");
W
wusongqing 已提交
818 819 820 821 822 823 824 825 826 827 828 829
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.query(
    "dataability:///com.example.DataAbility",
    cars,
    da,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```



W
wusongqing 已提交
830
## DataAbilityHelper.query
W
wusongqing 已提交
831

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

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

W
wusongqing 已提交
836
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
837

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

W
wusongqing 已提交
840
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
841
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
842 843 844
| 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 已提交
845

W
wusongqing 已提交
846
**Return value**
W
wusongqing 已提交
847

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

W
wusongqing 已提交
852
**Example**
W
wusongqing 已提交
853 854 855 856 857 858 859

```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 已提交
860
var cars=new Array("value1", "value2", "value3", "value4");
W
wusongqing 已提交
861 862 863 864 865 866 867 868 869
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.query(
    "dataability:///com.example.DataAbility",
    cars,
    da
	).then((data) => {
		console.info("==========================>queryCallback=======================>");
});
```
W
wusongqing 已提交
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

## DataAbilityHelper.call

call(uri: string, method: string, arg: string, extras: PacMap): Promise<PacMap>

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|
|------ | ------- |
|Promise<[PacMap](#pacmap)> | Promise used to return the result.|

**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

call(uri: string, method: string, arg: string, extras: PacMap, callback: AsyncCallback<PacMap>): void

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.      |
| callback | AsyncCallback<[PacMap](#pacmap)> | Yes| Callback used to return the result.    |

**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.|