@@ -18,7 +18,7 @@ A relational database (RDB) store is used to store data in complex relational mo
...
@@ -18,7 +18,7 @@ A relational database (RDB) store is used to store data in complex relational mo
**RelationalStore** provides APIs for applications to perform data operations. With SQLite as the underlying persistent storage engine, **RelationalStore** provides SQLite database features, including transactions, indexes, views, triggers, foreign keys, parameterized queries, prepared SQL statements, and more.
**RelationalStore** provides APIs for applications to perform data operations. With SQLite as the underlying persistent storage engine, **RelationalStore** provides SQLite database features, including transactions, indexes, views, triggers, foreign keys, parameterized queries, prepared SQL statements, and more.
@@ -37,15 +37,15 @@ A relational database (RDB) store is used to store data in complex relational mo
...
@@ -37,15 +37,15 @@ A relational database (RDB) store is used to store data in complex relational mo
The following table lists the APIs used for RDB data persistence. Most of the APIs are executed asynchronously, using a callback or promise to return the result. The following table uses the callback-based APIs as an example. For more information about the APIs, see [RDB Store](../reference/apis/js-apis-data-relationalStore.md).
The following table lists the APIs used for RDB data persistence. Most of the APIs are executed asynchronously, using a callback or promise to return the result. The following table uses the callback-based APIs as an example. For more information about the APIs, see [RDB Store](../reference/apis/js-apis-data-relationalStore.md).
| API| Description|
| API| Description|
| -------- | -------- |
| -------- | -------- |
| getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback<RdbStore>): void | Obtains a **RdbStore** instance to implement RDB store operations. You can set **RdbStore** parameters based on actual requirements and use **RdbStore** APIs to perform data operations.|
| getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback<RdbStore>): void | Obtains a **RdbStore** instance to implement RDB store operations. You can set **RdbStore** parameters based on actual requirements and use **RdbStore** APIs to perform data operations.|
| executeSql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<void>):void | Executes an SQL statement that contains specified arguments but returns no value.|
| executeSql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<void>):void | Executes an SQL statement that contains specified arguments but returns no value.|
| insert(table: string, values: ValuesBucket, callback: AsyncCallback<number>):void | Inserts a row of data into a table.|
| insert(table: string, values: ValuesBucket, callback: AsyncCallback<number>):void | Inserts a row of data into a table.|
| update(values: ValuesBucket, predicates: RdbPredicates, callback: AsyncCallback<number>):void | Updates data in the RDB store based on the specified **RdbPredicates** instance.|
| update(values: ValuesBucket, predicates: RdbPredicates, callback: AsyncCallback<number>):void | Updates data in the RDB store based on the specified **RdbPredicates** instance.|
| delete(predicates: RdbPredicates, callback: AsyncCallback<number>):void | Deletes data from the RDB store based on the specified **RdbPredicates** instance.|
| delete(predicates: RdbPredicates, callback: AsyncCallback<number>):void | Deletes data from the RDB store based on the specified **RdbPredicates** instance.|
| query(predicates: RdbPredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void | Queries data in the RDB store based on specified conditions.|
| query(predicates: RdbPredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void | Queries data in the RDB store based on specified conditions.|
// The current RDB store version is 3, and the table structure is EMPLOYEE (NAME, AGE, SALARY, CODES).
constSQL_CREATE_TABLE='CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)';// SQL statement for creating a data table.
constSQL_CREATE_TABLE='CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)';// SQL statement for creating a data table.
console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);
console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);
return;
return;
}
}
console.info(`Succeeded in getting RdbStore.`);
console.info(`Succeeded in getting RdbStore.`);
store.executeSql(SQL_CREATE_TABLE);// Create a data table.
// When the RDB store is created, the default version is 0.
if(store.version==0){
store.executeSql(SQL_CREATE_TABLE);// Create a data table.
// Set the RDB store version, which must be an integer greater than 0.
store.version=3;
}
// If the RDB store version is not 0 and does not match the current version, upgrade or downgrade the RDB store.
// For example, upgrade the RDB store from version 1 to version 2.
if(store.version!=3&&store.version==1){
// Upgrade the RDB store from version 1 to version 2, and change the table structure from EMPLOYEE (NAME, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS).
store.executeSql("ALTER TABLE EMPLOYEE ADD COLUMN AGE INTEGER",null);
store.version=2;
}
// For example, upgrade the RDB store from version 2 to version 3.
if(store.version!=3&&store.version==2){
// Upgrade the RDB store from version 2 to version 3, and change the table structure from EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES).
store.executeSql("ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS TEXT",null);
store.version=3;
}
// Perform operations such as adding, deleting, modifying, and querying data in the RDB store.
// Perform operations such as adding, deleting, modifying, and querying data in the RDB store.
});
});
}
}
}
}
...
@@ -84,7 +106,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
...
@@ -84,7 +106,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
FA model:
FA model:
```js
```js
importrelationalStorefrom'@ohos.data.relationalStore';// Import the module.
importrelationalStorefrom'@ohos.data.relationalStore';// Import the module.
// The RDB store version is 3, and the table structure is EMPLOYEE (NAME, AGE, SALARY, CODES).
constSQL_CREATE_TABLE='CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)';// SQL statement for creating a data table.
constSQL_CREATE_TABLE='CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)';// SQL statement for creating a data table.
console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);
console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);
return;
return;
}
}
console.info(`Succeeded in getting RdbStore.`);
console.info(`Succeeded in getting RdbStore.`);
store.executeSql(SQL_CREATE_TABLE);// Create a data table.
// When the RDB store is created, the default version is 0.
if(store.version==0){
store.executeSql(SQL_CREATE_TABLE);// Create a data table.
// Set the RDB store version, which must be an integer greater than 0.
store.version=3;
}
// If the RDB store version is not 0 and does not match the current version, upgrade or downgrade the RDB store.
// For example, upgrade the RDB store from version 1 to version 2.
if(store.version!=3&&store.version==1){
// Upgrade the RDB store from version 1 to version 2, and change the table structure from EMPLOYEE (NAME, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS).
store.executeSql("ALTER TABLE EMPLOYEE ADD COLUMN AGE INTEGER",null);
store.version=2;
}
// For example, upgrade the RDB store from version 2 to version 3.
if(store.version!=3&&store.version==2){
// Upgrade the RDB store from version 2 to version 3, and change the table structure from EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES).
store.executeSql("ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS TEXT",null);
store.version=3;
}
// Perform operations such as adding, deleting, modifying, and querying data in the RDB store.
// Perform operations such as adding, deleting, modifying, and querying data in the RDB store.
...
@@ -119,7 +163,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
...
@@ -119,7 +163,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
> - When an application calls **getRdbStore()** to obtain an RDB store instance for the first time, the corresponding database file is generated in the application sandbox. If you want to move the files of an RDB store to another place for view, you must also move the temporary files with finename extensions **-wal** or **-shm** in the same directory. Once an application is uninstalled, the database files and temporary files generated by the application on the device are also removed.
> - When an application calls **getRdbStore()** to obtain an RDB store instance for the first time, the corresponding database file is generated in the application sandbox. If you want to move the files of an RDB store to another place for view, you must also move the temporary files with finename extensions **-wal** or **-shm** in the same directory. Once an application is uninstalled, the database files and temporary files generated by the application on the device are also removed.
2. Use **insert()** to insert data to the RDB store. Example:
2. Use **insert()** to insert data to the RDB store. Example:
```js
```js
constvalueBucket={
constvalueBucket={
'NAME':'Lisa',
'NAME':'Lisa',
...
@@ -185,13 +229,22 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
...
@@ -185,13 +229,22 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
The relational database (RDB) store manages data based on relational models. With the underlying SQLite database, the RDB store provides a complete mechanism for managing local databases. 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.
The relational database (RDB) store manages data based on relational models. The RDB store 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.
The **relationalStore** module provides the following functions:
The **relationalStore** module provides the following functions:
...
@@ -323,7 +323,7 @@ Enumerates the RDB store security levels.
...
@@ -323,7 +323,7 @@ Enumerates the RDB store security levels.
> **NOTE**
> **NOTE**
>
>
> To perform data synchronization operations, the RDB store security level must be lower than or equal to that of the peer device. For details, see the [Cross-Device Data Synchronization Mechanism]( ../../database/sync-app-data-across-devices-overview.md#cross-device-data-synchronization-mechanism).
> To perform data synchronization operations, the RDB store security level must be lower than or equal to that of the peer device. For details, see the [Access Control Mechanism in Cross-Device Synchronization]( ../../database/access-control-by-device-and-data-level.md#access-control-mechanism-in-cross-device-synchronization).
@@ -334,6 +334,45 @@ Enumerates the RDB store security levels.
...
@@ -334,6 +334,45 @@ Enumerates the RDB store security levels.
| S3 | 3 | The RDB store security level is high. If data leakage occurs, major impact will be caused on the database. For example, an RDB store that contains information such as user fitness, health, and location data.|
| S3 | 3 | The RDB store security level is high. If data leakage occurs, major impact will be caused on the database. For example, an RDB store that contains information such as user fitness, health, and location data.|
| S4 | 4 | The RDB store security level is critical. If data leakage occurs, severe impact will be caused on the database. For example, an RDB store that contains information such as authentication credentials and financial data.|
| S4 | 4 | The RDB store security level is critical. If data leakage occurs, severe impact will be caused on the database. For example, an RDB store that contains information such as authentication credentials and financial data.|
## AssetStatus<sup>10+</sup>
Enumerates the asset statuses. Use the enum names instead of the enum values.
| SYNC_MODE_PUSH | 0 | Data is pushed from a local device to a remote device.|
| SYNC_MODE_PUSH | 0 | Push data from a local device to a remote device.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| SYNC_MODE_PULL | 1 | Data is pulled from a remote device to a local device. |
| SYNC_MODE_PULL | 1 | Pull data from a remote device to a local device.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| SYNC_MODE_TIME_FIRST<sup>10+</sup> | - | Synchronize with the data with the latest modification time. Use the enum names instead of the enum values. Currently, manual synchronization between the device and cloud is not supported.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client|
| SYNC_MODE_NATIVE_FIRST<sup>10+</sup> | - | Synchronize data from a local device to the cloud. Use the enum names instead of the enum values. Currently, manual synchronization between the device and cloud is not supported.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client |
| SYNC_MODE_CLOUD_FIRST<sup>10+</sup> | - | Synchronize data from the cloud to a local device. Use the enum names instead of the enum values. Currently, manual synchronization between the device and cloud is not supported.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client |
| SUBSCRIBE_TYPE_REMOTE | 0 | Subscribe to remote data changes.|
| SUBSCRIBE_TYPE_REMOTE | 0 | Subscribe to remote data changes.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core|
| SUBSCRIBE_TYPE_CLOUD<sup>10+</sup> | 1 | Subscribe to cloud data changes.|
| SUBSCRIBE_TYPE_CLOUD<sup>10+</sup> | - | Subscribe to cloud data changes. Use the enum names instead of the enum values.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client|
| SUBSCRIBE_TYPE_CLOUD_DETAILS<sup>10+</sup> | - | Subscribe to cloud data change details. Use the enum names instead of the enum values.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client|
## ChangeType<sup>10+</sup>
Enumerates data change types. Use the enum names instead of the enum values.
| table | string | Yes | Name of the table with data changes.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| type | [ChangeType](#changetype10) | Yes | Type of the data changed, which can be data or asset.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| inserted | Array\<string\>\| Array\<number\> | Yes | Location where data is inserted. If the primary key of the table is of the string type, the value is the value of the primary key. Otherwise, the value is the row number of the inserted data.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| updated | Array\<string\>\| Array\<number\> | Yes | Location where data is updated. If the primary key of the table is of the string type, the value is the value of the primary key. Otherwise, the value is the row number of the updated data.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| deleted | Array\<string\>\| Array\<number\> | Yes | Location where data is deleted. If the primary key of the table is of the string type, the value is the value of the primary key. Otherwise, the value is the row number of the deleted data.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
## DistributedType<sup>10+</sup>
Enumerates the distributed table types. Use the enum names instead of the enum values.
| DISTRIBUTED_CLOUD | - | Distributed database table between the device and the cloud.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client|
## DistributedConfig<sup>10+</sup>
Defines the configuration of the distributed mode of tables.
| autoSync | boolean | Yes | The value **true** means both automatic synchronization and manual synchronization are supported for the table. The value **false** means only manual synchronization is supported for the table.|
## ConflictResolution<sup>10+</sup>
## ConflictResolution<sup>10+</sup>
...
@@ -2886,9 +2977,9 @@ Sets distributed tables. This API uses a promise to return the result.
...
@@ -2886,9 +2977,9 @@ Sets distributed tables. This API uses a promise to return the result.
| tables | Array<string> | Yes | Names of the distributed tables to set. |
| type | number | Yes | Distributed type of the tables. Currently, only **relationalStore.DistributedType.DISTRIBUTED_DEVICE** and **relationalStore.DistributedType.DISTRIBUTED_CLOUD** are supported.<br> The value **relationalStore.DistributedType.DISTRIBUTED_DEVICE** indicates distributed tables across different devices.<br> The value **relationalStore.DistributedType.DISTRIBUTED_CLOUD** indicates distributed tables between the device and cloud.|
| config | [DistributedConfig](#distributedconfig10) | Yes| Configuration of the distributed mode.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.|
| tables | Array<string> | Yes | Names of the distributed tables to set. |
| type | number | No | Distributed type of the tables. The default value is **relationalStore.DistributedType.DISTRIBUTED_DEVICE**.<br> Currently, only **relationalStore.DistributedType.DISTRIBUTED_DEVICE** and **relationalStore.DistributedType.DISTRIBUTED_CLOUD** are supported.<br> The value **relationalStore.DistributedType.DISTRIBUTED_DEVICE** indicates distributed tables across different devices.<br> The value **relationalStore.DistributedType.DISTRIBUTED_CLOUD** indicates distributed tables between the device and cloud.|
| config | [DistributedConfig](#distributedconfig10) | No | Configuration of the distributed mode. If this parameter is not specified, the value of **autoSync** is **false** by default, which means only manual synchronization is supported.|
| mode | [SyncMode](#syncmode) | Yes | Data synchronization mode. The value can be **push** or **pull**. |
| mode | [SyncMode](#syncmode) | Yes | Data synchronization mode. The value can be **relationalStore.SyncMode.SYNC_MODE_PUSH** or **relationalStore.SyncMode.SYNC_MODE_PULL**. |
| predicates | [RdbPredicates](#rdbpredicates) | Yes | **RdbPredicates** object that specifies the data and devices to synchronize. |
| predicates | [RdbPredicates](#rdbpredicates) | Yes | **RdbPredicates** object that specifies the data and devices to synchronize. |
| callback | AsyncCallback<Array<[string, number]>> | Yes | Callback invoked to send the synchronization result to the caller. <br>**string** indicates the device ID. <br>**number** indicates the synchronization status of that device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure. |
| callback | AsyncCallback<Array<[string, number]>> | Yes | Callback invoked to send the synchronization result to the caller. <br>**string** indicates the device ID. <br>**number** indicates the synchronization status of that device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure. |
...
@@ -3104,7 +3265,7 @@ Synchronizes data between devices. This API uses a promise to return the result.
...
@@ -3104,7 +3265,7 @@ Synchronizes data between devices. This API uses a promise to return the result.
| mode | [SyncMode](#syncmode) | Yes | Data synchronization mode. The value can be **push** or **pull**.|
| mode | [SyncMode](#syncmode) | Yes | Data synchronization mode. The value can be **relationalStore.SyncMode.SYNC_MODE_PUSH** or **relationalStore.SyncMode.SYNC_MODE_PULL**.|
| predicates | [RdbPredicates](#rdbpredicates) | Yes | **RdbPredicates** object that specifies the data and devices to synchronize. |
| predicates | [RdbPredicates](#rdbpredicates) | Yes | **RdbPredicates** object that specifies the data and devices to synchronize. |
| event | string | Yes | Event to observe. The value is **dataChange**, which indicates a data change event. |
| type | [SubscribeType](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md#subscribetype) | Yes | Subscription type to register. |
| observer | Callback<Array<string>> | Yes | Callback invoked to return the data change. **Array<string>** indicates the IDs of the peer devices whose data in the database is changed.|
**Example**
```
function storeObserver(devices) {
for (let i = 0; i < devices.length; i++) {
console.info(`device= ${devices[i]} data changed`);
| event | string | Yes | Event to observe. The value is **dataChange**, which indicates a data change event. |
| event | string | Yes | Event to observe. The value is **dataChange**, which indicates a data change event. |
| type | [SubscribeType](#subscribetype) | Yes | Subscription type to register.|
| type | [SubscribeType](#subscribetype) | Yes | Subscription type to register.|
| observer | Callback<Array<string>>| Yes | Callback invoked to return the data change event. **Array<string>** indicates the IDs of the peer devices whose data in the database is changed.|
| observer | Callback<Array<string>>\| Callback<Array<[ChangeInfo](#changeinfo10)>> | Yes | Callback invoked to return the data change event.<br>If **type** is **SUBSCRIBE_TYPE_REMOTE**, **observer** must be **Callback<Array<string>>**, where **Array<string>** specifies the IDs of the peer devices with data changes.<br> If **type** is **SUBSCRIBE_TYPE_CLOUD**, **observer** must be **Callback<Array<string>>**, where **Array<string>** specifies the cloud accounts with data changes.<br> If **type** is **SUBSCRIBE_TYPE_CLOUD_DETAILS**, **observer** must be **Callback<Array<ChangeInfo>>**, where **Array<ChangeInfo>** specifies the details about the device-cloud synchronization.|
| event | string | Yes | Event type. The value is **dataChange**, which indicates a data change event. |
| type | [SubscribeType](#subscribetype)| Yes | Subscription type to unregister. |
| observer | Callback<Array<string>> | Yes | Callback for the data change event. **Array<string>** indicates the IDs of the peer devices whose data in the database is changed.|
**Example**
```
function storeObserver(devices) {
for (let i = 0; i < devices.length; i++) {
console.info(`device= ${devices[i]} data changed`);
| event | string | Yes | Event type. The value is **dataChange**, which indicates a data change event. |
| event | string | Yes | Event to observe. The value is **dataChange**, which indicates a data change event. |
| type | [SubscribeType](#subscribetype) | Yes | Subscription type to unregister. |
| type | [SubscribeType](#subscribetype) | Yes | Subscription type to register. |
| observer | Callback<Array<string>> | Yes | Callback for the data change event. **Array<string>** indicates the IDs of the peer devices whose data in the database is changed.|
| observer | Callback<Array<string>>\| Callback<Array<[ChangeInfo](#changeinfo10)>> | No| Callback invoked to return the result.<br>If **type** is **SUBSCRIBE_TYPE_REMOTE**, **observer** must be **Callback<Array<string>>**, where **Array<string>** specifies the IDs of the peer devices with data changes.<br> If **type** is **SUBSCRIBE_TYPE_CLOUD**, **observer** must be **Callback<Array<string>>**, where **Array<string>** specifies the cloud accounts with data changes.<br> If **type** is **SUBSCRIBE_TYPE_CLOUD_DETAILS**, **observer** must be **Callback<Array<ChangeInfo>>**, where **Array<ChangeInfo>** specifies the details about the device-cloud synchronization.<br> If **observer** is not specified, listening for all data change events of the specified **type** will be canceled.|
**Example**
**Example**
...
@@ -3668,7 +3891,7 @@ Obtains the value of the double type based on the specified column and the curre
...
@@ -3668,7 +3891,7 @@ Obtains the value of the double type based on the specified column and the curre
| Type | Description |
| Type | Description |
| ------ | ---------------------------- |
| ------ | ---------------------------- |
| number | Value obtained.|
| number | Returns the value obtained.|
**Error codes**
**Error codes**
...
@@ -3684,6 +3907,75 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
...
@@ -3684,6 +3907,75 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode