Accessing a DataAbility
To access a DataAbility, import the basic dependency packages and obtain the URI string for communicating with the DataAbility.
The basic dependency packages include:
-
@ohos.ability.featureAbility
-
@ohos.data.dataAbility
-
@ohos.data.relationalStore
The sample code for accessing a DataAbility is as follows:
-
Create a DataAbilityHelper instance.
// Different from the URI defined in the config.json file, the URI passed in the parameter has an extra slash (/), three slashes in total. import featureAbility from '@ohos.ability.featureAbility' import ohos_data_ability from '@ohos.data.dataAbility' import relationalStore from '@ohos.data.relationalStore' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
-
Construct RDB data.
import ohos_data_ability from '@ohos.data.dataAbility' import rdb from '@ohos.data.rdb' let valuesBucket: rdb.ValuesBucket = {"name": "gaolu"} let da = new ohos_data_ability.DataAbilityPredicates() let valArray = new Array("value1"); let cars = new Array({"batchInsert1" : "value1",} as rdb.ValuesBucket);
For details about DataAbilityPredicates, see DataAbility Predicates.
-
Use insert to insert data to the DataAbility.
import { BusinessError } from '@ohos.base'; // Callback mode: DAHelper.insert( urivar, valuesBucket, (error: BusinessError, data: number) => { console.info("DAHelper insert result: " + data) } );
import featureAbility from '@ohos.ability.featureAbility' import { BusinessError } from '@ohos.base'; let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Promise mode (await needs to be used in the asynchronous method): let datainsert = await DAHelper.insert(urivar, valuesBucket).then((data) => { console.info("insert success."); }).catch((error: BusinessError) => { console.error("insert failed."); });
-
Use delete to delete data from the DataAbility.
import featureAbility from '@ohos.ability.featureAbility' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Callback mode: DAHelper.delete( urivar, da, (error, data) => { console.info("DAHelper delete result: " + data) } );
import featureAbility from '@ohos.ability.featureAbility' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Promise mode (await needs to be used in the asynchronous method): let datadelete = await DAHelper.delete( urivar, da, );
-
Use update to update data in the DataAbility.
import featureAbility from '@ohos.ability.featureAbility' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Callback mode: DAHelper.update( urivar, valuesBucket, da, (error, data) => { console.info("DAHelper update result: " + data) } );
import featureAbility from '@ohos.ability.featureAbility' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Promise mode (await needs to be used in the asynchronous method): let dataupdate = await DAHelper.update( urivar, valuesBucket, da, );
-
Use query to query data in the DataAbility.
import featureAbility from '@ohos.ability.featureAbility' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Callback mode: DAHelper.query( urivar, valArray, da, (error, data) => { console.info("DAHelper query result: " + data) } );
import featureAbility from '@ohos.ability.featureAbility' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Promise mode (await needs to be used in the asynchronous method): let dataquery = await DAHelper.query( urivar, valArray, da );
-
Use batchInsert to insert data in batches to the DataAbility.
import featureAbility from '@ohos.ability.featureAbility' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Callback mode: DAHelper.batchInsert( urivar, cars, (error, data) => { console.info("DAHelper batchInsert result: " + data) } );
import featureAbility from '@ohos.ability.featureAbility' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Promise mode (await needs to be used in the asynchronous method): let databatchInsert = await DAHelper.batchInsert( urivar, cars );
-
Use executeBatch to process data in batches in the DataAbility.
import featureAbility from '@ohos.ability.featureAbility' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Callback mode: DAHelper.executeBatch( urivar, [ { uri: urivar, type: featureAbility.DataAbilityOperationType.TYPE_INSERT, valuesBucket: {"executeBatch" : "value1",}, predicates: da, expectedCount:0, predicatesBackReferences: undefined, interrupted:true, } ], (error, data) => { console.info("DAHelper executeBatch result: " + data) } );
import featureAbility from '@ohos.ability.featureAbility' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar); // Promise mode (await needs to be used in the asynchronous method): let dataexecuteBatch = await DAHelper.executeBatch( urivar, [ { uri: urivar, type: featureAbility.DataAbilityOperationType.TYPE_INSERT, valuesBucket: { "executeBatch" : "value1", }, predicates: da, expectedCount:0, predicatesBackReferences: undefined, interrupted:true, } ] );
The APIs for operating a DataAbility are provided by DataAbilityHelper. For details about the APIs, see DataAbilityHelper.