@ohos.data.relationalStore (RDB Store) (System API)

The relational database (RDB) store manages data based on relational models. It provides a complete mechanism for managing local databases based on the underlying SQLite. To satisfy different needs in complicated scenarios, the RDB store offers a series of APIs for performing operations such as adding, deleting, modifying, and querying data, and supports direct execution of SQL statements. The worker threads are not supported.

ArkTS supports the following basic data types: number, string, binary data, and boolean. The maximum size of a data record is 2 MB. If a data record exceeds 2 MB, it can be inserted successfully but cannot be read.

The relationalStore module provides the following:

  • RdbPredicates: provides predicates indicating the nature, feature, or relationship of a data entity in an RDB store. It is used to define the operation conditions for an RDB store.
  • RdbStore: provides APIs for managing data in an RDB store.
  • ResultSet: provides APIs for accessing the result set obtained from the RDB store.

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.

  • This topic describes only the system APIs provided by the module. For details about its public APIs, see @ohos.data.relationalStore.

Modules to Import

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

StoreConfig

Defines the RDB store configuration.

Name Type Mandatory Description
isSearchable11+ boolean No Whether the RDB store is searchable. The value true means the RDB store is searchable; the value false means the opposite. The default value is false.
System API: This is a system API.
This parameter is supported since API version 11.
System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Reference11+

Represents the reference between tables by field. If table b references table a, table a is the source table and b is the target table.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

System API: This is a system API.

Name Type Mandatory Description
sourceTable string Yes Source table.
targetTable string Yes Target table.
refFields Record<string, string> Yes Fields referenced. In a KV pair, the key indicates the field in the source table, and the value indicates the field in the target table.

DistributedConfig10+

Defines the configuration of the distributed mode of tables.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Name Type Mandatory Description
references11+ Array<Reference> No References between tables. You can reference multiple fields, and their values must be the same in the source and target tables. By default, database tables are not referenced with each other.
System API: This is a system API.
This parameter is supported since API version 11.

RdbStore

Provides APIs for managing data in an RDB store. Before using the RdbStore APIs, use executeSql to initialize the database table structure and related data.

update

update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<number>):void

Updates data based on the specified DataSharePredicates object. This API uses an asynchronous callback to return the result. Due to the limit of the shared memory (max. 2 MB), a single data record cannot exceed 2 MB. Otherwise, the query operation will fail.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

Parameters

Name Type Mandatory Description
table string Yes Name of the target table.
values ValuesBucket Yes Rows of data to update in the RDB store. The key-value pair is associated with the column name in the target table.
predicates dataSharePredicates.DataSharePredicates Yes Update conditions specified by the DataSharePredicates object.
callback AsyncCallback<number> Yes Callback invoked to return the number of rows updated.

Error codes

For details about the error codes, see RDB Error Codes.

ID Error Message
14800047 The WAL file size exceeds the default limit.
14800000 Inner error.

Example

import dataSharePredicates from '@ohos.data.dataSharePredicates'
import { ValuesBucket } from '@ohos.data.ValuesBucket';

let value1 = "Rose";
let value2 = 22;
let value3 = 200.5;
let value4 = new Uint8Array([1, 2, 3, 4, 5]);

// You can use either of the following:
const valueBucket1: ValuesBucket = {
  'NAME': value1,
  'AGE': value2,
  'SALARY': value3,
  'CODES': value4,
};
const valueBucket2: ValuesBucket = {
  NAME: value1,
  AGE: value2,
  SALARY: value3,
  CODES: value4,
};
const valueBucket3: ValuesBucket = {
  "NAME": value1,
  "AGE": value2,
  "SALARY": value3,
  "CODES": value4,
};

let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
if(store != undefined) {
  (store as relationalStore.RdbStore).update("EMPLOYEE", valueBucket1, predicates, (err, rows) => {
    if (err) {
      console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
      return;
    }
    console.info(`Updated row count: ${rows}`);
  })
}

update

update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates):Promise<number>

Updates data based on the specified DataSharePredicates object. This API uses a promise to return the result. Due to the limit of the shared memory (max. 2 MB), a single data record cannot exceed 2 MB. Otherwise, the query operation will fail.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

Parameters

Name Type Mandatory Description
table string Yes Name of the target table.
values ValuesBucket Yes Rows of data to update in the RDB store. The key-value pair is associated with the column name in the target table.
predicates dataSharePredicates.DataSharePredicates Yes Update conditions specified by the DataSharePredicates object.

