0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

OpenHarmony關(guān)系型數(shù)據(jù)庫查詢結(jié)果呈現(xiàn)

ITMING ? 來源:ITMING ? 作者: ITMING ? 2023-03-28 18:06 ? 次閱讀

1 ResultSet(結(jié)果集)

ResultSet(結(jié)果集)是OpenHarmony關(guān)系型數(shù)據(jù)庫提供查詢數(shù)據(jù)表返回結(jié)果的方法,提供了多種靈活的數(shù)據(jù)訪問方式,以便于開發(fā)者獲取各項數(shù)據(jù),ResultSet屬性如表1-1所示,ResultSet方法如表1-2所示。

表1-1 ResultSet屬性

名稱 類型 必填 說明
columnNames Array 結(jié)果集中所有列的名稱
columnCount number 結(jié)果集中的列數(shù)
rowCount number 結(jié)果集中的行數(shù)
rowIndex number 結(jié)果集當(dāng)前行的索引
isAtFirstRow boolean 結(jié)果集是否位于第一行
isAtLastRow boolean 結(jié)果集是否位于最后一行
isEnded boolean 結(jié)果集是否位于最后一行之后
isStarted boolean 指針是否移動過
isClosed boolean 當(dāng)前結(jié)果集是否關(guān)閉

表1-2 ResultSet方法

名稱 描述
getColumnIndex(columnName: string): number 根據(jù)指定的列名獲取列索引columnName: 結(jié)果集中指定列的名稱 number: 返回指定列的索引
getColumnName(columnIndex: number): string 根據(jù)指定的列索引獲取列名columnIndex: 結(jié)果集中指定列的索引string: 返回指定列的名稱
goTo(offset: number): boolean 向前或向后轉(zhuǎn)至結(jié)果集的指定行,相對于當(dāng)前行位置偏移offset: 表示相對于當(dāng)前行位置偏移量boolean:操作成功,則為true,否則為false
goToRow(position: number): boolean 轉(zhuǎn)到結(jié)果集的指定行position: 表示要移動到的指定位置boolean: 操作成功,則為true,否則為false
goToFirstRow(): boolean 轉(zhuǎn)到結(jié)果集的第一行boolean: 操作成功,則為true,否則為false
goToLastRow(): boolean 轉(zhuǎn)到結(jié)果集的最后一行boolean: 操作成功,則為true,否則為false
goToNextRow(): boolean 轉(zhuǎn)到結(jié)果集的下一行boolean: 操作成功,則為true,否則為false
goToPreviousRow(): boolean 轉(zhuǎn)到結(jié)果集上一行boolean: 操作成功,則為true,否則為false
getBlob(columnIndex: number): Uint8Array 以字節(jié)數(shù)組的形式獲取當(dāng)前行中指定列的值指定的列索引,從0開始Uint8Array: 以字節(jié)數(shù)組的形式返回指定列的值
getString(columnIndex: number): string 以字符串形式獲取當(dāng)前行中指定列的值columnIndex: 指定的列索引,從0開始string: 以字符串形式返回指定列的值
getLong(columnIndex: number): number 以Long形式獲取當(dāng)前行中指定列的值columnIndex: 指定的列索引,從0開始number: 以Long形式返回指定列的值。該接口支持的數(shù)據(jù)范圍是:Number.MIN_SAFE_INTEGER~Number.MAX_SAFE_INTEGER,若超出該范圍,則建議使用getDouble
getDouble(columnIndex: number): number 以double形式獲取當(dāng)前行中指定列的值columnIndex: 指定的列索引,從0開始number: 以double形式返回指定列的值
isColumnNull(columnIndex: number): boolean 檢查當(dāng)前行中指定列的值是否為nullcolumnIndex: 指定的列索引,從0開始boolean: 當(dāng)前行中指定列的值為null,則返回true,否則為false
close(): void 關(guān)閉結(jié)果集

2 流程

3 步驟

3.1 獲取ResultSet結(jié)果集

通過RdbStore實例的query()querySql()方法獲得ResultSet結(jié)果集。

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

3.2 自定義返回結(jié)果類

自定義TableResultSet類用于前臺展示。

export class TableResultSet {
    private total: number;                 // 總條數(shù)
    private data: any;                     // 數(shù)據(jù)表數(shù)據(jù)

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

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

3.3 結(jié)果集轉(zhuǎn)返回結(jié)果

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 {
      // 從數(shù)據(jù)第一行開始讀取
      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 呈現(xiàn)結(jié)果

  • 使用斷點調(diào)試方式

  • 使用日志調(diào)試方式
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)
    // 顯示表數(shù)據(jù)
    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%')

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
收藏 人收藏

    評論

    相關(guān)推薦

    HarmonyOS開發(fā)案例:【搭建關(guān)系數(shù)據(jù)庫】(4)

    本節(jié)將介紹如何調(diào)用關(guān)系數(shù)據(jù)庫接口在本地搭建數(shù)據(jù)庫,并讀寫相應(yīng)的用戶數(shù)據(jù)。
    的頭像 發(fā)表于 05-11 10:27 ?693次閱讀
    HarmonyOS開發(fā)案例:【搭建<b class='flag-5'>關(guān)系</b><b class='flag-5'>型</b><b class='flag-5'>數(shù)據(jù)庫</b>】(4)

    關(guān)系數(shù)據(jù)庫與非關(guān)系數(shù)據(jù)庫的區(qū)別淺析

    關(guān)系數(shù)據(jù)庫的一個劣勢就是 阻抗失諧(impedance mismatch):關(guān)系模型和內(nèi)存中的數(shù)據(jù)結(jié)構(gòu)之間存在差異
    發(fā)表于 06-03 06:03

