js-apis-inner-ability-dataAbilityResult.md 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
# DataAbilityResult

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.

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

G
Gloria 已提交
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 22 23 24 25
| count?     | number    |      No   | Number of rows affected by the operation. |

**Example**

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

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

G
Gloria 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    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,
    }
    ];
66

G
Gloria 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79
    try {
        DAHelper.executeBatch(dataAbilityUri, operations).then((data) => {
            for (let i = 0; i < data.length; i++) {
                let dataAbilityResult: ability.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) {
        console.error('executeBatch error: ${JSON.stringify(err)}');
    }
80 81
}
```