js-apis-dataAbilityHelper.md 29.8 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
W
wusongqing 已提交
12

W
wusongqing 已提交
13
### openFile
W
wusongqing 已提交
14

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

W
wusongqing 已提交
17
Opens a file with a specified URI. This method uses a callback to return the result.
W
wusongqing 已提交
18

W
wusongqing 已提交
19 20 21 22
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

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
### 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 method uses a promise to return the result.
W
wusongqing 已提交
52

W
wusongqing 已提交
53 54 55 56
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
57
**Parameters**
W
wusongqing 已提交
58

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

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

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

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

W
wusongqing 已提交
84
### on('dataChange')
W
wusongqing 已提交
85

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

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

W
wusongqing 已提交
90 91 92 93
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
94
**Parameters**
W
wusongqing 已提交
95

W
wusongqing 已提交
96
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
97
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
98 99 100
| 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 已提交
101

W
wusongqing 已提交
102
**Example**
W
wusongqing 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

```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 已提交
119
### off('dataChange')
W
wusongqing 已提交
120

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

W
wusongqing 已提交
123
Unregisters the observer used to observe data specified by a given URI. This method uses a callback to return the result.
W
wusongqing 已提交
124

W
wusongqing 已提交
125 126 127 128
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

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

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

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

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

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

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

W
wusongqing 已提交
164 165 166 167
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

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

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

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

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

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

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

W
wusongqing 已提交
195 196 197 198
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

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

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

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

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

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

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

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

W
wusongqing 已提交
228
Obtains the supported MIME types of a specified file. This method uses a callback to return the result.
W
wusongqing 已提交
229

W
wusongqing 已提交
230 231 232 233
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
234
**Parameters**
W
wusongqing 已提交
235

W
wusongqing 已提交
236
| Name          | Type                          | Mandatory| Description                              |
W
wusongqing 已提交
237
| -------------- | ------------------------------ | ---- | ---------------------------------- |
W
wusongqing 已提交
238 239 240
| 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 已提交
241

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

```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 已提交
259 260 261
### getFileTypes

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

W
wusongqing 已提交
263
Obtains the supported MIME types of a specified file. This method uses a promise to return the result.
W
wusongqing 已提交
264

W
wusongqing 已提交
265 266 267 268
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
269
**Parameters**
W
wusongqing 已提交
270

W
wusongqing 已提交
271
| Name          | Type  | Mandatory| Description                        |
W
wusongqing 已提交
272
| -------------- | ------ | ---- | ---------------------------- |
W
wusongqing 已提交
273 274
| uri            | string | Yes  | URI of the file.    |
| mimeTypeFilter | string | Yes  | MIME type of the file.|
W
wusongqing 已提交
275

W
wusongqing 已提交
276
**Return value**
W
wusongqing 已提交
277
| Type                    | Description                    |
W
wusongqing 已提交
278 279
| ------------------------ | ------------------------ |
| Promise\<Array\<string>> | Promise used to return the supported MIME types.|
W
wusongqing 已提交
280

W
wusongqing 已提交
281
**Example**
W
wusongqing 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295

```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 已提交
296
### normalizeUri
W
wusongqing 已提交
297

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

W
wusongqing 已提交
300
Converts the URI that refers to a Data ability into a normalized URI. This method uses a callback to return the result.
W
wusongqing 已提交
301

W
wusongqing 已提交
302 303 304 305
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
306
**Parameters**
W
wusongqing 已提交
307

W
wusongqing 已提交
308
| Name    | Type                  | Mandatory| Description                                                        |
W
wusongqing 已提交
309
| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
310 311
| 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 已提交
312

W
wusongqing 已提交
313
**Example**
W
wusongqing 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326

```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 已提交
327
### normalizeUri
W
wusongqing 已提交
328

W
wusongqing 已提交
329
normalizeUri(uri: string): Promise\<string>
W
wusongqing 已提交
330

W
wusongqing 已提交
331
Converts the URI that refers to a Data ability into a normalized URI. This method uses a promise to return the result.
W
wusongqing 已提交
332

W
wusongqing 已提交
333 334 335 336
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
337
**Parameters**
W
wusongqing 已提交
338

W
wusongqing 已提交
339
| Name| Type  | Mandatory| Description                   |
W
wusongqing 已提交
340
| ---- | ------ | ---- | ----------------------- |
W
wusongqing 已提交
341
| uri  | string | Yes  | URI object to normalize.|
W
wusongqing 已提交
342

