HarmonyOS 键值型数据库该怎么使用query去查询?文档中的‘fieId’字段该怎么填?

我向键值型数据库中存入了4条数据,使用 query.equalTo(“field”, “value”) 的方式去查询时,始终查不到数据。现在需求是把第一条数据查出来,该怎么使用query.equalTo()方法?里面的两个参数该怎么填?

HarmonyOS
2024-11-01 11:59:27
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

​键值型数据库通过 数据库名.get() 方法进行数据获取,可参考​https://blog.csdn.net/advent_java/article/details/134753667​​; 另外equalTo()方法里的field是对value值的扩充,具体可参考​https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-distributedkvstore-V5#equalto

schema的使用可以参考以下代码

1、定义schema,创建options:​

let child1 = new distributedKVStore.FieldNode('id'); 
child1.type = distributedKVStore.ValueType.INTEGER; 
child1.nullable = false; 
let child2 = new distributedKVStore.FieldNode('name'); 
child2.type = distributedKVStore.ValueType.STRING; 
child2.nullable = false; 
let child3 = new distributedKVStore.FieldNode('age'); 
child3.type = distributedKVStore.ValueType.INTEGER; 
child3.nullable = false; 
let schema = new distributedKVStore.Schema(); 
schema.root.appendChild(child1); 
schema.root.appendChild(child2); 
schema.root.appendChild(child3); 
schema.indexes = ['$.id', '$.name']; 
schema.mode = 1 
schema.skip = 0 
 
let options: distributedKVStore.Options = { 
  createIfMissing: true, 
  encrypt: false, 
  backup: true, 
  autoSync: true, 
  kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, 
  securityLevel: distributedKVStore.SecurityLevel.S1, 
  schema: schema 
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

2、创建kvstore。

const kvManagerConfig: distributedKVStore.KVManagerConfig = { 
  context: this.context, 
  bundleName: 'com.demo.HarmonyOSDemo', 
}; 
this.kvManager = distributedKVStore.createKVManager(kvManagerConfig); 
const kvstore = await this.kvManager.getKVStore('MyStoreId', options)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

​3、写入数据。

await this.kvStore.put(‘test_key_1’, ‘{“id”:0,“name”:“zhangsan”,“age”:0}’);

4、查询数据​。

let query = new distributedKVStore.Query(); 
query.equalTo('name', 'zhangsan'); 
await this.kvStore.getResultSet(query).then((result: distributedKVStore.KVStoreResultSet) => { 
  console.info(`DistributedKVStoreDemo queryData key:${key}, count: ${JSON.stringify(result.getCount())}`); 
  let entries: distributedKVStore.Entry[] = [] 
  while (result.moveToNext()) { 
    let entry = result.getEntry(); 
    entries.push(entry) 
  } 
}).catch((err: BusinessError) => { 
  console.error(`DistributedKVStoreDemo Query failed,key is ${key}, err is ${JSON.stringify(err)}`); 
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
分享
微博
QQ
微信
回复
2024-11-01 18:01:52


相关问题
键值数据库跨设备数据同步demo
1396浏览 • 1回复 待解决
HarmonyOS 应用怎么分渠道包
350浏览 • 1回复 待解决
键值数据库是否有做bundleName校验
2019浏览 • 1回复 待解决
关系数据库查询问题
987浏览 • 1回复 待解决
关系数据库使用分享
1865浏览 • 1回复 待解决
分布式键值数据库使用分享
1854浏览 • 1回复 待解决