1e41f4b71Sopenharmony_ci# Sharing Data Using DataShareExtensionAbility
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## When to Use
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciIf complex services are involved in cross-application data access, you can use **DataShareExtensionAbility** to start the application of the data provider to implement data access.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ciYou need to implement flexible service logics via callbacks of the service provider.  
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## Working Principles
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciThere are two roles in **DataShare**:
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci- Data provider: implements operations, such as adding, deleting, modifying, and querying data, and opening a file, using [DataShareExtensionAbility](../reference/apis-arkdata/js-apis-application-dataShareExtensionAbility-sys.md).
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci- Data consumer: accesses the data provided by the provider using [createDataShareHelper()](../reference/apis-arkdata/js-apis-data-dataShare-sys.md#datasharecreatedatasharehelper).
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci**Figure 1** Data sharing mechanism 
20e41f4b71Sopenharmony_ci![dataShare](figures/dataShare.jpg)
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci- The **DataShareExtensionAbility** module, as the data provider, implements services related to data sharing between applications.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci- The **DataShareHelper** module, as the data consumer, provides APIs for accessing data, including adding, deleting, modifying, and querying data.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci- The data consumer communicates with the data provider via inter-process communication (IPC). The data provider can be implemented through a database or other data storage.
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci- The **ResultSet** module is implemented through shared memory. Shared memory stores the result sets, and interfaces are provided to traverse result sets.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci## How to Develop
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci### Data Provider Application Development (Only for System Applications)
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ciThe [DataShareExtensionAbility](../reference/apis-arkdata/js-apis-application-dataShareExtensionAbility-sys.md) provides the following APIs. You can override these APIs as required.
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci- **onCreate**: called by the server to initialize service logic when the DataShare client connects to the DataShareExtensionAbility server.
39e41f4b71Sopenharmony_ci- **insert**: called to insert data upon the request of the client. Data insertion must be implemented in this callback on the server.
40e41f4b71Sopenharmony_ci- **update**: called to update data upon the request of the client. Data update must be implemented in this callback on the server.
41e41f4b71Sopenharmony_ci- **batchUpdate**: called to update batch data upon the request of the client. Batch data update must be implemented in this callback on the server.
42e41f4b71Sopenharmony_ci- **delete**: called to delete data upon the request of the client. Data deletion must be implemented in this callback on the server.
43e41f4b71Sopenharmony_ci- **query**: called to query data upon the request of the client. Data query must be implemented in this callback on the server.
44e41f4b71Sopenharmony_ci- **batchInsert**: called to batch insert data upon the request of the client. Batch data insertion must be implemented in this callback on the server.
45e41f4b71Sopenharmony_ci- **normalizeUri**: converts the URI provided by the client to the URI used by the server.
46e41f4b71Sopenharmony_ci- **denormalizeUri**: converts the URI used by the server to the initial URI passed by the client.
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ciBefore implementing a **DataShare** service, you need to create a **DataShareExtensionAbility** object in the DevEco Studio project as follows:
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci1. In the **ets** directory of the **Module** project, right-click and choose **New > Directory** to create a directory named **DataShareExtAbility**.
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci2. Right-click the **DataShareAbility** directory, and choose **New > ArkTS File** to create a file named **DataShareExtAbility.ets**.
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci3. In the **DataShareExtAbility.ets** file, import the **DataShareExtensionAbility** module. You can override the service implementation as required. For example, if the data provider provides only the insert, delete, and query services, you can override only these APIs and import the dependency modules. If permission verification is required, override the callbacks using [getCallingPid](../reference/apis-ipc-kit/js-apis-rpc.md#getcallingpid), [getCallingUid](../reference/apis-ipc-kit/js-apis-rpc.md#getcallinguid), and [getCallingTokenId](../reference/apis-ipc-kit/js-apis-rpc.md#getcallingtokenid8) provided by the IPC module to obtain the data consumer information for permission verification.
55e41f4b71Sopenharmony_ci   
56e41f4b71Sopenharmony_ci   ```ts
57e41f4b71Sopenharmony_ci   import { DataShareExtensionAbility, dataShare, dataSharePredicates, relationalStore, DataShareResultSet } from '@kit.ArkData';
58e41f4b71Sopenharmony_ci   import { Want } from '@kit.AbilityKit';
59e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit'
60e41f4b71Sopenharmony_ci   ```
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci4. Implement the data provider services. For example, implement data storage of the data provider by creating and using a database, reading and writing files, or accessing the network.
63e41f4b71Sopenharmony_ci   
64e41f4b71Sopenharmony_ci   ```ts
65e41f4b71Sopenharmony_ci   const DB_NAME = 'DB00.db';
66e41f4b71Sopenharmony_ci   const TBL_NAME = 'TBL00';
67e41f4b71Sopenharmony_ci   const DDL_TBL_CREATE = "CREATE TABLE IF NOT EXISTS "
68e41f4b71Sopenharmony_ci     + TBL_NAME
69e41f4b71Sopenharmony_ci     + ' (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, isStudent BOOLEAN, Binary BINARY)';
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci   let rdbStore: relationalStore.RdbStore;
72e41f4b71Sopenharmony_ci   let result: string;
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci   export default class DataShareExtAbility extends DataShareExtensionAbility {
75e41f4b71Sopenharmony_ci     // Override onCreate().
76e41f4b71Sopenharmony_ci     onCreate(want: Want, callback: Function) {
77e41f4b71Sopenharmony_ci       result = this.context.cacheDir + '/datashare.txt';
78e41f4b71Sopenharmony_ci       // Create an RDB store.
79e41f4b71Sopenharmony_ci       relationalStore.getRdbStore(this.context, {
80e41f4b71Sopenharmony_ci         name: DB_NAME,
81e41f4b71Sopenharmony_ci         securityLevel: relationalStore.SecurityLevel.S3
82e41f4b71Sopenharmony_ci       }, (err:BusinessError, data:relationalStore.RdbStore) => {
83e41f4b71Sopenharmony_ci         rdbStore = data;
84e41f4b71Sopenharmony_ci         rdbStore.executeSql(DDL_TBL_CREATE, [], (err) => {
85e41f4b71Sopenharmony_ci           console.info(`DataShareExtAbility onCreate, executeSql done err:${err}`);
86e41f4b71Sopenharmony_ci         });
87e41f4b71Sopenharmony_ci         if (callback) {
88e41f4b71Sopenharmony_ci           callback();
89e41f4b71Sopenharmony_ci         }
90e41f4b71Sopenharmony_ci       });
91e41f4b71Sopenharmony_ci     }
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci     // Override query().
94e41f4b71Sopenharmony_ci     query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
95e41f4b71Sopenharmony_ci       if (predicates === null || predicates === undefined) {
96e41f4b71Sopenharmony_ci         console.info('invalid predicates');
97e41f4b71Sopenharmony_ci       }
98e41f4b71Sopenharmony_ci       try {
99e41f4b71Sopenharmony_ci         rdbStore.query(TBL_NAME, predicates, columns, (err:BusinessError, resultSet:relationalStore.ResultSet) => {
100e41f4b71Sopenharmony_ci           if (resultSet !== undefined) {
101e41f4b71Sopenharmony_ci             console.info(`resultSet.rowCount:${resultSet.rowCount}`);
102e41f4b71Sopenharmony_ci           }
103e41f4b71Sopenharmony_ci           if (callback !== undefined) {
104e41f4b71Sopenharmony_ci             callback(err, resultSet);
105e41f4b71Sopenharmony_ci           }
106e41f4b71Sopenharmony_ci         });
107e41f4b71Sopenharmony_ci       } catch (err) {
108e41f4b71Sopenharmony_ci         let code = (err as BusinessError).code;
109e41f4b71Sopenharmony_ci         let message = (err as BusinessError).message
110e41f4b71Sopenharmony_ci         console.error(`Failed to query. Code:${code},message:${message}`);
111e41f4b71Sopenharmony_ci       }
112e41f4b71Sopenharmony_ci     }
113e41f4b71Sopenharmony_ci     // Override the batchUpdate API.
114e41f4b71Sopenharmony_ci     batchUpdate(operations:Record<string, Array<dataShare.UpdateOperation>>, callback:Function) {
115e41f4b71Sopenharmony_ci       let recordOps : Record<string, Array<dataShare.UpdateOperation>> = operations;
116e41f4b71Sopenharmony_ci       let results : Record<string, Array<number>> = {};
117e41f4b71Sopenharmony_ci       let a = Object.entries(recordOps);
118e41f4b71Sopenharmony_ci       for (let i = 0; i < a.length; i++) {
119e41f4b71Sopenharmony_ci         let key = a[i][0];
120e41f4b71Sopenharmony_ci         let values = a[i][1];
121e41f4b71Sopenharmony_ci         let result : number[] = [];
122e41f4b71Sopenharmony_ci         for (const value of values) {
123e41f4b71Sopenharmony_ci           rdbStore.update(TBL_NAME, value.values, value.predicates).then(async (rows) => {
124e41f4b71Sopenharmony_ci             console.info('Update row count is ' + rows);
125e41f4b71Sopenharmony_ci             result.push(rows);
126e41f4b71Sopenharmony_ci           }).catch((err:BusinessError) => {
127e41f4b71Sopenharmony_ci             console.info('Update failed, err is ' + JSON.stringify(err));
128e41f4b71Sopenharmony_ci             result.push(-1)
129e41f4b71Sopenharmony_ci           })
130e41f4b71Sopenharmony_ci         }
131e41f4b71Sopenharmony_ci         results[key] = result;
132e41f4b71Sopenharmony_ci       }
133e41f4b71Sopenharmony_ci       callback(null, results);
134e41f4b71Sopenharmony_ci     }
135e41f4b71Sopenharmony_ci     // Override other APIs as required.
136e41f4b71Sopenharmony_ci   };
137e41f4b71Sopenharmony_ci   ```
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci5. Define **DataShareExtensionAbility** in **module.json5**.
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci     **Table 1** Fields in module.json5
142e41f4b71Sopenharmony_ci   
143e41f4b71Sopenharmony_ci   | Field| Description| Mandatory|
144e41f4b71Sopenharmony_ci   | -------- | -------- | -------- |
145e41f4b71Sopenharmony_ci   | name | Ability name, corresponding to the **ExtensionAbility** class name derived from **Ability**.| Yes|
146e41f4b71Sopenharmony_ci   | type | Ability type. The value **dataShare** indicates the development is based on the **datashare** template.| Yes|
147e41f4b71Sopenharmony_ci   | uri | Unique identifier for the data consumer to access the data provider.| Yes|
148e41f4b71Sopenharmony_ci   | exported | Whether it is visible to other applications. Data sharing is allowed only when the value is **true**.| Yes|
149e41f4b71Sopenharmony_ci   | readPermission | Permission required for data access. If this parameter is not set, read permission verification is not performed by default.| No|
150e41f4b71Sopenharmony_ci   | writePermission | Permission required for data modification. If this parameter is not set, write permission verification is not performed by default.| No|
151e41f4b71Sopenharmony_ci   | metadata   | Silent access configuration, which includes the following:<br>- **name**: identifies the configuration, which has a fixed value of **ohos.extension.dataShare**.<br>- **resource**: has a fixed value of **$profile:data_share_config**, which indicates that the profile name is **data_share_config.json**.| **metadata** is mandatory when the ability launch type is **singleton**. For details about the ability launch type, see **launchType** in the [Internal Structure of the abilities Attribute](../quick-start/module-structure.md#internal-structure-of-the-abilities-attribute).|
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci   **module.json5 example**
154e41f4b71Sopenharmony_ci   
155e41f4b71Sopenharmony_ci   ```json
156e41f4b71Sopenharmony_ci   "extensionAbilities": [
157e41f4b71Sopenharmony_ci     {
158e41f4b71Sopenharmony_ci       "srcEntry": "./ets/DataShareExtAbility/DataShareExtAbility.ets",
159e41f4b71Sopenharmony_ci       "name": "DataShareExtAbility",
160e41f4b71Sopenharmony_ci       "icon": "$media:icon",
161e41f4b71Sopenharmony_ci       "description": "$string:description_datashareextability",
162e41f4b71Sopenharmony_ci       "type": "dataShare",
163e41f4b71Sopenharmony_ci       "uri": "datashare://com.samples.datasharetest.DataShare",
164e41f4b71Sopenharmony_ci       "exported": true,
165e41f4b71Sopenharmony_ci       "metadata": [{"name": "ohos.extension.dataShare", "resource": "$profile:data_share_config"}]
166e41f4b71Sopenharmony_ci     }
167e41f4b71Sopenharmony_ci   ]
168e41f4b71Sopenharmony_ci   ```
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci   **Table 2** Fields in the data_share_config.json file
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci   | Field           | Description                                                    | Mandatory|
173e41f4b71Sopenharmony_ci   | ------------------- | ------------------------------------------------------------ | ---- |
174e41f4b71Sopenharmony_ci   | tableConfig         | Configuration label, which includes **uri** and **crossUserMode**.<br>- **uri**: specifies the range for which the configuration takes effect. The URI supports the following formats in descending order by priority:<br> 1. *****: indicates all databases and tables.<br> 2. **datashare:///{*bundleName*}/{*moduleName*}/{*storeName*}**: specifies a database.<br> 3. **datashare:///{*bundleName*}/{*moduleName*}/{*storeName*}/{*tableName*}**: specifies a table.<br>If URIs of different formats are configured, only the URI with the higher priority takes effect.<br>- **crossUserMode**: Whether to share data between multiple users.<br>The value **1** means to share data between multiple users, and the value **2** means the opposite.| Yes  |
175e41f4b71Sopenharmony_ci   | isSilentProxyEnable | Whether to enable silent access for this ExtensionAbility.<br>The value **true** means to enable silent access, and the value **false** means the opposite.<br>The default  value is **true**.<br>If an application has multiple ExtensionAbilities and this field is set to **false** for one of them, silent access is disabled for the application.<br>If the data provider has called **enableSilentProxy** or **disableSilentProxy**, silent access is enabled or disabled based on the API settings. Otherwise, the setting here takes effect.| No  |
176e41f4b71Sopenharmony_ci   | launchInfos         | Launch information, which includes **storeId** and **tableNames**.<br>If the data in a table involved in the configuration changes, an extensionAbility will be started based on the URI in **extensionAbilities**. You need to set this parameter only when the service needs to start an extensionAbility to process data that is not actively changed by the service.<br>- **storeId**: database name, excluding the file name extension. For example, if the database name is **test.db**, set this parameter to **test**.<br>- **tableNames**: names of the database tables. Any change in a table will start an an extensionAbility. | No  |
177e41f4b71Sopenharmony_ci   
178e41f4b71Sopenharmony_ci   **data_share_config.json Example**
179e41f4b71Sopenharmony_ci   
180e41f4b71Sopenharmony_ci   ```json
181e41f4b71Sopenharmony_ci   {
182e41f4b71Sopenharmony_ci       "tableConfig":[
183e41f4b71Sopenharmony_ci           {
184e41f4b71Sopenharmony_ci               "uri":"*",
185e41f4b71Sopenharmony_ci               "crossUserMode":1
186e41f4b71Sopenharmony_ci           },
187e41f4b71Sopenharmony_ci           {
188e41f4b71Sopenharmony_ci               "uri":"datashare:///com.acts.datasharetest/entry/DB00",
189e41f4b71Sopenharmony_ci               "crossUserMode":1
190e41f4b71Sopenharmony_ci           },
191e41f4b71Sopenharmony_ci           {
192e41f4b71Sopenharmony_ci               "uri":"datashare:///com.acts.datasharetest/entry/DB00/TBL00",
193e41f4b71Sopenharmony_ci               "crossUserMode":2
194e41f4b71Sopenharmony_ci           }
195e41f4b71Sopenharmony_ci       ],
196e41f4b71Sopenharmony_ci       "isSilentProxyEnable":true,
197e41f4b71Sopenharmony_ci       "launchInfos":[
198e41f4b71Sopenharmony_ci           {
199e41f4b71Sopenharmony_ci               "storeId": "test",
200e41f4b71Sopenharmony_ci               "tableNames":["test1", "test2"]
201e41f4b71Sopenharmony_ci           }
202e41f4b71Sopenharmony_ci       ]
203e41f4b71Sopenharmony_ci   }
204e41f4b71Sopenharmony_ci   ```
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci### Data Consumer Application Development
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci1. Import the dependencies.
210e41f4b71Sopenharmony_ci   
211e41f4b71Sopenharmony_ci   ```ts
212e41f4b71Sopenharmony_ci   import { UIAbility } from '@kit.AbilityKit';
213e41f4b71Sopenharmony_ci   import { dataShare, dataSharePredicates, ValuesBucket } from '@kit.ArkData';
214e41f4b71Sopenharmony_ci   import { window } from '@kit.ArkUI';
215e41f4b71Sopenharmony_ci   ```
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci2. Define the URI string for communicating with the data provider.
218e41f4b71Sopenharmony_ci   
219e41f4b71Sopenharmony_ci   ```ts
220e41f4b71Sopenharmony_ci   // Different from the URI defined in the module.json5 file, the URI passed in the parameter has an extra slash (/), because there is a DeviceID parameter between the second and the third slash (/).
221e41f4b71Sopenharmony_ci   let dseUri = ('datashare:///com.samples.datasharetest.DataShare');
222e41f4b71Sopenharmony_ci   ```
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci3. Create a **DataShareHelper** instance.
225e41f4b71Sopenharmony_ci   
226e41f4b71Sopenharmony_ci   ```ts
227e41f4b71Sopenharmony_ci   let dsHelper: dataShare.DataShareHelper | undefined = undefined;
228e41f4b71Sopenharmony_ci   let abilityContext: Context;
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci   export default class EntryAbility extends UIAbility {
231e41f4b71Sopenharmony_ci     onWindowStageCreate(windowStage: window.WindowStage) {
232e41f4b71Sopenharmony_ci       abilityContext = this.context;
233e41f4b71Sopenharmony_ci       dataShare.createDataShareHelper(abilityContext, dseUri, (err, data) => {
234e41f4b71Sopenharmony_ci         dsHelper = data;
235e41f4b71Sopenharmony_ci       });
236e41f4b71Sopenharmony_ci     }
237e41f4b71Sopenharmony_ci   }
238e41f4b71Sopenharmony_ci   ```
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci4. Use the APIs provided by **DataShareHelper** to access the services provided by the provider, for example, adding, deleting, modifying, and querying data.
241e41f4b71Sopenharmony_ci   
242e41f4b71Sopenharmony_ci   ```ts
243e41f4b71Sopenharmony_ci   // Construct a piece of data.
244e41f4b71Sopenharmony_ci   let key1 = 'name';
245e41f4b71Sopenharmony_ci   let key2 = 'age';
246e41f4b71Sopenharmony_ci   let key3 = 'isStudent';
247e41f4b71Sopenharmony_ci   let key4 = 'Binary';
248e41f4b71Sopenharmony_ci   let valueName1 = 'ZhangSan';
249e41f4b71Sopenharmony_ci   let valueName2 = 'LiSi';
250e41f4b71Sopenharmony_ci   let valueAge1 = 21;
251e41f4b71Sopenharmony_ci   let valueAge2 = 18;
252e41f4b71Sopenharmony_ci   let valueIsStudent1 = false;
253e41f4b71Sopenharmony_ci   let valueIsStudent2 = true;
254e41f4b71Sopenharmony_ci   let valueBinary = new Uint8Array([1, 2, 3]);
255e41f4b71Sopenharmony_ci   let valuesBucket: ValuesBucket = { key1: valueName1, key2: valueAge1, key3: valueIsStudent1, key4: valueBinary };
256e41f4b71Sopenharmony_ci   let updateBucket: ValuesBucket = { key1: valueName2, key2: valueAge2, key3: valueIsStudent2, key4: valueBinary };
257e41f4b71Sopenharmony_ci   let predicates = new dataSharePredicates.DataSharePredicates();
258e41f4b71Sopenharmony_ci   let valArray = ['*'];
259e41f4b71Sopenharmony_ci   
260e41f4b71Sopenharmony_ci   let record: Record<string, Array<dataShare.UpdateOperation>> = {};
261e41f4b71Sopenharmony_ci   let operations1: Array<dataShare.UpdateOperation> = [];
262e41f4b71Sopenharmony_ci   let operations2: Array<dataShare.UpdateOperation> = [];
263e41f4b71Sopenharmony_ci   let operation1: dataShare.UpdateOperation = {
264e41f4b71Sopenharmony_ci     values: valuesBucket,
265e41f4b71Sopenharmony_ci     predicates: predicates
266e41f4b71Sopenharmony_ci   }
267e41f4b71Sopenharmony_ci   operations1.push(operation1);
268e41f4b71Sopenharmony_ci   let operation2: dataShare.UpdateOperation = {
269e41f4b71Sopenharmony_ci     values: updateBucket,
270e41f4b71Sopenharmony_ci     predicates: predicates
271e41f4b71Sopenharmony_ci   }
272e41f4b71Sopenharmony_ci   operations2.push(operation2);
273e41f4b71Sopenharmony_ci   record["uri1"] = operations1;
274e41f4b71Sopenharmony_ci   record["uri2"] = operations2;
275e41f4b71Sopenharmony_ci   
276e41f4b71Sopenharmony_ci   if (dsHelper != undefined) {
277e41f4b71Sopenharmony_ci     // Insert a piece of data.
278e41f4b71Sopenharmony_ci     (dsHelper as dataShare.DataShareHelper).insert(dseUri, valuesBucket, (err:BusinessError, data:number) => {
279e41f4b71Sopenharmony_ci       console.info(`dsHelper insert result:${data}`);
280e41f4b71Sopenharmony_ci     });
281e41f4b71Sopenharmony_ci     // Update data.
282e41f4b71Sopenharmony_ci     (dsHelper as dataShare.DataShareHelper).update(dseUri, predicates, updateBucket, (err:BusinessError, data:number) => {
283e41f4b71Sopenharmony_ci       console.info(`dsHelper update result:${data}`);
284e41f4b71Sopenharmony_ci     });
285e41f4b71Sopenharmony_ci     // Query data.
286e41f4b71Sopenharmony_ci     (dsHelper as dataShare.DataShareHelper).query(dseUri, predicates, valArray, (err:BusinessError, data:DataShareResultSet) => {
287e41f4b71Sopenharmony_ci       console.info(`dsHelper query result:${data}`);
288e41f4b71Sopenharmony_ci     });
289e41f4b71Sopenharmony_ci     // Delete data.
290e41f4b71Sopenharmony_ci     (dsHelper as dataShare.DataShareHelper).delete(dseUri, predicates, (err:BusinessError, data:number) => {
291e41f4b71Sopenharmony_ci       console.info(`dsHelper delete result:${data}`);
292e41f4b71Sopenharmony_ci     });
293e41f4b71Sopenharmony_ci     // Update data in batches.
294e41f4b71Sopenharmony_ci     (dsHelper as dataShare.DataShareHelper).batchUpdate(record).then((data: Record<string, Array<number>>) => {
295e41f4b71Sopenharmony_ci        // Traverse data to obtain the update result of each data record. value indicates the number of data records that are successfully updated. If value is less than 0, the update fails.
296e41f4b71Sopenharmony_ci        let a = Object.entries(data);
297e41f4b71Sopenharmony_ci        for (let i = 0; i < a.length; i++) {
298e41f4b71Sopenharmony_ci          let key = a[i][0];
299e41f4b71Sopenharmony_ci          let values = a[i][1]
300e41f4b71Sopenharmony_ci          console.info(`Update uri:${key}`);
301e41f4b71Sopenharmony_ci          for (const value of values) {
302e41f4b71Sopenharmony_ci            console.info(`Update result:${value}`);
303e41f4b71Sopenharmony_ci          }
304e41f4b71Sopenharmony_ci        }
305e41f4b71Sopenharmony_ci      });
306e41f4b71Sopenharmony_ci     // Close the DataShareHelper instance.
307e41f4b71Sopenharmony_ci     (dsHelper as dataShare.DataShareHelper).close();
308e41f4b71Sopenharmony_ci   }
309e41f4b71Sopenharmony_ci   ```
310