Return value

Type Description
Promise<number> Promise used to return the number of rows updated.

Error codes

For details about the error codes, see RDB Error Codes.

ID Error Message
14800047 The WAL file size exceeds the default limit.
14800000 Inner error.

Example

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

let value1 = "Rose";
let value2 = 22;
let value3 = 200.5;
let value4 = new Uint8Array([1, 2, 3, 4, 5]);

// You can use either of the following:
const valueBucket1: ValuesBucket = {
  'NAME': value1,
  'AGE': value2,
  'SALARY': value3,
  'CODES': value4,
};
const valueBucket2: ValuesBucket = {
  NAME: value1,
  AGE: value2,
  SALARY: value3,
  CODES: value4,
};
const valueBucket3: ValuesBucket = {
  "NAME": value1,
  "AGE": value2,
  "SALARY": value3,
  "CODES": value4,
};

let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
if(store != undefined) {
  (store as relationalStore.RdbStore).update("EMPLOYEE", valueBucket1, predicates).then(async (rows: Number) => {
    console.info(`Updated row count: ${rows}`);
  }).catch((err: BusinessError) => {
    console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
  })
}

delete

delete(table: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<number>):void

Deletes data from the RDB store based on the specified DataSharePredicates object. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

Parameters

Name Type Mandatory Description
table string Yes Name of the target table.
predicates dataSharePredicates.DataSharePredicates Yes Conditions specified by the DataSharePredicates object for deleting data.
callback AsyncCallback<number> Yes Callback invoked to return the number of rows deleted.

Error codes

For details about the error codes, see RDB Error Codes.

ID Error Message
14800047 The WAL file size exceeds the default limit.
14800000 Inner error.

Example

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

let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
if(store != undefined) {
  (store as relationalStore.RdbStore).delete("EMPLOYEE", predicates, (err, rows) => {
    if (err) {
      console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
      return;
    }
    console.info(`Delete rows: ${rows}`);
  })
}

delete

delete(table: string, predicates: dataSharePredicates.DataSharePredicates):Promise<number>

Deletes data from the RDB store based on the specified DataSharePredicates object. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

Parameters

Name Type Mandatory Description
table string Yes Name of the target table.
predicates dataSharePredicates.DataSharePredicates Yes Conditions specified by the DataSharePredicates object for deleting data.

Return value

Type Description
Promise<number> Promise used to return the number of rows deleted.

Error codes

For details about the error codes, see RDB Error Codes.

ID Error Message
14800047 The WAL file size exceeds the default limit.
14800000 Inner error.

Example

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

let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
if(store != undefined) {
  (store as relationalStore.RdbStore).delete("EMPLOYEE", predicates).then((rows: Number) => {
    console.info(`Delete rows: ${rows}`);
  }).catch((err: BusinessError) => {
    console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
  })
}

query10+

query(table: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<ResultSet>):void

Queries data from the RDB store based on specified conditions. This API uses an asynchronous callback to return the result. Due to the limit of the shared memory (max. 2 MB), a single data record cannot exceed 2 MB. Otherwise, the query operation will fail.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

Parameters

Name Type Mandatory Description
table string Yes Name of the target table.
predicates dataSharePredicates.DataSharePredicates Yes Query conditions specified by the DataSharePredicates object.
callback AsyncCallback<ResultSet> Yes Callback invoked to return the result. If the operation is successful, a ResultSet object will be returned.

Error codes

For details about the error codes, see RDB Error Codes.

ID Error Message
14800000 Inner error.

Example

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

let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Rose");
if(store != undefined) {
  (store as relationalStore.RdbStore).query("EMPLOYEE", predicates, (err, resultSet) => {
    if (err) {
      console.error(`Query failed, code is ${err.code},message is ${err.message}`);
      return;
    }
    console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
    // resultSet is a cursor of a data set. By default, the cursor points to the -1st record. Valid data starts from 0.
    while (resultSet.goToNextRow()) {
      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"));
      console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
    }
    // Release the dataset memory.
    resultSet.close();
  })
}

query

