harmony 鸿蒙resultSet

2022-08-09 浏览 (876)

resultSet

A result set is a set of results returned after the relational database (RDB) query APIs are called. You can use the resultset APIs to obtain required data.

NOTE

The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The APIs of this module are no longer maintained since API version 9. You are advised to use @ohos.data.relationalStore#ResultSet.

ResultSet

Provides methods to access the result set, which is obtained by querying the RDB store.

Usage

You need to obtain a resultSet object by using RdbStore.query().

import dataRdb from '@ohos.data.rdb';
let predicates = new dataRdb.RdbPredicates("EMPLOYEE");
predicates.equalTo("AGE", 18);
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promise.then((resultSet) => {
    console.log(TAG + "resultSet columnNames:" + resultSet.columnNames);
    console.log(TAG + "resultSet columnCount:" + resultSet.columnCount);
});

Attributes

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

NameTypeMandatoryDescription
columnNamesArray<string>YesNames of all columns in the result set.
columnCountnumberYesNumber of columns in the result set.
rowCountnumberYesNumber of rows in the result set.
rowIndexnumberYesIndex of the current row in the result set.
isAtFirstRowbooleanYesWhether the cursor is in the first row of the result set.
isAtLastRowbooleanYesWhether the cursor is in the last row of the result set.
isEndedbooleanYesWhether the cursor is after the last row of the result set.
isStartedbooleanYesWhether the cursor has been moved.
isClosedbooleanYesWhether the result set is closed.

getColumnIndex

getColumnIndex(columnName: string): number

Obtains the column index based on the column name.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Parameters

NameTypeMandatoryDescription
columnNamestringYesColumn name specified.

Return value

TypeDescription
numberIndex of the column obtained.

Example

resultSet.goToFirstRow();
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"));

getColumnName

getColumnName(columnIndex: number): string

Obtains the column name based on the column index.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesColumn index specified.

Return value

TypeDescription
stringColumn name obtained.

Example

const id = resultSet.getColumnName(0);
const name = resultSet.getColumnName(1);
const age = resultSet.getColumnName(2);

goTo

goTo(offset:number): boolean

Moves the cursor to the row based on the specified offset.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Parameters

NameTypeMandatoryDescription
offsetnumberYesOffset relative to the current position.

Return value

TypeDescription
booleanReturns true if the operation is successful; returns false otherwise.

Example

let predicatesgoto = new dataRdb.RdbPredicates("EMPLOYEE");
let promisequerygoto = rdbStore.query(predicatesgoto, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promisequerygoto.then((resultSet) => {
    resultSet.goTo(1);
    resultSet.close();
}).catch((err) => {
    console.log('query failed');
});

goToRow

goToRow(position: number): boolean

Moves the cursor to the specified row in the result set.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Parameters

NameTypeMandatoryDescription
positionnumberYesPosition to which the cursor is to be moved.

Return value

TypeDescription
booleanReturns true if the operation is successful; returns false otherwise.

Example

let predicatesgotorow = new dataRdb.RdbPredicates("EMPLOYEE");
let promisequerygotorow = rdbStore.query(predicatesgotorow, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promisequerygotorow.then((resultSet) => {
    resultSet.goToRow(5);
    resultSet.close();
}).catch((err) => {
    console.log('query failed');
});

goToFirstRow

goToFirstRow(): boolean

Moves the cursor to the first row of the result set.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Return value

TypeDescription
booleanReturns true if the operation is successful; returns false otherwise.

Example

let predicatesgoFirst = new dataRdb.RdbPredicates("EMPLOYEE");
let promisequerygoFirst = rdbStore.query(predicatesgoFirst, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promisequerygoFirst.then((resultSet) => {
    resultSet.goToFirstRow();
    resultSet.close();
}).catch((err) => {
    console.log('query failed');
});

goToLastRow

goToLastRow(): boolean

Moves the cursor to the last row of the result set.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Return value

TypeDescription
booleanReturns true if the operation is successful; returns false otherwise.

Example

let predicatesgoLast = new dataRdb.RdbPredicates("EMPLOYEE");
let promisequerygoLast = rdbStore.query(predicatesgoLast, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promisequerygoLast.then((resultSet) => {
    resultSet.goToLastRow();
    resultSet.close();
}).catch((err) => {
    console.log('query failed');
});

goToNextRow

goToNextRow(): boolean

Moves the cursor to the next row in the result set.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Return value

TypeDescription
booleanReturns true if the operation is successful; returns false otherwise.

Example

let predicatesgoNext = new dataRdb.RdbPredicates("EMPLOYEE");
let promisequerygoNext = rdbStore.query(predicatesgoNext, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promisequerygoNext.then((resultSet) => {
    resultSet.goToNextRow();
    resultSet.close();
}).catch((err) => {
    console.log('query failed');
});

goToPreviousRow

goToPreviousRow(): boolean

Moves the cursor to the previous row in the result set.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Return value

TypeDescription
booleanReturns true if the operation is successful; returns false otherwise.

Example

let predicatesgoPrev = new dataRdb.RdbPredicates("EMPLOYEE");
let promisequerygoPrev = rdbStore.query(predicatesgoPrev, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promisequerygoPrev.then((resultSet) => {
    resultSet.goToPreviousRow();
    resultSet.close();
}).catch((err) => {
    console.log('query failed');
});

getBlob

getBlob(columnIndex: number): Uint8Array

Obtains the value in the specified column in the current row as a byte array.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesIndex of the specified column, starting from 0.

Return value

TypeDescription
Uint8ArrayValue in the specified column as a byte array.

Example

const codes = resultSet.getBlob(resultSet.getColumnIndex("CODES"));

getString

getString(columnIndex: number): string

Obtains the value in the specified column in the current row as a string.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesIndex of the specified column, starting from 0.

Return value

TypeDescription
stringValue in the specified column as a string.

Example

const name = resultSet.getString(resultSet.getColumnIndex("NAME"));

getLong

getLong(columnIndex: number): number

Obtains the value in the specified column in the current row as a Long integer.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesIndex of the specified column, starting from 0.

Return value

TypeDescription
numberValue in the specified column as a Long integer.
The value range supported by this API is Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER. If the value is out of this range, use getDouble.

Example

const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));

getDouble

getDouble(columnIndex: number): number

Obtains the value in the specified column in the current row as a double.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesIndex of the specified column, starting from 0.

Return value

TypeDescription
numberValue in the specified column as a double.

Example

const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));

isColumnNull

isColumnNull(columnIndex: number): boolean

Checks whether the value in the specified column of the current row is null.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesIndex of the specified column, starting from 0.

Return value

TypeDescription
booleanReturns true if the value is null; returns false otherwise.

Example

const isColumnNull = resultSet.isColumnNull(resultSet.getColumnIndex("CODES"));

close

close(): void

Closes this result set.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Example

let predicatesClose = new dataRdb.RdbPredicates("EMPLOYEE");
let promiseClose = rdbStore.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
promiseClose.then((resultSet) => {
    resultSet.close();
}).catch((err) => {
    console.log('Failed to close the resultset');
});

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/ad262ebebd7b478493c04e32a9924fdd