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

W
wusongqing 已提交
3 4
> **NOTE**
>
Z
zengyawen 已提交
5 6
> 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 已提交
7
## Modules to Import
W
wusongqing 已提交
8 9 10 11 12 13 14

```
import featureAbility from '@ohos.ability.featureAbility'
import ohos_data_ability from '@ohos.data.dataability'
import ohos_data_rdb from '@ohos.data.rdb'
```

W
wusongqing 已提交
15
## DataAbilityHelper.openFile
W
wusongqing 已提交
16

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

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

W
wusongqing 已提交
21
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
22

W
wusongqing 已提交
23
**Parameters**
W
wusongqing 已提交
24

W
wusongqing 已提交
25
| Name    | Type                  | Mandatory| Description                              |
W
wusongqing 已提交
26
| -------- | ---------------------- | ---- | ---------------------------------- |
W
wusongqing 已提交
27 28 29
| 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 已提交
30

W
wusongqing 已提交
31
**Example**
W
wusongqing 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

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

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

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

W
wusongqing 已提交
53
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
54

W
wusongqing 已提交
55
**Parameters**
W
wusongqing 已提交
56

W
wusongqing 已提交
57
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
58
| ---- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
59 60
| uri  | string | Yes  | URI of the file to open.|
| mode | string | Yes  | Mode for opening the file. The value can be **rwt**. |
W
wusongqing 已提交
61

W
wusongqing 已提交
62
**Return value**
W
wusongqing 已提交
63

W
wusongqing 已提交
64
| Type            | Description            |
W
wusongqing 已提交
65 66
| ---------------- | ---------------- |
| Promise\<number> | Promise used to return the file descriptor.|
W
wusongqing 已提交
67

W
wusongqing 已提交
68
**Example**
W
wusongqing 已提交
69 70 71 72 73 74 75 76 77

```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 已提交
78
    mode).then((data) => {
W
wusongqing 已提交
79 80 81 82
		console.info("==========================>openFileCallback=======================>");
});
```

W
wusongqing 已提交
83
## DataAbilityHelper.on
W
wusongqing 已提交
84

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

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

W
wusongqing 已提交
89
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
90

W
wusongqing 已提交
91
**Parameters**
W
wusongqing 已提交
92

W
wusongqing 已提交
93
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
94
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
95 96 97
| 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 已提交
98

W
wusongqing 已提交
99
**Example**
W
wusongqing 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

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

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

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

W
wusongqing 已提交
122
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
123

W
wusongqing 已提交
124
**Parameters**
W
wusongqing 已提交
125

W
wusongqing 已提交
126
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
127
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
128 129 130
| 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 已提交
131

W
wusongqing 已提交
132
**Example**
W
wusongqing 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

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

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

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

W
wusongqing 已提交
159
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
160

W
wusongqing 已提交
161
**Parameters**
W
wusongqing 已提交
162

W
wusongqing 已提交
163
| Name    | Type                  | Mandatory| Description                                         |
W
wusongqing 已提交
164
| -------- | ---------------------- | ---- | --------------------------------------------- |
W
wusongqing 已提交
165 166
| uri      | string                 | Yes  | URI of the data.                     |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the MIME type.|
W
wusongqing 已提交
167

W
wusongqing 已提交
168
**Example**
W
wusongqing 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181

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

W
wusongqing 已提交
184
getType(uri: string): Promise\<string>
W
wusongqing 已提交
185

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

W
wusongqing 已提交
188
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
189

W
wusongqing 已提交
190
**Parameters**
W
wusongqing 已提交
191

W
wusongqing 已提交
192
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
193
| ---- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
194
| uri  | string | Yes  | URI of the data.|
W
wusongqing 已提交
195

W
wusongqing 已提交
196
**Return value**
W
wusongqing 已提交
197

W
wusongqing 已提交
198
| Type            | Description                               |
W
wusongqing 已提交
199 200
| ---------------- | ----------------------------------- |
| Promise\<string> | Promise used to return the MIME type.|
W
wusongqing 已提交
201

W
wusongqing 已提交
202
**Example**
W
wusongqing 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215

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

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

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

W
wusongqing 已提交
222
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
223

W
wusongqing 已提交
224
**Parameters**
W
wusongqing 已提交
225