    HarmonyOS關(guān)系數(shù)據(jù)庫和對象關(guān)系數(shù)據(jù)庫的使用方法

    容易就上手的知識。本篇速成教程直接使用最精準(zhǔn)和簡短的文字,再配上講解代碼,讓我們能在10分鐘左右就能掌握最基本的數(shù)據(jù)庫使用方法。數(shù)據(jù)庫的三大要素:數(shù)據(jù)庫、表、字段,接下來為大家介紹關(guān)系
    發(fā)表于 03-29 14:10

    基于數(shù)據(jù)庫查詢過程優(yōu)化設(shè)計

    在大型關(guān)系數(shù)據(jù)庫管理與開發(fā)中,優(yōu)化設(shè)計極大地提高數(shù)據(jù)庫的性能。通過對一大數(shù)據(jù)庫查詢語句執(zhí)行過程的討論,提出了對同一表格進(jìn)行多個選擇運(yùn)算的優(yōu)
    發(fā)表于 02-27 16:05 ?18次下載

    數(shù)據(jù)查詢

    查詢一、實驗?zāi)康耐ㄟ^基于關(guān)系網(wǎng)絡(luò)數(shù)據(jù)庫管理系統(tǒng)SQL Server的上機(jī)實驗,使學(xué)生進(jìn)一步了解關(guān)系
    發(fā)表于 05-10 10:55 ?0次下載

    查詢數(shù)據(jù)庫的最完美技巧

    查詢數(shù)據(jù)庫的最完美技巧.rar
    發(fā)表于 03-15 14:15 ?22次下載

    什么是關(guān)系數(shù)據(jù)庫

    什么是關(guān)系數(shù)據(jù)庫 關(guān)系數(shù)據(jù)庫簡介   關(guān)系
    發(fā)表于 06-17 07:38 ?9112次閱讀

    什么是非關(guān)系數(shù)據(jù)庫

    什么是非關(guān)系數(shù)據(jù)庫 談到非關(guān)系數(shù)據(jù)庫設(shè)計的難點,朱海峰說:“我們可以從一些場景來看這個問題
    發(fā)表于 06-17 15:49 ?3113次閱讀

    hbase和關(guān)系數(shù)據(jù)庫的區(qū)別

    hbase和關(guān)系數(shù)據(jù)庫的區(qū)別就是對于傳統(tǒng)數(shù)據(jù)庫,增加列對于一個項目來講,改變是非常大的。但是對于nosql,插入列和刪除列,跟傳統(tǒng)數(shù)據(jù)庫
    發(fā)表于 12-27 15:51 ?1.1w次閱讀
    hbase和<b class='flag-5'>關(guān)系</b><b class='flag-5'>型</b><b class='flag-5'>數(shù)據(jù)庫</b>的區(qū)別

    數(shù)據(jù)庫系統(tǒng)概論之如何進(jìn)行關(guān)系查詢處理和查詢優(yōu)化

    本文檔的主要內(nèi)容詳細(xì)介紹的是數(shù)據(jù)庫系統(tǒng)概論之如何進(jìn)行關(guān)系查詢處理和查詢優(yōu)化主要內(nèi)容包括了:1、關(guān)系數(shù)據(jù)庫系統(tǒng)的
    發(fā)表于 11-15 15:12 ?11次下載
    <b class='flag-5'>數(shù)據(jù)庫</b>系統(tǒng)概論之如何進(jìn)行<b class='flag-5'>關(guān)系</b><b class='flag-5'>查詢</b>處理和<b class='flag-5'>查詢</b>優(yōu)化

    數(shù)據(jù)庫原理的關(guān)系代數(shù)詳細(xì)講解

    關(guān)系代數(shù)與關(guān)系數(shù)據(jù)庫操作   關(guān)系代數(shù)是關(guān)系數(shù)據(jù)庫系統(tǒng)查詢語言的理論基礎(chǔ)。
    發(fā)表于 10-31 11:53 ?5次下載

    OpenHarmony關(guān)系數(shù)據(jù)庫概述

    關(guān)系數(shù)據(jù)庫(Relational Database, 以下簡稱RDB)是一種基于關(guān)系模型來管理數(shù)據(jù)數(shù)
    的頭像 發(fā)表于 03-28 18:08 ?951次閱讀
    <b class='flag-5'>OpenHarmony</b><b class='flag-5'>關(guān)系</b><b class='flag-5'>型</b><b class='flag-5'>數(shù)據(jù)庫</b>概述

    關(guān)系數(shù)據(jù)庫的基本原理(什么是關(guān)系數(shù)據(jù)庫

    組成。關(guān)系數(shù)據(jù)庫是基于實用和可重復(fù)使用的概念,是支持高性能交互查詢、交易處理能力、安全性和靈活性的關(guān)鍵數(shù)據(jù)存儲和維護(hù)方法。關(guān)系
    的頭像 發(fā)表于 07-10 09:06 ?1336次閱讀

    python讀取數(shù)據(jù)庫數(shù)據(jù) python查詢數(shù)據(jù)庫 python數(shù)據(jù)庫連接

    python讀取數(shù)據(jù)庫數(shù)據(jù) python查詢數(shù)據(jù)庫 python數(shù)據(jù)庫連接 Python是一門高級編程語言,廣泛應(yīng)用于各種領(lǐng)域。其中,Pyt
    的頭像 發(fā)表于 08-28 17:09 ?1683次閱讀

    常見的存儲Idea數(shù)據(jù)庫的地方

    。它們使用表格和行的結(jié)構(gòu)來組織數(shù)據(jù),并使用SQL語言來查詢和管理數(shù)據(jù)。一些著名的關(guān)系數(shù)據(jù)庫包括
    的頭像 發(fā)表于 12-06 14:15 ?860次閱讀