中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
键值对数据库,如何对非关系数据库进行查询,有完整的demo 吗?
微信扫码分享
// myKV 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}`); } } 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}`); }); } } } // Index @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%') } }