# RDB Development ## When to Use On the basis of the SQLite database, the RDB allows you to operate data with or without native SQL statements. An RDB is also called RDB store. ## Available APIs **Creating and Deleting an RDB Store** The following table describes APIs available for creating and deleting an RDB store. **Table 1** APIs for creating and deleting an RDB store

Class

API

Description

dataRdb

getRdbStore(config: StoreConfig, version: number, callback: AsyncCallback<RdbStore>): void

Obtains an RDB store. You can set parameters for the RDB store based on service requirements, call APIs to perform data operations, and use a callback to return the result.

  • config: configuration of the RDB store.
  • version: version of the RDB store.
  • callback: callback invoked to return the RDB store.

dataRdb

getRdbStore(config: StoreConfig, version: number): Promise<RdbStore>

Obtains an RDB store. You can set parameters for the RDB store based on service requirements, call APIs to perform data operations, and use a promise to return the result.

  • config: configuration of the RDB store.
  • version: version of the RDB store.

dataRdb

deleteRdbStore(name: string, callback: AsyncCallback<void>): void

Deletes an RDB store. This method uses a callback to return the result.

  • name: name of the RDB store to delete.
  • callback: callback invoked to return the result. If the RDB store is deleted, true will be returned. Otherwise, false will be returned.

dataRdb

deleteRdbStore(name: string): Promise<void>

Deletes an RDB store. This method uses a promise to return the result.

  • name: name of the RDB store to delete.
**Managing Data in an RDB Store** The RDB provides APIs for inserting, deleting, modifying, and querying data in the local RDB store. - **Inserting data** The RDB provides methods for inserting data through **ValuesBucket** in a data table. If the data is inserted successfully, the row number of the data inserted is returned; otherwise, **-1** is returned. **Table 2** APIs for inserting data to a data table

Class

API

Description

RdbStore

insert(name: string, values: ValuesBucket, callback: AsyncCallback<number>):void

Inserts a row of data into a table. This method uses a callback to return the result.

  • name: name of the target table.
  • values: data to be inserted into the table.
  • callback: callback invoked to return the result. If the operation is successful, the row ID will be returned. Otherwise, -1 will be returned.

RdbStore

insert(name: string, values: ValuesBucket): Promise<number>

Inserts a row of data into a table. This method uses a promise to return the result.

  • name: name of the target table.
  • values: data to be inserted into the table.
- **Updating data** Call the **update\(\)** method to pass the new data and specify the update conditions by using **RdbPredicates**. If the data is successfully updated, the row number of the updated data is returned; otherwise, **0** is returned. **Table 3** APIs for updating data

Class

API

Description

RdbStore

update(values: ValuesBucket, rdbPredicates: RdbPredicates, callback: AsyncCallback<number>):void

Updates the data that meets the conditions specified by the RdbPredicates object. This method uses a callback to return the result.

  • values: data to update, which is stored in ValuesBucket.
  • rdbPredicates: conditions for updating data.
  • callback: callback invoked to return the number of rows updated.

RdbStore

update(values: ValuesBucket, rdbPredicates: RdbPredicates): Promise

Updates the data that meets the conditions specified by the RdbPredicates object. This method uses a promise to return the result.

  • values: data to update, which is stored in ValuesBucket.
  • rdbPredicates: conditions for updating data.
- **Deleting data** Call the **delete\(\)** method to delete data meeting the conditions specified by **RdbPredicates**. If the data is deleted, the row number of the deleted data is returned; otherwise, **0** is returned. **Table 4** APIs for deleting data

Class

API

Description

RdbStore

delete(rdbPredicates: RdbPredicates, callback: AsyncCallback<number>):void

Deletes data based on the conditions specified by RdbPredicates. This method uses a callback to return the result.

  • rdbPredicates: conditions for deleting data.
  • callback: callback invoked to return the number of rows deleted.

RdbStore

delete(rdbPredicates: RdbPredicates): Promise

Deletes data based on the conditions specified by RdbPredicates. This method uses a promise to return the result.

  • rdbPredicates: conditions for deleting data.
- **Querying data** You can query data in the RDB in either of the following ways: - Call the **query** method to query data based on the predicates, without passing any SQL statement. - Run the native SQL statement. **Table 5** APIs for querying data

Class

API

Description

RdbStore

query(rdbPredicates: RdbPredicates, columns: Array, callback: AsyncCallback<ResultSet>): void;

Queries data in the database based on specified conditions. This method uses a callback to return the result.

  • rdbPredicates: conditions for querying data.
  • columns: columns to query. If this parameter is not specified, the query applies to all columns.
  • callback: callback invoked to return the result. If the operation is successful, a ResultSet object will be returned.

RdbStore

query(rdbPredicates: RdbPredicates, columns: Array): Promise<ResultSet>;

Queries data in the database based on specified conditions. This method uses a promise to return the result.

  • rdbPredicates: conditions for querying data.
  • columns: columns to query. If this parameter is not specified, the query applies to all columns.

RdbStore

