未验证 提交 a0dfd763 编写于 作者: O openharmony_ci 提交者: Gitee

!6249 [翻译完成】#I5BY22

Merge pull request !6249 from Annie_wang/PR5200A
# Relational Database
The relational database (RDB) manages data based on relational models. With the underlying SQLite database, the RDB provides a complete mechanism for managing local databases. To satisfy different needs in complicated scenarios, the RDB offers a series of methods for performing operations such as adding, deleting, modifying, and querying data, and supports direct execution of SQL statements.
This module provides the following RDB-related functions:
- [RdbPredicates](#rdbpredicates): 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](#rdbstore): provides APIs for managing an RDB store.
> **NOTE**<br/>
>
> 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
......@@ -11,71 +19,9 @@ import data_rdb from '@ohos.data.rdb';
## data_rdb.getRdbStore
getRdbStore(config: StoreConfig, version: number, callback: AsyncCallback&lt;RdbStore&gt;): void
Obtains a relational database (RDB) store. This API uses an asynchronous callback to return the result. You can set parameters for the RDB store based on service requirements and call APIs to perform data operations.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| config | [StoreConfig](#storeconfig) | Yes| Configuration of the RDB store.|
| version | number | Yes| RDB store version.|
| callback | AsyncCallback&lt;[RdbStore](#rdbstore)&gt; | Yes| Callback invoked to return the RDB store obtained.|
**Example**
```js
const STORE_CONFIG = { name: "RdbTest.db"}
data_rdb.getRdbStore(STORE_CONFIG, 1, function (err, rdbStore) {
if (err) {
console.info("Failed to get RdbStore, err: " + err)
return
}
console.log("Got RdbStore successfully.")
})
```
## data_rdb.getRdbStore
getRdbStore(config: StoreConfig, version: number): Promise&lt;RdbStore&gt;
Obtains an RDB store. This API uses a promise to return the result. You can set parameters for the RDB store based on service requirements and call APIs to perform data operations.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| config | [StoreConfig](#storeconfig) | Yes| Configuration of the RDB store.|
| version | number | Yes| RDB store version.|
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;[RdbStore](#rdbstore)&gt; | Promise used to return the RDB store obtained.|
**Example**
```js
const STORE_CONFIG = { name: "RdbTest.db" }
let promise = data_rdb.getRdbStore(STORE_CONFIG, 1);
promise.then(async (rdbStore) => {
console.log("Got RdbStore successfully.")
}).catch((err) => {
console.log("Failed to get RdbStore, err: " + err)
})
```
## data_rdb.getRdbStore<sup>8+</sup>
getRdbStore(context: Context, config: StoreConfig, version: number, callback: AsyncCallback&lt;RdbStore&gt;): void
Obtains a RDB store. This API uses an asynchronous callback to return the result. You can set parameters for the RDB store based on service requirements and call APIs to perform data operations.
Obtains an RDB store. This API uses an asynchronous callback to return the result. You can set parameters for the RDB store based on service requirements and call APIs to perform data operations.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
......@@ -83,7 +29,7 @@ Obtains a RDB store. This API uses an asynchronous callback to return the result
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context<sup>8+</sup> | Context | Yes| Context of the app or functionality.<br>For the definition of **Context** of API version 8, see [Context](js-apis-Context.md).<br>For the definition of **Context** of API version 9, see [Context](js-apis-ability-context.md).|
| context | Context | Yes| Context of the application or functionality.<br>See [Context](js-apis-Context.md) for versions earlier than API version 9.<br>See [Context](js-apis-ability-context.md) for API version 9 or later.|
| config | [StoreConfig](#storeconfig) | Yes| Configuration of the RDB store.|
| version | number | Yes| RDB store version.|
| callback | AsyncCallback&lt;[RdbStore](#rdbstore)&gt; | Yes| Callback invoked to return the RDB store obtained.|
......@@ -101,7 +47,7 @@ data_rdb.getRdbStore(this.context, STORE_CONFIG, 1, function (err, rdbStore) {
})
```
## data_rdb.getRdbStore<sup>8+</sup>
## data_rdb.getRdbStore
getRdbStore(context: Context, config: StoreConfig, version: number): Promise&lt;RdbStore&gt;
......@@ -113,7 +59,7 @@ Obtains an RDB store. This API uses a promise to return the result. You can set
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context<sup>8+</sup> | Context | Yes| Context of the app or functionality.<br>For the definition of **Context** of API version 8, see [Context](js-apis-Context.md).<br>For the definition of **Context** of API version 9, see [Context](js-apis-ability-context.md).|
| context | Context | Yes| Context of the application or functionality.<br>See [Context](js-apis-Context.md) for versions earlier than API version 9.<br>See [Context](js-apis-ability-context.md) for API version 9 or later.|
| config | [StoreConfig](#storeconfig) | Yes| Configuration of the RDB store.|
| version | number | Yes| RDB store version.|
......@@ -137,68 +83,16 @@ promise.then(async (rdbStore) => {
## data_rdb.deleteRdbStore
deleteRdbStore(name: string, callback: AsyncCallback&lt;void&gt;): void
Deletes an RDB store. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the RDB store to delete.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked to return the result.|
**Example**
```js
data_rdb.deleteRdbStore("RdbTest.db", function (err, rdbStore) {
if (err) {
console.info("Failed to delete RdbStore, err: " + err)
return
}
console.log("Deleted RdbStore successfully.")
})
```
## data_rdb.deleteRdbStore
deleteRdbStore(name: string): Promise&lt;void&gt;
Deletes an RDB store. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the RDB store to delete.|
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Example**
```js
let promise = data_rdb.deleteRdbStore("RdbTest.db")
promise.then(()=>{
console.log("Deleted RdbStore successfully.")
}).catch((err) => {
console.info("Failed to delete RdbStore, err: " + err)
})
```
## data_rdb.deleteRdbStore<sup>8+</sup>
deleteRdbStore(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void
Deletes an RDB store. This API uses a callback to return the result.
Deletes an RDB store. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context<sup>8+</sup> | Context | Yes| Context of the app or functionality.<br>For the definition of **Context** of API version 8, see [Context](js-apis-Context.md).<br>For the definition of **Context** of API version 9, see [Context](js-apis-ability-context.md).|
| context | Context | Yes| Context of the application or functionality.<br>See [Context](js-apis-Context.md) for versions earlier than API version 9.<br>See [Context](js-apis-ability-context.md) for API version 9 or later.|
| name | string | Yes| Name of the RDB store to delete.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback invoked to return the result.|
......@@ -213,7 +107,7 @@ data_rdb.deleteRdbStore(this.context, "RdbTest.db", function (err, rdbStore) {
})
```
## data_rdb.deleteRdbStore<sup>8+</sup>
## data_rdb.deleteRdbStore
deleteRdbStore(context: Context, name: string): Promise&lt;void&gt;
......@@ -224,7 +118,7 @@ Deletes an RDB store. This API uses a promise to return the result.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| context<sup>8+</sup> | Context | Yes| Context of the app or functionality.<br>For the definition of **Context** of API version 8, see [Context](js-apis-Context.md).<br>For the definition of **Context** of API version 9, see [Context](js-apis-ability-context.md).|
| context | Context | Yes| Context of the application or functionality.<br>See [Context](js-apis-Context.md) for versions earlier than API version 9.<br>See [Context](js-apis-ability-context.md) for API version 9 or later.|
| name | string | Yes| Name of the RDB store to delete.|
**Return value**
......@@ -234,7 +128,7 @@ Deletes an RDB store. This API uses a promise to return the result.
**Example**
```js
let promise = data_rdb.deleteRdbStore("RdbTest.db")
let promise = data_rdb.deleteRdbStore(this.context, "RdbTest.db")
promise.then(()=>{
console.log("Deleted RdbStore successfully.")
}).catch((err) => {
......@@ -1090,7 +984,7 @@ rdbStore.insert("EMPLOYEE", valueBucket, function (err, ret) {
console.info("Failed to insert data, err: " + err)
return
}
console.log("Insert first done: " + ret)
console.log("Inserted first row: " + ret)
})
```
......@@ -1215,7 +1109,7 @@ Updates data in the database based on the specified **DataSharePredicates** obje
| -------- | -------- | -------- | -------- |
| table | string | Yes| Name of the target table.|
| values | [ValuesBucket](#valuesbucket) | Yes| Rows of data to be updated in the database. The key-value pair is associated with the column name in the target table.|
| predicates | [DataSharePredicates](js-apis-data-DataSharePredicates.md#datasharepredicates) | Yes| Update conditions specified by the **DataSharePredicates** object.|
| predicates | [DataSharePredicates](js-apis-data-DataSharePredicates.md#datasharepredicates)| Yes| Update conditions specified by the **DataSharePredicates** object.|
| callback | AsyncCallback&lt;number&gt; | Yes| Callback used to return the number of rows updated.|
**Example**
......@@ -1239,7 +1133,7 @@ rdbStore.update("EMPLOYEE", valueBucket, predicates, function (err, ret) {
```
### update<sup>9+</sup>
update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates):Promise&lt;number&gt;
update(table: string, values: ValuesBucket, predicates: DataSharePredicates):Promise&lt;number&gt;
Updates data in the database based on the specified **DataSharePredicates** object. This API uses a promise to return the result.
......@@ -1300,7 +1194,7 @@ rdbStore.delete(predicates, function (err, rows) {
console.info("Failed to delete data, err: " + err)
return
}
console.log("Delete rows: " + rows)
console.log("Deleted rows: " + rows)
})
```
......@@ -1329,7 +1223,7 @@ let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
let promise = rdbStore.delete(predicates)
promise.then((rows) => {
console.log("Delete rows: " + rows)
console.log("Deleted rows: " + rows)
}).catch((err) => {
console.info("Failed to delete data, err: " + err)
})
......@@ -1337,7 +1231,7 @@ promise.then((rows) => {
### delete<sup>9+</sup>
delete(table: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback&lt;number&gt;):void
delete(table: string, predicates: DataSharePredicates, callback: AsyncCallback&lt;number&gt;):void
Deletes data from the database based on the specified **DataSharePredicates** object. This API uses an asynchronous callback to return the result.
......@@ -1361,12 +1255,12 @@ rdbStore.delete("EMPLOYEE", predicates, function (err, rows) {
console.info("Failed to delete data, err: " + err)
return
}
console.log("Delete rows: " + rows)
console.log("Deleted rows: " + rows)
})
```
### delete<sup>9+</sup>
delete(table: string, predicates: DataSharePredicates):Promise&lt;number&gt;
delete(table: string, predicates: dataSharePredicates.DataSharePredicates):Promise&lt;number&gt;
Deletes data from the database based on the specified **DataSharePredicates** object. This API uses a promise to return the result.
......@@ -1390,7 +1284,7 @@ let predicates = new dataSharePredicates.DataSharePredicates()
predicates.equalTo("NAME", "Lisa")
let promise = rdbStore.delete("EMPLOYEE", predicates)
promise.then((rows) => {
console.log("Delete rows: " + rows)
console.log("Deleted rows: " + rows)
}).catch((err) => {
console.info("Failed to delete data, err: " + err)
})
......@@ -1417,7 +1311,7 @@ let predicates = new data_rdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Rose")
rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
if (err) {
console.info("Query failed, err: " + err)
console.info("Failed to query data, err: " + err)
return
}
console.log("ResultSet column names: " + resultSet.columnNames)
......@@ -1454,13 +1348,13 @@ Queries data in the database based on specified conditions. This API uses a prom
console.log("ResultSet column names: " + resultSet.columnNames)
console.log("ResultSet column count: " + resultSet.columnCount)
}).catch((err) => {
console.info("Query failed, err: " + err)
console.info("Failed to query data, err: " + err)
})
```
### query<sup>9+</sup>
query(predicates: dataSharePredicates.DataSharePredicates, columns: Array&lt;string&gt;, callback: AsyncCallback&lt;ResultSet&gt;):void
query(predicates: DataSharePredicates, columns: Array&lt;string&gt;, callback: AsyncCallback&lt;ResultSet&gt;):void
Queries data in the database based on specified conditions. This API uses an asynchronous callback to return the result.
......@@ -1480,7 +1374,7 @@ let predicates = new dataSharePredicates.DataSharePredicates()
predicates.equalTo("NAME", "Rose")
rdbStore.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
if (err) {
console.info("Query failed, err: " + err)
console.info("Failed to query data, err: " + err)
return
}
console.log("ResultSet column names: " + resultSet.columnNames)
......@@ -1490,7 +1384,7 @@ rdbStore.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"],
### query<sup>9+</sup>
query(predicates: dataSharePredicates.DataSharePredicates, columns?: Array&lt;string&gt;):Promise&lt;ResultSet&gt;
query(predicates: DataSharePredicates, columns?: Array&lt;string&gt;):Promise&lt;ResultSet&gt;
Queries data in the database based on specified conditions. This API uses a promise to return the result.
......@@ -1517,7 +1411,7 @@ promise.then((resultSet) => {
console.log("ResultSet column names: " + resultSet.columnNames)
console.log("ResultSet column count: " + resultSet.columnCount)
}).catch((err) => {
console.info("Query failed, err: " + err)
console.info("Failed to query data, err: " + err)
})
```
......@@ -1540,7 +1434,7 @@ Queries data in the RDB store using the specified SQL statement. This API uses a
```js
rdbStore.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], function (err, resultSet) {
if (err) {
console.info("Query failed, err: " + err)
console.info("Failed to query data, err: " + err)
return
}
console.log("ResultSet column names: " + resultSet.columnNames)
......@@ -1575,7 +1469,7 @@ promise.then((resultSet) => {
console.log("ResultSet column names: " + resultSet.columnNames)
console.log("ResultSet column count: " + resultSet.columnCount)
}).catch((err) => {
console.info("Query failed, err: " + err)
console.info("Failed to query data, err: " + err)
})
```
......@@ -1600,7 +1494,7 @@ Runs the SQL statement that contains the specified parameters but does not retur
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)"
rdbStore.executeSql(SQL_CREATE_TABLE, null, function(err) {
if (err) {
console.info("ExecuteSql failed, err: " + err)
console.info("Failed to execute SQL, err: " + err)
return
}
console.info('Create table done.')
......@@ -1634,7 +1528,7 @@ let promise = rdbStore.executeSql(SQL_CREATE_TABLE)
promise.then(() => {
console.info('Create table done.')
}).catch((err) => {
console.info("ExecuteSql failed, err: " + err)
console.info("Failed to execute SQL, err: " + err)
})
```
......@@ -1745,10 +1639,10 @@ Backs up the database with the specified name. This API uses an asynchronous cal
```js
rdbStore.backup("dbBackup.db", function(err) {
if (err) {
console.info('Backup failed, err: ' + err)
console.info('Failed to back up data, err: ' + err)
return
}
console.info('Backup success.')
console.info('Backup successful.')
})
```
......@@ -1774,9 +1668,9 @@ Backs up the database with the specified name. This API uses a promise to return
```js
let promiseBackup = rdbStore.backup("dbBackup.db")
promiseBackup.then(()=>{
console.info('Backup success.')
console.info('Backup successful.')
}).catch((err)=>{
console.info('Backup failed, err: ' + err)
console.info('Failed to back up data, err: ' + err)
})
```
......@@ -1798,10 +1692,10 @@ Restores a database from a specified database backup file. This API uses an asyn
```js
rdbStore.restore("dbBackup.db", function(err) {
if (err) {
console.info('Restore failed, err: ' + err)
console.info('Failed to restore data, err: ' + err)
return
}
console.info('Restore success.')
console.info('Restore successful.')
})
```
......@@ -1827,9 +1721,9 @@ Restores a database from a specified database backup file. This API uses a promi
```js
let promiseRestore = rdbStore.restore("dbBackup.db")
promiseRestore.then(()=>{
console.info('Restore success.')
console.info('Restore successful.')
}).catch((err)=>{
console.info('Restore failed, err: ' + err)
console.info('Failed to restore data, err: ' + err)
})
```
......@@ -1910,12 +1804,12 @@ Obtains the distributed table name for a remote device based on the local table
**Example**
```js
rdbStore.obtainDistributedTableName(deviceId, "EMPLOYEE", function (err, tableName) {
rdbStore.obtainDistributedTableName("12345678abcde", "EMPLOYEE", function (err, tableName) {
if (err) {
console.info('Failed to obtain distributed table name, err: ' + err)
console.info('Failed to obtain DistributedTableName, err: ' + err)
return
}
console.info('Obtained distributed table name successfully, tableName=.' + tableName)
console.info('Obtained DistributedTableName successfully, tableName=.' + tableName)
})
```
......@@ -1943,11 +1837,11 @@ Obtains the distributed table name for a remote device based on the local table
**Example**
```js
let promise = rdbStore.obtainDistributedTableName(deviceId, "EMPLOYEE")
let promise = rdbStore.obtainDistributedTableName("12345678abcde", "EMPLOYEE")
promise.then((tableName) => {
console.info('Obtained distributed table name successfully, tableName= ' + tableName)
console.info('Obtained DistributedTableName successfully, tableName= ' + tableName)
}).catch((err) => {
console.info('Failed to obtain distributed table name, err: ' + err)
console.info('Failed to obtain DistributedTableName, err: ' + err)
})
```
......@@ -1970,9 +1864,9 @@ Synchronizes data between devices. This API uses an asynchronous callback to ret
**Example**
```js
let predicates = new rdb.RdbPredicates('EMPLOYEE')
let predicates = new data_rdb.RdbPredicates('EMPLOYEE')
predicates.inDevices(['12345678abcde'])
rdbStore.sync(rdb.SyncMode.SYNC_MODE_PUSH, predicates, function (err, result) {
rdbStore.sync(data_rdb.SyncMode.SYNC_MODE_PUSH, predicates, function (err, result) {
if (err) {
console.log('Sync failed, err: ' + err)
return
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册