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

W
wusongqing 已提交
3
## Modules to Import
W
wusongqing 已提交
4 5 6 7 8 9 10

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

W
wusongqing 已提交
11
## DataAbilityHelper.openFile
W
wusongqing 已提交
12

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

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

W
wusongqing 已提交
17
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
18

W
wusongqing 已提交
19
**Parameters**
W
wusongqing 已提交
20

W
wusongqing 已提交
21
| Name    | Type                  | Mandatory| Description                              |
W
wusongqing 已提交
22
| -------- | ---------------------- | ---- | ---------------------------------- |
W
wusongqing 已提交
23 24 25
| 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 已提交
26

W
wusongqing 已提交
27
**Example**
W
wusongqing 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

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

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

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

W
wusongqing 已提交
49
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
50

W
wusongqing 已提交
51
**Parameters**
W
wusongqing 已提交
52

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

W
wusongqing 已提交
58
**Return value**
W
wusongqing 已提交
59

W
wusongqing 已提交
60
| Type            | Description            |
W
wusongqing 已提交
61 62
| ---------------- | ---------------- |
| Promise\<number> | Promise used to return the file descriptor.|
W
wusongqing 已提交
63

W
wusongqing 已提交
64
**Example**
W
wusongqing 已提交
65 66 67 68 69 70 71 72 73

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

W
wusongqing 已提交
79
## DataAbilityHelper.on
W
wusongqing 已提交
80

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

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

W
wusongqing 已提交
85
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
86

W
wusongqing 已提交
87
**Parameters**
W
wusongqing 已提交
88

W
wusongqing 已提交
89
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
90
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
91 92 93
| 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 已提交
94

W
wusongqing 已提交
95
**Example**
W
wusongqing 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

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

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

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

W
wusongqing 已提交
118
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
119

W
wusongqing 已提交
120
**Parameters**
W
wusongqing 已提交
121

W
wusongqing 已提交
122
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
123
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
124 125 126
| 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 已提交
127

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

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

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

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

W
wusongqing 已提交
155
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
156

W
wusongqing 已提交
157
**Parameters**
W
wusongqing 已提交
158

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

W
wusongqing 已提交
164
**Example**
W
wusongqing 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177

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

W
wusongqing 已提交
180
getType(uri: string): Promise\<string>
W
wusongqing 已提交
181

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

W
wusongqing 已提交
184
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
185

W
wusongqing 已提交
186
**Parameters**
W
wusongqing 已提交
187

W
wusongqing 已提交
188
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
189
| ---- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
190
| uri  | string | Yes  | URI of the data.|
W
wusongqing 已提交
191

W
wusongqing 已提交
192
**Return value**
W
wusongqing 已提交
193

W
wusongqing 已提交
194
| Type            | Description                               |
W
wusongqing 已提交
195 196
| ---------------- | ----------------------------------- |
| Promise\<string> | Promise used to return the MIME type.|
W
wusongqing 已提交
197

W
wusongqing 已提交
198
**Example**
W
wusongqing 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211

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

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

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

W
wusongqing 已提交
218
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
219

W
wusongqing 已提交
220
**Parameters**
W
wusongqing 已提交
221

W
wusongqing 已提交
222
| Name          | Type                          | Mandatory| Description                              |
W
wusongqing 已提交
223
| -------------- | ------------------------------ | ---- | ---------------------------------- |
W
wusongqing 已提交
224 225 226
| 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 已提交
227

W
wusongqing 已提交
228
**Example**
W
wusongqing 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

