#鸿蒙学习大百科#关系型数据库如何实现条件查询?

关系型数据库如何实现条件查询?

HarmonyOS
2024-10-23 11:37:49
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
花鸟吹雪
import { ValuesBucket } from '@ohos.data.ValuesBucket';
import { relationalStore } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';

let store: relationalStore.RdbStore | undefined = undefined;
@Entry
@Component
struct Index {
  build() {
    Column() {
      Button("query").onClick(() => {
        const valueBucket: ValuesBucket = {
          NAME:"TOM",
          AGE: 12,
          SALARY: 100.5,
          CODES:  new Uint8Array([1, 2, 3, 4, 5]),
        };
        //获取store步骤省略
        let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
        predicates.equalTo('NAME', 'Rose');
        if (store !== undefined) {
          (store as relationalStore.RdbStore).query(predicates, ['ID', 'NAME', 'AGE', 'SALARY'], (err: BusinessError, resultSet) => {
            if (err) {
              console.error(`Failed to query data. Code:${err.code}, message:${err.message}`);
              return;
            }
            console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
            // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。
            while (resultSet.goToNextRow()) {
              const id = resultSet.getLong(resultSet.getColumnIndex('ID'));
              const name = resultSet.getString(resultSet.getColumnIndex('NAME'));
              const age = resultSet.getLong(resultSet.getColumnIndex('AGE'));
              const salary = resultSet.getDouble(resultSet.getColumnIndex('SALARY'));
              console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
            }
            // 释放数据集的内存
            resultSet.close();
          })
        }

      })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
分享
微博
QQ
微信
回复
2024-10-23 17:26:07
相关问题