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

Z
zengyawen 已提交
3 4 5
> ![icon-note.gif](public_sys-resources/icon-note.gif)**NOTE**
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

W
wusongqing 已提交
6
## Modules to Import
W
wusongqing 已提交
7 8 9 10 11 12 13

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

```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 已提交
512
## DataAbilityHelper.insert
W
wusongqing 已提交
513

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

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

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

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

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

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

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

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

```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 已提交
554
## DataAbilityHelper.batchInsert
W
wusongqing 已提交
555

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

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

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

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

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

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

```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 已提交
588
## DataAbilityHelper.batchInsert
W
wusongqing 已提交
589

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

```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 已提交
697
## DataAbilityHelper.update
W
wusongqing 已提交
698

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

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

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

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

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

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

```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 已提交
738
## DataAbilityHelper.update
W
wusongqing 已提交
739

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

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

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

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

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

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

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

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

```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 已提交
784
## DataAbilityHelper.query
W
wusongqing 已提交
785

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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

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

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

## DataAbilityHelper.call

G
ge-yafang 已提交
865
call(uri: string, method: string, arg: string, extras: PacMap): Promise\<PacMap>
W
wusongqing 已提交
866 867 868 869 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

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

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

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

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