```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 已提交
245
## DataAbilityHelper.getFileTypes
W
wusongqing 已提交
246 247

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

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

W
wusongqing 已提交
251
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
252

W
wusongqing 已提交
253
**Parameters**
W
wusongqing 已提交
254

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

W
wusongqing 已提交
260
**Return value**
W
wusongqing 已提交
261

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

W
wusongqing 已提交
266
**Example**
W
wusongqing 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280

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

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

W
wusongqing 已提交
285
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 已提交
286

W
wusongqing 已提交
287
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
288

W
wusongqing 已提交
289
**Parameters**
W
wusongqing 已提交
290

W
wusongqing 已提交
291
| Name    | Type                  | Mandatory| Description                                                        |
W
wusongqing 已提交
292
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
293 294
| 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 已提交
295

W
wusongqing 已提交
296
**Example**
W
wusongqing 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309

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

W
wusongqing 已提交
312
normalizeUri(uri: string): Promise\<string>
W
wusongqing 已提交
313

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

W
wusongqing 已提交
316
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
317

W
wusongqing 已提交
318
**Parameters**
W
wusongqing 已提交
319

W
wusongqing 已提交
320
| Name| Type  | Mandatory| Description                   |
W
wusongqing 已提交
321
| ---- | ------ | ---- | ----------------------- |
W
wusongqing 已提交
322
| uri  | string | Yes  | URI object to normalize.|
W
wusongqing 已提交
323

W
wusongqing 已提交
324
**Return value**
W
wusongqing 已提交
325

W
wusongqing 已提交
326
| Type            | Description                                                  |
W
wusongqing 已提交
327 328
| ---------------- | ------------------------------------------------------ |
| 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 已提交
329

W
wusongqing 已提交
330
**Example**
W
wusongqing 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343

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

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

W
wusongqing 已提交
348
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 已提交
349

W
wusongqing 已提交
350
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
351

W
wusongqing 已提交
352
**Parameters**
W
wusongqing 已提交
353

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

W
wusongqing 已提交
359
**Example**
W
wusongqing 已提交
360 361 362 363 364 365 366

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



W
wusongqing 已提交
375
## DataAbilityHelper.denormalizeUri
W
wusongqing 已提交
376 377

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

W
wusongqing 已提交
379
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 已提交
380

W
wusongqing 已提交
381
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
382

W
wusongqing 已提交
383
**Parameters**
W
wusongqing 已提交
384

W
wusongqing 已提交
385
| Name| Type  | Mandatory| Description                   |
W
wusongqing 已提交
386
| ---- | ------ | ---- | ----------------------- |
W
wusongqing 已提交
387
| uri  | string | Yes  | URI object to normalize.|
W
wusongqing 已提交
388

W
wusongqing 已提交
389
**Return value**
W
wusongqing 已提交
390

W
wusongqing 已提交
391
| Type            | Description                                     |
W
wusongqing 已提交
392 393
| ---------------- | ----------------------------------------- |
| Promise\<string> | Promise used to return the denormalized URI object.|
W
wusongqing 已提交
394

W
wusongqing 已提交
395
**Example**
W
wusongqing 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408

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

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

W
wusongqing 已提交
413
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 已提交
414

W
wusongqing 已提交
415
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
416

W
wusongqing 已提交
417
**Parameters**
W
wusongqing 已提交
418

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

W
wusongqing 已提交
424
**Example**
W
wusongqing 已提交
425 426 427 428 429 430 431

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

W
wusongqing 已提交
438
## DataAbilityHelper.notifyChange
W
wusongqing 已提交
439

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

W
wusongqing 已提交
442
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 已提交
443

W
wusongqing 已提交
444
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
445

W
wusongqing 已提交
446
**Parameters**
W
wusongqing 已提交
447

W
wusongqing 已提交
448
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
449
| ---- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
450
| uri  | string | Yes  | URI of the data.|
W
wusongqing 已提交
451

W
wusongqing 已提交
452
**Return value**
W
wusongqing 已提交
453

W
wusongqing 已提交
454
| Type          | Description                 |
W
wusongqing 已提交
455 456
| -------------- | --------------------- |
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
457

W
wusongqing 已提交
458
**Example**
W
wusongqing 已提交
459 460 461 462 463 464 465 466

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

W
wusongqing 已提交
472
## DataAbilityHelper.insert
W
wusongqing 已提交
473

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

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

W
wusongqing 已提交
478
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
479

W
wusongqing 已提交
480
**Parameters**
W
wusongqing 已提交
481

W
wusongqing 已提交
482
| Name        | Type                  | Mandatory| Description                                                  |
W
wusongqing 已提交
483
| ------------ | ---------------------- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
484 485 486
| 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 已提交
487

W
wusongqing 已提交
488
**Example**
W
wusongqing 已提交
489 490 491 492 493 494 495 496 497 498 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,
    "blobType": u8,
}
DAHelper.insert(
    "dataability:///com.example.DataAbility",
    valueBucket,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
509
## DataAbilityHelper.insert
W
wusongqing 已提交
510

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

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

W
wusongqing 已提交
515
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
516

W
wusongqing 已提交
517
**Parameters**
W
wusongqing 已提交
518

W
wusongqing 已提交
519
| Name        | Type            | Mandatory| Description                                                  |
W
wusongqing 已提交
520
| ------------ | ---------------- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
521 522
| 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 已提交
523

W
wusongqing 已提交
524
**Return value**
W
wusongqing 已提交
525

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

W
wusongqing 已提交
530
**Example**
W
wusongqing 已提交
531 532 533 534 535 536 537 538 539 540 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,
    "blobType": u8,
}
DAHelper.insert(
    "dataability:///com.example.DataAbility",
    valueBucket
	).then((data) => {
		console.info("==========================>insertCallback=======================>");
});
```