querySql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<ResultSet>):void

Queries data in the RDB store using the specified SQL statement. This method uses a callback to return the result.

  • sql: SQL statement.
  • bindArgs: arguments in the SQL statement.
  • callback: callback invoked to return the result. If the operation is successful, a ResultSet object will be returned.

RdbStore

querySql(sql: string, bindArgs?: Array<ValueType>):Promise<ResultSet>

Queries data in the RDB store using the specified SQL statement. This method uses a promise to return the result.

  • sql: SQL statement.
  • bindArgs: arguments in the SQL statement.
**Using Predicates** The RDB provides **RdbPredicates** for you to set database operation conditions. **Table 6** APIs for RDB predicates

Class

API

Description

RdbPredicates

equalTo(field: string, value: ValueType): RdbPredicates

Sets the RdbPredicates to match the field with data type ValueType and value equal to the specified value.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

notEqualTo(field: string, value: ValueType): RdbPredicates

Sets the RdbPredicates to match the field with data type ValueType and value not equal to the specified value.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

beginWrap(): RdbPredicates

Adds a left parenthesis to the RdbPredicates.

  • RdbPredicates: RdbPredicates with a left parenthesis.

RdbPredicates

endWrap(): RdbPredicates

Adds a right parenthesis to the RdbPredicates.

  • RdbPredicates: RdbPredicates with a right parenthesis.

RdbPredicates

or(): RdbPredicates

Adds the OR condition to the RdbPredicates.

  • RdbPredicates: RdbPredicates with the OR condition.

RdbPredicates

and(): RdbPredicates

Adds the AND condition to the RdbPredicates.

  • RdbPredicates: RdbPredicates with the AND condition.

RdbPredicates

contains(field: string, value: string): RdbPredicats

Sets the RdbPredicates to match a string containing the specified value.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

beginsWith(field: string, value: string): RdbPredicates

Sets the RdbPredicates to match a string that starts with the specified value.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

endsWith(field: string, value: string): RdbPredicates

Sets the RdbPredicates to match a string that ends with the specified value.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

isNull(field: string): RdbPredicates

Sets the RdbPredicates to match the field whose value is null.

  • field: column name in the database table.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

isNotNull(field: string): RdbPredicates

Sets the RdbPredicates to match the field whose value is not null.

  • field: column name in the database table.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

like(field: string, value: string): RdbPredicates

Sets the RdbPredicates to match a string that is similar to the specified value.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

glob(field: string, value: string): RdbPredicates

Sets the RdbPredicates to match the specified string.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

between(field: string, low: ValueType, high: ValueType): RdbPredicates

Sets the RdbPredicates to match the field with data type ValueType and value within the specified range.

  • field: column name in the database table.
  • low: minimum value that matches the RdbPredicates.
  • high: maximum value that matches the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

notBetween(field: string, low: ValueType, high: ValueType): RdbPredicates

Sets the RdbPredicates to match the field with data type ValueType and value out of the specified range.

  • field: column name in the database table.
  • low: minimum value that matches the RdbPredicates.
  • high: maximum value that matches the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

greaterThan(field: string, value: ValueType): RdbPredicatesgr

Sets the RdbPredicates to match the field with data type ValueType and value greater than the specified value.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

lessThan(field: string, value: ValueType): RdbPredicates

Sets the RdbPredicates to match the field with data type ValueType and value less than the specified value.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

greaterThanOrEqualTo(field: string, value: ValueType): RdbPredicates

Sets the RdbPredicates to match the field with data type ValueType and value greater than or equal to the specified value.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

lessThanOrEqualTo(field: string, value: ValueType): RdbPredicates

Sets the RdbPredicates to match the field with data type ValueType and value less than or equal to the specified value.

  • field: column name in the database table.
  • value: value to match the RdbPredicates.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

orderByAsc(field: string): RdbPredicates

Sets the RdbPredicates to match the column with values sorted in ascending order.

  • field: column name in the database table.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

orderByDesc(field: string): RdbPredicates

Sets the RdbPredicates to match the column with values sorted in descending order.

  • field: column name in the database table.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

distinct(): RdbPredicates

Sets the RdbPredicates to filter out duplicate records.

  • RdbPredicates: RdbPredicates that can filter out duplicate records.

RdbPredicates

limitAs(value: number): RdbPredicates

Sets the RdbPredicates to specify the maximum number of records to match in a column.

  • value: maximum number of records in a column.
  • RdbPredicates: RdbPredicates that can be used to set the maximum number of records to match in a column.

RdbPredicates

offsetAs(rowOffset: number): RdbPredicates

Sets the RdbPredicates to specify the start position of the returned result.

  • rowOffset: start position of the returned result. The value is a positive integer.
  • RdbPredicates: RdbPredicates object that specifies the start position of the returned result.

RdbPredicates

groupBy(fields: Array<string>): RdbPredicates

Sets the RdbPredicates to group rows that have the same value into summary rows.

  • fields: names of the columns grouped for querying data.
  • RdbPredicates: RdbPredicates that groups rows that have the same value.

