創(chuàng)建DataAbility
實(shí)現(xiàn)DataAbility中Insert、Query、Update、Delete接口的業(yè)務(wù)內(nèi)容。保證能夠滿(mǎn)足數(shù)據(jù)庫(kù)存儲(chǔ)業(yè)務(wù)的基本需求。BatchInsert與ExecuteBatch接口已經(jīng)在系統(tǒng)中實(shí)現(xiàn)遍歷邏輯,依賴(lài)Insert、Query、Update、Delete接口邏輯,來(lái)實(shí)現(xiàn)數(shù)據(jù)的批量處理。 開(kāi)發(fā)前請(qǐng)熟悉鴻蒙開(kāi)發(fā)指導(dǎo)文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
創(chuàng)建DataAbility的代碼示例如下:
import featureAbility from '@ohos.ability.featureAbility';
import type common from '@ohos.app.ability.common';
import type Want from '@ohos.app.ability.Want';
import type { AsyncCallback, BusinessError } from '@ohos.base';
import dataAbility from '@ohos.data.dataAbility';
import rdb from '@ohos.data.rdb';
import hilog from '@ohos.hilog';
let TABLE_NAME = 'book';
let STORE_CONFIG: rdb.StoreConfig = { name: 'book.db' };
let SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, introduction TEXT NOT NULL)';
let rdbStore: rdb.RdbStore | undefined = undefined;
const TAG: string = '[Sample_FAModelAbilityDevelop]';
const domain: number = 0xFF00;
class DataAbility {
onInitialized(want: Want): void {
hilog.info(domain, TAG, 'DataAbility onInitialized, abilityInfo:' + want.bundleName);
let context: common.BaseContext = { stageMode: featureAbility.getContext().stageMode };
rdb.getRdbStore(context, STORE_CONFIG, 1, (err, store) = > {
hilog.info(domain, TAG, 'DataAbility getRdbStore callback');
store.executeSql(SQL_CREATE_TABLE, []);
rdbStore = store;
});
}
insert(uri: string, valueBucket: rdb.ValuesBucket, callback: AsyncCallback< number >): void {
hilog.info(domain, TAG, 'DataAbility insert start');
if (rdbStore) {
rdbStore.insert(TABLE_NAME, valueBucket, callback);
}
}
batchInsert(uri: string, valueBuckets: Array< rdb.ValuesBucket >, callback: AsyncCallback< number >): void {
hilog.info(domain, TAG, 'DataAbility batch insert start');
if (rdbStore) {
for (let i = 0; i < valueBuckets.length; i++) {
hilog.info(domain, TAG, 'DataAbility batch insert i=' + i);
if (i < valueBuckets.length - 1) {
rdbStore.insert(TABLE_NAME, valueBuckets[i], (err: BusinessError, num: number) = > {
hilog.info(domain, TAG, 'DataAbility batch insert ret=' + num);
});
} else {
rdbStore.insert(TABLE_NAME, valueBuckets[i], callback);
}
}
}
}
query(uri: string, columns: Array< string >, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback< rdb.ResultSet >): void {
hilog.info(domain, TAG, 'DataAbility query start');
let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates);
if (rdbStore) {
rdbStore.query(rdbPredicates, columns, callback);
}
}
update(uri: string, valueBucket: rdb.ValuesBucket, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback< number >): void {
hilog.info(domain, TAG, 'DataAbility update start');
let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates);
if (rdbStore) {
rdbStore.update(valueBucket, rdbPredicates, callback);
}
}
delete(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback< number >): void {
hilog.info(domain, TAG, 'DataAbility delete start');
let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates);
if (rdbStore) {
rdbStore.delete(rdbPredicates, callback);
}
}
}
export default new DataAbility();
審核編輯 黃宇
-
組件
+關(guān)注
關(guān)注
1文章
503瀏覽量
17786 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2303瀏覽量
42689
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論