W
wusongqing 已提交
551
## DataAbilityHelper.batchInsert
W
wusongqing 已提交
552

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

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

W
wusongqing 已提交
557
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
558

W
wusongqing 已提交
559
**Parameters**
W
wusongqing 已提交
560

W
wusongqing 已提交
561
| Name        | Type                   | Mandatory| Description                            |
W
wusongqing 已提交
562
| ------------ | ----------------------- | ---- | -------------------------------- |
W
wusongqing 已提交
563 564 565
| 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 已提交
566

W
wusongqing 已提交
567
**Example**
W
wusongqing 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

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

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

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

W
wusongqing 已提交
591
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
592

W
wusongqing 已提交
593
**Parameters**
W
wusongqing 已提交
594

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

W
wusongqing 已提交
600
**Return value**
W
wusongqing 已提交
601

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

W
wusongqing 已提交
606
**Example**
W
wusongqing 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

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

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

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

W
wusongqing 已提交
630
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
631

W
wusongqing 已提交
632
**Parameters**
W
wusongqing 已提交
633

W
wusongqing 已提交
634
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
635
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
636 637 638
| 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 已提交
639

W
wusongqing 已提交
640
**Example**
W
wusongqing 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656

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

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

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

W
wusongqing 已提交
663
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
664

W
wusongqing 已提交
665
**Parameters**
W
wusongqing 已提交
666

W
wusongqing 已提交
667
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
668
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
669 670
| 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 已提交
671

W
wusongqing 已提交
672
**Return value**
W
wusongqing 已提交
673

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

W
wusongqing 已提交
678
**Example**
W
wusongqing 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

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

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

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

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

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

W
wusongqing 已提交
704
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
705
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
706 707 708 709
| 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 已提交
710

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

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

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

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

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

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

W
wusongqing 已提交
745
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
746
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
747 748 749
| 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 已提交
750

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

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

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

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

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

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

W
wusongqing 已提交
787
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
788

W
wusongqing 已提交
789
**Parameters**
W
wusongqing 已提交
790

W
wusongqing 已提交
791
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
792
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
793 794 795 796
| 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 已提交
797

W
wusongqing 已提交
798
**Example**
W
wusongqing 已提交
799 800 801 802 803 804 805

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



W
wusongqing 已提交
819
## DataAbilityHelper.query
W
wusongqing 已提交
820

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

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

W
wusongqing 已提交
825
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
826

W
wusongqing 已提交
827
**Parameters**
W
wusongqing 已提交
828

W
wusongqing 已提交
829
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
830
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
831 832 833
| 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 已提交
834

W
wusongqing 已提交
835
**Return value**
W
wusongqing 已提交
836

W
wusongqing 已提交
837
| Type               | Description          |
W
wusongqing 已提交
838 839
| ------------------- | -------------- |
| Promise\<ResultSet> | Promise used to return the data queried.|
W
wusongqing 已提交
840

W
wusongqing 已提交
841
**Example**
W
wusongqing 已提交
842 843 844 845 846 847 848

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

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