KV数据库基本功能使用

键值型数据库存储键值对形式的数据,当需要存储的数据没有复杂的关系模型,比如存储商品名称及对应价格、员工工号及今日是否已出勤等,由于数据复杂度低,更容易兼容不同数据库版本和设备类型,因此推荐使用键值型数据库持久化此类数据。

HarmonyOS
2024-05-22 23:22:49
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
是潘达不是胖达

preferences首选项基础功能:存储、获取数据;数据持久化。

import distributedKVStore from '@ohos.data.distributedKVStore'; 
import { BusinessError } from '@ohos.base'; 
import dataAbility from '@ohos.data.dataAbility'; 
import { ValueType } from '@ohos.data.ValuesBucket'; 
import { ValuesBucket } from '@ohos.data.ValuesBucket'; 
 
export class myKV { 
  kvManager?: distributedKVStore.KVManager; 
  kvStore?: distributedKVStore.SingleKVStore; 
  context = getContext(this) 
 
  createMyKVManager() { 
    // 复现 
    // let values: ValuesBucket = { 
    //   'name': '', 
    //   'value': new Uint8Array() 
    // } 
    const kvManagerConfig: distributedKVStore.KVManagerConfig = { 
      context: this.context, 
      bundleName: 'com.example.kvStore', 
    } 
    try { 
      this.kvManager = distributedKVStore.createKVManager(kvManagerConfig); 
      console.info("Succeeded in creating KVManager"); 
    } catch (e) { 
      let error = e as BusinessError; 
      console.error(`Failed to create KVManager.code is ${error.code},message is ${error.message}`); 
    } 
  } 
 
  query() { 
    try { 
      let query = new distributedKVStore.Query(); 
      query.like("key_test_string", "value-test-stri"); 
      let sql1 = query.getSqlLike(); 
      console.info(`GetSqlLike sql= ${sql1}`); 
      console.log("111") 
 
    } catch (e) { 
      console.error(`duplicated calls should be ok.code is ${e.code},message is ${e.message}`); 
    } 
  } 
 
  getMyKVStore() { 
    try { 
      const options: distributedKVStore.Options = { 
        createIfMissing: true, 
        encrypt: false, 
        backup: false, 
        autoSync: true, 
        kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, 
        securityLevel: distributedKVStore.SecurityLevel.S2, 
      }; 
      if (this.kvManager) { 
        this.kvManager.getKVStore('storeId', options, (err, store: distributedKVStore.SingleKVStore) => { 
          if (err) { 
            console.error(`Failed to get KVStore.code is ${err.code},message is ${err.message}`); 
            return; 
          } 
          console.info("Succeeded in getting KVStore"); 
          this.kvStore = store; 
        }); 
      } 
    } catch (e) { 
      let error = e as BusinessError; 
      console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 
    } 
  } 
 
  async PutData() { 
    const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 
    const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 
    console.info("开始插入"); 
    for (let i = 0; i < 10000; i++) { 
      if (this.kvStore) { 
        this.kvStore.put("" + i, "" + i, (err) => { 
          if (err != undefined) { 
            console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 
            return; 
          } 
          // console.info("成功插入"+i); 
        }); 
      } 
    } 
    console.info("插入完成"); 
  } 
 
  DeleteData() { 
    const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 
    for (let i = 1; i < 10; i++) { 
      if (this.kvStore) { 
        this.kvStore.delete(KEY_TEST_STRING_ELEMENT, (err) => { 
          if (err != undefined) { 
            console.error(`Failed to delete.code is ${err.code},message is ${err.message}`); 
            return; 
          } 
          console.info('Succeeded in deleting'); 
        }); 
      } 
    } 
  } 
 
  GetData() { 
    const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 
    if (this.kvStore) { 
      this.kvStore.get(KEY_TEST_STRING_ELEMENT, (err, data) => { 
        if (err != undefined) { 
          console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 
          return; 
        } 
        console.info(`Succeeded in getting data.data=${data}`); 
      }); 
    } 
  } 
 
 
  aboutToAppear() { 
    let kvManager: distributedKVStore.KVManager 
    const kvManagerConfig: distributedKVStore.KVManagerConfig = { 
      context: getContext(), 
      bundleName: getContext().applicationInfo.name 
    } 
    try { 
      kvManager = distributedKVStore.createKVManager(kvManagerConfig) 
      console.info("Succeeded in creating KVManager"); 
      const options: distributedKVStore.Options = { 
        createIfMissing: true, 
        encrypt: false, 
        backup: false, 
        autoSync: true, 
        kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, 
        securityLevel: distributedKVStore.SecurityLevel.S2, 
      }; 
      kvManager.getKVStore<distributedKVStore.SingleKVStore>('storeId', options) 
        .then((store) => { 
          console.info("Succeeded in getting KVStore"); 
        }) 
        .catch((err: BusinessError) => { 
          console.error(`Failed to get KVStore.code is ${err.code},message is ${err.message}`); 
        }) 
    } catch (e) { 
      let error = e as BusinessError 
      console.error(`Failed to create KVManager.code is ${error.code},message is ${error.message}`); 
    } 
  } 
 
  ceshi() { 
    const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 
    const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 
    if (this.kvStore) { 
      this.kvStore.get(KEY_TEST_STRING_ELEMENT, (err, data) => { 
        if (err != undefined) { 
          console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 
          return; 
        } 
        console.info(`Succeeded in getting data.data=${data}`); 
      }); 
    } 
    try { 
      let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 
      query.like(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT); 
      console.log(query.getSqlLike()); 
      console.info(`query is ${query.getSqlLike()}`); 
      query = null; 
    } catch (e) { 
      let error = e as BusinessError; 
      console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-05-23 21:07:13
相关问题
关系型数据库使用分享
928浏览 • 1回复 待解决
HarmonyOS skills 功能使用不成功
221浏览 • 1回复 待解决
wgcloud支持使用postgresql做数据库
3751浏览 • 1回复 待解决
分布式键值数据库使用分享
939浏览 • 1回复 待解决
关系型数据库使用有懂的吗?
2148浏览 • 1回复 待解决
使用PolarDB数据库是否需要分库分表
4692浏览 • 1回复 待解决
求解分布式数据库使用问题?
4196浏览 • 1回复 待解决
HarmonyOS 数据库框架
186浏览 • 1回复 待解决
OrmDatabase 数据库问题
3412浏览 • 1回复 待解决
多个数据库,同步数据
1743浏览 • 1回复 待解决
HarmonyOS 数据库表名里可以使用-吗
292浏览 • 1回复 待解决
数据中台数据库选择问题
1910浏览 • 1回复 待解决