W
wusongqing 已提交
343
**Return value**
W
wusongqing 已提交
344
| Type            | Description                                                  |
W
wusongqing 已提交
345 346
| ---------------- | ------------------------------------------------------ |
| 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 已提交
347

W
wusongqing 已提交
348
**Example**
W
wusongqing 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361

```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 已提交
362
### denormalizeUri
W
wusongqing 已提交
363

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

W
wusongqing 已提交
366
Converts a normalized URI generated by **DataAbilityHelper.normalizeUri(uri: string, callback: AsyncCallback\<string>)** to a denormalized one. This method uses a callback to return the result.
W
wusongqing 已提交
367

W
wusongqing 已提交
368 369 370 371
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
372
**Parameters**
W
wusongqing 已提交
373

W
wusongqing 已提交
374
| Name    | Type                  | Mandatory| Description                                               |
W
wusongqing 已提交
375
| -------- | ---------------------- | ---- | --------------------------------------------------- |
W
wusongqing 已提交
376 377
| uri      | string                 | Yes  | URI object to normalize.                            |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the denormalized URI object.|
W
wusongqing 已提交
378

W
wusongqing 已提交
379
**Example**
W
wusongqing 已提交
380 381 382 383 384 385 386

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.denormalizeUri(
W
wusongqing 已提交
387
    "dataability:///com.example.DataAbility",
W
wusongqing 已提交
388 389 390 391 392 393 394
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```



W
wusongqing 已提交
395 396 397
### denormalizeUri

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

W
wusongqing 已提交
399
Converts a normalized URI generated by **DataAbilityHelper.normalizeUri(uri: string)** to a denormalized one. This method uses a promise to return the result.
W
wusongqing 已提交
400

W
wusongqing 已提交
401 402 403 404
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
405
**Parameters**
W
wusongqing 已提交
406

W
wusongqing 已提交
407
| Name| Type  | Mandatory| Description                   |
W
wusongqing 已提交
408
| ---- | ------ | ---- | ----------------------- |
W
wusongqing 已提交
409
| uri  | string | Yes  | URI object to normalize.|
W
wusongqing 已提交
410

W
wusongqing 已提交
411
**Return value**
W
wusongqing 已提交
412
| Type            | Description                                     |
W
wusongqing 已提交
413 414
| ---------------- | ----------------------------------------- |
| Promise\<string> | Promise used to return the denormalized URI object.|
W
wusongqing 已提交
415

W
wusongqing 已提交
416
**Example**
W
wusongqing 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429

```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 已提交
430
### notifyChange
W
wusongqing 已提交
431

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

W
wusongqing 已提交
434
Notifies the registered observer of a change to the data specified by the URI. This method uses a callback to return the result.
W
wusongqing 已提交
435

W
wusongqing 已提交
436 437 438 439
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
440
**Parameters**
W
wusongqing 已提交
441

W
wusongqing 已提交
442
| Name    | Type                | Mandatory| Description                    |
W
wusongqing 已提交
443
| -------- | -------------------- | ---- | ------------------------ |
W
wusongqing 已提交
444 445
| uri      | string               | Yes  | URI of the data.|
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.              |
W
wusongqing 已提交
446

W
wusongqing 已提交
447
**Example**
W
wusongqing 已提交
448 449 450 451 452 453 454

```js
import featureAbility from '@ohos.ability.featureAbility'
var helper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
helper.notifyChange(
W
wusongqing 已提交
455
    "dataability:///com.example.DataAbility",
W
wusongqing 已提交
456 457 458 459 460
    (err) => {
		console.info("==========================>Called=======================>");
});
```

W
wusongqing 已提交
461
### notifyChange
W
wusongqing 已提交
462

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

W
wusongqing 已提交
465
Notifies the registered observer of a change to the data specified by the URI. This method uses a promise to return the result.
W
wusongqing 已提交
466

W
wusongqing 已提交
467 468 469 470
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
471
**Parameters**
W
wusongqing 已提交
472

W
wusongqing 已提交
473
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
474
| ---- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
475
| uri  | string | Yes  | URI of the data.|
W
wusongqing 已提交
476

W
wusongqing 已提交
477
**Return value**
W
wusongqing 已提交
478
| Type          | Description                 |
W
wusongqing 已提交
479 480
| -------------- | --------------------- |
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
481

W
wusongqing 已提交
482
**Example**
W
wusongqing 已提交
483 484 485 486 487 488 489 490

```js
import featureAbility from '@ohos.ability.featureAbility'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
DAHelper.notifyChange(
    "dataability:///com.example.DataAbility",
W
wusongqing 已提交
491
	).then(() => {
W
wusongqing 已提交
492 493 494 495
		console.info("==========================>notifyChangeCallback=======================>");
});
```

W
wusongqing 已提交
496
### insert
W
wusongqing 已提交
497

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

W
wusongqing 已提交
500
Inserts a single data record into the database. This method uses a callback to return the result.
W
wusongqing 已提交
501

W
wusongqing 已提交
502 503 504 505
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
506
**Parameters**
W
wusongqing 已提交
507

W
wusongqing 已提交
508
| Name        | Type                  | Mandatory| Description                                                  |
W
wusongqing 已提交
509
| ------------ | ---------------------- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
510 511 512
| 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 已提交
513

W
wusongqing 已提交
514
**Example**
W
wusongqing 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

```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 已提交
535
### insert
W
wusongqing 已提交
536

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

