js-apis-inner-ability-dataAbilityResult.md 2.7 KB
Newer Older
Z
zengyawen 已提交
1
# DataAbilityResult
M
m00512953 已提交
2

D
dy_study 已提交
3
定义DataAbility数据操作结果,通过[executeBatch](js-apis-inner-ability-dataAbilityHelper.md#dataabilityhelperexecutebatch)操作数据库时,操作结果使用DataAbilityResult对象返回。
M
m00512953 已提交
4 5 6 7 8 9

> **说明:**
> 
> 本接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
> 本接口仅可在FA模型下使用

10 11 12 13 14 15
## 导入模块

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

M
m00512953 已提交
16 17
**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.FAModel

D
merge  
donglin 已提交
18 19
| 名称       | 类型  |     必填    |       说明   |
| --------  | --------  | --------    | --------    |
M
mingxihua 已提交
20
| uri?      | string    |      否    | 指示待处理的DataAbility。例:'dataability:///com.example.xxx.xxxx'。  |
D
dy_study 已提交
21
| count?     | number    |      否    | 指示受操作影响的数据数量。  |
M
m00512953 已提交
22 23

**示例:**
Z
zhongjianfei 已提交
24

M
m00512953 已提交
25
```ts
M
mingxihua 已提交
26
import featureAbility from '@ohos.ability.featureAbility';
M
m00512953 已提交
27

28 29
// 批量执行数据库操作
function executeBatchOperation() {
M
mingxihua 已提交
30
    let dataAbilityUri = ('dataability:///com.example.myapplication.TestDataAbility');
31
    let DAHelper: ability.DataAbilityHelper;
32 33
    try {
        DAHelper = featureAbility.acquireDataAbilityHelper(dataAbilityUri);
M
mingxihua 已提交
34
        if (DAHelper === null) {
35 36 37
            console.error('DAHelper is null');
        }
    } catch (err) {
M
mingxihua 已提交
38
        console.error('acquireDataAbilityHelper fail, error: ${JSON.stringify(err)}');
M
m00512953 已提交
39 40
    }

41
    let valueBucket = {
M
mingxihua 已提交
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,
    }
    ];
M
m00512953 已提交
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];
M
mingxihua 已提交
71 72
                console.log('dataAbilityResult.uri: ${dataAbilityResult.uri}');
                console.log('dataAbilityResult.count: ${dataAbilityResult.count}');
73 74
            }
        }).catch(err => {
M
mingxihua 已提交
75
            console.error('executeBatch error: ${JSON.stringify(err)}');
76 77
        });
    } catch (err) {
M
mingxihua 已提交
78
        console.error('executeBatch error: ${JSON.stringify(err)}');
79
    }
M
m00512953 已提交
80 81
}
```