W
wusongqing 已提交
226
| Name          | Type                          | Mandatory| Description                              |
W
wusongqing 已提交
227
| -------------- | ------------------------------ | ---- | ---------------------------------- |
W
wusongqing 已提交
228 229 230
| 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 已提交
231

W
wusongqing 已提交
232
**Example**
W
wusongqing 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

```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 已提交
249
## DataAbilityHelper.getFileTypes
W
wusongqing 已提交
250 251

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

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

W
wusongqing 已提交
255
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
256

W
wusongqing 已提交
257
**Parameters**
W
wusongqing 已提交
258

W
wusongqing 已提交
259
| Name          | Type  | Mandatory| Description                        |
W
wusongqing 已提交
260
| -------------- | ------ | ---- | ---------------------------- |
W
wusongqing 已提交
261 262
| uri            | string | Yes  | URI of the file.    |
| mimeTypeFilter | string | Yes  | MIME type of the file.|
W
wusongqing 已提交
263

W
wusongqing 已提交
264
**Return value**
W
wusongqing 已提交
265

W
wusongqing 已提交
266
| Type                    | Description                    |
W
wusongqing 已提交
267 268
| ------------------------ | ------------------------ |
| Promise\<Array\<string>> | Promise used to return the supported MIME types.|
W
wusongqing 已提交
269

W
wusongqing 已提交
270
**Example**
W
wusongqing 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284

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

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

W
wusongqing 已提交
289
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 已提交
290

W
wusongqing 已提交
291
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
292

W
wusongqing 已提交
293
**Parameters**
W
wusongqing 已提交
294

W
wusongqing 已提交
295
| Name    | Type                  | Mandatory| Description                                                        |
W
wusongqing 已提交
296
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
297 298
| 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 已提交
299

W
wusongqing 已提交
300
**Example**
W
wusongqing 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313

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

W
wusongqing 已提交
316
normalizeUri(uri: string): Promise\<string>
W
wusongqing 已提交
317

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

W
wusongqing 已提交
320
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
321

W
wusongqing 已提交
322
**Parameters**
W
wusongqing 已提交
323

W
wusongqing 已提交
324
| Name| Type  | Mandatory| Description                   |
W
wusongqing 已提交
325
| ---- | ------ | ---- | ----------------------- |
W
wusongqing 已提交
326
| uri  | string | Yes  | URI object to normalize.|
W
wusongqing 已提交
327

W
wusongqing 已提交
328
**Return value**
W
wusongqing 已提交
329

W
wusongqing 已提交
330
| Type            | Description                                                  |
W
wusongqing 已提交
331 332
| ---------------- | ------------------------------------------------------ |
| 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 已提交
333

W
wusongqing 已提交
334
**Example**
W
wusongqing 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347

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

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

W
wusongqing 已提交
352
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 已提交
353

W
wusongqing 已提交
354
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
355

W
wusongqing 已提交
356
**Parameters**
W
wusongqing 已提交
357

W
wusongqing 已提交
358
| Name    | Type                  | Mandatory| Description                                               |
W
wusongqing 已提交
359
| -------- | ---------------------- | ---- | --------------------------------------------------- |
W
wusongqing 已提交
360 361
| uri      | string                 | Yes  | URI object to normalize.                            |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the denormalized URI object.|
W
wusongqing 已提交
362

W
wusongqing 已提交
363
**Example**
W
wusongqing 已提交
364 365 366 367 368 369 370

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.denormalizeUri(
W
wusongqing 已提交
371
    "dataability:///com.example.DataAbility",
W
wusongqing 已提交
372 373 374 375 376 377 378
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```



W
wusongqing 已提交
379
## DataAbilityHelper.denormalizeUri
W
wusongqing 已提交
380 381

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

W
wusongqing 已提交
383
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 已提交
384

W
wusongqing 已提交
385
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
386

W
wusongqing 已提交
387
**Parameters**
W
wusongqing 已提交
388

W
wusongqing 已提交
389
| Name| Type  | Mandatory| Description                   |
W
wusongqing 已提交
390
| ---- | ------ | ---- | ----------------------- |
W
wusongqing 已提交
391
| uri  | string | Yes  | URI object to normalize.|
W
wusongqing 已提交
392

W
wusongqing 已提交
393
**Return value**
W
wusongqing 已提交
394

