js-apis-inner-ability-dataAbilityResult.md 2.7 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 10 11 12 13

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

**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel

| Name      | Type |     Mandatory   |       Description  |
| --------  | --------  | --------    | --------    |
14
| uri?      | string    |      No   | URI of the DataAbility. Example: 'dataability:///com.example.xxx.xxxx'. |
15
| count?     | number    |      No   | Number of rows affected by the operation. |
16 17

**Example**
18

19
```ts
20
import featureAbility from '@ohos.ability.featureAbility';
21

22 23
// Perform database operations in batches.
function executeBatchOperation() {
24
    let dataAbilityUri = ('dataability:///com.example.myapplication.TestDataAbility');
25 26 27
    let DAHelper;
    try {
        DAHelper = featureAbility.acquireDataAbilityHelper(dataAbilityUri);
28
        if (DAHelper === null) {
29 30 31
            console.error('DAHelper is null');
        }
    } catch (err) {
32
        console.error('acquireDataAbilityHelper fail, error: ${JSON.stringify(err)}');
33 34
    }

35
    let valueBucket = {
36 37 38
        'name': 'DataAbilityHelperTest',
        'age': 24,
        'salary': 2024.20,
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    };
    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,
    }
    ];
60

61 62 63 64
    try {
        DAHelper.executeBatch(dataAbilityUri, operations).then((data) => {
            for (let i = 0; i < data.length; i++) {
                let dataAbilityResult = data[i];
65 66
                console.log('dataAbilityResult.uri: ${dataAbilityResult.uri}');
                console.log('dataAbilityResult.count: ${dataAbilityResult.count}');
67 68
            }
        }).catch(err => {
69
            console.error('executeBatch error: ${JSON.stringify(err)}');
70 71
        });
    } catch (err) {
72
        console.error('executeBatch error: ${JSON.stringify(err)}');
73
    }
74 75
}
```