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 15
| uri?      | string    |      No   | URI of the DataAbility. Example: "dataability:///com.example.xxx.xxxx". |
| count?     | number    |      No   | Number of rows affected by the operation. |
16 17

**Example**
18

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

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

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

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