W
wusongqing 已提交
395
| Type            | Description                                     |
W
wusongqing 已提交
396 397
| ---------------- | ----------------------------------------- |
| Promise\<string> | Promise used to return the denormalized URI object.|
W
wusongqing 已提交
398

W
wusongqing 已提交
399
**Example**
W
wusongqing 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412

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

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

W
wusongqing 已提交
417
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 已提交
418

W
wusongqing 已提交
419
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
420

W
wusongqing 已提交
421
**Parameters**
W
wusongqing 已提交
422

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

W
wusongqing 已提交
428
**Example**
W
wusongqing 已提交
429 430 431 432 433 434 435

```js
import featureAbility from '@ohos.ability.featureAbility'
var helper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
helper.notifyChange(
W
wusongqing 已提交
436
    "dataability:///com.example.DataAbility",
W
wusongqing 已提交
437 438 439 440 441
    (err) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
442
## DataAbilityHelper.notifyChange
W
wusongqing 已提交
443

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

W
wusongqing 已提交
446
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 已提交
447

W
wusongqing 已提交
448
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
449

W
wusongqing 已提交
450
**Parameters**
W
wusongqing 已提交
451

W
wusongqing 已提交
452
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
453
| ---- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
454
| uri  | string | Yes  | URI of the data.|
W
wusongqing 已提交
455

W
wusongqing 已提交
456
**Return value**
W
wusongqing 已提交
457

W
wusongqing 已提交
458
| Type          | Description                 |
W
wusongqing 已提交
459 460
| -------------- | --------------------- |
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
461

W
wusongqing 已提交
462
**Example**
W
wusongqing 已提交
463 464 465 466 467 468 469 470

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.notifyChange(
    "dataability:///com.example.DataAbility",
W
wusongqing 已提交
471
	).then(() => {
W
wusongqing 已提交
472 473 474 475
		console.info("==========================>notifyChangeCallback=======================>");
});
```

W
wusongqing 已提交
476
## DataAbilityHelper.insert
W
wusongqing 已提交
477

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

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

W
wusongqing 已提交
482
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
483

W
wusongqing 已提交
484
**Parameters**
W
wusongqing 已提交
485

W
wusongqing 已提交
486
| Name        | Type                  | Mandatory| Description                                                  |
W
wusongqing 已提交
487
| ------------ | ---------------------- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
488 489 490
| 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 已提交
491