RdbPredicates

indexedBy(indexName: string): RdbPredicates

Sets the RdbPredicates object to specify the index column.

  • indexName: name of the index column.
  • RdbPredicates: RdbPredicates object that specifies the index column.

RdbPredicates

in(field: string, value: Array<ValueType>): RdbPredicates

Sets the RdbPredicates to match the field with data type Array<ValueType> and value within the specified range.

  • field: column name in the database table.
  • value: array of ValueType to match.
  • RdbPredicates: RdbPredicates object that matches the specified field.

RdbPredicates

notIn(field: string, value: Array<ValueType>): RdbPredicates

Sets the RdbPredicates to match the field with data type Array<ValueType> and value out of the specified range.

  • field: column name in the database table.
  • value: array of ValueType to match.
  • RdbPredicates: RdbPredicates object that matches the specified field.
**Using the Result Set** A result set can be regarded as rows of data in the queried results. It allows you to traverse and access the data you have queried. The following table describes the external APIs of **ResultSet**. >![](public_sys-resources/icon-notice.gif) **NOTICE:** >After a result set is used, you must call the **close\(\)** method to close it explicitly. **Table 7** APIs for using the result set

Class

API

Description

ResultSet

goTo(offset:number): boolean

Moves the result set forwards or backwards by an offset relative to its current position.

ResultSet

goToRow(position: number): boolean

Moves the result set to a specified row.

ResultSet

goToNextRow(): boolean

Moves the result set to the next row.

ResultSet

goToPreviousRow(): boolean

Moves the result set to the previous row.

ResultSet

getColumnIndex(columnName: string): number

Obtains the column index based on the specified column name.

ResultSet

getColumnName(columnIndex: number): string

Obtains the column name based on the specified column index.

ResultSet

goToFirstRow(): boolean

Checks whether the result set is located in the first row.

ResultSet

goToLastRow(): boolean

Checks whether the result set is located in the last row.

ResultSet

getString(columnIndex: number): string

Obtains the values in the specified column of the current row, in strings.

ResultSet

getBlob(columnIndex: number): Uint8Array

Obtains the values in the specified column of the current row, in a byte array.

ResultSet

getDouble(columnIndex: number): number

Obtains the values in the specified column of the current row, in double.

ResultSet

isColumnNull(columnIndex: number): boolean

Checks whether the values in the specified column of the current row are null.

ResultSet

close(): void

Closes the result set.

**Encrypting an RDB Store** You can encrypt an RDB store. When creating an RDB store, you can add a key for security purposes. After that, the RDB store can be accessed only with the correct key. You can change the key but cannot delete it. Once an RDB store is created without a key, you cannot add a key any longer. **Table 8** APIs for changing the key

Class

API

Description

RdbStore

changeEncryptKey(newEncryptKey:Uint8Array, callback: AsyncCallback<number>):void;

Changes the encryption key for an RDB store. This method uses a callback to return the result. If the key is changed, 0 is returned. Otherwise, a non-zero value is returned.

RdbStore

changeEncryptKey(newEncryptKey:Uint8Array): Promise<number>;

Changes the encryption key for an RDB store. This method uses a promise to return the result. If the key is changed, 0 is returned. Otherwise, a non-zero value is returned.

## How to Develop 1. Create an RDB store. 1. Configure the RDB attributes, including the name and storage mode of the database and whether it is read-only. 2. Initialize the table structure and related data in the database. 3. Create an RDB store. The sample code is as follows: ``` import dataRdb from '@ohos.data.rdb'; const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"; const STORE_CONFIG = {name: "rdbstore.db",} let rdbStore = await dataRdb.getRdbStore(STORE_CONFIG, 1); await rdbStore.executeSql(CREATE_TABLE_TEST); ``` 2. Insert data. 1. Create a **ValuesBucket** object to store the data you need to insert. 2. Call the **insert\(\)** method to insert data into the RDB store. The sample code is as follows: ``` var u8 = new Uint8Array([1, 2, 3]) const valueBucket = {"name": "Tom", "age": 18, "salary": 100.5, "blobType": u8,} let insertPromise = rdbStore.insert("test", valueBucket) ``` 3. Query data. 1. Create an **RdbPredicates** object to specify query conditions. 2. Call the **query \(\)** method to query data in the RDB store. 3. Call the **ResultSet\(\)** method to obtain the query result. The sample code is as follows: ``` let predicates = new dataRdb.RdbPredicates("test"); predicates.equalTo("name", "Tom") let resultSet = await rdbStore.query(predicates) resultSet.goToFirstRow() const id = await resultSet.getLong(resultSet.getColumnIndex("id")) const name = await resultSet.getString(resultSet.getColumnIndex("name")) const age = await resultSet.getLong(resultSet.getColumnIndex("age")) const salary = await resultSet.getDouble(resultSet.getColumnIndex("salary")) const blobType = await resultSet.getBlob(resultSet.getColumnIndex("blobType")) resultSet.close() ```