harmony 鸿蒙创建DataAbility

  • 2022-12-13
  • 浏览 (518)

创建DataAbility

实现DataAbility中Insert、Query、Update、Delete接口的业务内容。保证能够满足数据库存储业务的基本需求。BatchInsert与ExecuteBatch接口已经在系统中实现遍历逻辑,依赖Insert、Query、Update、Delete接口逻辑,来实现数据的批量处理。

创建DataAbility的代码示例如下:

import featureAbility from '@ohos.ability.featureAbility';
import dataAbility from '@ohos.data.dataAbility';
import Want from '@ohos.app.ability.Want';
import common from '@ohos.app.ability.common';
import { AsyncCallback, BusinessError } from '@ohos.base';
import rdb from '@ohos.data.rdb';

const TABLE_NAME = 'book'
const STORE_CONFIG: rdb.StoreConfig = { name: 'book.db'}
const 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

class DataAbility {
  onInitialized(want: Want) {
    console.info('DataAbility onInitialized, abilityInfo:' + want.bundleName)
    let context: common.BaseContext = {stageMode: featureAbility.getContext().stageMode}
    rdb.getRdbStore(context, STORE_CONFIG, 1, (err, store) => {
      console.info('DataAbility getRdbStore callback')
      store.executeSql(SQL_CREATE_TABLE, [])
      rdbStore = store
    });
  }
  insert(uri: string, valueBucket: rdb.ValuesBucket, callback: AsyncCallback<number>) {
    console.info('DataAbility insert start')
    if (rdbStore) {
      rdbStore.insert(TABLE_NAME, valueBucket, callback)
    }
  }
  batchInsert(uri: string, valueBuckets: Array<rdb.ValuesBucket>, callback: AsyncCallback<number>) {
    console.info('DataAbility batch insert start')
    if (rdbStore) {
      for (let i = 0;i < valueBuckets.length; i++) {
        console.info('DataAbility batch insert i=' + i)
        if (i < valueBuckets.length - 1) {
          rdbStore.insert(TABLE_NAME, valueBuckets[i], (err: BusinessError, num: number) => {
            console.info('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>) {
    console.info('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>) {
    console.info('DataAbilityupdate start')
    let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates)
    if (rdbStore) {
      rdbStore.update(valueBucket, rdbPredicates, callback)
    }
  }
  delete(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback<number>) {
    console.info('DataAbilitydelete start')
    let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates)
    if (rdbStore) {
      rdbStore.delete(rdbPredicates, callback)
    }
  }
}

export default new DataAbility()

你可能感兴趣的鸿蒙文章

harmony 鸿蒙应用模型

harmony 鸿蒙使用显式Want启动应用组件

harmony 鸿蒙使用隐式Want打开网址

harmony 鸿蒙AbilityStage组件容器

harmony 鸿蒙访问DataAbility

harmony 鸿蒙FA模型访问Stage模型DataShareExtensionAbility

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙常见action与entities

harmony 鸿蒙API切换概述

harmony 鸿蒙app和deviceConfig的切换

0  赞