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.rdb
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 ohos_data_rdb from '@ohos.data.rdb' let urivar = "dataability:///com.ix.DataAbility" let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
-
Construct RDB data.
let valuesBucket = {"name": "gaolu"} let da = new ohos_data_ability.DataAbilityPredicates() let valArray =new Array("value1"); let cars = new Array({"batchInsert1" : "value1",});
For details about DataAbilityPredicates, see DataAbility Predicates.
-
Use insert to insert data to the DataAbility.
// Callback mode: DAHelper.insert( urivar, valuesBucket, (error, data) => { console.info("DAHelper insert result: " + data) } );
// 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) => { console.error("insert failed."); });
-
Use delete to delete data from the DataAbility.
// Callback mode: DAHelper.delete( urivar, da, (error, data) => { console.info("DAHelper delete result: " + data) } );
// 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.
// Callback mode: DAHelper.update( urivar, valuesBucket, da, (error, data) => { console.info("DAHelper update result: " + data) } );
// 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.
// Callback mode: DAHelper.query( urivar, valArray, da, (error, data) => { console.info("DAHelper query result: " + data) } );
// 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.
// Callback mode: DAHelper.batchInsert( urivar, cars, (error, data) => { console.info("DAHelper batchInsert result: " + data) } );
// 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.
// Callback mode: DAHelper.executeBatch( urivar, [ { uri: urivar, type: featureAbility.DataAbilityOperationType.TYPE_INSERT, valuesBucket: {"executeBatch" : "value1",}, predicates: da, expectedCount:0, predicatesBackReferences: null, interrupted:true, } ], (error, data) => { console.info("DAHelper executeBatch result: " + data) } );
// 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: null, interrupted:true, } ] );
The APIs for operating a DataAbility are provided by DataAbilityHelper. For details about the APIs, see DataAbilityHelper.