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

键值对数据库,如何对非关系数据库进行查询,有完整的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
相关问题
关系数据库如何保存图片?
6263浏览 • 2回复 待解决
关系数据库无法拷贝怎么回事?
500浏览 • 1回复 待解决
关系数据库rdb中如何进行加密
271浏览 • 1回复 待解决
用户查询消息数据库设计
1201浏览 • 1回复 待解决
关于harmony关系数据库升级问题
4400浏览 • 1回复 待解决
关系数据库有知道的吗?
991浏览 • 1回复 待解决
后端查询数据库的性能问题
2093浏览 • 1回复 待解决
关系数据库RdbStore执行sql语句失败
168浏览 • 1回复 待解决
关系数据库使用有懂的吗?
839浏览 • 1回复 待解决
数据库多表关联查询有了解的吗?
656浏览 • 1回复 待解决
sql数据库查询数据量太大查不出来?
2212浏览 • 1回复 待解决