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

  • 2022-08-09
  • 浏览 (800)

@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

Name Type Mandatory Description
columnNames Array<string> Yes Names of all columns in the result set.
columnCount number Yes Number of columns in the result set.
rowCount number Yes Number of rows in the result set.
isClosed boolean Yes Whether 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

Type Description
boolean Returns 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

Type Description
boolean Returns 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

Type Description
boolean Returns 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

Type Description
boolean Returns 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

Name Type Mandatory Description
offset number Yes Offset relative to the current position. A negative value means to move backward, and a positive value means to move forward.

Return value

Type Description
boolean Returns 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

Name Type Mandatory Description
position number Yes Destination position to move to.

Return value

Type Description
boolean Returns 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

Name Type Mandatory Description
columnIndex number Yes Index of the target column, starting from 0.

Return value

Type Description
Uint8Array Value 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

Name Type Mandatory Description
columnIndex number Yes Index of the target column, starting from 0.

Return value

Type Description
string Value 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

Name Type Mandatory Description
columnIndex number Yes Index of the target column, starting from 0.

Return value

Type Description
number Value 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

Name Type Mandatory Description
columnIndex number Yes Index of the target column, starting from 0.

Return value

Type Description
number Value 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

Name Type Mandatory Description
columnName string Yes Column name.

Return value

Type Description
number Column 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

Name Type Mandatory Description
columnIndex number Yes Column index.

Return value

Type Description
string Column 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

Name Type Mandatory Description
columnIndex number Yes Column index.

Return value

Type Description
DataType Data 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

Name Value Description
TYPE_NULL 0 Null.
TYPE_LONG 1 Long integer.
TYPE_DOUBLE 2 Double-precision floating-point number.
TYPE_STRING 3 String.
TYPE_BLOB 4 Byte 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)

0  赞