harmony 鸿蒙@ohos.data.dataShareResultSet (DataShare Result Set)

2022-08-09 浏览 (1062)

@ohos.data.dataShareResultSet (DataShare Result Set)

The DataShareResultSet module provides APIs for accessing the result set obtained from the database. You can access the values in the specified rows or the value of the specified data type.

NOTE

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

  • The APIs provided by this module are system APIs.

  • The APIs of this module can be used only in the stage model.

Modules to Import

import DataShareResultSet from '@ohos.data.DataShareResultSet';

Usage

You can call query() to obtain the DataShareResultSet object.

import dataShare from '@ohos.data.dataShare';
import dataSharePredicates from '@ohos.data.dataSharePredicates'
import { BusinessError } from '@ohos.base'

let dataShareHelper: dataShare.DataShareHelper|undefined = undefined;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
dataShare.createDataShareHelper(this.context, uri, (err, data) => {
  if (err != undefined) {
    console.error("createDataShareHelper fail, error message : " + err);
  } else {
    console.info("createDataShareHelper end, data : " + data);
    dataShareHelper = data;
  }
});

let columns = ["*"];
let da = new dataSharePredicates.DataSharePredicates();
let resultSet: DataShareResultSet|undefined = undefined;
da.equalTo("name", "ZhangSan");
if (dataShareHelper != undefined) {
  (dataShareHelper as dataShare.DataShareHelper).query(uri, da, columns).then((data: DataShareResultSet) => {
    console.info("query end, data : " + data);
    resultSet = data;
  }).catch((err: BusinessError) => {
    console.error("query fail, error message : " + err);
  });
}

DataShareResultSet

Provides methods for accessing the result sets generated by querying the database.

Attributes

System capability: SystemCapability.DistributedDataManager.DataShare.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.
isClosedbooleanYesWhether the result set is closed.

goToFirstRow

goToFirstRow(): boolean

Moves to the first row of the result set.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Return value

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

Example

if (resultSet != undefined) {
  let isGoToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
  console.info('resultSet.goToFirstRow: ' + isGoToFirstRow);
}

goToLastRow

goToLastRow(): boolean

Moves to the last row of the result set.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Return value

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

Example

if (resultSet != undefined) {
  let isGoToLastRow = (resultSet as DataShareResultSet).goToLastRow();
  console.info('resultSet.goToLastRow: ' + isGoToLastRow);
}

goToNextRow

goToNextRow(): boolean

Moves to the next row in the result set.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Return value

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

Example

if (resultSet != undefined) {
  let isGoToNextRow = (resultSet as DataShareResultSet).goToNextRow();
  console.info('resultSet.goToNextRow: ' + isGoToNextRow);
}

goToPreviousRow

goToPreviousRow(): boolean

Moves to the previous row in the result set.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Return value

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

Example

if (resultSet != undefined) {
  let isGoToPreviousRow = (resultSet as DataShareResultSet).goToPreviousRow();
  console.info('resultSet.goToPreviousRow: ' + isGoToPreviousRow);
}

goTo

goTo(offset:number): boolean

Moves based on the specified offset.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

NameTypeMandatoryDescription
offsetnumberYesOffset relative to the current position. A negative value means to move backward, and a positive value means to move forward.

Return value

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

Example

let goToNum = 1;
if (resultSet != undefined) {
  let isGoTo = (resultSet as DataShareResultSet).goTo(goToNum);
  console.info('resultSet.goTo: ' + isGoTo);
}

goToRow

goToRow(position: number): boolean

Moves to the specified row in the result set.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

NameTypeMandatoryDescription
positionnumberYesDestination position to move to.

Return value

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

Example

let goToRowNum = 2;
if (resultSet != undefined) {
  let isGoToRow = (resultSet as DataShareResultSet).goToRow(goToRowNum);
  console.info('resultSet.goToRow: ' + isGoToRow);
}

getBlob

getBlob(columnIndex: number): Uint8Array

Obtains the value in the form of a byte array based on the specified column and the current row.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesIndex of the target column, starting from 0.

Return value

TypeDescription
Uint8ArrayValue obtained.

Example

let columnIndex = 1;
if (resultSet != undefined) {
  let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
  let getBlob = (resultSet as DataShareResultSet).getBlob(columnIndex);
  console.info('resultSet.getBlob: ' + getBlob);
}

getString

getString(columnIndex: number): string

Obtains the value in the form of a string based on the specified column and the current row.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesIndex of the target column, starting from 0.

Return value

TypeDescription
stringValue obtained.

Example

let columnIndex = 1;
if (resultSet != undefined) {
  let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
  let getString = (resultSet as DataShareResultSet).getString(columnIndex);
  console.info('resultSet.getString: ' + getString);
}

getLong

getLong(columnIndex: number): number

Obtains the value in the form of a long integer based on the specified column and the current row.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesIndex of the target column, starting from 0.

Return value

TypeDescription
numberValue obtained.

Example

let columnIndex = 1;
if (resultSet != undefined) {
  let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
  let getLong = (resultSet as DataShareResultSet).getLong(columnIndex);
  console.info('resultSet.getLong: ' + getLong);
}

getDouble

getDouble(columnIndex: number): number

Obtains the value in the form of a double-precision floating-point number based on the specified column and the current row.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesIndex of the target column, starting from 0.

Return value

TypeDescription
numberValue obtained.

Example

let columnIndex = 1;
if (resultSet != undefined) {
  let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow();
  let getDouble = (resultSet as DataShareResultSet).getDouble(columnIndex);
  console.info('resultSet.getDouble: ' + getDouble);
}

close

close(): void

Closes this result set.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Example

if (resultSet != undefined) {
  (resultSet as DataShareResultSet).close();
}

getColumnIndex

getColumnIndex(columnName: string): number

Obtains the column index based on the column name.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

NameTypeMandatoryDescription
columnNamestringYesColumn name.

Return value

TypeDescription
numberColumn index obtained.

Example

let ColumnName = "name";
if (resultSet != undefined) {
  let getColumnIndex = (resultSet as DataShareResultSet).getColumnIndex(ColumnName);
  console.info('resultSet.getColumnIndex: ' + getColumnIndex);
}

getColumnName

getColumnName(columnIndex: number): string

Obtains the column name based on the column index.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesColumn index.

Return value

TypeDescription
stringColumn name obtained.

Example

let columnIndex = 1;
if (resultSet != undefined) {
  let getColumnName = (resultSet as DataShareResultSet).getColumnName(columnIndex);
  console.info('resultSet.getColumnName: ' + getColumnName);
}

getDataType

getDataType(columnIndex: number): DataType

Obtains the data type based on the specified column index.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

Parameters

NameTypeMandatoryDescription
columnIndexnumberYesColumn index.

Return value

TypeDescription
DataTypeData type obtained.

Example

let columnIndex = 1;
if (resultSet != undefined) {
  let getDataType = (resultSet as DataShareResultSet).getDataType(columnIndex);
  console.info('resultSet.getDataType: ' + getDataType);
}

DataType

Enumerates the data types.

System capability: SystemCapability.DistributedDataManager.DataShare.Core

NameValueDescription
TYPE_NULL0Null.
TYPE_LONG1Long integer.
TYPE_DOUBLE2Double-precision floating-point number.
TYPE_STRING3String.
TYPE_BLOB4Byte array.

你可能感兴趣的鸿蒙文章

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/87240f51a7b3440a8f0583b62da98f0c