分布式键值数据库使用分享

通过@ohos.data.distributedKVStore相关接口,完成键值型数据库的简单使用。

HarmonyOS
2024-05-22 23:26:10
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
励志做个有为人

使用的核心API

@ohos.data.distributedKVStore

核心代码解释

创建并获取分布式键值数据库及分布式键值数据库管理实例,并将增删改查等操作封装为相应的函数。

import distributedKVStore from '@ohos.data.distributedKVStore'; 
import { BusinessError } from '@ohos.base'; 
  
  
export class myKV { 
  kvManager?: distributedKVStore.KVManager; 
  kvStore?: distributedKVStore.SingleKVStore; 
  context = getContext(this) 
  //a = "AJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOWAJIFOWIHOCISHIOAHFGIOWHIOFDHSIONDJIHFUIOW"; 
  
  createMyKVManager() { 
    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}`); 
    } 
  } 
  
  PutData() { 
    const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 
    const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 
    if (this.kvStore != undefined) { 
      try { 
        this.kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err) => { 
          if (err != undefined) { 
            console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 
            return; 
          } 
          console.info("Succeeded in putting"); 
        }); 
      } catch (e) { 
        let error = e as BusinessError; 
        console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 
      } 
    } 
  } 
  
  DeleteData() { 
    const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 
    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}`); 
      }); 
    } 
  } 
  
} 
 

通过界面分别调用

import { myKV } from '../data/myKV'; 
  
@Entry 
@Component 
struct Index { 
  
  @State message: string = 'Hello World'; 
  myStore:myKV = new myKV() 
  
  build() { 
    Row() { 
      Column() { 
        Button("createMyKVManager") 
          .onClick(() => { 
            this.myStore.createMyKVManager() 
          }) 
        Button("getMyKVStore") 
          .onClick(() => { 
            this.myStore.getMyKVStore() 
          }) 
        Button("PutData") 
          .onClick(() => { 
            this.myStore.PutData() 
          }) 
        Button("GetData") 
          .onClick(() => { 
            this.myStore.GetData() 
          }) 
        Button("DeleteData") 
          .onClick(() => { 
            this.myStore.DeleteData() 
          }) 
  
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}

实现效果

依次调用结果

适配的版本信息

IDE:DevEco Studio 4.1.1.400

SDK:HarmoneyOS 4.1.0(11)

分享
微博
QQ
微信
回复
2024-05-23 21:13:16
相关问题
求解分布式数据库使用问题?
4391浏览 • 1回复 待解决
HarmonyOS 分布式数据库??
173浏览 • 0回复 待解决
关于鸿蒙分布式数据库key疑问
5841浏览 • 1回复 待解决
分布式数据库的疑惑有懂的吗?
3043浏览 • 1回复 待解决
分布式数据库选型问题有知道的吗?
3792浏览 • 1回复 待解决
分布式数据库一致性可以分为哪些?
523浏览 • 1回复 待解决
关系型数据库使用分享
1220浏览 • 1回复 待解决
分布式存储数据的方法
428浏览 • 1回复 待解决
spark 分布式数据到oracle
1990浏览 • 1回复 待解决
分布式数据管理没有了吗?
3127浏览 • 1回复 待解决
分布式恢复数据的方法
443浏览 • 1回复 待解决