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.
import featureAbility from '@ohos.ability.featureAbility'; import ohos_data_ability from '@ohos.data.dataAbility'; import relationalStore from '@ohos.data.relationalStore'; import ability from '@ohos.ability.ability'; // Different from the URI defined in the config.json file, the URI passed in the parameter has an extra slash (/), three slashes in total. let uri: string = 'dataability:///com.samples.famodelabilitydevelop.DataAbility'; let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(uri);
-
Construct RDB data.
import ohos_data_ability from '@ohos.data.dataAbility'; import rdb from '@ohos.data.rdb'; let valuesBucket_insert: rdb.ValuesBucket = { name: 'Rose', introduction: 'insert' }; let valuesBucket_update: rdb.ValuesBucket = { name: 'Rose', introduction: 'update' }; let crowd = new Array({ name: 'Rose', introduction: 'batchInsert_one' } as rdb.ValuesBucket, { name: 'Rose', introduction: 'batchInsert_two' } as rdb.ValuesBucket); let columnArray = new Array('id', 'name', 'introduction'); let predicates = new ohos_data_ability.DataAbilityPredicates();
For details about DataAbilityPredicates, see DataAbility Predicates.
-
Use insert to insert data to the DataAbility.
import { BusinessError } from '@ohos.base'; import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Callback mode: this.DAHelper.insert(this.uri, this.valuesBucket_insert, (error: BusinessError, data: number) => { if (error && error.code !== 0) { promptAction.showToast({ message: 'insert_failed_toast' }); } else { promptAction.showToast({ message: 'insert_success_toast' }); } Logger.info(TAG, 'DAHelper insert result: ' + data + ', error: ' + JSON.stringify(error)); } );
import featureAbility from '@ohos.ability.featureAbility' import { BusinessError } from '@ohos.base'; import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Promise mode (await needs to be used in the asynchronous method): this.DAHelper.insert(this.uri, this.valuesBucket_insert).then((datainsert) => { promptAction.showToast({ message: 'insert_success_toast' }); Logger.info(TAG, 'DAHelper insert result: ' + datainsert); }).catch((error: BusinessError) => { promptAction.showToast({ message: 'insert_success_toast' }); Logger.error(TAG, `DAHelper insert failed. Cause: ${error.message}`); });
-
Use delete to delete data from the DataAbility.
import featureAbility from '@ohos.ability.featureAbility' import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Callback mode: this.DAHelper.delete(this.uri, this.predicates, (error, data) => { if (error && error.code !== 0) { promptAction.showToast({ message: 'delete_failed_toast' }); } else { promptAction.showToast({ message: 'delete_success_toast' }); } Logger.info(TAG, 'DAHelper delete result: ' + data + ', error: ' + JSON.stringify(error)); } );
import featureAbility from '@ohos.ability.featureAbility' import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Promise mode (await needs to be used in the asynchronous method): this.DAHelper.delete(this.uri, this.predicates).then((datadelete) => { promptAction.showToast({ message: 'delete_success_toast' }); Logger.info(TAG, 'DAHelper delete result: ' + datadelete); }).catch((error: BusinessError) => { promptAction.showToast({ message: 'delete_failed_toast' }); Logger.error(TAG, `DAHelper delete failed. Cause: ${error.message}`); });
-
Use update to update data in the DataAbility.
import featureAbility from '@ohos.ability.featureAbility' import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Callback mode: this.predicates.equalTo('name', 'Rose'); this.DAHelper.update(this.uri, this.valuesBucket_update, this.predicates, (error, data) => { if (error && error.code !== 0) { promptAction.showToast({ message: 'update_failed_toast' }); } else { promptAction.showToast({ message: 'update_success_toast' }); } Logger.info(TAG, 'DAHelper update result: ' + data + ', error: ' + JSON.stringify(error)); } );
import featureAbility from '@ohos.ability.featureAbility' import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Promise mode (await needs to be used in the asynchronous method): this.predicates.equalTo('name', 'Rose'); this.DAHelper.update(this.uri, this.valuesBucket_update, this.predicates).then((dataupdate) => { promptAction.showToast({ message: 'update_success_toast' }); Logger.info(TAG, 'DAHelper update result: ' + dataupdate); }).catch((error: BusinessError) => { promptAction.showToast({ message: 'update_failed_toast' }); Logger.error(TAG, `DAHelper update failed. Cause: ${error.message}`); });
-
Use query to query data in the DataAbility.
import featureAbility from '@ohos.ability.featureAbility' import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Callback mode: this.predicates.equalTo('name', 'Rose'); this.DAHelper.query(this.uri, this.columnArray, this.predicates, (error, data) => { if (error && error.code !== 0) { promptAction.showToast({ message: 'query_failed_toast' }); Logger.error(TAG, `DAHelper query failed. Cause: ${error.message}`); } else { promptAction.showToast({ message: 'query_success_toast' }); } // ResultSet is a cursor of a data set. By default, the cursor points to the -1st record. Valid data starts from 0. while (data.goToNextRow()) { const id = data.getLong(data.getColumnIndex('id')); const name = data.getString(data.getColumnIndex('name')); const introduction = data.getString(data.getColumnIndex('introduction')); Logger.info(TAG, `DAHelper query result:id = [${id}], name = [${name}], introduction = [${introduction}]`); } // Release the data set memory. data.close(); } );
import featureAbility from '@ohos.ability.featureAbility' import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Promise mode (await needs to be used in the asynchronous method): this.predicates.equalTo('name', 'Rose'); this.DAHelper.query(this.uri, this.columnArray, this.predicates).then((dataquery) => { promptAction.showToast({ message: 'query_success_toast' }); // ResultSet is a cursor of a data set. By default, the cursor points to the -1st record. Valid data starts from 0. while (dataquery.goToNextRow()) { const id = dataquery.getLong(dataquery.getColumnIndex('id')); const name = dataquery.getString(dataquery.getColumnIndex('name')); const introduction = dataquery.getString(dataquery.getColumnIndex('introduction')); Logger.info(TAG, `DAHelper query result:id = [${id}], name = [${name}], introduction = [${introduction}]`); } // Release the data set memory. dataquery.close(); }).catch((error: BusinessError) => { promptAction.showToast({ message: 'query_failed_toast' }); Logger.error(TAG, `DAHelper query failed. Cause: ${error.message}`); });
-
Use batchInsert to insert data in batches to the DataAbility.
import featureAbility from '@ohos.ability.featureAbility' import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Callback mode: this.DAHelper.batchInsert(this.uri, this.crowd, (error, data) => { if (error && error.code !== 0) { promptAction.showToast({ message: 'batchInsert_failed_toast' }); } else { promptAction.showToast({ message: 'batchInsert_success_toast' }); } Logger.info(TAG, 'DAHelper batchInsert result: ' + data + ', error: ' + JSON.stringify(error)); } );
import featureAbility from '@ohos.ability.featureAbility' import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Promise mode (await needs to be used in the asynchronous method): this.DAHelper.batchInsert(this.uri, this.crowd).then((databatchInsert) => { promptAction.showToast({ message: 'batchInsert_success_toast' }); Logger.info(TAG, 'DAHelper batchInsert result: ' + databatchInsert); }).catch((error: BusinessError) => { promptAction.showToast({ message: 'batchInsert_failed_toast' }); Logger.error(TAG, `DAHelper batchInsert failed. Cause: ${error.message}`); });
-
Use executeBatch to process data in batches in the DataAbility.
import featureAbility from '@ohos.ability.featureAbility' import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Callback mode: let operations: Array<ability.DataAbilityOperation> = [{ uri: this.uri, type: featureAbility.DataAbilityOperationType.TYPE_INSERT, valuesBucket: { name: 'Rose', introduction: 'executeBatch' }, predicates: this.predicates, expectedCount: 0, predicatesBackReferences: undefined, interrupted: true, }]; this.DAHelper.executeBatch(this.uri, operations, (error, data) => { if (error && error.code !== 0) { promptAction.showToast({ message: 'executeBatch_failed_toast' }); } else { promptAction.showToast({ message: 'executeBatch_success_toast' }); } Logger.info(TAG, `DAHelper executeBatch, result: ` + JSON.stringify(data) + ', error: ' + JSON.stringify(error)); } );
import featureAbility from '@ohos.ability.featureAbility' import promptAction from '@ohos.promptAction'; import Logger from '../../utils/Logger'; const TAG: string = 'PageDataAbility'; // Promise mode (await needs to be used in the asynchronous method): let operations: Array<ability.DataAbilityOperation> = [{ uri: this.uri, type: featureAbility.DataAbilityOperationType.TYPE_INSERT, valuesBucket: { name: 'Rose', introduction: 'executeBatch' }, predicates: this.predicates, expectedCount: 0, predicatesBackReferences: undefined, interrupted: true, }]; this.DAHelper.executeBatch(this.uri, operations).then((dataquery) => { promptAction.showToast({ message: 'executeBatch_success_toast' }); Logger.info(TAG, 'DAHelper executeBatch result: ' + JSON.stringify(dataquery)); }).catch((error: BusinessError) => { promptAction.showToast({ message: 'executeBatch_failed_toast' }); Logger.error(TAG, `DAHelper executeBatch failed. Cause: ${error.message}`); });
The APIs for operating a DataAbility are provided by DataAbilityHelper. For details about the APIs, see DataAbilityHelper.