query(table: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void

Queries data from the RDB store based on specified conditions. This API uses an asynchronous callback to return the result. Due to the limit of the shared memory (max. 2 MB), a single data record cannot exceed 2 MB. Otherwise, the query operation will fail.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

Parameters

Name Type Mandatory Description
table string Yes Name of the target table.
predicates dataSharePredicates.DataSharePredicates Yes Query conditions specified by the DataSharePredicates object.
columns Array<string> Yes Columns to query. If this parameter is not specified, the query applies to all columns.
callback AsyncCallback<ResultSet> Yes Callback invoked to return the result. If the operation is successful, a ResultSet object will be returned.

Error codes

For details about the error codes, see RDB Error Codes.

ID Error Message
14800000 Inner error.

Example

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

let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Rose");
if(store != undefined) {
  (store as relationalStore.RdbStore).query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], (err, resultSet) => {
    if (err) {
      console.error(`Query failed, code is ${err.code},message is ${err.message}`);
      return;
    }
    console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
    // resultSet is a cursor of a data set. By default, the cursor points to the -1st record. Valid data starts from 0.
    while (resultSet.goToNextRow()) {
      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"));
      console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
    }
    // Release the dataset memory.
    resultSet.close();
  })
}

query

query(table: string, predicates: dataSharePredicates.DataSharePredicates, columns?: Array<string>):Promise<ResultSet>

Queries data from the RDB store based on specified conditions. This API uses a promise to return the result. Due to the limit of the shared memory (max. 2 MB), a single data record cannot exceed 2 MB. Otherwise, the query operation will fail.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

Model restriction: This API can be used only in the stage model.

System API: This is a system API.

Parameters

Name Type Mandatory Description
table string Yes Name of the target table.
predicates dataSharePredicates.DataSharePredicates Yes Query conditions specified by the DataSharePredicates object.
columns Array<string> No Columns to query. If this parameter is not specified, the query applies to all columns.

Return value

Type Description
Promise<ResultSet> Promise used to return the result. If the operation is successful, a ResultSet object will be returned.

Error codes

For details about the error codes, see RDB Error Codes.

ID Error Message
14800000 Inner error.

Example

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

let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Rose");
if(store != undefined) {
  (store as relationalStore.RdbStore).query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((resultSet: relationalStore.ResultSet) => {
    console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
    // resultSet is a cursor of a data set. By default, the cursor points to the -1st record. Valid data starts from 0.
    while (resultSet.goToNextRow()) {
      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"));
      console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
    }
    // Release the dataset memory.
    resultSet.close();
  }).catch((err: BusinessError) => {
    console.error(`Query failed, code is ${err.code},message is ${err.message}`);
  })
}

cloudSync11+

cloudSync(mode: SyncMode, predicates: RdbPredicates, progress: Callback<ProgressDetails>, callback: AsyncCallback<void>): void

Manually performs device-cloud sync based on specified conditions. This API uses an asynchronous callback to return the result. The cloud sync function must be implemented. Otherwise, this API cannot be used.

System capability: SystemCapability.DistributedDataManager.CloudSync.Client

System API: This is a system API.

Parameters

Name Type Mandatory Description
mode SyncMode Yes Sync mode of the database.
predicates RdbPredicates Yes Conditions for data sync.
progress Callback<ProgressDetails> Yes Callback invoked to process database sync details.
callback AsyncCallback<void> Yes Callback invoked to send the sync result to the caller.

Example

let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.in("id", ["id1", "id2"]);

if(store != undefined) {
  (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, predicates, (progressDetail: relationalStore.ProgressDetails) => {
    console.info(`progress: ${progressDetail}`);
   }, (err) => {
     if (err) {
       console.error(`cloudSync failed, code is ${err.code},message is ${err.message}}`);
       return;
     }
     console.info('Cloud sync succeeded');
  });
};

cloudSync11+

cloudSync(mode: SyncMode, predicates: RdbPredicates, progress: Callback<ProgressDetails>): Promise<void>

Manually performs device-cloud sync based on specified conditions. This API uses a promise to return the result. The cloud sync function must be implemented. Otherwise, this API cannot be used.

System capability: SystemCapability.DistributedDataManager.CloudSync.Client

System API: This is a system API.

Parameters

Name Type Mandatory Description
mode SyncMode Yes Sync mode of the database.
predicates RdbPredicates Yes Conditions for data sync.
progress Callback<ProgressDetails> Yes Callback invoked to process database sync details.

Return value

Type Description
Promise<void> Promise used to return the sync result.

Example

import {BusinessError} from "@ohos.base";

let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.in("id", ["id1", "id2"]);

