>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.
## Modules to Import<a name="s56d19203690d4782bfc74069abb6bd71"></a>
Obtains a relational database \(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.
<td class="cellrowborder" valign="top" width="49.614822038030226%" headers="mcps1.1.5.1.4 "><p id="p9351151163112"><a name="p9351151163112"></a><a name="p9351151163112"></a>Callback invoked to return the RDB store.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
import dataRdb from '@ohos.data.rdb'
const STORE_CONFIG = { name: "RdbTest.db"}
const SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)"
dataRdb.getRdbStore(STORE_CONFIG, 1, function (err, 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.
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p9368840152411"><a name="p9368840152411"></a><a name="p9368840152411"></a>Promise used to return the RDB store.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
import dataRdb from '@ohos.data.rdb'
const STORE_CONFIG = { name: "RdbTest.db" }
const SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)"
let promise = dataRdb.getRdbStore(STORE_CONFIG, 1);
<td class="cellrowborder" valign="top" width="60.360799609946355%" headers="mcps1.1.5.1.4 "><p id="p7722105418272"><a name="p7722105418272"></a><a name="p7722105418272"></a>Name of the RDB store to delete.</p>
<td class="cellrowborder" valign="top" width="60.360799609946355%" headers="mcps1.1.5.1.4 "><p id="p3473123212716"><a name="p3473123212716"></a><a name="p3473123212716"></a>Callback invoked to return the result. If the RDB store is deleted, <strong id="b1271976267"><a name="b1271976267"></a><a name="b1271976267"></a>true</strong> will be returned. Otherwise, <strong id="b1729162013266"><a name="b1729162013266"></a><a name="b1729162013266"></a>false</strong> will be returned.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
import dataRdb from '@ohos.data.rdb'
dataRdb.deleteRdbStore("RdbTest.db", function (err, rdbStore) {
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p157012058162714"><a name="p157012058162714"></a><a name="p157012058162714"></a>Name of the RDB store to delete.</p>
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p11918175962910"><a name="p11918175962910"></a><a name="p11918175962910"></a>Promise used to return the result. If the RDB store is deleted, <strong id="b139941828205711"><a name="b139941828205711"></a><a name="b139941828205711"></a>true</strong> will be returned. Otherwise, <strong id="b13782985717"><a name="b13782985717"></a><a name="b13782985717"></a>false</strong> will be returned.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
import dataRdb from '@ohos.data.rdb'
let promise = dataRdb.deleteRdbStore("RdbTest.db")
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p13948172810562"><a name="p13948172810562"></a><a name="p13948172810562"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p16532133810511"><a name="p16532133810511"></a><a name="p16532133810511"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1595612471811"><a name="p1595612471811"></a><a name="p1595612471811"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p35911455102217"><a name="p35911455102217"></a><a name="p35911455102217"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p17593942142517"><a name="p17593942142517"></a><a name="p17593942142517"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1996411246271"><a name="p1996411246271"></a><a name="p1996411246271"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1529081319307"><a name="p1529081319307"></a><a name="p1529081319307"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p201222193217"><a name="p201222193217"></a><a name="p201222193217"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p12823846193519"><a name="p12823846193519"></a><a name="p12823846193519"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p11585122912376"><a name="p11585122912376"></a><a name="p11585122912376"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p65868296372"><a name="p65868296372"></a><a name="p65868296372"></a>Minimum value to match the <strong id="b14859186102319"><a name="b14859186102319"></a><a name="b14859186102319"></a>RdbPredicates</strong>.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p2022844615374"><a name="p2022844615374"></a><a name="p2022844615374"></a>Maximum value to match the <strong id="b49418293236"><a name="b49418293236"></a><a name="b49418293236"></a>RdbPredicates</strong>.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1981444134313"><a name="p1981444134313"></a><a name="p1981444134313"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1381844194316"><a name="p1381844194316"></a><a name="p1381844194316"></a>Minimum value to match the <strong id="b14792430192610"><a name="b14792430192610"></a><a name="b14792430192610"></a>RdbPredicates</strong>.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p88354494313"><a name="p88354494313"></a><a name="p88354494313"></a>Maximum value to match the <strong id="b1097254010269"><a name="b1097254010269"></a><a name="b1097254010269"></a>RdbPredicates</strong>.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1691124318464"><a name="p1691124318464"></a><a name="p1691124318464"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p113631950154917"><a name="p113631950154917"></a><a name="p113631950154917"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p514164619518"><a name="p514164619518"></a><a name="p514164619518"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p17334204215534"><a name="p17334204215534"></a><a name="p17334204215534"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p68841111195510"><a name="p68841111195510"></a><a name="p68841111195510"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p77969322566"><a name="p77969322566"></a><a name="p77969322566"></a><strong id="b5671449111214"><a name="b5671449111214"></a><a name="b5671449111214"></a>RdbPredicates</strong> object that matches the column with values sorted in the specified order.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1387316305811"><a name="p1387316305811"></a><a name="p1387316305811"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p1987416318588"><a name="p1987416318588"></a><a name="p1987416318588"></a><strong id="b196751495120"><a name="b196751495120"></a><a name="b196751495120"></a>RdbPredicates</strong> object that matches the column with values sorted in the specified order.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.orderByDesc("AGE")
```
### distinct<a name="section45891667519"></a>
distinct\(\): RdbPredicates
Sets the **RdbPredicates** to filter out duplicate records.
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p1556711111755"><a name="p1556711111755"></a><a name="p1556711111755"></a><strong id="b8722115093117"><a name="b8722115093117"></a><a name="b8722115093117"></a>RdbPredicates</strong> object that specifies the maximum number of records.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Rose").limitAs(3)
```
### offsetAs<a name="section73879413300"></a>
offsetAs\(rowOffset: number\): RdbPredicates
Sets the **RdbPredicates** to specify the start position of the returned result.
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p643616810715"><a name="p643616810715"></a><a name="p643616810715"></a>Number of rows to offset from the beginning. The value is a positive integer.</p>
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p1259123415819"><a name="p1259123415819"></a><a name="p1259123415819"></a><strong id="b1293220983814"><a name="b1293220983814"></a><a name="b1293220983814"></a>RdbPredicates</strong> object that specifies the start position of the returned result.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Rose").offsetAs(3)
```
### groupBy<a name="section87095506331"></a>
groupBy\(fields: Array<string\>\): RdbPredicates
Sets the **RdbPredicates** to group rows that have the same value into summary rows.
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p158461234133315"><a name="p158461234133315"></a><a name="p158461234133315"></a><strong id="b16445510428"><a name="b16445510428"></a><a name="b16445510428"></a>RdbPredicates</strong> object that groups rows with the same value.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.groupBy(["AGE", "NAME"])
```
### indexedBy<a name="section1874631393616"></a>
indexedBy\(indexName: string\): RdbPredicates
Sets the **RdbPredicates** object to specify the index column.
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p654944414119"><a name="p654944414119"></a><a name="p654944414119"></a>Name of the index column.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p163145220191"><a name="p163145220191"></a><a name="p163145220191"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p4140741183519"><a name="p4140741183519"></a><a name="p4140741183519"></a>Column name in the database table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p124921153185019"><a name="p124921153185019"></a><a name="p124921153185019"></a>Row of data to insert.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p742233425213"><a name="p742233425213"></a><a name="p742233425213"></a>Callback invoked to return the result. If the operation is successful, the row ID will be returned. If the operation fails, <strong id="b122361652125110"><a name="b122361652125110"></a><a name="b122361652125110"></a>-1</strong> will be returned.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
const valueBucket = {
"NAME": "Lisa",
"AGE": 18,
"SALARY": 100.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]),
}
rdbStore.insert("EMPLOYEE", valueBucket, function (err, ret) {
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p673516184593"><a name="p673516184593"></a><a name="p673516184593"></a>Row of data to insert.</p>
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p966518169235"><a name="p966518169235"></a><a name="p966518169235"></a>Promise used to return the result. If the operation is successful, the row ID will be returned. If the operation fails, <strong id="b3817192716336"><a name="b3817192716336"></a><a name="b3817192716336"></a>-1</strong> will be returned.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
const valueBucket = {
"NAME": "Lisa",
"AGE": 18,
"SALARY": 100.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]),
}
let promise = rdbStore.insert("EMPLOYEE", valueBucket)
promise.then(async (ret) => {
await console.log(TAG + "insert first done: " + ret)
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p115701665813"><a name="p115701665813"></a><a name="p115701665813"></a>Data to update. The value specifies the row of data to be updated in the database. The key-value pair is associated with the column name in the target table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p142261739615"><a name="p142261739615"></a><a name="p142261739615"></a>Row of data to insert.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p4162947172417"><a name="p4162947172417"></a><a name="p4162947172417"></a>Callback used to return the number of rows updated.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
const valueBucket = {
"NAME": "Rose",
"AGE": 22,
"SALARY": 200.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]),
}
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
rdbStore.update(valueBucket, predicates, function (err, ret) {
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1690919394126"><a name="p1690919394126"></a><a name="p1690919394126"></a>Data to update. The value specifies the row of data to be updated in the database. The key-value pair is associated with the column name in the target table.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p12910739101212"><a name="p12910739101212"></a><a name="p12910739101212"></a>Row of data to insert.</p>
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p1752322401217"><a name="p1752322401217"></a><a name="p1752322401217"></a>Promise used to return the number of rows updated.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
const valueBucket = {
"NAME": "Rose",
"AGE": 22,
"SALARY": 200.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]),
}
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
let promise = rdbStore.update(valueBucket, predicates)
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p5850183112010"><a name="p5850183112010"></a><a name="p5850183112010"></a>Callback invoked to return the number of rows deleted.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
rdbStore.delete(predicates, function (err, rows) {
<td class="cellrowborder" valign="top" width="69.56%" headers="mcps1.1.3.1.2 "><p id="p141862692016"><a name="p141862692016"></a><a name="p141862692016"></a>Promise used to return the number of rows deleted.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p1140502514190"><a name="p1140502514190"></a><a name="p1140502514190"></a>Columns to query. If this parameter is not specified, the query applies to all columns.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p97851358184916"><a name="p97851358184916"></a><a name="p97851358184916"></a>Callback invoked to return the result. If the operation is successful, a <strong id="b987410446454"><a name="b987410446454"></a><a name="b987410446454"></a>ResultSet</strong> object will be returned.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Rose")
rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p129372299204"><a name="p129372299204"></a><a name="p129372299204"></a>Columns to query. If this parameter is not specified, the query applies to all columns.</p>
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p13207156165218"><a name="p13207156165218"></a><a name="p13207156165218"></a>Promise used to return the result. If the operation is successful, a <strong id="b58428584715"><a name="b58428584715"></a><a name="b58428584715"></a>ResultSet</strong> object will be returned.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Rose")
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"])
<td class="cellrowborder" valign="top" width="59.76596782057533%" headers="mcps1.1.5.1.4 "><p id="p1890095820554"><a name="p1890095820554"></a><a name="p1890095820554"></a>Values of the parameters in the SQL statement.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p15628721142612"><a name="p15628721142612"></a><a name="p15628721142612"></a>Values of the parameters in the SQL statement.</p>
<td class="cellrowborder" valign="top" width="82.99%" headers="mcps1.1.3.1.2 "><p id="p3871251119"><a name="p3871251119"></a><a name="p3871251119"></a>Promise used to return the result.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let promise = rdbStore.executeSql("DELETE FROM EMPLOYEE")