W
wusongqing 已提交
539
Inserts a single data record into the database. This method uses a promise to return the result.
W
wusongqing 已提交
540

W
wusongqing 已提交
541 542 543 544
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
545
**Parameters**
W
wusongqing 已提交
546

W
wusongqing 已提交
547
| Name        | Type            | Mandatory| Description                                                  |
W
wusongqing 已提交
548
| ------------ | ---------------- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
549 550
| 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 已提交
551

W
wusongqing 已提交
552
**Return value**
W
wusongqing 已提交
553
| Type            | Description                    |
W
wusongqing 已提交
554 555
| ---------------- | ------------------------ |
| Promise\<number> | Promise used to return the index of the inserted data record.|
W
wusongqing 已提交
556

W
wusongqing 已提交
557
**Example**
W
wusongqing 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

```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 已提交
578
### batchInsert
W
wusongqing 已提交
579

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

W
wusongqing 已提交
582
Inserts multiple data records into the database. This method uses a callback to return the result.
W
wusongqing 已提交
583

W
wusongqing 已提交
584 585 586 587
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
588
**Parameters**
W
wusongqing 已提交
589

W
wusongqing 已提交
590
| Name        | Type                   | Mandatory| Description                            |
W
wusongqing 已提交
591
| ------------ | ----------------------- | ---- | -------------------------------- |
W
wusongqing 已提交
592 593 594
| 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 已提交
595

W
wusongqing 已提交
596
**Example**
W
wusongqing 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

```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 已提交
614
### batchInsert
W
wusongqing 已提交
615

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

W
wusongqing 已提交
618
Inserts multiple data records into the database. This method uses a promise to return the result.
W
wusongqing 已提交
619

W
wusongqing 已提交
620 621 622 623
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
624
**Parameters**
W
wusongqing 已提交
625

W
wusongqing 已提交
626
| Name        | Type                   | Mandatory| Description                    |
W
wusongqing 已提交
627
| ------------ | ----------------------- | ---- | ------------------------ |
W
wusongqing 已提交
628 629
| uri          | string                  | Yes  | URI of the data to insert.|
| valuesBucket | Array<rdb.ValuesBucket> | Yes  | Data record to insert.  |
W
wusongqing 已提交
630

W
wusongqing 已提交
631
**Return value**
W
wusongqing 已提交
632
| Type            | Description                  |
W
wusongqing 已提交
633 634
| ---------------- | ---------------------- |
| Promise\<number> | Promise used to return the number of inserted data records.|
W
wusongqing 已提交
635

W
wusongqing 已提交
636
**Example**
W
wusongqing 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

```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 已提交
654
### delete
W
wusongqing 已提交
655

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

W
wusongqing 已提交
658
Deletes one or more data records from the database. This method uses a callback to return the result.
W
wusongqing 已提交
659

W
wusongqing 已提交
660 661 662 663
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

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

W
wusongqing 已提交
666
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
667
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
668 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**.|
| callback     | AsyncCallback\<number>            | Yes  | Callback used to return the number of deleted data records.              |
W
wusongqing 已提交
671

W
wusongqing 已提交
672
**Example**
W
wusongqing 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

```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 已提交
689
### delete
W
wusongqing 已提交
690

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

