主页 > 互联网 > 内容页

OpenHarmony关系型数据库查询结果呈现 世界最资讯

2023-03-28 18:14:21 来源:ITMING


(资料图片)

1 ResultSet(结果集)

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

表1-1 ResultSet属性

名称类型必填说明
columnNamesArray结果集中所有列的名称
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检查当前行中指定列的值是否为nullcolumnIndex: 指定的列索引,从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 = [];    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%")

标签:

上一篇:
下一篇: