js-apis-inner-ability-dataAbilityResult.md 2.8 KB
Newer Older
1 2
# DataAbilityResult

3
The **DataAbilityResult** module defines the operation result on DataAbilities. When you call [executeBatch](js-apis-inner-ability-dataAbilityHelper.md#dataabilityhelperexecutebatch) to operate the database, the operation result is returned through the **DataAbilityResult** object.
4 5 6 7 8 9

> **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.
> The APIs of this module can be used only in the FA model.

10 11 12 13 14 15
## Modules to Import

```ts
import ability from '@ohos.ability.ability';
```

16 17 18 19
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel

| Name      | Type |     Mandatory   |       Description  |
| --------  | --------  | --------    | --------    |
20
| uri?      | string    |      No   | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'. |
21
| count?     | number    |      No   | Number of rows affected by the operation. |
22 23

**Example**
24

25
```ts
26
import featureAbility from '@ohos.ability.featureAbility';
27

28 29
// Perform database operations in batches.
function executeBatchOperation() {
30
    let dataAbilityUri = ('dataability:///com.example.myapplication.TestDataAbility');
31
    let DAHelper: ability.DataAbilityHelper;
32 33
    try {
        DAHelper = featureAbility.acquireDataAbilityHelper(dataAbilityUri);
34
        if (DAHelper === null) {
35 36 37
            console.error('DAHelper is null');
        }
    } catch (err) {
38
        console.error('acquireDataAbilityHelper fail, error: ${JSON.stringify(err)}');
39 40
    }

41
    let valueBucket = {
42 43 44
        'name': 'DataAbilityHelperTest',
        'age': 24,
        'salary': 2024.20,
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    };
    let operations = [
    {
        uri: dataAbilityUri,
        type: featureAbility.DataAbilityOperationType.TYPE_INSERT,
        valuesBucket: valueBucket,
        predicates: null,
        expectedCount: 1,
        PredicatesBackReferences: {},
        interrupted: true,
    },
    {
        uri: dataAbilityUri,
        type: featureAbility.DataAbilityOperationType.TYPE_INSERT,
        valuesBucket: valueBucket,
        predicates: null,
        expectedCount: 1,
        PredicatesBackReferences: {},
        interrupted: true,
    }
    ];
66

67 68 69
    try {
        DAHelper.executeBatch(dataAbilityUri, operations).then((data) => {
            for (let i = 0; i < data.length; i++) {
70
                let dataAbilityResult: ability.DataAbilityResult = data[i];
71 72
                console.log('dataAbilityResult.uri: ${dataAbilityResult.uri}');
                console.log('dataAbilityResult.count: ${dataAbilityResult.count}');
73 74
            }
        }).catch(err => {
75
            console.error('executeBatch error: ${JSON.stringify(err)}');
76 77
        });
    } catch (err) {
78
        console.error('executeBatch error: ${JSON.stringify(err)}');
79
    }
80 81
}
```