W
wusongqing 已提交
492
**Example**
W
wusongqing 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
const valueBucket = {
    "name": "rose",
    "age": 22,
    "salary": 200.5,
    "blobType": u8,
}
DAHelper.insert(
    "dataability:///com.example.DataAbility",
    valueBucket,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
513
## DataAbilityHelper.insert
W
wusongqing 已提交
514

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

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

W
wusongqing 已提交
519
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
520

W
wusongqing 已提交
521
**Parameters**
W
wusongqing 已提交
522

W
wusongqing 已提交
523
| Name        | Type            | Mandatory| Description                                                  |
W
wusongqing 已提交
524
| ------------ | ---------------- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
525 526
| 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 已提交
527

W
wusongqing 已提交
528
**Return value**
W
wusongqing 已提交
529

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

W
wusongqing 已提交
534
**Example**
W
wusongqing 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
const valueBucket = {
    "name": "rose1",
    "age": 221,
    "salary": 20.5,
    "blobType": u8,
}
DAHelper.insert(
    "dataability:///com.example.DataAbility",
    valueBucket
	).then((data) => {
		console.info("==========================>insertCallback=======================>");
});
```

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

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

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

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

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

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

W
wusongqing 已提交
571
**Example**
W
wusongqing 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
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,})
DAHelper.batchInsert(
    "dataability:///com.example.DataAbility",
    cars,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

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

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

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

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

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

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

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

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

W
wusongqing 已提交
610
**Example**
W
wusongqing 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
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,})
DAHelper.batchInsert(
    "dataability:///com.example.DataAbility",
    cars
	).then((data) => {
		console.info("==========================>batchInsertCallback=======================>");
});
```

W
wusongqing 已提交
628
## DataAbilityHelper.delete
W
wusongqing 已提交
629

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

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

W
wusongqing 已提交
634
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
635

W
wusongqing 已提交
636
**Parameters**
W
wusongqing 已提交
637

W
wusongqing 已提交
638
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
639
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
640 641 642
| 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 已提交
643

W
wusongqing 已提交
644
**Example**
W
wusongqing 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

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

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

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

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

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

W
wusongqing 已提交
671
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
672
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
673 674
| 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 已提交
675

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

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

W
wusongqing 已提交
682
**Example**
W
wusongqing 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

```js
import featureAbility from '@ohos.ability.featureAbility'
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 已提交
698
## DataAbilityHelper.update
W
wusongqing 已提交
699

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

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

W
wusongqing 已提交
704
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
705

W
wusongqing 已提交
706
**Parameters**
W
wusongqing 已提交
707

W
wusongqing 已提交
708
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
709
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
710 711 712 713
| 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 已提交
714

W
wusongqing 已提交
715
**Example**
W
wusongqing 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738

```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,
    "blobType": u8,
}
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.update(
    "dataability:///com.example.DataAbility",
    va,
    da,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
739
## DataAbilityHelper.update
W
wusongqing 已提交
740

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

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

W
wusongqing 已提交
745
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
746

W
wusongqing 已提交
747
**Parameters**
W
wusongqing 已提交
748

W
wusongqing 已提交
749
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
750
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
751 752 753
| 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 已提交
754

W
wusongqing 已提交
755
**Return value**
W
wusongqing 已提交
756

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

W
wusongqing 已提交
761
**Example**
W
wusongqing 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784

```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,
    "blobType": u8,
}
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.update(
    "dataability:///com.example.DataAbility",
    va,
    da
	).then((data) => {
		console.info("==========================>updateCallback=======================>");
});
```

W
wusongqing 已提交
785
## DataAbilityHelper.query
W
wusongqing 已提交
786

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

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

W
wusongqing 已提交
791
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
792

W
wusongqing 已提交
793
**Parameters**
W
wusongqing 已提交
794

W
wusongqing 已提交
795
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
796
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
797 798 799 800
| 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 已提交
801

W
wusongqing 已提交
802
**Example**
W
wusongqing 已提交
803 804 805 806 807 808 809

```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 已提交
810
var cars=new Array("value1", "value2", "value3", "value4");
W
wusongqing 已提交
811 812 813 814 815 816 817 818 819 820 821 822
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.query(
    "dataability:///com.example.DataAbility",
    cars,
    da,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```



W
wusongqing 已提交
823
## DataAbilityHelper.query
W
wusongqing 已提交
824

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

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

W
wusongqing 已提交
829
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
830

W
wusongqing 已提交
831
**Parameters**
W
wusongqing 已提交
832

W
wusongqing 已提交
833
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
834
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
835 836 837
| 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 已提交
838

W
wusongqing 已提交
839
**Return value**
W
wusongqing 已提交
840

W
wusongqing 已提交
841
| Type               | Description          |
W
wusongqing 已提交
842 843
| ------------------- | -------------- |
| Promise\<ResultSet> | Promise used to return the data queried.|
W
wusongqing 已提交
844

W
wusongqing 已提交
845
**Example**
W
wusongqing 已提交
846 847 848 849 850 851 852

```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 已提交
853
var cars=new Array("value1", "value2", "value3", "value4");
W
wusongqing 已提交
854 855 856 857 858 859 860 861 862
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.query(
    "dataability:///com.example.DataAbility",
    cars,
    da
	).then((data) => {
		console.info("==========================>queryCallback=======================>");
});
```
W
wusongqing 已提交
863 864 865

## DataAbilityHelper.call

G
ge-yafang 已提交
866
call(uri: string, method: string, arg: string, extras: PacMap): Promise\<PacMap>
W
wusongqing 已提交
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884

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 已提交
885
|Promise\<[PacMap](#pacmap)> | Promise used to return the result.|
W
wusongqing 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901

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

G
ge-yafang 已提交
902
call(uri: string, method: string, arg: string, extras: PacMap, callback: AsyncCallback\<PacMap>): void
W
wusongqing 已提交
903 904 905 906 907 908 909 910 911 912 913 914 915

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.      |
G
ge-yafang 已提交
916
| callback | AsyncCallback\<[PacMap](#pacmap)> | Yes| Callback used to return the result.    |
W
wusongqing 已提交
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936

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