W
wusongqing 已提交
693
Deletes one or more data records from the database. This method uses a promise to return the result.
W
wusongqing 已提交
694

W
wusongqing 已提交
695 696 697 698
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
699
**Parameters**
W
wusongqing 已提交
700

W
wusongqing 已提交
701
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
702
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
703 704
| 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 已提交
705

W
wusongqing 已提交
706
**Return value**
W
wusongqing 已提交
707
| Type            | Description                    |
W
wusongqing 已提交
708 709
| ---------------- | ------------------------ |
| Promise\<number> | Promise used to return the number of deleted 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

```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 已提交
727
### update
W
wusongqing 已提交
728

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

W
wusongqing 已提交
731
Updates data records in the database. This method uses a callback to return the result.
W
wusongqing 已提交
732

W
wusongqing 已提交
733 734 735 736
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
737
**Parameters**
W
wusongqing 已提交
738

W
wusongqing 已提交
739
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
740
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
741 742 743 744
| 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 已提交
745

W
wusongqing 已提交
746
**Example**
W
wusongqing 已提交
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

```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 已提交
770
### update
W
wusongqing 已提交
771

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

W
wusongqing 已提交
774
Updates data records in the database. This method uses a promise to return the result.
W
wusongqing 已提交
775

W
wusongqing 已提交
776 777 778 779
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
780
**Parameters**
W
wusongqing 已提交
781

W
wusongqing 已提交
782
| Name        | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
783
| ------------ | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
784 785 786
| 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 已提交
787

W
wusongqing 已提交
788
**Return value**
W
wusongqing 已提交
789
| Type            | Description                                        |
W
wusongqing 已提交
790 791
| ---------------- | -------------------------------------------- |
| Promise\<number> | Promise used to return the number of updated data records.|
W
wusongqing 已提交
792

W
wusongqing 已提交
793
**Example**
W
wusongqing 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816

```js
import featureAbility from '@ohos.ability.featureAbility'
import ohos_data_ability from '@ohos.data.dataability'
var DAHelper = featureAbility.acquireDataAbilityHelper(
    "dataability:///com.example.DataAbility"
);
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 已提交
817
### query
W
wusongqing 已提交
818

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

W
wusongqing 已提交
821
Queries data in the database. This method uses a callback to return the result.
W
wusongqing 已提交
822

W
wusongqing 已提交
823 824 825 826
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

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

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

W
wusongqing 已提交
836
**Example**
W
wusongqing 已提交
837 838 839 840 841 842 843

```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 已提交
844
var cars=new Array("value1", "value2", "value3", "value4");
W
wusongqing 已提交
845 846 847 848 849 850 851 852 853 854 855 856
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.query(
    "dataability:///com.example.DataAbility",
    cars,
    da,
    (err, data) => {
		console.info("==========================>Called=======================>");
});
```



W
wusongqing 已提交
857
### query
W
wusongqing 已提交
858

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

W
wusongqing 已提交
861
Queries data in the database. This method uses a promise to return the result.
W
wusongqing 已提交
862

W
wusongqing 已提交
863 864 865 866
**System capabilities**

SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
867
**Parameters**
W
wusongqing 已提交
868

W
wusongqing 已提交
869
| Name      | Type                             | Mandatory| Description                                            |
W
wusongqing 已提交
870
| ---------- | --------------------------------- | ---- | ------------------------------------------------ |
W
wusongqing 已提交
871 872 873
| 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 已提交
874

W
wusongqing 已提交
875
**Return value**
W
wusongqing 已提交
876
| Type               | Description          |
W
wusongqing 已提交
877 878
| ------------------- | -------------- |
| Promise\<ResultSet> | Promise used to return the data queried.|
W
wusongqing 已提交
879

W
wusongqing 已提交
880
**Example**
W
wusongqing 已提交
881 882 883 884 885 886 887

```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 已提交
888
var cars=new Array("value1", "value2", "value3", "value4");
W
wusongqing 已提交
889 890 891 892 893 894 895 896 897
let da = new ohos_data_ability.DataAbilityPredicates()
DAHelper.query(
    "dataability:///com.example.DataAbility",
    cars,
    da
	).then((data) => {
		console.info("==========================>queryCallback=======================>");
});
```