如何对非关系数据库进行查询

键值对数据库,如何对非关系数据库进行查询,有完整的demo 吗?

HarmonyOS
2024-04-27 11:33:47
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
yu_qingbo
// 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%') 
  } 
}
分享
微博
QQ
微信
回复
2024-04-28 17:25:31
相关问题
关系数据库如何保存图片?
8040浏览 • 2回复 待解决
调用关系数据库插值报错
103浏览 • 1回复 待解决
关系数据库无法拷贝怎么回事?
1966浏览 • 1回复 待解决
关系数据库查询问题
137浏览 • 1回复 待解决
如何进行关系数据库的加密?
349浏览 • 2回复 待解决
关系数据库rdb中如何进行加密
1751浏览 • 1回复 待解决
用户查询消息数据库设计
2797浏览 • 1回复 待解决
关系数据库使用分享
1020浏览 • 1回复 待解决
HarmonyOS 关系数据库批量插入数据
155浏览 • 1回复 待解决
后端查询数据库的性能问题
3720浏览 • 1回复 待解决
HarmonyOS 关系数据库加密的问题
276浏览 • 1回复 待解决
HarmonyOS 关系数据库api异步返回
302浏览 • 1回复 待解决
关于harmony关系数据库升级问题
6032浏览 • 1回复 待解决