OpenHarmony关系型数据库查询结果呈现

文章介绍了OpenHarmony关系型数据库中的ResultSet对象,用于存储查询数据表的结果,提供多种数据访问方式。通过ResultSet的属性和方法,如goToRow、getLong等,可以便捷地操作和获取数据。在实际应用中,通过自定义TableResultSet类将ResultSet转换为适合前端展示的数据格式,并展示了如何在页面上呈现查询结果。
摘要由CSDN通过智能技术生成

1 ResultSet(结果集)

ResultSet(结果集)是OpenHarmony关系型数据库提供查询数据表返回结果的方法,提供了多种灵活的数据访问方式,以便于开发者获取各项数据,ResultSet属性如表1-1所示,ResultSet方法如表1-2所示。

表1-1 ResultSet属性

名称类型必填说明
columnNamesArray<string>结果集中所有列的名称
columnCountnumber结果集中的列数
rowCountnumber结果集中的行数
rowIndexnumber结果集当前行的索引
isAtFirstRowboolean结果集是否位于第一行
isAtLastRowboolean结果集是否位于最后一行
isEndedboolean结果集是否位于最后一行之后
isStartedboolean指针是否移动过
isClosedboolean当前结果集是否关闭

表1-2 ResultSet方法

名称描述
getColumnIndex(columnName: string): number根据指定的列名获取列索引
columnName: 结果集中指定列的名称
number: 返回指定列的索引
getColumnName(columnIndex: number): string根据指定的列索引获取列名
columnIndex: 结果集中指定列的索引
string: 返回指定列的名称
goTo(offset: number): boolean向前或向后转至结果集的指定行,相对于当前行位置偏移
offset: 表示相对于当前行位置偏移量
boolean:操作成功,则为true,否则为false
goToRow(position: number): boolean转到结果集的指定行
position: 表示要移动到的指定位置
boolean: 操作成功,则为true,否则为false
goToFirstRow(): boolean转到结果集的第一行
boolean: 操作成功,则为true,否则为false
goToLastRow(): boolean转到结果集的最后一行
boolean: 操作成功,则为true,否则为false
goToNextRow(): boolean转到结果集的下一行
boolean: 操作成功,则为true,否则为false
goToPreviousRow(): boolean转到结果集上一行
boolean: 操作成功,则为true,否则为false
getBlob(columnIndex: number): Uint8Array以字节数组的形式获取当前行中指定列的值
指定的列索引,从0开始
Uint8Array: 以字节数组的形式返回指定列的值
getString(columnIndex: number): string以字符串形式获取当前行中指定列的值
columnIndex: 指定的列索引,从0开始
string: 以字符串形式返回指定列的值
getLong(columnIndex: number): number以Long形式获取当前行中指定列的值
columnIndex: 指定的列索引,从0开始
number: 以Long形式返回指定列的值。该接口支持的数据范围是:Number.MIN_SAFE_INTEGER~Number.MAX_SAFE_INTEGER,若超出该范围,则建议使用getDouble
getDouble(columnIndex: number): number以double形式获取当前行中指定列的值
columnIndex: 指定的列索引,从0开始
number: 以double形式返回指定列的值
isColumnNull(columnIndex: number): boolean检查当前行中指定列的值是否为null
columnIndex: 指定的列索引,从0开始
boolean: 当前行中指定列的值为null,则返回true,否则为false
close(): void关闭结果集

2 流程

3 步骤

3.1 获取ResultSet结果集

通过RdbStore实例的query()querySql()方法获得ResultSet结果集。

let predicates = new relationalStore.RdbPredicates(this.tableName);
let result = await this.rdbStore.query(predicates, columns);

3.2 自定义返回结果类

自定义TableResultSet类用于前台展示。

export class TableResultSet {
    private total: number;                 // 总条数
    private data: any;                     // 数据表数据

    setTotal(total: number) {
        this.total = total;
    }

    setData(data: any) {
        this.data = data;
    }
}

3.3 结果集转返回结果

ResultSet并不能直接用来展示,通过ResultSet提供的各类方法获取需要的信息。

  private resultToObject(result: relationalStore.ResultSet) {
    let trs = new TableResultSet();
    trs.setData(result.rowCount);
    let data: Array<any> = [];
    let count = result.rowCount;
    if (count === 0 || typeof count === 'string') {
      trs.setData([]);
    } else {
      // 从数据第一行开始读取
      result.goToFirstRow();
      for (let j = 0; j < count; j++) {
        let temp: any = {};
        for (let i = 0; i < this.fields.length; i++) {
          let field = this.fields[i];
          if (field.type === 'INTEGER' || field.type === 'integer') {
            temp[field.name] = result.getLong(result.getColumnIndex(field.name));
          } else if (field.type === 'REAL' || field.type === 'real') {
            temp[field.name] = result.getDouble(result.getColumnIndex(field.name));
          } else if (field.type === 'TEXT' || field.type === 'text') {
            temp[field.name] = result.getString(result.getColumnIndex(field.name));
          } else if (field.type === 'BLOB' || field.type === 'blob') {
            temp[field.name] = result.getBlob(result.getColumnIndex(field.name));
          }
        }
        data.push(temp);
        result.goToNextRow();
      }
      trs.setData(data);
    }
    return trs;
  }

4 呈现结果

  • 使用断点调试方式

  • 使用日志调试方式

Log.info(TAG, `Query of ${this.tableName} table data succeeded. data: ` + JSON.stringify(result));

  • 页面显示

// 显示表名称
Text(TableConstants.T_ACCOUNT_NAME)
  .fontSize(18)
  .fontWeight(700)
  .width('90%').height(54)
Column({space: 5}) {
  if (this.result !== null) {
    // 显示表字段
    GridRow({
      columns: TableConstants.T_ACCOUNT_FIELDS.length,
      direction: GridRowDirection.Row
    }) {
      ForEach(this.result.fields, (field) => {
        GridCol() {
          Text(field)
            .width("100%").height(54)
            .fontSize(16)
            .textAlign(TextAlign.Center)
        }
        .colStyle()
      })
    }
    .width('90%').height(54)
    .backgroundColor(0xE5E5E5)
    // 显示表数据
    ForEach(this.result.data, (item) => {
      GridRow({
        columns: TableConstants.T_ACCOUNT_FIELDS.length,
        direction: GridRowDirection.Row
      }) {
        ForEach(TableConstants.T_ACCOUNT_FIELDS, (field) => {
          GridCol() {
            this.Label(item[field.name].toString())
          }
          .colStyle()
        })
      }
      .width('90%').height(54)
      .backgroundColor(0xF5F5F5)
    }, temp => temp.toString())
  }
}
.width('100%')

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白·晓明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值