HarmonyOS 如何通过键值型数据库实现数据持久化

有没有示例代码

HarmonyOS
7天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

封装好的demo可供参考

import distributedKVStore from '@ohos.data.distributedKVStore';
import { BusinessError } from '@ohos.base';

const KeyDataBase = 'KeyDataBase';

export  class  KeyValueDataBase {
  createDataBase(context: Context) {
    const kvManagerConfig: distributedKVStore.KVManagerConfig = {
      context: context,
      bundleName: 'com.example.myminitaskapplication'
    };
    try {
      // 创建KVManager实例
      let kvManager = distributedKVStore.createKVManager(kvManagerConfig);
      console.info('Succeeded in creating KVManager.');
      try {
        const options: distributedKVStore.Options = {
          createIfMissing: true,
          encrypt: false,
          backup: false,
          autoSync: false,
          // kvStoreType不填时,默认创建多设备协同数据库
          kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION,
          // 多设备协同数据库:kvStoreType: distributedKVStore.KVStoreType.DEVICE_COLLABORATION,
          securityLevel: distributedKVStore.SecurityLevel.S1
        };
        kvManager.getKVStore<distributedKVStore.SingleKVStore>('storeId', options, (err, store: distributedKVStore.SingleKVStore) => {
          if (err) {
            console.error(`Failed to get KVStore: Code:${err.code},message:${err.message}`);
            return;
          }
          console.info('Succeeded in getting KVStore.');
          AppStorage.setOrCreate<distributedKVStore.SingleKVStore>(KeyDataBase, store)

          // 请确保获取到键值数据库实例后,再进行相关数据操作
        });
      } catch (e) {
        let error = e as BusinessError;
        console.error(`An unexpected error occurred. Code:${error.code},message:${error.message}`);
      }
      // 继续创建获取数据库
    } catch (e) {
      let error = e as BusinessError;
      console.error(`Failed to create KVManager. Code:${error.code},message:${error.message}`);
    }
  }

  async put(key: string, value: string) {
    let kvStore: distributedKVStore.SingleKVStore = AppStorage.get(KeyDataBase) as distributedKVStore.SingleKVStore;
    if (kvStore !== undefined) {
      try {
        await kvStore.put(key, value, (err) => {
          if (err !== undefined) {
            console.error(`Failed to put data. Code:${err.code},message:${err.message}`);
            return;
          }
          console.info('Succeeded in putting data.');
        });
      } catch (e) {
        let error = e as BusinessError;
        console.error(`An unexpected error occurred. Code:${error.code},message:${error.message}`);
      }
    }
  }

  async get(key: string) {
    let kvStore: distributedKVStore.SingleKVStore = AppStorage.get(KeyDataBase) as distributedKVStore.SingleKVStore;
    if (kvStore !== undefined) {
      kvStore.get(key, (err, data) => {
        if (err != undefined) {
          console.error(`Failed to get data. Code:${err.code},message:${err.message}`);
          return;
        }
        console.info(`Succeeded in getting data. Data:${data}`);
        return (data as string)
      });
    }
  }

  async delete(key: string) {
    let kvStore: distributedKVStore.SingleKVStore = AppStorage.get(KeyDataBase) as distributedKVStore.SingleKVStore;
    if (kvStore !== undefined) {
      kvStore.delete(key, (err) => {
        if (err !== undefined) {
          console.error(`Failed to delete data. Code:${err.code},message:${err.message}`);
          return;
        }
        console.info('Succeeded in deleting data.');
      });
    }
  }
}

export default new KeyValueDataBase();
分享
微博
QQ
微信
回复
7天前
相关问题
键值数据库跨设备数据同步demo
956浏览 • 1回复 待解决
键值数据库是否有做bundleName校验
1654浏览 • 1回复 待解决
HarmonyOS 关系数据库批量插入数据
468浏览 • 1回复 待解决
如何实现应用数据持久存储
2284浏览 • 1回复 待解决
HarmonyOS 本地关系数据库
95浏览 • 1回复 待解决