if(store != undefined) {
  (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, predicates, (progressDetail: relationalStore.ProgressDetails) => {
    console.info(`progress: ${progressDetail}`);
  }).then(() => {
    console.info('Cloud sync succeeded');
  }).catch((err: BusinessError) => {
    console.error(`cloudSync failed, code is ${err.code},message is ${err.message}}`);
  });
};

querySharingResource11+

querySharingResource(predicates: RdbPredicates, columns?: Array<string>): Promise<ResultSet>

Queries the shared resource of the data matching the specified conditions. This API uses a promise to return the result set, which includes the shared resource ID and the column names if the column names are specified.

System capability: SystemCapability.DistributedDataManager.CloudSync.Client

System API: This is a system API.

Parameters

Name Type Mandatory Description
predicates RdbPredicates Yes Query conditions.
columns Array<string> No Columns to be searched for. If this parameter is not specified, the returned result set contains only the shared resource ID.

Return value

Name Description
Promise<ResultSet> Promise used to return the result set.

Error codes

For details about the error codes, see RDB Error Codes.

ID Error Message
14800000 Inner error.

Example

import { BusinessError } from "@ohos.base";

let sharingResource: string;
let predicates = new relationalStore.RdbPredicates('test_table');
predicates.equalTo('data', 'data_test');
if(store != undefined) {
  (store as relationalStore.RdbStore).querySharingResource(predicates, ['uuid', 'data']).then((resultSet) => {
    if (!resultSet.goToFirstRow()) {
      console.error(`resultSet error`);
      return;
    }
    const res = resultSet.getString(resultSet.getColumnIndex(relationalStore.Field.SHARING_RESOURCE_FIELD));
    console.info(`sharing resource: ${res}`);
    sharingResource = res;
  }).catch((err: BusinessError) => {
    console.error(`query sharing resource failed, code is ${err.code},message is ${err.message}`);
  })
}

querySharingResource11+

querySharingResource(predicates: RdbPredicates, callback: AsyncCallback<ResultSet>): void

Queries the shared resource of the data matching the specified conditions. This API uses an asynchronous callback to return the result set.

System capability: SystemCapability.DistributedDataManager.CloudSync.Client

System API: This is a system API.

Parameters

Name Type Mandatory Description
predicates RdbPredicates Yes Query conditions.
callback AsyncCallback<ResultSet> Yes Callback invoked to return the result set.

Error codes

For details about the error codes, see RDB Error Codes.

ID Error Message
14800000 Inner error.

Example

let sharingResource: string;
let predicates = new relationalStore.RdbPredicates('test_table');
predicates.equalTo('data', 'data_test');
if(store != undefined) {
  (store as relationalStore.RdbStore).querySharingResource(predicates,(err, resultSet) => {
    if (err) {
      console.error(`sharing resource failed, code is ${err.code},message is ${err.message}`);
      return;
    }
    if (!resultSet.goToFirstRow()) {
      console.error(`resultSet error`);
      return;
    }
    const res = resultSet.getString(resultSet.getColumnIndex(relationalStore.Field.SHARING_RESOURCE_FIELD));
    console.info(`sharing resource: ${res}`);
    sharingResource = res;
  })
}

querySharingResource11+

querySharingResource(predicates: RdbPredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>): void

Queries the shared resource of the data matching the specified conditions. This API uses an asynchronous callback to return the shared resource ID and the column names specified.

System capability: SystemCapability.DistributedDataManager.CloudSync.Client

System API: This is a system API.

Parameters

Name Type Mandatory Description
predicates RdbPredicates Yes Query conditions.
columns Array<string> Yes Columns to be searched for.
callback AsyncCallback<ResultSet> Yes Callback invoked to return the result set.

Error codes

For details about the error codes, see RDB Error Codes.

ID Error Message
14800000 Inner error.

Example

let sharingResource: string;
let predicates = new relationalStore.RdbPredicates('test_table');
predicates.equalTo('data', 'data_test');
if(store != undefined) {
  (store as relationalStore.RdbStore).querySharingResource(predicates, ['uuid', 'data'], (err, resultSet) => {
    if (err) {
      console.error(`sharing resource failed, code is ${err.code},message is ${err.message}`);
      return;
    }
    if (!resultSet.goToFirstRow()) {
      console.error(`resultSet error`);
      return;
    }
    const res = resultSet.getString(resultSet.getColumnIndex(relationalStore.Field.SHARING_RESOURCE_FIELD));
    console.info(`sharing resource: ${res}`);
    sharingResource = res;
  })
}