1e41f4b71Sopenharmony_ci# @ohos.data.dataShare (DataShare) (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **DataShare** module allows an application to manage its own data and share data with other applications on the same device. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> - The APIs provided by this module are system APIs. 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> - The APIs of this module can be used only in the stage model. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci## Modules to Import 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci```ts 17e41f4b71Sopenharmony_ciimport { dataShare } from '@kit.ArkData'; 18e41f4b71Sopenharmony_ci``` 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci## dataShare.createDataShareHelper 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_cicreateDataShareHelper(context: Context, uri: string, callback: AsyncCallback<DataShareHelper>): void 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciCreates a **DataShareHelper** instance. This API uses an asynchronous callback to return the result. 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci> **NOTE** 27e41f4b71Sopenharmony_ci> 28e41f4b71Sopenharmony_ci> For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci**Parameters** 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 35e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------------- | ---- | ------------------------------------------------------------ | 36e41f4b71Sopenharmony_ci| context | [Context](../apis-ability-kit/js-apis-inner-application-context.md#context) | Yes | Context of the application. | 37e41f4b71Sopenharmony_ci| uri | string | Yes | Uniform Resource Identifier (URI) of the server application to connect. | 38e41f4b71Sopenharmony_ci| callback | AsyncCallback<[DataShareHelper](#datasharehelper)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the **DataShareHelper** instance created. Otherwise, **err** is an error object. | 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**Error codes** 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci| ID | Error Message | 45e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | 46e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 47e41f4b71Sopenharmony_ci| 15700010 | The DataShareHelper is not initialized successfully. | 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci**Example** 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci```ts 52e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 53e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 56e41f4b71Sopenharmony_cilet dataShareHelper: dataShare.DataShareHelper | undefined = undefined; 57e41f4b71Sopenharmony_cilet context = getContext(UIAbility); 58e41f4b71Sopenharmony_citry { 59e41f4b71Sopenharmony_ci dataShare.createDataShareHelper(context, uri, (err:BusinessError, data:dataShare.DataShareHelper) => { 60e41f4b71Sopenharmony_ci if (err !== undefined) { 61e41f4b71Sopenharmony_ci console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `); 62e41f4b71Sopenharmony_ci return; 63e41f4b71Sopenharmony_ci } 64e41f4b71Sopenharmony_ci console.info("createDataShareHelper succeed, data : " + data); 65e41f4b71Sopenharmony_ci dataShareHelper = data; 66e41f4b71Sopenharmony_ci }); 67e41f4b71Sopenharmony_ci} catch (err) { 68e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 69e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 70e41f4b71Sopenharmony_ci console.error(`createDataShareHelper error: code: ${code}, message: ${message} `); 71e41f4b71Sopenharmony_ci}; 72e41f4b71Sopenharmony_ci``` 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci## dataShare.createDataShareHelper<sup>10+</sup> 75e41f4b71Sopenharmony_cicreateDataShareHelper(context: Context, uri: string, options: DataShareHelperOptions, callback: AsyncCallback<DataShareHelper>): void 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ciCreates a **DataShareHelper** instance. This API uses an asynchronous callback to return the result. 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci> **NOTE** 80e41f4b71Sopenharmony_ci> 81e41f4b71Sopenharmony_ci> For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 87e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------------- | ---- | ------------------------------------------------------------ | 88e41f4b71Sopenharmony_ci| context | [Context](../apis-ability-kit/js-apis-inner-application-context.md#context) | Yes | Context of the application. | 89e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the server application to connect. | 90e41f4b71Sopenharmony_ci| options | [DataShareHelperOptions](#datasharehelperoptions10)| Yes | Configuration specifying whether [DataShareHelper](#datasharehelper) is in proxy mode.| 91e41f4b71Sopenharmony_ci| callback | AsyncCallback<[DataShareHelper](#datasharehelper)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the **DataShareHelper** instance created. Otherwise, **err** is an error object. | 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci**Error codes** 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci| ID | Error Message | 98e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | 99e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 100e41f4b71Sopenharmony_ci| 15700010 | The DataShareHelper is not initialized successfully. | 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci**Example** 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci```ts 105e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 106e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_cilet uri = ("datashareproxy://com.samples.datasharetest.DataShare"); 109e41f4b71Sopenharmony_cilet dataShareHelper: dataShare.DataShareHelper | undefined = undefined; 110e41f4b71Sopenharmony_cilet context = getContext(UIAbility); 111e41f4b71Sopenharmony_citry { 112e41f4b71Sopenharmony_ci dataShare.createDataShareHelper(context, uri, {isProxy : true}, (err:BusinessError, data:dataShare.DataShareHelper) => { 113e41f4b71Sopenharmony_ci if (err !== undefined) { 114e41f4b71Sopenharmony_ci console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `); 115e41f4b71Sopenharmony_ci return; 116e41f4b71Sopenharmony_ci } 117e41f4b71Sopenharmony_ci console.info("createDataShareHelper succeed, data : " + data); 118e41f4b71Sopenharmony_ci dataShareHelper = data; 119e41f4b71Sopenharmony_ci }); 120e41f4b71Sopenharmony_ci} catch (err) { 121e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 122e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 123e41f4b71Sopenharmony_ci console.error(`createDataShareHelper error: code: ${code}, message: ${message} `); 124e41f4b71Sopenharmony_ci}; 125e41f4b71Sopenharmony_ci``` 126e41f4b71Sopenharmony_ci## dataShare.createDataShareHelper 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_cicreateDataShareHelper(context: Context, uri: string, options?: DataShareHelperOptions): Promise<DataShareHelper> 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ciCreates a **DataShareHelper** instance. This API uses a promise to return the result. 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci> **NOTE** 133e41f4b71Sopenharmony_ci> 134e41f4b71Sopenharmony_ci> For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci**Parameters** 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 141e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------- | ---- | ------------------------------ | 142e41f4b71Sopenharmony_ci| context | [Context](../apis-ability-kit/js-apis-inner-application-context.md#context) | Yes | Context of the application. | 143e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the server application to connect. | 144e41f4b71Sopenharmony_ci| options<sup>10+</sup> | [DataShareHelperOptions](#datasharehelperoptions10) | No | Configuration of the **DataShareHelper** instance. This parameter is supported from API version 10. If it is not set, [DataShareHelper](#datasharehelper) is not in proxy mode.| 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci**Return value** 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci| Type | Description | 149e41f4b71Sopenharmony_ci| -------------------------------------------------- | -------------------------------------- | 150e41f4b71Sopenharmony_ci| Promise<[DataShareHelper](#datasharehelper)> | Promise used to return the **DataShareHelper** instance created. | 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci**Error codes** 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci| ID | Error Message | 157e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | 158e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 159e41f4b71Sopenharmony_ci| 15700010 | The DataShareHelper is not initialized successfully. | 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci**Example** 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci```ts 164e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 165e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_cilet uri = ("datashareproxy://com.samples.datasharetest.DataShare"); 168e41f4b71Sopenharmony_cilet dataShareHelper: dataShare.DataShareHelper | undefined = undefined; 169e41f4b71Sopenharmony_cilet context = getContext(UIAbility); 170e41f4b71Sopenharmony_citry { 171e41f4b71Sopenharmony_ci dataShare.createDataShareHelper(context, uri, {isProxy : true}).then((data: dataShare.DataShareHelper) => { 172e41f4b71Sopenharmony_ci console.info("createDataShareHelper succeed, data : " + data); 173e41f4b71Sopenharmony_ci dataShareHelper = data; 174e41f4b71Sopenharmony_ci }). catch((err: BusinessError) => { 175e41f4b71Sopenharmony_ci console.error(`createDataShareHelper error: code: ${err.code}, message: ${err.message} `); 176e41f4b71Sopenharmony_ci }); 177e41f4b71Sopenharmony_ci} catch (err) { 178e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 179e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 180e41f4b71Sopenharmony_ci console.error(`createDataShareHelper error: code: ${code}, message: ${message} `); 181e41f4b71Sopenharmony_ci}; 182e41f4b71Sopenharmony_ci``` 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci## dataShare.enableSilentProxy<sup>11+</sup> 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_cienableSilentProxy(context: Context, uri?: string): Promise<void> 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ciEnables silent access. This API uses a promise to return the result. 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ciObserve the following when using this API: 191e41f4b71Sopenharmony_ci - The data provider calls this API to enable silent access. 192e41f4b71Sopenharmony_ci - Whether silent access is enabled is determined based on the return value of this API and the [isSilentProxyEnable](../../database/share-data-by-datashareextensionability.md) field in the **data_share_config.json** file together. 193e41f4b71Sopenharmony_ci - If silent access is enabled for a URI using this API, silent access takes effect when the related **datashareHelper** API is called. Otherwise, the setting of **isSilentProxyEnable** in the **data_share_config.json** file is used to determine whether to enable silent access. 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci**Parameters** 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 200e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------- | ---- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 201e41f4b71Sopenharmony_ci| context | [Context](../apis-ability-kit/js-apis-inner-application-context.md#context) | Yes | Context of the application. | 202e41f4b71Sopenharmony_ci| uri | string | No | URI of the data, for which silent access is to be enabled.<br>Global setting: If **uri** is **undefined** or **null** or is not specified, all the previous settings will be cleared and silent access will be enabled globally for the data provider.<br>URI-specific setting: If a URI is specified, silent access to the specified URI will be enabled.<br>When datashareHelper APIs are called, the URI-specific setting is preferentially applied. If no match is found, the global setting is applied.<br>URI format: **datashare:///{bundleName}/{moduleName}/{storeName}/{tableName}** | 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci**Return value** 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci| Type | Description | 207e41f4b71Sopenharmony_ci| -------------------------------------------------- | -------------------------------------- | 208e41f4b71Sopenharmony_ci| Promise<void> | returns no value. | 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci**Error codes** 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci| ID | Error Message | 215e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | 216e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 217e41f4b71Sopenharmony_ci| 15700011 | The URI is not exist. | 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci**Example** 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci```ts 222e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 223e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_cilet uri = ("datashare:///com.acts.datasharetest/entry/DB00/TBL00?Proxy=true"); 226e41f4b71Sopenharmony_cilet context = getContext(UIAbility); 227e41f4b71Sopenharmony_cidataShare.enableSilentProxy(context, uri).then(() => { 228e41f4b71Sopenharmony_ci console.info("enableSilentProxy succeed"); 229e41f4b71Sopenharmony_ci}). catch((err: BusinessError) => { 230e41f4b71Sopenharmony_ci console.error(`enableSilentProxy error: code: ${err.code}, message: ${err.message} `); 231e41f4b71Sopenharmony_ci}); 232e41f4b71Sopenharmony_ci``` 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci## dataShare.disableSilentProxy<sup>11+</sup> 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_cidisableSilentProxy(context: Context, uri?: string): Promise<void> 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ciDisables silent access. This API uses a promise to return the result. 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ciObserve the following when using this API: 241e41f4b71Sopenharmony_ci - The data provider calls this API to disable silent access. 242e41f4b71Sopenharmony_ci - Whether silent access is disabled is determined based on the return value of this API and the [isSilentProxyEnable](../../database/share-data-by-datashareextensionability.md) field in the **data_share_config.json** file together. 243e41f4b71Sopenharmony_ci - If silent access is disabled for a URI using this API, the setting takes effect when the related **datashareHelper** API is called. Otherwise, the setting of **isSilentProxyEnable** in the **data_share_config.json** file is used to determine whether to disable silent access. 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci**Parameters** 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 250e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------- | ---- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 251e41f4b71Sopenharmony_ci| context | [Context](../apis-ability-kit/js-apis-inner-application-context.md#context) | Yes | Context of the application. | 252e41f4b71Sopenharmony_ci| uri | string | No | URI of the data, for which silent access is to be disabled.<br>Global setting: If **uri** is **undefined** or **null** or is not specified, all the previous settings will be cleared and silent access will be disabled globally for the data provider.<br>URI-specific setting: If a URI is specified, silent access to the specified URI will be disabled.<br>When datashareHelper APIs are called, the URI-specific setting is preferentially applied. If no match is found, the global setting is applied.<br>URI format: **datashare:///{bundleName}/{moduleName}/{storeName}/{tableName}** | 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci**Return value** 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci| Type | Description | 257e41f4b71Sopenharmony_ci| -------------------------------------------------- | -------------------------------------- | 258e41f4b71Sopenharmony_ci| Promise<void> | returns no value. | 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci**Error codes** 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci| ID | Error Message | 265e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | 266e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 267e41f4b71Sopenharmony_ci| 15700011 | The URI is not exist. | 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci**Example** 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci```ts 272e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 273e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_cilet uri = ("datashare:///com.acts.datasharetest/entry/DB00/TBL00?Proxy=true"); 276e41f4b71Sopenharmony_cilet context = getContext(UIAbility); 277e41f4b71Sopenharmony_cidataShare.disableSilentProxy(context, uri).then(() => { 278e41f4b71Sopenharmony_ci console.info("disableSilentProxy succeed"); 279e41f4b71Sopenharmony_ci}). catch((err: BusinessError) => { 280e41f4b71Sopenharmony_ci console.error(`disableSilentProxy error: code: ${err.code}, message: ${err.message} `); 281e41f4b71Sopenharmony_ci}); 282e41f4b71Sopenharmony_ci``` 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci## DataShareHelperOptions<sup>10+</sup> 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ciDefines whether [DataShareHelper](#datasharehelper) is in proxy mode. 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 291e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 292e41f4b71Sopenharmony_ci| isProxy | boolean | No | Whether the [DataShareHelper](#datasharehelper) is in proxy mode.<br/>The default value is **false**.<br>If the value is **true**, the [DataShareHelper](#datasharehelper) to be created is in proxy mode, and all operations will not open the data provider application unless the database does not exist. If the database does not exist, [createDataShareHelper](#datasharecreatedatasharehelper10) will start the data provider to create a database. | 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci## TemplateId<sup>10+</sup> 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ciDefines the **TemplateId** struct. **TemplateId** is generated by [**addTemplate**](#addtemplate10) to identify a template. 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 301e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 302e41f4b71Sopenharmony_ci| subscriberId | string | Yes | ID of the subscriber who handles the callback. The value must the same as the **subscriberId** in [**addTemplate**](#addtemplate10). The ID of each subscriber must be unique. | 303e41f4b71Sopenharmony_ci| bundleNameOfOwner | string | Yes | Bundle name of the template owner. The value must be the same as the **bundleName** in [**addTemplate**](#addtemplate10). | 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci## PublishedItem<sup>10+</sup> 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ciDefines the data to publish. 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 312e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 313e41f4b71Sopenharmony_ci| key | string | Yes | Key of the data to publish. | 314e41f4b71Sopenharmony_ci| data | string \| ArrayBuffer | Yes | Data to publish. If the data to publish exceeds 20 KB, you are advised to use the data in ArrayBuffer format. | 315e41f4b71Sopenharmony_ci| subscriberId | string | Yes | Subscriber ID. | 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci## RdbDataChangeNode<sup>10+</sup> 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ciDefines the subscription/unsubscription result of the RDB data changes. 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 324e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 325e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the callback. | 326e41f4b71Sopenharmony_ci| templateId | [TemplateId](#templateid10) | Yes | ID of the template that triggers the callback. | 327e41f4b71Sopenharmony_ci| data | Array<string> | Yes | Data of the callback. | 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci## PublishedDataChangeNode<sup>10+</sup> 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ciDefines the subscription/unsubscription result of the changes in the published data. 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 336e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 337e41f4b71Sopenharmony_ci| bundleName | string | Yes | Bundle name of the callback. | 338e41f4b71Sopenharmony_ci| data | Array<[PublishedItem](#publisheditem10)> | Yes | Data of the callback. | 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci## Template<sup>10+</sup> 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ciDefines the struct of the template used in a subscription. 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 347e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 348e41f4b71Sopenharmony_ci| predicates | Record<string, string> | Yes | Predicates to use. When [**on**](#onrdbdatachange10) is called, the predicates are used to generate data. This parameter applies only to RDB data storage. | 349e41f4b71Sopenharmony_ci| scheduler | string | Yes | Template scheduler SQL, which is embedded with a custom function. Currently, the **remindTimer** function is embedded. The **remindTimer** triggers a subscription-based update in specified scenarios.<br>The scheduler SQL statement is triggered when:<br>1. The subscribed data is modified.<br>2. The first subscription is added to the corresponding database. | 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci## OperationResult<sup>10+</sup> 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ciDefines the result of the operation for subscribing to or unsubscribing from the data changes or published data. 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 358e41f4b71Sopenharmony_ci| -------- | -------- | ----- | -------- | 359e41f4b71Sopenharmony_ci| key | string | Yes | Key of the operation result. | 360e41f4b71Sopenharmony_ci| result | number | Yes | Operation result. If the operation is successful, **0** is returned; otherwise, an error code is returned. | 361e41f4b71Sopenharmony_ci## UpdateOperation<sup>12+</sup> 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ciRepresents the batch update operation information. 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 368e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | -------------- | 369e41f4b71Sopenharmony_ci| values | [ValuesBucket](js-apis-data-valuesBucket.md#valuesbucket) | Yes | Data to be updated, which | 370e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes | Conditions for deleting the data. | 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_ci## ChangeType<sup>12+</sup> 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ciEnumerates the data change types. 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ci| Name | Value | Description | 379e41f4b71Sopenharmony_ci| ---------| ------------| --------------| 380e41f4b71Sopenharmony_ci| INSERT | 0 | Data is added.| 381e41f4b71Sopenharmony_ci| DELETE | 1 | Data is deleted.| 382e41f4b71Sopenharmony_ci| UPDATE | 2 | Data is updated.| 383e41f4b71Sopenharmony_ci 384e41f4b71Sopenharmony_ci## SubscriptionType<sup>12+</sup> 385e41f4b71Sopenharmony_ci 386e41f4b71Sopenharmony_ciEnumerates the data subscription types. 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ci| Name | Value | Description | 391e41f4b71Sopenharmony_ci| ----------------------------|------| ---------------------------- | 392e41f4b71Sopenharmony_ci| SUBSCRIPTION_TYPE_EXACT_URI | 0 | Data change of the specified URI.| 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci## ChangeInfo<sup>12+</sup> 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ciRepresents the data change information, including the data change type, URI of the data changed, and changed data content. 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 401e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | -------------- | 402e41f4b71Sopenharmony_ci| type | [ChangeType](#changetype12) | Yes | Data change type. | 403e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data changed. | 404e41f4b71Sopenharmony_ci| values | Array<[ValuesBucket](js-apis-data-valuesBucket.md#valuesbucket)>| Yes | Changed data. | 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci## DataShareHelper 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ciProvides a **DataShareHelper** instance to access or manage data on the server. Before calling an API provided by **DataShareHelper**, you must create a **DataShareHelper** instance using [createDataShareHelper](#datasharecreatedatasharehelper). 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci### on('dataChange') 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_cion(type: 'dataChange', uri: string, callback: AsyncCallback<void>): void 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ciSubscribes to the data change of the specified URI. After an observer is registered, the subscriber will receive a notification when the **notifyChange()** API is called. This API uses an asynchronous callback to return the result. 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci**Parameters** 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 421e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ------------------------ | 422e41f4b71Sopenharmony_ci| type | string | Yes | Event/callback type. The value is **dataChange**, which indicates the data change. | 423e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to be observed. | 424e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the data change. If the data is changed, **err** is **undefined**. Otherwise, this callback is not invoked or **err** is an error object. | 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ci**Error codes** 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci| ID | Error Message | 431e41f4b71Sopenharmony_ci| -------- | -------------------- | 432e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 433e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 434e41f4b71Sopenharmony_ci 435e41f4b71Sopenharmony_ci**Example** 436e41f4b71Sopenharmony_ci 437e41f4b71Sopenharmony_ci```ts 438e41f4b71Sopenharmony_cilet onCallback: () => void = (): void => { 439e41f4b71Sopenharmony_ci console.info("**** Observer on callback ****"); 440e41f4b71Sopenharmony_ci} 441e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 442e41f4b71Sopenharmony_ciif (dataShareHelper !== undefined) { 443e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).on("dataChange", uri, onCallback); 444e41f4b71Sopenharmony_ci} 445e41f4b71Sopenharmony_ci``` 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci### on('dataChange')<sup>12+</sup> 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_cion(event: 'dataChange', type:SubscriptionType, uri: string, callback: AsyncCallback<ChangeInfo>): void 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ciSubscribes to the data change of the specified URI. This API uses an asynchronous callback to return the result. After a change notification is registered, the subscriber will receive a notification when the **notifyChange()** API is called. The change notification contains the data change type, URI of the data changed, and the changed data. Silent access is not supported. 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci**Parameters** 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 458e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ------------------------ | 459e41f4b71Sopenharmony_ci| event | string | Yes | Event/callback type. The value is **dataChange**, which indicates the data change. | 460e41f4b71Sopenharmony_ci| type | [SubscriptionType](#subscriptiontype12)| Yes | Subscription type. | 461e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to be observed. | 462e41f4b71Sopenharmony_ci| callback | AsyncCallback<[ChangeInfo](#changeinfo12)> | Yes | Callback used to return the data change when the change notification is triggered.| 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ci**Error codes** 465e41f4b71Sopenharmony_ci 466e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 467e41f4b71Sopenharmony_ci 468e41f4b71Sopenharmony_ci| ID | Error Message | 469e41f4b71Sopenharmony_ci| -------- | -------------------- | 470e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 471e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci**Example** 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci<!--code_no_check--> 476e41f4b71Sopenharmony_ci```ts 477e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_cilet uri = ("datashare:///com.acts.datasharetest"); 480e41f4b71Sopenharmony_ciexport function callback(error:BusinessError, ChangeInfo:dataShare.ChangeInfo) { 481e41f4b71Sopenharmony_ci console.info(' **** Observer callback **** ChangeInfo:' + JSON.stringify(ChangeInfo)); 482e41f4b71Sopenharmony_ci} 483e41f4b71Sopenharmony_ciif (dataShareHelper !== undefined) { 484e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).on('dataChange', dataShare.SubscriptionType.SUBSCRIPTION_TYPE_EXACT_URI, uri, callback); 485e41f4b71Sopenharmony_ci} 486e41f4b71Sopenharmony_ci``` 487e41f4b71Sopenharmony_ci 488e41f4b71Sopenharmony_ci### off('dataChange') 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_cioff(type: 'dataChange', uri: string, callback?: AsyncCallback<void>): void 491e41f4b71Sopenharmony_ci 492e41f4b71Sopenharmony_ciUnsubscribes from the data change of the specified URI. 493e41f4b71Sopenharmony_ci 494e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 495e41f4b71Sopenharmony_ci 496e41f4b71Sopenharmony_ci**Parameters** 497e41f4b71Sopenharmony_ci 498e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 499e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ------------------------ | 500e41f4b71Sopenharmony_ci| type | string | Yes | Event/callback type. The value is **dataChange**, which indicates the data change. | 501e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to be observed. | 502e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | No | Callback to unregister. If this parameter is **undefined**, **null**, or left empty, this API unregisters all callbacks for the specified URI. | 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci**Error codes** 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci| ID | Error Message | 509e41f4b71Sopenharmony_ci| -------- | -------------------- | 510e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 511e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ci**Example** 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci```ts 516e41f4b71Sopenharmony_cilet callback: () => void = (): void => { 517e41f4b71Sopenharmony_ci console.info("**** Observer on callback ****"); 518e41f4b71Sopenharmony_ci} 519e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 520e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 521e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).on("dataChange", uri, callback); 522e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).off("dataChange", uri, callback); 523e41f4b71Sopenharmony_ci} 524e41f4b71Sopenharmony_ci``` 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ci### off('dataChange')<sup>12+</sup> 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_cioff(event: 'dataChange', type:SubscriptionType, uri: string, callback?: AsyncCallback<ChangeInfo>): void 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ciUnsubscribes from the data change of the specified URI. Silent access is not supported. 532e41f4b71Sopenharmony_ci 533e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 534e41f4b71Sopenharmony_ci 535e41f4b71Sopenharmony_ci**Parameters** 536e41f4b71Sopenharmony_ci 537e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 538e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ------------------------ | 539e41f4b71Sopenharmony_ci| event | string | Yes | Event or callback type. The value is **dataChange**, which indicates the data change. | 540e41f4b71Sopenharmony_ci| type | [SubscriptionType](#subscriptiontype12)| Yes | Subscription type. | 541e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to be observed. | 542e41f4b71Sopenharmony_ci| callback | AsyncCallback<[ChangeInfo](#changeinfo12)>| No | Callback to unregister. If this parameter is **undefined**, **null**, or left empty, this API unregisters all callbacks for the specified URI. If this parameter is specified, the callback must be the one registered in [on('datachange')](#ondatachange12).| 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci**Error codes** 545e41f4b71Sopenharmony_ci 546e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_ci| ID | Error Message | 549e41f4b71Sopenharmony_ci| -------- | -------------------- | 550e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 551e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ci**Example** 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ci<!--code_no_check--> 556e41f4b71Sopenharmony_ci```ts 557e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 558e41f4b71Sopenharmony_ci 559e41f4b71Sopenharmony_cilet uri = ("datashare:///com.acts.datasharetest"); 560e41f4b71Sopenharmony_ciexport function callback(error:BusinessError, ChangeInfo:dataShare.ChangeInfo) { 561e41f4b71Sopenharmony_ci console.info(' **** Observer callback **** ChangeInfo:' + JSON.stringify(ChangeInfo)); 562e41f4b71Sopenharmony_ci} 563e41f4b71Sopenharmony_ciif (dataShareHelper !== undefined) { 564e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).on("dataChange", dataShare.SubscriptionType.SUBSCRIPTION_TYPE_EXACT_URI, uri, callback); 565e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).off("dataChange", dataShare.SubscriptionType.SUBSCRIPTION_TYPE_EXACT_URI, uri, callback); 566e41f4b71Sopenharmony_ci} 567e41f4b71Sopenharmony_ci``` 568e41f4b71Sopenharmony_ci 569e41f4b71Sopenharmony_ci### addTemplate<sup>10+</sup> 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ciaddTemplate(uri: string, subscriberId: string, template: Template): void 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_ciAdds a data template with the specified subscriber. 574e41f4b71Sopenharmony_ci 575e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci**Parameters** 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 580e41f4b71Sopenharmony_ci| -------- | ------------------------ | ---- | -------------------------| 581e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to add. | 582e41f4b71Sopenharmony_ci| subscriberId | string | Yes | Unique ID of the template subscriber. | 583e41f4b71Sopenharmony_ci| template | [Template](#template10) | Yes | Data template to add. | 584e41f4b71Sopenharmony_ci 585e41f4b71Sopenharmony_ci**Error codes** 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ci| ID | Error Message | 590e41f4b71Sopenharmony_ci| -------- | -------------------- | 591e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 592e41f4b71Sopenharmony_ci| 15700011 | The URI is not exist.| 593e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ci**Example** 596e41f4b71Sopenharmony_ci 597e41f4b71Sopenharmony_ci```ts 598e41f4b71Sopenharmony_cilet uri = ("datashareproxy://com.samples.datasharetest.DataShare"); 599e41f4b71Sopenharmony_cilet subscriberId = '11'; 600e41f4b71Sopenharmony_cilet key1: string = "p1"; 601e41f4b71Sopenharmony_cilet value1: string = "select cityColumn as city_1, visitedCilumn as visited_1 from citys where like = true"; 602e41f4b71Sopenharmony_cilet key2: string = "p2"; 603e41f4b71Sopenharmony_cilet value2: string = "select cityColumn as city_2, visitedCilumn as visited_2 from citys where like = false"; 604e41f4b71Sopenharmony_cilet template: dataShare.Template = { 605e41f4b71Sopenharmony_ci predicates : { 606e41f4b71Sopenharmony_ci key1 : value1, 607e41f4b71Sopenharmony_ci key2 : value2, 608e41f4b71Sopenharmony_ci }, 609e41f4b71Sopenharmony_ci scheduler : "select remindTimer(time) from TBL00" 610e41f4b71Sopenharmony_ci} 611e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 612e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).addTemplate(uri, subscriberId, template); 613e41f4b71Sopenharmony_ci} 614e41f4b71Sopenharmony_ci``` 615e41f4b71Sopenharmony_ci 616e41f4b71Sopenharmony_ci### delTemplate<sup>10+</sup> 617e41f4b71Sopenharmony_ci 618e41f4b71Sopenharmony_cidelTemplate(uri: string, subscriberId: string): void 619e41f4b71Sopenharmony_ci 620e41f4b71Sopenharmony_ciDeletes a data template based on the specified subscriber. 621e41f4b71Sopenharmony_ci 622e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 623e41f4b71Sopenharmony_ci 624e41f4b71Sopenharmony_ci**Parameters** 625e41f4b71Sopenharmony_ci 626e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 627e41f4b71Sopenharmony_ci| -------- | -------------| ---- | ------------------------- | 628e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to delete. | 629e41f4b71Sopenharmony_ci| subscriberId | string | Yes | Unique ID of the subscriber. | 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci**Error codes** 632e41f4b71Sopenharmony_ci 633e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_ci| ID | Error Message | 636e41f4b71Sopenharmony_ci| -------- | -------------------- | 637e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 638e41f4b71Sopenharmony_ci| 15700011 | The URI is not exist.| 639e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 640e41f4b71Sopenharmony_ci 641e41f4b71Sopenharmony_ci**Example** 642e41f4b71Sopenharmony_ci 643e41f4b71Sopenharmony_ci```ts 644e41f4b71Sopenharmony_cilet uri = ("datashareproxy://com.samples.datasharetest.DataShare"); 645e41f4b71Sopenharmony_cilet subscriberId = '11'; 646e41f4b71Sopenharmony_cilet key1: string = "p1"; 647e41f4b71Sopenharmony_cilet value1: string = "select cityColumn as city_1, visitedCilumn as visited_1 from citys where like = true"; 648e41f4b71Sopenharmony_cilet key2: string = "p2"; 649e41f4b71Sopenharmony_cilet value2: string = "select cityColumn as city_2, visitedCilumn as visited_2 from citys where like = false"; 650e41f4b71Sopenharmony_cilet template: dataShare.Template = { 651e41f4b71Sopenharmony_ci predicates : { 652e41f4b71Sopenharmony_ci key1 : value1, 653e41f4b71Sopenharmony_ci key2 : value2, 654e41f4b71Sopenharmony_ci }, 655e41f4b71Sopenharmony_ci scheduler : "select remindTimer(time) from TBL00" 656e41f4b71Sopenharmony_ci} 657e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 658e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).addTemplate(uri, subscriberId, template); 659e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).delTemplate(uri, subscriberId); 660e41f4b71Sopenharmony_ci} 661e41f4b71Sopenharmony_ci``` 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_ci### on('rdbDataChange')<sup>10+</sup> 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_cion(type: 'rdbDataChange', uris: Array<string>, templateId: TemplateId, callback: AsyncCallback<RdbDataChangeNode>): Array<OperationResult> 666e41f4b71Sopenharmony_ci 667e41f4b71Sopenharmony_ciSubscribes to the changes of the data corresponding to the specified URI and template. 668e41f4b71Sopenharmony_ci 669e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_ci**Parameters** 672e41f4b71Sopenharmony_ci 673e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 674e41f4b71Sopenharmony_ci| -------- | ----------------------------------| ---- | ------------------------------------------------------------ | 675e41f4b71Sopenharmony_ci| type | string | Yes | Event type. The value is **rdbDataChange**, which indicates the change of the RDB data. If **type** is any other value, there is no response to this API. | 676e41f4b71Sopenharmony_ci| uris | Array<string> | Yes | URIs of the target data. | 677e41f4b71Sopenharmony_ci| templateId | [TemplateId](#templateid10) | Yes | ID of the template that triggers the callback. | 678e41f4b71Sopenharmony_ci| callback | AsyncCallback<[RdbDataChangeNode](#rdbdatachangenode10)> | Yes | Callback used to return the data change. If the operation is successful, **err** is **undefined** and **node** is the data changed. Otherwise, this callback is not invoked or **err** is an error object. | 679e41f4b71Sopenharmony_ci 680e41f4b71Sopenharmony_ci**Return value** 681e41f4b71Sopenharmony_ci 682e41f4b71Sopenharmony_ci| Type | Description | 683e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 684e41f4b71Sopenharmony_ci| Array<[OperationResult](#operationresult10)> | Returns the operation result.| 685e41f4b71Sopenharmony_ci 686e41f4b71Sopenharmony_ci**Error codes** 687e41f4b71Sopenharmony_ci 688e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 689e41f4b71Sopenharmony_ci 690e41f4b71Sopenharmony_ci| ID | Error Message | 691e41f4b71Sopenharmony_ci| -------- | -------------------- | 692e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 693e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 694e41f4b71Sopenharmony_ci 695e41f4b71Sopenharmony_ci**Example** 696e41f4b71Sopenharmony_ci 697e41f4b71Sopenharmony_ci```ts 698e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 699e41f4b71Sopenharmony_ci 700e41f4b71Sopenharmony_cilet onCallback: (err: BusinessError, node: dataShare.RdbDataChangeNode) => void = (err: BusinessError, node:dataShare.RdbDataChangeNode): void => { 701e41f4b71Sopenharmony_ci console.info("onCallback " + JSON.stringify(node.uri)); 702e41f4b71Sopenharmony_ci console.info("onCallback " + JSON.stringify(node.templateId)); 703e41f4b71Sopenharmony_ci console.info("onCallback " + node.data.length); 704e41f4b71Sopenharmony_ci for (let i = 0; i < node.data.length; i++) { 705e41f4b71Sopenharmony_ci console.info("onCallback " + typeof node.data[i] + " " + node.data[i]); 706e41f4b71Sopenharmony_ci } 707e41f4b71Sopenharmony_ci} 708e41f4b71Sopenharmony_ci 709e41f4b71Sopenharmony_cilet uri = ("datashareproxy://com.samples.datasharetest.DataShare"); 710e41f4b71Sopenharmony_cilet templateId:dataShare.TemplateId = {subscriberId:"11", bundleNameOfOwner:"com.acts.ohos.data.datasharetest"}; 711e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 712e41f4b71Sopenharmony_ci let result: Array<dataShare.OperationResult> = (dataShareHelper as dataShare.DataShareHelper).on("rdbDataChange", [uri], templateId, onCallback); 713e41f4b71Sopenharmony_ci} 714e41f4b71Sopenharmony_ci``` 715e41f4b71Sopenharmony_ci 716e41f4b71Sopenharmony_ci### off('rdbDataChange')<sup>10+</sup> 717e41f4b71Sopenharmony_ci 718e41f4b71Sopenharmony_cioff(type: 'rdbDataChange', uris: Array<string>, templateId: TemplateId, callback?: AsyncCallback<RdbDataChangeNode>): Array<OperationResult> 719e41f4b71Sopenharmony_ci 720e41f4b71Sopenharmony_ciUnsubscribes from the changes of the data corresponding to the specified URI and template. 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 723e41f4b71Sopenharmony_ci 724e41f4b71Sopenharmony_ci**Parameters** 725e41f4b71Sopenharmony_ci 726e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 727e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | ---- | ---------------------------------------------------------- | 728e41f4b71Sopenharmony_ci| type | string | Yes | Event type. The value is **rdbDataChange**, which indicates the change of the RDB data. | 729e41f4b71Sopenharmony_ci| uris | Array<string> | Yes | URIs of the target data. | 730e41f4b71Sopenharmony_ci| templateId | [TemplateId](#templateid10) | Yes | ID of the template that triggers the callback. | 731e41f4b71Sopenharmony_ci| callback | AsyncCallback<[RdbDataChangeNode](#rdbdatachangenode10)> | No | Callback to unregister. If this parameter is **undefined**, **null**, or left empty, this API unregisters all callbacks for the specified URI. | 732e41f4b71Sopenharmony_ci 733e41f4b71Sopenharmony_ci**Return value** 734e41f4b71Sopenharmony_ci 735e41f4b71Sopenharmony_ci| Type | Description | 736e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 737e41f4b71Sopenharmony_ci| Array<[OperationResult](#operationresult10)> | Returns the operation result.| 738e41f4b71Sopenharmony_ci 739e41f4b71Sopenharmony_ci**Error codes** 740e41f4b71Sopenharmony_ci 741e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 742e41f4b71Sopenharmony_ci 743e41f4b71Sopenharmony_ci| ID | Error Message | 744e41f4b71Sopenharmony_ci| -------- | -------------------- | 745e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 746e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 747e41f4b71Sopenharmony_ci 748e41f4b71Sopenharmony_ci**Example** 749e41f4b71Sopenharmony_ci 750e41f4b71Sopenharmony_ci```ts 751e41f4b71Sopenharmony_cilet uri = ("datashareproxy://com.samples.datasharetest.DataShare"); 752e41f4b71Sopenharmony_cilet templateId:dataShare.TemplateId = {subscriberId:"11", bundleNameOfOwner:"com.acts.ohos.data.datasharetest"}; 753e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 754e41f4b71Sopenharmony_ci let result: Array<dataShare.OperationResult> = (dataShareHelper as dataShare.DataShareHelper).off("rdbDataChange", [uri], templateId); 755e41f4b71Sopenharmony_ci} 756e41f4b71Sopenharmony_ci``` 757e41f4b71Sopenharmony_ci 758e41f4b71Sopenharmony_ci### on('publishedDataChange')<sup>10+</sup> 759e41f4b71Sopenharmony_ci 760e41f4b71Sopenharmony_cion(type: 'publishedDataChange', uris: Array<string>, subscriberId: string, callback: AsyncCallback<PublishedDataChangeNode>): Array<OperationResult> 761e41f4b71Sopenharmony_ci 762e41f4b71Sopenharmony_ciSubscribes to the change of the published data. 763e41f4b71Sopenharmony_ci 764e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 765e41f4b71Sopenharmony_ci 766e41f4b71Sopenharmony_ci**Parameters** 767e41f4b71Sopenharmony_ci 768e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 769e41f4b71Sopenharmony_ci| -------- | ----------------------------------| ---- | ------------------------------------------------------------ | 770e41f4b71Sopenharmony_ci| type | string | Yes | Event type. The value is **publishedDataChange**, which indicates the change of the published data. | 771e41f4b71Sopenharmony_ci| uris | Array<string> | Yes | URIs of the target data. | 772e41f4b71Sopenharmony_ci| subscriberId | string | Yes | Subscriber ID of the callback. | 773e41f4b71Sopenharmony_ci| callback | AsyncCallback<[PublishedDataChangeNode](#publisheddatachangenode10)> | Yes | Callback used to return the data change. If the operation is successful, **err** is **undefined** and **node** is the data changed. Otherwise, this callback is not invoked or **err** is an error object. | 774e41f4b71Sopenharmony_ci 775e41f4b71Sopenharmony_ci**Return value** 776e41f4b71Sopenharmony_ci 777e41f4b71Sopenharmony_ci| Type | Description | 778e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 779e41f4b71Sopenharmony_ci| Array<[OperationResult](#operationresult10)> | Returns the operation result.| 780e41f4b71Sopenharmony_ci 781e41f4b71Sopenharmony_ci**Error codes** 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 784e41f4b71Sopenharmony_ci 785e41f4b71Sopenharmony_ci| ID | Error Message | 786e41f4b71Sopenharmony_ci| -------- | -------------------- | 787e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 788e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 789e41f4b71Sopenharmony_ci 790e41f4b71Sopenharmony_ci**Example** 791e41f4b71Sopenharmony_ci 792e41f4b71Sopenharmony_ci```ts 793e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 794e41f4b71Sopenharmony_ci 795e41f4b71Sopenharmony_cilet onPublishCallback: (err: BusinessError, node: dataShare.PublishedDataChangeNode) => void = (err: BusinessError, node:dataShare.PublishedDataChangeNode): void => { 796e41f4b71Sopenharmony_ci console.info("onPublishCallback node bundleName " + JSON.stringify(node.bundleName)); 797e41f4b71Sopenharmony_ci console.info("onPublishCallback node data size" + node.data.length); 798e41f4b71Sopenharmony_ci for (let i = 0; i < node.data.length; i++) { 799e41f4b71Sopenharmony_ci console.info("onPublishCallback node " + typeof node.data[i].data); 800e41f4b71Sopenharmony_ci if (typeof node.data[i].data != 'string') { 801e41f4b71Sopenharmony_ci let array: ArrayBuffer = node.data[i].data as ArrayBuffer; 802e41f4b71Sopenharmony_ci let data: Uint8Array = new Uint8Array(array); 803e41f4b71Sopenharmony_ci console.info("onPublishCallback " + i + " " + JSON.stringify(data)); 804e41f4b71Sopenharmony_ci } 805e41f4b71Sopenharmony_ci console.info("onPublishCallback data " + i + " " + JSON.stringify(node.data[i])); 806e41f4b71Sopenharmony_ci } 807e41f4b71Sopenharmony_ci} 808e41f4b71Sopenharmony_cilet uris:Array<string> = ['city', 'datashareproxy://com.acts.ohos.data.datasharetest/appInfo', 'key2']; 809e41f4b71Sopenharmony_cilet subscriberId = '11'; 810e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 811e41f4b71Sopenharmony_ci let result: Array<dataShare.OperationResult> = (dataShareHelper as dataShare.DataShareHelper).on('publishedDataChange', uris, subscriberId, onPublishCallback); 812e41f4b71Sopenharmony_ci} 813e41f4b71Sopenharmony_ci``` 814e41f4b71Sopenharmony_ci 815e41f4b71Sopenharmony_ci### off('publishedDataChange')<sup>10+</sup> 816e41f4b71Sopenharmony_ci 817e41f4b71Sopenharmony_cioff(type: 'publishedDataChange', uris: Array<string>, subscriberId: string, callback?: AsyncCallback<PublishedDataChangeNode>): Array<OperationResult> 818e41f4b71Sopenharmony_ci 819e41f4b71Sopenharmony_ciUnsubscribes from the change of the published data. 820e41f4b71Sopenharmony_ci 821e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 822e41f4b71Sopenharmony_ci 823e41f4b71Sopenharmony_ci**Parameters** 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 826e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | ---- | ---------------------------------------------------------- | 827e41f4b71Sopenharmony_ci| type | string | Yes | Event type. The value is **publishedDataChange**, which indicates the change of the published data.| 828e41f4b71Sopenharmony_ci| uris | Array<string> | Yes | URIs of the target data. | 829e41f4b71Sopenharmony_ci| subscriberId | string | Yes | Subscriber ID of the callback. | 830e41f4b71Sopenharmony_ci| callback | AsyncCallback<[PublishedDataChangeNode](#publisheddatachangenode10)> | No | Callback to unregister. If this parameter is **undefined**, **null**, or left empty, this API unregisters all callbacks for the specified URI.| 831e41f4b71Sopenharmony_ci 832e41f4b71Sopenharmony_ci**Return value** 833e41f4b71Sopenharmony_ci 834e41f4b71Sopenharmony_ci| Type | Description | 835e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 836e41f4b71Sopenharmony_ci| Array<[OperationResult](#operationresult10)> | Returns the operation result.| 837e41f4b71Sopenharmony_ci 838e41f4b71Sopenharmony_ci**Error codes** 839e41f4b71Sopenharmony_ci 840e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 841e41f4b71Sopenharmony_ci 842e41f4b71Sopenharmony_ci| ID | Error Message | 843e41f4b71Sopenharmony_ci| -------- | -------------------- | 844e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 845e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 846e41f4b71Sopenharmony_ci 847e41f4b71Sopenharmony_ci**Example** 848e41f4b71Sopenharmony_ci 849e41f4b71Sopenharmony_ci```ts 850e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 851e41f4b71Sopenharmony_ci 852e41f4b71Sopenharmony_cilet offCallback: (err: BusinessError, node: dataShare.PublishedDataChangeNode) => void = (err: BusinessError, node:dataShare.PublishedDataChangeNode): void => { 853e41f4b71Sopenharmony_ci console.info("**** Observer off callback ****"); 854e41f4b71Sopenharmony_ci} 855e41f4b71Sopenharmony_cilet uris:Array<string> = ["city", "datashareproxy://com.acts.ohos.data.datasharetest/appInfo", "key2"]; 856e41f4b71Sopenharmony_cilet subscriberId = '11'; 857e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 858e41f4b71Sopenharmony_ci let result: Array<dataShare.OperationResult> = (dataShareHelper as dataShare.DataShareHelper).off("publishedDataChange", uris, subscriberId, offCallback); 859e41f4b71Sopenharmony_ci} 860e41f4b71Sopenharmony_ci``` 861e41f4b71Sopenharmony_ci 862e41f4b71Sopenharmony_ci### publish<sup>10+</sup> 863e41f4b71Sopenharmony_ci 864e41f4b71Sopenharmony_cipublish(data: Array<PublishedItem>, bundleName: string, version: number, callback: AsyncCallback<Array<OperationResult>>): void 865e41f4b71Sopenharmony_ci 866e41f4b71Sopenharmony_ciPublishes data to the database. 867e41f4b71Sopenharmony_ci 868e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 869e41f4b71Sopenharmony_ci 870e41f4b71Sopenharmony_ci**Parameters** 871e41f4b71Sopenharmony_ci 872e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 873e41f4b71Sopenharmony_ci| --------- | -------------------------------------------------| ---- | ------------------- | 874e41f4b71Sopenharmony_ci| data | Array<[PublishedItem](#publisheditem10)> | Yes | Data to publish. | 875e41f4b71Sopenharmony_ci| bundleName | string | Yes | Application of the data to publish. This parameter is valid only for the private data published. Only the application can read the data. | 876e41f4b71Sopenharmony_ci| version | number | Yes | Version of the data to publish. A larger value indicates a later version. If the version of the data published is earlier than that of the data in the database, the data in the database will not be updated. | 877e41f4b71Sopenharmony_ci| callback | AsyncCallback<Array<[OperationResult](#operationresult10)>> | Yes | Callback used to return the result. If data is published, **err** is **undefined**, and **result** is the data publish result. Otherwise, this callback is not triggered or **err** is an error object. | 878e41f4b71Sopenharmony_ci 879e41f4b71Sopenharmony_ci**Error codes** 880e41f4b71Sopenharmony_ci 881e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 882e41f4b71Sopenharmony_ci 883e41f4b71Sopenharmony_ci| ID | Error Message | 884e41f4b71Sopenharmony_ci| -------- | -------------------------- | 885e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 886e41f4b71Sopenharmony_ci| 15700012 | The data area is not exist.| 887e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 888e41f4b71Sopenharmony_ci 889e41f4b71Sopenharmony_ci**Example** 890e41f4b71Sopenharmony_ci 891e41f4b71Sopenharmony_ci```ts 892e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 893e41f4b71Sopenharmony_ci 894e41f4b71Sopenharmony_cilet arrayBuffer = new ArrayBuffer(1); 895e41f4b71Sopenharmony_cilet version = 1; 896e41f4b71Sopenharmony_cilet dataArray : Array<dataShare.PublishedItem> = [{key:"key2", subscriberId:"11", data:arrayBuffer}]; 897e41f4b71Sopenharmony_cilet publishCallback: (err: BusinessError, result: Array<dataShare.OperationResult>) => void = (err: BusinessError, result: Array<dataShare.OperationResult>): void => { 898e41f4b71Sopenharmony_ci console.info("publishCallback " + JSON.stringify(result)); 899e41f4b71Sopenharmony_ci} 900e41f4b71Sopenharmony_citry { 901e41f4b71Sopenharmony_ci console.info("dataArray length is:", dataArray.length); 902e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 903e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).publish(dataArray, "com.acts.ohos.data.datasharetest", version, publishCallback); 904e41f4b71Sopenharmony_ci } 905e41f4b71Sopenharmony_ci} catch (e) { 906e41f4b71Sopenharmony_ci console.error("publish error " + JSON.stringify(e)); 907e41f4b71Sopenharmony_ci} 908e41f4b71Sopenharmony_ci``` 909e41f4b71Sopenharmony_ci 910e41f4b71Sopenharmony_ci### publish<sup>10+</sup> 911e41f4b71Sopenharmony_ci 912e41f4b71Sopenharmony_cipublish(data: Array<PublishedItem>, bundleName: string, callback: AsyncCallback<Array<OperationResult>>): void 913e41f4b71Sopenharmony_ci 914e41f4b71Sopenharmony_ciPublishes data to the database. 915e41f4b71Sopenharmony_ci 916e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 917e41f4b71Sopenharmony_ci 918e41f4b71Sopenharmony_ci**Parameters** 919e41f4b71Sopenharmony_ci 920e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 921e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | ---- | ---------------------------------- | 922e41f4b71Sopenharmony_ci| data | Array<[PublishedItem](#publisheditem10)> | Yes | Data to publish. | 923e41f4b71Sopenharmony_ci| bundleName | string | Yes | Application of the data to publish. This parameter is valid only for the private data published. Only the application can read the data. | 924e41f4b71Sopenharmony_ci| callback | AsyncCallback<Array<[OperationResult](#operationresult10)>> | Yes | Callback used to return the result. If data is published, **err** is **undefined**, and **result** is the data publish result. Otherwise, this callback is not triggered or **err** is an error object. | 925e41f4b71Sopenharmony_ci 926e41f4b71Sopenharmony_ci**Error codes** 927e41f4b71Sopenharmony_ci 928e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 929e41f4b71Sopenharmony_ci 930e41f4b71Sopenharmony_ci| ID | Error Message | 931e41f4b71Sopenharmony_ci| -------- | -------------------------- | 932e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 933e41f4b71Sopenharmony_ci| 15700012 | The data area is not exist.| 934e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 935e41f4b71Sopenharmony_ci 936e41f4b71Sopenharmony_ci**Example** 937e41f4b71Sopenharmony_ci 938e41f4b71Sopenharmony_ci```ts 939e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 940e41f4b71Sopenharmony_ci 941e41f4b71Sopenharmony_cilet publishCallback: (err: BusinessError, result: Array<dataShare.OperationResult>) => void = (err: BusinessError, result: Array<dataShare.OperationResult>): void => { 942e41f4b71Sopenharmony_ci console.info("publishCallback " + JSON.stringify(result)); 943e41f4b71Sopenharmony_ci} 944e41f4b71Sopenharmony_cilet dataArray : Array<dataShare.PublishedItem> = [ 945e41f4b71Sopenharmony_ci {key:"city", subscriberId:"11", data:"xian"}, 946e41f4b71Sopenharmony_ci {key:"datashareproxy://com.acts.ohos.data.datasharetest/appInfo", subscriberId:"11", data:"appinfo is just a test app"}, 947e41f4b71Sopenharmony_ci {key:"empty", subscriberId:"11", data:"nobody sub"}]; 948e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 949e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).publish(dataArray, "com.acts.ohos.data.datasharetest", publishCallback); 950e41f4b71Sopenharmony_ci} 951e41f4b71Sopenharmony_ci``` 952e41f4b71Sopenharmony_ci 953e41f4b71Sopenharmony_ci### publish<sup>10+</sup> 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_cipublish(data: Array<PublishedItem>, bundleName: string, version?: number): Promise<Array<OperationResult>> 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ciPublishes data to the database. 958e41f4b71Sopenharmony_ci 959e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 960e41f4b71Sopenharmony_ci 961e41f4b71Sopenharmony_ci**Parameters** 962e41f4b71Sopenharmony_ci 963e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 964e41f4b71Sopenharmony_ci| -------- | ----------------------------- | ---- | ------------------------------ | 965e41f4b71Sopenharmony_ci| data | Array<[PublishedItem](#publisheditem10)> | Yes | Data to publish.| 966e41f4b71Sopenharmony_ci| bundleName | string | Yes | Application of the data to publish. This parameter is valid only for the private data published. Only the application can read the data. | 967e41f4b71Sopenharmony_ci| version | number | No | Version of the data to publish. A larger value indicates a later version. If the version of the data published is earlier than that of the data in the database, the data in the database will not be updated.<br>If the data version is not checked, leave this parameter unspecified. | 968e41f4b71Sopenharmony_ci 969e41f4b71Sopenharmony_ci**Return value** 970e41f4b71Sopenharmony_ci 971e41f4b71Sopenharmony_ci| Type | Description | 972e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 973e41f4b71Sopenharmony_ci| Promise<Array<[OperationResult](#operationresult10)>> | Returns the operation result.| 974e41f4b71Sopenharmony_ci 975e41f4b71Sopenharmony_ci**Error codes** 976e41f4b71Sopenharmony_ci 977e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 978e41f4b71Sopenharmony_ci 979e41f4b71Sopenharmony_ci| ID | Error Message | 980e41f4b71Sopenharmony_ci| -------- | -------------------------- | 981e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 982e41f4b71Sopenharmony_ci| 15700012 | The data area is not exist.| 983e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 984e41f4b71Sopenharmony_ci 985e41f4b71Sopenharmony_ci**Example** 986e41f4b71Sopenharmony_ci 987e41f4b71Sopenharmony_ci```ts 988e41f4b71Sopenharmony_cilet dataArray: Array<dataShare.PublishedItem> = [ 989e41f4b71Sopenharmony_ci {key:"city", subscriberId:"11", data:"xian"}, 990e41f4b71Sopenharmony_ci {key:"datashareproxy://com.acts.ohos.data.datasharetest/appInfo", subscriberId:"11", data:"appinfo is just a test app"}, 991e41f4b71Sopenharmony_ci {key:"empty", subscriberId:"11", data:"nobody sub"}]; 992e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 993e41f4b71Sopenharmony_ci let result: Promise<Array<dataShare.OperationResult>> = (dataShareHelper as dataShare.DataShareHelper).publish(dataArray, "com.acts.ohos.data.datasharetest"); 994e41f4b71Sopenharmony_ci} 995e41f4b71Sopenharmony_ci``` 996e41f4b71Sopenharmony_ci 997e41f4b71Sopenharmony_ci### getPublishedData<sup>10+</sup> 998e41f4b71Sopenharmony_ci 999e41f4b71Sopenharmony_cigetPublishedData(bundleName: string, callback: AsyncCallback<Array<PublishedItem>>): void 1000e41f4b71Sopenharmony_ci 1001e41f4b71Sopenharmony_ciObtains the published data of an application. 1002e41f4b71Sopenharmony_ci 1003e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1004e41f4b71Sopenharmony_ci 1005e41f4b71Sopenharmony_ci**Parameters** 1006e41f4b71Sopenharmony_ci 1007e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1008e41f4b71Sopenharmony_ci| -------- | -----------------| ---- | ----------------------------- | 1009e41f4b71Sopenharmony_ci| bundleName | string | Yes | Application to which the data belongs. | 1010e41f4b71Sopenharmony_ci| callback | AsyncCallback<Array<[PublishedItem](#publisheditem10)>> | Yes | Callback used to return the published data obtained. | 1011e41f4b71Sopenharmony_ci 1012e41f4b71Sopenharmony_ci**Error codes** 1013e41f4b71Sopenharmony_ci 1014e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1015e41f4b71Sopenharmony_ci 1016e41f4b71Sopenharmony_ci| ID | Error Message | 1017e41f4b71Sopenharmony_ci| -------- | -------------------------- | 1018e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1019e41f4b71Sopenharmony_ci| 15700012 | The data area is not exist.| 1020e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1021e41f4b71Sopenharmony_ci 1022e41f4b71Sopenharmony_ci**Example** 1023e41f4b71Sopenharmony_ci 1024e41f4b71Sopenharmony_ci```ts 1025e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1026e41f4b71Sopenharmony_ci 1027e41f4b71Sopenharmony_cilet publishCallback: (err: BusinessError, data: Array<dataShare.PublishedItem>) => void = (err: BusinessError, result: Array<dataShare.PublishedItem>): void => { 1028e41f4b71Sopenharmony_ci console.info("**** Observer publish callback ****"); 1029e41f4b71Sopenharmony_ci} 1030e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 1031e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).getPublishedData("com.acts.ohos.data.datasharetest", publishCallback); 1032e41f4b71Sopenharmony_ci} 1033e41f4b71Sopenharmony_ci``` 1034e41f4b71Sopenharmony_ci 1035e41f4b71Sopenharmony_ci### getPublishedData<sup>10+</sup> 1036e41f4b71Sopenharmony_ci 1037e41f4b71Sopenharmony_cigetPublishedData(bundleName: string): Promise<Array<PublishedItem>> 1038e41f4b71Sopenharmony_ci 1039e41f4b71Sopenharmony_ciObtains the published data of an application. 1040e41f4b71Sopenharmony_ci 1041e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1042e41f4b71Sopenharmony_ci 1043e41f4b71Sopenharmony_ci**Parameters** 1044e41f4b71Sopenharmony_ci 1045e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1046e41f4b71Sopenharmony_ci| -------- | --------------| ---- | -------------------------------------- | 1047e41f4b71Sopenharmony_ci| bundleName | string | Yes | Application to which the data belongs. | 1048e41f4b71Sopenharmony_ci 1049e41f4b71Sopenharmony_ci**Return value** 1050e41f4b71Sopenharmony_ci 1051e41f4b71Sopenharmony_ci| Type | Description | 1052e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ----------------------------------- | 1053e41f4b71Sopenharmony_ci| Promise<Array<[PublishedItem](#publisheditem10)>> | Promise used to return the published data obtained. | 1054e41f4b71Sopenharmony_ci 1055e41f4b71Sopenharmony_ci**Error codes** 1056e41f4b71Sopenharmony_ci 1057e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1058e41f4b71Sopenharmony_ci 1059e41f4b71Sopenharmony_ci| ID | Error Message | 1060e41f4b71Sopenharmony_ci| -------- | -------------------------- | 1061e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1062e41f4b71Sopenharmony_ci| 15700012 | The data area is not exist.| 1063e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1064e41f4b71Sopenharmony_ci 1065e41f4b71Sopenharmony_ci**Example** 1066e41f4b71Sopenharmony_ci 1067e41f4b71Sopenharmony_ci```ts 1068e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 1069e41f4b71Sopenharmony_ci let publishedData: Promise<Array<dataShare.PublishedItem>> = (dataShareHelper as dataShare.DataShareHelper).getPublishedData("com.acts.ohos.data.datasharetest"); 1070e41f4b71Sopenharmony_ci} 1071e41f4b71Sopenharmony_ci``` 1072e41f4b71Sopenharmony_ci 1073e41f4b71Sopenharmony_ci### insert 1074e41f4b71Sopenharmony_ci 1075e41f4b71Sopenharmony_ciinsert(uri: string, value: ValuesBucket, callback: AsyncCallback<number>): void 1076e41f4b71Sopenharmony_ci 1077e41f4b71Sopenharmony_ciInserts a single data record into the database. This API uses an asynchronous callback to return the result. 1078e41f4b71Sopenharmony_ci 1079e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1080e41f4b71Sopenharmony_ci 1081e41f4b71Sopenharmony_ci**Parameters** 1082e41f4b71Sopenharmony_ci 1083e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1084e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ | 1085e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to insert. | 1086e41f4b71Sopenharmony_ci| value | [ValuesBucket](js-apis-data-valuesBucket.md#valuesbucket) | Yes | Data to insert. If this parameter is left empty, a blank row will be inserted. | 1087e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the index of the inserted data record. Otherwise, **err** is an error object.<br>The data index is not returned if the APIs of the database in use, for example, the key-value database (KVDB), do not support the return of indexes. | 1088e41f4b71Sopenharmony_ci 1089e41f4b71Sopenharmony_ci**Error codes** 1090e41f4b71Sopenharmony_ci 1091e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1092e41f4b71Sopenharmony_ci 1093e41f4b71Sopenharmony_ci| ID | Error Message | 1094e41f4b71Sopenharmony_ci| -------- | -------------------- | 1095e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1096e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1097e41f4b71Sopenharmony_ci 1098e41f4b71Sopenharmony_ci**Example** 1099e41f4b71Sopenharmony_ci 1100e41f4b71Sopenharmony_ci```ts 1101e41f4b71Sopenharmony_ciimport { ValuesBucket } from '@kit.ArkData' 1102e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1103e41f4b71Sopenharmony_ci 1104e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1105e41f4b71Sopenharmony_cilet key1: string = "name"; 1106e41f4b71Sopenharmony_cilet value1: string = "rose"; 1107e41f4b71Sopenharmony_cilet key2: string = "age"; 1108e41f4b71Sopenharmony_cilet value2: number = 22; 1109e41f4b71Sopenharmony_cilet key3: string = "salary"; 1110e41f4b71Sopenharmony_cilet value3: number = 200.5; 1111e41f4b71Sopenharmony_ciconst valueBucket: ValuesBucket = { 1112e41f4b71Sopenharmony_ci key1: value1, 1113e41f4b71Sopenharmony_ci key2: value2, 1114e41f4b71Sopenharmony_ci key3: value3, 1115e41f4b71Sopenharmony_ci} 1116e41f4b71Sopenharmony_citry { 1117e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1118e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).insert(uri, valueBucket, (err: BusinessError, data: number) => { 1119e41f4b71Sopenharmony_ci if (err !== undefined) { 1120e41f4b71Sopenharmony_ci console.error(`insert error: code: ${err.code}, message: ${err.message} `); 1121e41f4b71Sopenharmony_ci return; 1122e41f4b71Sopenharmony_ci } 1123e41f4b71Sopenharmony_ci console.info("insert succeed, data : " + data); 1124e41f4b71Sopenharmony_ci }); 1125e41f4b71Sopenharmony_ci } 1126e41f4b71Sopenharmony_ci} catch (err) { 1127e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1128e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 1129e41f4b71Sopenharmony_ci console.error(`insert error: code: ${code}, message: ${message} `); 1130e41f4b71Sopenharmony_ci}; 1131e41f4b71Sopenharmony_ci``` 1132e41f4b71Sopenharmony_ci 1133e41f4b71Sopenharmony_ci### insert 1134e41f4b71Sopenharmony_ci 1135e41f4b71Sopenharmony_ciinsert(uri: string, value: ValuesBucket): Promise<number> 1136e41f4b71Sopenharmony_ci 1137e41f4b71Sopenharmony_ciInserts a single data record into the database. This API uses a promise to return the result. 1138e41f4b71Sopenharmony_ci 1139e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1140e41f4b71Sopenharmony_ci 1141e41f4b71Sopenharmony_ci**Parameters** 1142e41f4b71Sopenharmony_ci 1143e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1144e41f4b71Sopenharmony_ci| ----- | --------------------------------------------------------- | ---- | -------------------------------------------------- | 1145e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to insert. | 1146e41f4b71Sopenharmony_ci| value | [ValuesBucket](js-apis-data-valuesBucket.md#valuesbucket) | Yes | Data to insert. If this parameter is left empty, a blank row will be inserted. | 1147e41f4b71Sopenharmony_ci 1148e41f4b71Sopenharmony_ci**Return value** 1149e41f4b71Sopenharmony_ci 1150e41f4b71Sopenharmony_ci| Type | Description | 1151e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 1152e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the index of the inserted data record.<br>The data index is not returned if the APIs of the database in use (for example, KVDB) do not support the return of indexes. | 1153e41f4b71Sopenharmony_ci 1154e41f4b71Sopenharmony_ci**Error codes** 1155e41f4b71Sopenharmony_ci 1156e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1157e41f4b71Sopenharmony_ci 1158e41f4b71Sopenharmony_ci| ID | Error Message | 1159e41f4b71Sopenharmony_ci| -------- | -------------------- | 1160e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1161e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1162e41f4b71Sopenharmony_ci 1163e41f4b71Sopenharmony_ci**Example** 1164e41f4b71Sopenharmony_ci 1165e41f4b71Sopenharmony_ci```ts 1166e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1167e41f4b71Sopenharmony_ciimport { ValuesBucket } from '@kit.ArkData' 1168e41f4b71Sopenharmony_ci 1169e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1170e41f4b71Sopenharmony_cilet key1: string = "name"; 1171e41f4b71Sopenharmony_cilet value1: string = "rose1"; 1172e41f4b71Sopenharmony_cilet key2: string = "age"; 1173e41f4b71Sopenharmony_cilet value2: number = 21; 1174e41f4b71Sopenharmony_cilet key3: string = "salary"; 1175e41f4b71Sopenharmony_cilet value3: number = 20.5; 1176e41f4b71Sopenharmony_ciconst valueBucket: ValuesBucket = { 1177e41f4b71Sopenharmony_ci key1: value1, 1178e41f4b71Sopenharmony_ci key2: value2, 1179e41f4b71Sopenharmony_ci key3: value3, 1180e41f4b71Sopenharmony_ci} 1181e41f4b71Sopenharmony_citry { 1182e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1183e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).insert(uri, valueBucket).then((data: number) => { 1184e41f4b71Sopenharmony_ci console.info("insert succeed, data : " + data); 1185e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1186e41f4b71Sopenharmony_ci console.error(`insert error: code: ${err.code}, message: ${err.message} `); 1187e41f4b71Sopenharmony_ci }); 1188e41f4b71Sopenharmony_ci } 1189e41f4b71Sopenharmony_ci} catch (err) { 1190e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1191e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 1192e41f4b71Sopenharmony_ci console.error(`insert error: code: ${code}, message: ${message} `); 1193e41f4b71Sopenharmony_ci}; 1194e41f4b71Sopenharmony_ci``` 1195e41f4b71Sopenharmony_ci 1196e41f4b71Sopenharmony_ci### delete 1197e41f4b71Sopenharmony_ci 1198e41f4b71Sopenharmony_cidelete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<number>): void 1199e41f4b71Sopenharmony_ci 1200e41f4b71Sopenharmony_ciDeletes one or more data records from the database. This API uses an asynchronous callback to return the result. 1201e41f4b71Sopenharmony_ci 1202e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1203e41f4b71Sopenharmony_ci 1204e41f4b71Sopenharmony_ci**Parameters** 1205e41f4b71Sopenharmony_ci 1206e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1207e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1208e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to delete. | 1209e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes | Conditions for deleting the data.<br>The predicate methods supported by **delete()** vary depending on the database in use. For example, the KVDB supports only **inKeys**. If this parameter is left empty, the entire table will be deleted by default. | 1210e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the number of deleted data records. Otherwise, **err** is an error object.<br>The number of deleted data records is not returned if the APIs of the database in use (for example, KVDB) do not support this return. | 1211e41f4b71Sopenharmony_ci 1212e41f4b71Sopenharmony_ci**Error codes** 1213e41f4b71Sopenharmony_ci 1214e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1215e41f4b71Sopenharmony_ci 1216e41f4b71Sopenharmony_ci| ID | Error Message | 1217e41f4b71Sopenharmony_ci| -------- | -------------------- | 1218e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1219e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1220e41f4b71Sopenharmony_ci 1221e41f4b71Sopenharmony_ci**Example** 1222e41f4b71Sopenharmony_ci 1223e41f4b71Sopenharmony_ci```ts 1224e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData' 1225e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1226e41f4b71Sopenharmony_ci 1227e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1228e41f4b71Sopenharmony_cilet da = new dataSharePredicates.DataSharePredicates(); 1229e41f4b71Sopenharmony_cida.equalTo("name", "ZhangSan"); 1230e41f4b71Sopenharmony_citry { 1231e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1232e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).delete(uri, da, (err: BusinessError, data: number) => { 1233e41f4b71Sopenharmony_ci if (err !== undefined) { 1234e41f4b71Sopenharmony_ci console.error(`delete error: code: ${err.code}, message: ${err.message} `); 1235e41f4b71Sopenharmony_ci return; 1236e41f4b71Sopenharmony_ci } 1237e41f4b71Sopenharmony_ci console.info("delete succeed, data : " + data); 1238e41f4b71Sopenharmony_ci }); 1239e41f4b71Sopenharmony_ci } 1240e41f4b71Sopenharmony_ci} catch (err) { 1241e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1242e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 1243e41f4b71Sopenharmony_ci console.error(`delete error: code: ${code}, message: ${message} `); 1244e41f4b71Sopenharmony_ci}; 1245e41f4b71Sopenharmony_ci``` 1246e41f4b71Sopenharmony_ci 1247e41f4b71Sopenharmony_ci### delete 1248e41f4b71Sopenharmony_ci 1249e41f4b71Sopenharmony_cidelete(uri: string, predicates: dataSharePredicates.DataSharePredicates): Promise<number> 1250e41f4b71Sopenharmony_ci 1251e41f4b71Sopenharmony_ciDeletes one or more data records from the database. This API uses a promise to return the result. 1252e41f4b71Sopenharmony_ci 1253e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1254e41f4b71Sopenharmony_ci 1255e41f4b71Sopenharmony_ci**Parameters** 1256e41f4b71Sopenharmony_ci 1257e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1258e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1259e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to delete. | 1260e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes | Conditions for deleting the data.<br>The predicate methods supported by **delete()** vary depending on the database in use. For example, the KVDB supports only **inKeys**. If this parameter is left empty, the entire table will be deleted by default. | 1261e41f4b71Sopenharmony_ci 1262e41f4b71Sopenharmony_ci**Return value** 1263e41f4b71Sopenharmony_ci 1264e41f4b71Sopenharmony_ci| Type | Description | 1265e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 1266e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the number of deleted data records.<br>The number of deleted data records is not returned if the APIs of the database in use (for example, KVDB) do not support this return. | 1267e41f4b71Sopenharmony_ci 1268e41f4b71Sopenharmony_ci**Error codes** 1269e41f4b71Sopenharmony_ci 1270e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1271e41f4b71Sopenharmony_ci 1272e41f4b71Sopenharmony_ci| ID | Error Message | 1273e41f4b71Sopenharmony_ci| -------- | -------------------- | 1274e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1275e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1276e41f4b71Sopenharmony_ci 1277e41f4b71Sopenharmony_ci**Example** 1278e41f4b71Sopenharmony_ci 1279e41f4b71Sopenharmony_ci```ts 1280e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData' 1281e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1282e41f4b71Sopenharmony_ci 1283e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1284e41f4b71Sopenharmony_cilet da = new dataSharePredicates.DataSharePredicates(); 1285e41f4b71Sopenharmony_cida.equalTo("name", "ZhangSan"); 1286e41f4b71Sopenharmony_citry { 1287e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1288e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).delete(uri, da).then((data: number) => { 1289e41f4b71Sopenharmony_ci console.info("delete succeed, data : " + data); 1290e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1291e41f4b71Sopenharmony_ci console.error(`delete error: code: ${err.code}, message: ${err.message} `); 1292e41f4b71Sopenharmony_ci }); 1293e41f4b71Sopenharmony_ci } 1294e41f4b71Sopenharmony_ci} catch (err) { 1295e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1296e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 1297e41f4b71Sopenharmony_ci console.error(`delete error: code: ${code}, message: ${message} `); 1298e41f4b71Sopenharmony_ci}; 1299e41f4b71Sopenharmony_ci``` 1300e41f4b71Sopenharmony_ci 1301e41f4b71Sopenharmony_ci### query 1302e41f4b71Sopenharmony_ci 1303e41f4b71Sopenharmony_ciquery(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: AsyncCallback<DataShareResultSet>): void 1304e41f4b71Sopenharmony_ci 1305e41f4b71Sopenharmony_ciQueries data in the database. This API uses an asynchronous callback to return the result. 1306e41f4b71Sopenharmony_ci 1307e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1308e41f4b71Sopenharmony_ci 1309e41f4b71Sopenharmony_ci**Parameters** 1310e41f4b71Sopenharmony_ci 1311e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1312e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1313e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to query. | 1314e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes | Conditions for querying the data.<br>The predicate methods supported by **query()** vary depending on the database used. For example, the KVDB supports only **inKeys** and **prefixKey**. If this parameter is left empty, the entire table will be queried by default. | 1315e41f4b71Sopenharmony_ci| columns | Array<string> | Yes | Column to query. If this parameter is left empty, all columns will be queried. | 1316e41f4b71Sopenharmony_ci| callback | AsyncCallback<[DataShareResultSet](js-apis-data-DataShareResultSet-sys.md#datashareresultset)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the result set obtained. Otherwise, **err** is an error object. | 1317e41f4b71Sopenharmony_ci 1318e41f4b71Sopenharmony_ci**Error codes** 1319e41f4b71Sopenharmony_ci 1320e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1321e41f4b71Sopenharmony_ci 1322e41f4b71Sopenharmony_ci| ID | Error Message | 1323e41f4b71Sopenharmony_ci| -------- | -------------------- | 1324e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1325e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1326e41f4b71Sopenharmony_ci 1327e41f4b71Sopenharmony_ci**Example** 1328e41f4b71Sopenharmony_ci 1329e41f4b71Sopenharmony_ci```ts 1330e41f4b71Sopenharmony_ciimport { dataSharePredicates, DataShareResultSet } from '@kit.ArkData' 1331e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1332e41f4b71Sopenharmony_ci 1333e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1334e41f4b71Sopenharmony_cilet columns = ["*"]; 1335e41f4b71Sopenharmony_cilet da = new dataSharePredicates.DataSharePredicates(); 1336e41f4b71Sopenharmony_cida.equalTo("name", "ZhangSan"); 1337e41f4b71Sopenharmony_citry { 1338e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1339e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).query(uri, da, columns, (err: BusinessError, data: DataShareResultSet) => { 1340e41f4b71Sopenharmony_ci if (err !== undefined) { 1341e41f4b71Sopenharmony_ci console.error(`query error: code: ${err.code}, message: ${err.message} `); 1342e41f4b71Sopenharmony_ci return; 1343e41f4b71Sopenharmony_ci } 1344e41f4b71Sopenharmony_ci console.info("query succeed, rowCount : " + data.rowCount); 1345e41f4b71Sopenharmony_ci }); 1346e41f4b71Sopenharmony_ci } 1347e41f4b71Sopenharmony_ci} catch (err) { 1348e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1349e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 1350e41f4b71Sopenharmony_ci console.error(`query error: code: ${code}, message: ${message} `); 1351e41f4b71Sopenharmony_ci}; 1352e41f4b71Sopenharmony_ci``` 1353e41f4b71Sopenharmony_ci 1354e41f4b71Sopenharmony_ci### query 1355e41f4b71Sopenharmony_ci 1356e41f4b71Sopenharmony_ciquery(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>): Promise<DataShareResultSet> 1357e41f4b71Sopenharmony_ci 1358e41f4b71Sopenharmony_ciQueries data in the database. This API uses a promise to return the result. 1359e41f4b71Sopenharmony_ci 1360e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1361e41f4b71Sopenharmony_ci 1362e41f4b71Sopenharmony_ci**Parameters** 1363e41f4b71Sopenharmony_ci 1364e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1365e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1366e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to query. | 1367e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes | Conditions for querying the data.<br>The predicate methods supported by **query()** vary depending on the database used. For example, the KVDB supports only **inKeys** and **prefixKey**. If this parameter is left empty, the entire table will be queried by default. | 1368e41f4b71Sopenharmony_ci| columns | Array<string> | Yes | Column to query. If this parameter is left empty, all columns will be queried. | 1369e41f4b71Sopenharmony_ci 1370e41f4b71Sopenharmony_ci**Return value** 1371e41f4b71Sopenharmony_ci 1372e41f4b71Sopenharmony_ci| Type | Description | 1373e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | --------------------------------- | 1374e41f4b71Sopenharmony_ci| Promise<[DataShareResultSet](js-apis-data-DataShareResultSet-sys.md#datashareresultset)> | Promise used to return the result set obtained. | 1375e41f4b71Sopenharmony_ci 1376e41f4b71Sopenharmony_ci**Error codes** 1377e41f4b71Sopenharmony_ci 1378e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1379e41f4b71Sopenharmony_ci 1380e41f4b71Sopenharmony_ci| ID | Error Message | 1381e41f4b71Sopenharmony_ci| -------- | -------------------- | 1382e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1383e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1384e41f4b71Sopenharmony_ci 1385e41f4b71Sopenharmony_ci**Example** 1386e41f4b71Sopenharmony_ci 1387e41f4b71Sopenharmony_ci```ts 1388e41f4b71Sopenharmony_ciimport { dataSharePredicates, DataShareResultSet } from '@kit.ArkData' 1389e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1390e41f4b71Sopenharmony_ci 1391e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1392e41f4b71Sopenharmony_cilet columns = ["*"]; 1393e41f4b71Sopenharmony_cilet da = new dataSharePredicates.DataSharePredicates(); 1394e41f4b71Sopenharmony_cida.equalTo("name", "ZhangSan"); 1395e41f4b71Sopenharmony_citry { 1396e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1397e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).query(uri, da, columns).then((data: DataShareResultSet) => { 1398e41f4b71Sopenharmony_ci console.info("query succeed, rowCount : " + data.rowCount); 1399e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1400e41f4b71Sopenharmony_ci console.error(`query error: code: ${err.code}, message: ${err.message} `); 1401e41f4b71Sopenharmony_ci }); 1402e41f4b71Sopenharmony_ci } 1403e41f4b71Sopenharmony_ci} catch (err) { 1404e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1405e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 1406e41f4b71Sopenharmony_ci console.error(`query error: code: ${code}, message: ${message} `); 1407e41f4b71Sopenharmony_ci}; 1408e41f4b71Sopenharmony_ci``` 1409e41f4b71Sopenharmony_ci 1410e41f4b71Sopenharmony_ci### update 1411e41f4b71Sopenharmony_ci 1412e41f4b71Sopenharmony_ciupdate(uri: string, predicates: dataSharePredicates.DataSharePredicates, value: ValuesBucket, callback: AsyncCallback<number>): void 1413e41f4b71Sopenharmony_ci 1414e41f4b71Sopenharmony_ciUpdates data in the database. This API uses an asynchronous callback to return the result. 1415e41f4b71Sopenharmony_ci 1416e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1417e41f4b71Sopenharmony_ci 1418e41f4b71Sopenharmony_ci**Parameters** 1419e41f4b71Sopenharmony_ci 1420e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1421e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1422e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to update. | 1423e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes | Conditions for updating the data.<br>The predicate methods supported by **update()** vary depending on the database in use. For example, only the relational database (RDB) supports predicates. If this parameter is left empty, the entire table will be updated by default. | 1424e41f4b71Sopenharmony_ci| value | [ValuesBucket](js-apis-data-valuesBucket.md#valuesbucket) | Yes | New data, which can be null. | 1425e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the number of updated data records. Otherwise, **err** is an error object.<br>The number of updated data records is not returned if the APIs of the database in use (for example, KVDB) do not support this return. | 1426e41f4b71Sopenharmony_ci 1427e41f4b71Sopenharmony_ci**Error codes** 1428e41f4b71Sopenharmony_ci 1429e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1430e41f4b71Sopenharmony_ci 1431e41f4b71Sopenharmony_ci| ID | Error Message | 1432e41f4b71Sopenharmony_ci| -------- | -------------------- | 1433e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1434e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1435e41f4b71Sopenharmony_ci 1436e41f4b71Sopenharmony_ci**Example** 1437e41f4b71Sopenharmony_ci 1438e41f4b71Sopenharmony_ci```ts 1439e41f4b71Sopenharmony_ciimport { dataSharePredicates, ValuesBucket } from '@kit.ArkData' 1440e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1441e41f4b71Sopenharmony_ci 1442e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1443e41f4b71Sopenharmony_cilet da = new dataSharePredicates.DataSharePredicates(); 1444e41f4b71Sopenharmony_cida.equalTo("name", "ZhangSan"); 1445e41f4b71Sopenharmony_cilet key1: string = "name"; 1446e41f4b71Sopenharmony_cilet value1: string = "roe1" 1447e41f4b71Sopenharmony_cilet key2: string = "age"; 1448e41f4b71Sopenharmony_cilet value2: number = 21 1449e41f4b71Sopenharmony_cilet key3: string = "salary"; 1450e41f4b71Sopenharmony_cilet value3: number = 20.5; 1451e41f4b71Sopenharmony_ciconst va: ValuesBucket = { 1452e41f4b71Sopenharmony_ci key1: value1, 1453e41f4b71Sopenharmony_ci key2: value2, 1454e41f4b71Sopenharmony_ci key3: value3, 1455e41f4b71Sopenharmony_ci} 1456e41f4b71Sopenharmony_citry { 1457e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1458e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).update(uri, da, va, (err: BusinessError, data: number) => { 1459e41f4b71Sopenharmony_ci if (err !== undefined) { 1460e41f4b71Sopenharmony_ci console.error(`update error: code: ${err.code}, message: ${err.message} `); 1461e41f4b71Sopenharmony_ci return; 1462e41f4b71Sopenharmony_ci } 1463e41f4b71Sopenharmony_ci console.info("update succeed, data : " + data); 1464e41f4b71Sopenharmony_ci }); 1465e41f4b71Sopenharmony_ci } 1466e41f4b71Sopenharmony_ci} catch (err) { 1467e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1468e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 1469e41f4b71Sopenharmony_ci console.error(`update error: code: ${code}, message: ${message} `); 1470e41f4b71Sopenharmony_ci}; 1471e41f4b71Sopenharmony_ci``` 1472e41f4b71Sopenharmony_ci 1473e41f4b71Sopenharmony_ci### update 1474e41f4b71Sopenharmony_ci 1475e41f4b71Sopenharmony_ciupdate(uri: string, predicates: dataSharePredicates.DataSharePredicates, value: ValuesBucket): Promise<number> 1476e41f4b71Sopenharmony_ci 1477e41f4b71Sopenharmony_ciUpdates data in the database. This API uses a promise to return the result. 1478e41f4b71Sopenharmony_ci 1479e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1480e41f4b71Sopenharmony_ci 1481e41f4b71Sopenharmony_ci**Parameters** 1482e41f4b71Sopenharmony_ci 1483e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1484e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1485e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to update. | 1486e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes | Conditions for updating the data.<br>The predicate methods supported by **update()** vary depending on the database in use. For example, only the relational database (RDB) supports predicates. If this parameter is left empty, the entire table will be updated by default. | 1487e41f4b71Sopenharmony_ci| value | [ValuesBucket](js-apis-data-valuesBucket.md#valuesbucket) | Yes | New data, which can be null. | 1488e41f4b71Sopenharmony_ci 1489e41f4b71Sopenharmony_ci**Return value** 1490e41f4b71Sopenharmony_ci 1491e41f4b71Sopenharmony_ci| Type | Description | 1492e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 1493e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the number of data records updated.<br>The number of updated data records is not returned if the APIs of the database in use (for example, KVDB) do not support this return. | 1494e41f4b71Sopenharmony_ci 1495e41f4b71Sopenharmony_ci**Error codes** 1496e41f4b71Sopenharmony_ci 1497e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1498e41f4b71Sopenharmony_ci 1499e41f4b71Sopenharmony_ci| ID | Error Message | 1500e41f4b71Sopenharmony_ci| -------- | -------------------- | 1501e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1502e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1503e41f4b71Sopenharmony_ci 1504e41f4b71Sopenharmony_ci**Example** 1505e41f4b71Sopenharmony_ci 1506e41f4b71Sopenharmony_ci```ts 1507e41f4b71Sopenharmony_ciimport { dataSharePredicates, ValuesBucket } from '@kit.ArkData' 1508e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1509e41f4b71Sopenharmony_ci 1510e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1511e41f4b71Sopenharmony_cilet da = new dataSharePredicates.DataSharePredicates(); 1512e41f4b71Sopenharmony_cida.equalTo("name", "ZhangSan"); 1513e41f4b71Sopenharmony_cilet key1: string = "name"; 1514e41f4b71Sopenharmony_cilet value1: string = "roe1" 1515e41f4b71Sopenharmony_cilet key2: string = "age"; 1516e41f4b71Sopenharmony_cilet value2: number = 21 1517e41f4b71Sopenharmony_cilet key3: string = "salary"; 1518e41f4b71Sopenharmony_cilet value3: number = 20.5; 1519e41f4b71Sopenharmony_ciconst va: ValuesBucket = { 1520e41f4b71Sopenharmony_ci key1: value1, 1521e41f4b71Sopenharmony_ci key2: value2, 1522e41f4b71Sopenharmony_ci key3: value3, 1523e41f4b71Sopenharmony_ci} 1524e41f4b71Sopenharmony_citry { 1525e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1526e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).update(uri, da, va).then((data: number) => { 1527e41f4b71Sopenharmony_ci console.info("update succeed, data : " + data); 1528e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1529e41f4b71Sopenharmony_ci console.error(`update error: code: ${err.code}, message: ${err.message} `); 1530e41f4b71Sopenharmony_ci }); 1531e41f4b71Sopenharmony_ci } 1532e41f4b71Sopenharmony_ci} catch (err) { 1533e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1534e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 1535e41f4b71Sopenharmony_ci console.error(`update error: code: ${code}, message: ${message} `); 1536e41f4b71Sopenharmony_ci}; 1537e41f4b71Sopenharmony_ci``` 1538e41f4b71Sopenharmony_ci 1539e41f4b71Sopenharmony_ci### batchUpdate<sup>12+</sup> 1540e41f4b71Sopenharmony_ci 1541e41f4b71Sopenharmony_cibatchUpdate(operations: Record<string, Array<UpdateOperation>>): Promise<Record<string, Array<number>>> 1542e41f4b71Sopenharmony_ci 1543e41f4b71Sopenharmony_ciUpdates data in batches. A maximum of 900 KB data can be updated at a time. If the data volume exceeds 900 KB, the update will fail. The transaction of this API depends on the data provider. This API uses a promise to return the result. 1544e41f4b71Sopenharmony_ci 1545e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1546e41f4b71Sopenharmony_ci 1547e41f4b71Sopenharmony_ci**Parameters** 1548e41f4b71Sopenharmony_ci 1549e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1550e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | -------------------------------------- | 1551e41f4b71Sopenharmony_ci| operations | Record<string, Array<[UpdateOperation](#updateoperation12)>> | Yes | Collection of the path of the data to update, update conditions, and new data. | 1552e41f4b71Sopenharmony_ci 1553e41f4b71Sopenharmony_ci**Return value** 1554e41f4b71Sopenharmony_ci 1555e41f4b71Sopenharmony_ci| Type | Description | 1556e41f4b71Sopenharmony_ci| ----------------------------------------------------- | ------------------------------------------------------------ | 1557e41f4b71Sopenharmony_ci| Promise<Record<string, Array<number>>> | Promise used to return an array of updated data records. The value **-1** means the update operation fails.<br>The number of updated data records is not returned if the APIs of the database in use (for example, KVDB) do not support this return. | 1558e41f4b71Sopenharmony_ci 1559e41f4b71Sopenharmony_ci**Error codes** 1560e41f4b71Sopenharmony_ci 1561e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1562e41f4b71Sopenharmony_ci 1563e41f4b71Sopenharmony_ci| ID | Error Message | 1564e41f4b71Sopenharmony_ci| -------- | ------------------------------------ | 1565e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1566e41f4b71Sopenharmony_ci| 15700000 | Inner error. | 1567e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed. | 1568e41f4b71Sopenharmony_ci 1569e41f4b71Sopenharmony_ci**Example** 1570e41f4b71Sopenharmony_ci 1571e41f4b71Sopenharmony_ci```ts 1572e41f4b71Sopenharmony_ciimport { dataSharePredicates, ValuesBucket } from '@kit.ArkData' 1573e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1574e41f4b71Sopenharmony_ci 1575e41f4b71Sopenharmony_cilet record: Record<string, Array<dataShare.UpdateOperation>> = {}; 1576e41f4b71Sopenharmony_cilet operations1: Array<dataShare.UpdateOperation> = []; 1577e41f4b71Sopenharmony_cilet operations2: Array<dataShare.UpdateOperation> = []; 1578e41f4b71Sopenharmony_ci 1579e41f4b71Sopenharmony_cilet pre1: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1580e41f4b71Sopenharmony_cipre1.equalTo("name", "ZhangSan"); 1581e41f4b71Sopenharmony_cilet vb1: ValuesBucket = { 1582e41f4b71Sopenharmony_ci "name": "ZhangSan1", 1583e41f4b71Sopenharmony_ci} 1584e41f4b71Sopenharmony_cilet operation1: dataShare.UpdateOperation = { 1585e41f4b71Sopenharmony_ci values: vb1, 1586e41f4b71Sopenharmony_ci predicates: pre1 1587e41f4b71Sopenharmony_ci} 1588e41f4b71Sopenharmony_cioperations1.push(operation1); 1589e41f4b71Sopenharmony_ci 1590e41f4b71Sopenharmony_cilet pre2: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1591e41f4b71Sopenharmony_cipre2.equalTo("name", "ZhangSan2"); 1592e41f4b71Sopenharmony_cilet vb2: ValuesBucket = { 1593e41f4b71Sopenharmony_ci "name": "ZhangSan3", 1594e41f4b71Sopenharmony_ci} 1595e41f4b71Sopenharmony_cilet operation2: dataShare.UpdateOperation = { 1596e41f4b71Sopenharmony_ci values: vb2, 1597e41f4b71Sopenharmony_ci predicates: pre2 1598e41f4b71Sopenharmony_ci} 1599e41f4b71Sopenharmony_cioperations2.push(operation2); 1600e41f4b71Sopenharmony_cirecord["uri1"] = operations1; 1601e41f4b71Sopenharmony_cirecord["uri2"] = operations2; 1602e41f4b71Sopenharmony_ci 1603e41f4b71Sopenharmony_citry { 1604e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1605e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).batchUpdate(record).then((data: Record<string, Array<number>>) => { 1606e41f4b71Sopenharmony_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. 1607e41f4b71Sopenharmony_ci let a = Object.entries(data); 1608e41f4b71Sopenharmony_ci for (let i = 0; i < a.length; i++) { 1609e41f4b71Sopenharmony_ci let key = a[i][0]; 1610e41f4b71Sopenharmony_ci let values = a[i][1] 1611e41f4b71Sopenharmony_ci console.info(`Update uri:${key}`); 1612e41f4b71Sopenharmony_ci for (const value of values) { 1613e41f4b71Sopenharmony_ci console.info(`Update result:${value}`); 1614e41f4b71Sopenharmony_ci } 1615e41f4b71Sopenharmony_ci } 1616e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1617e41f4b71Sopenharmony_ci console.error(`Batch update error: code: ${err.code}, message: ${err.message} `); 1618e41f4b71Sopenharmony_ci }); 1619e41f4b71Sopenharmony_ci } 1620e41f4b71Sopenharmony_ci} catch (err) { 1621e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1622e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 1623e41f4b71Sopenharmony_ci console.error(`Batch update error: code: ${code}, message: ${message} `); 1624e41f4b71Sopenharmony_ci}; 1625e41f4b71Sopenharmony_ci``` 1626e41f4b71Sopenharmony_ci 1627e41f4b71Sopenharmony_ci### batchInsert 1628e41f4b71Sopenharmony_ci 1629e41f4b71Sopenharmony_cibatchInsert(uri: string, values: Array<ValuesBucket>, callback: AsyncCallback<number>): void 1630e41f4b71Sopenharmony_ci 1631e41f4b71Sopenharmony_ciBatch inserts data into the database. This API uses an asynchronous callback to return the result. Silent access is not supported currently. 1632e41f4b71Sopenharmony_ci 1633e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1634e41f4b71Sopenharmony_ci 1635e41f4b71Sopenharmony_ci**Parameters** 1636e41f4b71Sopenharmony_ci 1637e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1638e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1639e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to insert. | 1640e41f4b71Sopenharmony_ci| values | Array<[ValuesBucket](js-apis-data-valuesBucket.md#valuesbucket)> | Yes | Data to insert. | 1641e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the number of data records inserted. Otherwise, **err** is an error object.<br>The number of inserted data records is not returned if the APIs of the database in use (for example, KVDB) do not support this return. | 1642e41f4b71Sopenharmony_ci 1643e41f4b71Sopenharmony_ci**Error codes** 1644e41f4b71Sopenharmony_ci 1645e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1646e41f4b71Sopenharmony_ci 1647e41f4b71Sopenharmony_ci| ID | Error Message | 1648e41f4b71Sopenharmony_ci| -------- | -------------------- | 1649e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1650e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1651e41f4b71Sopenharmony_ci 1652e41f4b71Sopenharmony_ci**Example** 1653e41f4b71Sopenharmony_ci 1654e41f4b71Sopenharmony_ci```ts 1655e41f4b71Sopenharmony_ciimport { ValuesBucket } from '@kit.ArkData' 1656e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1657e41f4b71Sopenharmony_ci 1658e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1659e41f4b71Sopenharmony_cilet key1: string = "name"; 1660e41f4b71Sopenharmony_cilet value11: string = "roe11" 1661e41f4b71Sopenharmony_cilet key2: string = "age"; 1662e41f4b71Sopenharmony_cilet value21: number = 21; 1663e41f4b71Sopenharmony_cilet key3: string = "salary"; 1664e41f4b71Sopenharmony_cilet value31: number = 20.5; 1665e41f4b71Sopenharmony_cilet valuesBucket1: ValuesBucket = { 1666e41f4b71Sopenharmony_ci key1: value11, 1667e41f4b71Sopenharmony_ci key2: value21, 1668e41f4b71Sopenharmony_ci key3: value31, 1669e41f4b71Sopenharmony_ci} 1670e41f4b71Sopenharmony_cilet vbs = new Array(valuesBucket1); 1671e41f4b71Sopenharmony_citry { 1672e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1673e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).batchInsert(uri, vbs, (err, data) => { 1674e41f4b71Sopenharmony_ci if (err !== undefined) { 1675e41f4b71Sopenharmony_ci console.error(`batchInsert error: code: ${err.code}, message: ${err.message} `); 1676e41f4b71Sopenharmony_ci return; 1677e41f4b71Sopenharmony_ci } 1678e41f4b71Sopenharmony_ci console.info("batchInsert succeed, data : " + data); 1679e41f4b71Sopenharmony_ci }); 1680e41f4b71Sopenharmony_ci } 1681e41f4b71Sopenharmony_ci} catch (err) { 1682e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1683e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 1684e41f4b71Sopenharmony_ci console.error(`batchInsert error: code: ${code}, message: ${message} `); 1685e41f4b71Sopenharmony_ci}; 1686e41f4b71Sopenharmony_ci``` 1687e41f4b71Sopenharmony_ci 1688e41f4b71Sopenharmony_ci### batchInsert 1689e41f4b71Sopenharmony_ci 1690e41f4b71Sopenharmony_cibatchInsert(uri: string, values: Array<ValuesBucket>): Promise<number> 1691e41f4b71Sopenharmony_ci 1692e41f4b71Sopenharmony_ciBatch inserts data into the database. This API uses a promise to return the result. Silent access is not supported currently. 1693e41f4b71Sopenharmony_ci 1694e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1695e41f4b71Sopenharmony_ci 1696e41f4b71Sopenharmony_ci**Parameters** 1697e41f4b71Sopenharmony_ci 1698e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1699e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | ------------------------ | 1700e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data to insert. | 1701e41f4b71Sopenharmony_ci| values | Array<[ValuesBucket](js-apis-data-valuesBucket.md#valuesbucket)> | Yes | Data to insert. | 1702e41f4b71Sopenharmony_ci 1703e41f4b71Sopenharmony_ci**Return value** 1704e41f4b71Sopenharmony_ci 1705e41f4b71Sopenharmony_ci| Type | Description | 1706e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 1707e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the number of data records inserted.<br>The number of inserted data records is not returned if the APIs of the database (for example, KVDB) in use do not the return of the number of data records. | 1708e41f4b71Sopenharmony_ci 1709e41f4b71Sopenharmony_ci**Error codes** 1710e41f4b71Sopenharmony_ci 1711e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1712e41f4b71Sopenharmony_ci 1713e41f4b71Sopenharmony_ci| ID | Error Message | 1714e41f4b71Sopenharmony_ci| -------- | -------------------- | 1715e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1716e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1717e41f4b71Sopenharmony_ci 1718e41f4b71Sopenharmony_ci**Example** 1719e41f4b71Sopenharmony_ci 1720e41f4b71Sopenharmony_ci```ts 1721e41f4b71Sopenharmony_ciimport { ValuesBucket } from '@kit.ArkData' 1722e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1723e41f4b71Sopenharmony_ci 1724e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1725e41f4b71Sopenharmony_cilet key1: string = "name"; 1726e41f4b71Sopenharmony_cilet value11: string = "roe11" 1727e41f4b71Sopenharmony_cilet key2: string = "age"; 1728e41f4b71Sopenharmony_cilet value21: number = 21; 1729e41f4b71Sopenharmony_cilet key3: string = "salary"; 1730e41f4b71Sopenharmony_cilet value31: number = 20.5; 1731e41f4b71Sopenharmony_cilet valuesBucket1: ValuesBucket = { 1732e41f4b71Sopenharmony_ci key1: value11, 1733e41f4b71Sopenharmony_ci key2: value21, 1734e41f4b71Sopenharmony_ci key3: value31, 1735e41f4b71Sopenharmony_ci} 1736e41f4b71Sopenharmony_cilet vbs = new Array(valuesBucket1); 1737e41f4b71Sopenharmony_citry { 1738e41f4b71Sopenharmony_ci if (dataShareHelper != undefined) { 1739e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).batchInsert(uri, vbs).then((data: number) => { 1740e41f4b71Sopenharmony_ci console.info("batchInsert succeed, data : " + data); 1741e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1742e41f4b71Sopenharmony_ci console.error(`batchInsert error: code: ${err.code}, message: ${err.message} `); 1743e41f4b71Sopenharmony_ci }); 1744e41f4b71Sopenharmony_ci } 1745e41f4b71Sopenharmony_ci} catch (err) { 1746e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 1747e41f4b71Sopenharmony_ci let message = (err as BusinessError).message 1748e41f4b71Sopenharmony_ci console.error(`batchInsert error: code: ${code}, message: ${message} `); 1749e41f4b71Sopenharmony_ci}; 1750e41f4b71Sopenharmony_ci``` 1751e41f4b71Sopenharmony_ci 1752e41f4b71Sopenharmony_ci### close<sup>12+</sup> 1753e41f4b71Sopenharmony_ci 1754e41f4b71Sopenharmony_ciclose(): Promise <void> 1755e41f4b71Sopenharmony_ci 1756e41f4b71Sopenharmony_ciCloses the **DataShareHelper** instance. After this API is called, the instance becomes invalid. This API uses a promise to return the result. 1757e41f4b71Sopenharmony_ci 1758e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1759e41f4b71Sopenharmony_ci 1760e41f4b71Sopenharmony_ci**Return value** 1761e41f4b71Sopenharmony_ci 1762e41f4b71Sopenharmony_ci| Type | Description | 1763e41f4b71Sopenharmony_ci| ------------------- | -------------------------------------- | 1764e41f4b71Sopenharmony_ci| Promise<void> | returns no value. | 1765e41f4b71Sopenharmony_ci 1766e41f4b71Sopenharmony_ci**Error codes** 1767e41f4b71Sopenharmony_ci 1768e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md). 1769e41f4b71Sopenharmony_ci 1770e41f4b71Sopenharmony_ci| ID | Error Message | 1771e41f4b71Sopenharmony_ci| -------- | ------------ | 1772e41f4b71Sopenharmony_ci| 15700000 | Inner error. | 1773e41f4b71Sopenharmony_ci 1774e41f4b71Sopenharmony_ci**Example** 1775e41f4b71Sopenharmony_ci 1776e41f4b71Sopenharmony_ci```ts 1777e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 1778e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).close(); 1779e41f4b71Sopenharmony_ci} 1780e41f4b71Sopenharmony_ci``` 1781e41f4b71Sopenharmony_ci 1782e41f4b71Sopenharmony_ci### normalizeUri 1783e41f4b71Sopenharmony_ci 1784e41f4b71Sopenharmony_cinormalizeUri(uri: string, callback: AsyncCallback<string>): void 1785e41f4b71Sopenharmony_ci 1786e41f4b71Sopenharmony_ciNormalizes a **DataShare** URI. The **DataShare** URI can be used only by the local device, but the normalized URI can be used across devices. This API uses an asynchronous callback to return the result. Silent access is not supported currently. 1787e41f4b71Sopenharmony_ci 1788e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1789e41f4b71Sopenharmony_ci 1790e41f4b71Sopenharmony_ci**Parameters** 1791e41f4b71Sopenharmony_ci 1792e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1793e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | -------------------------------------------------------- | 1794e41f4b71Sopenharmony_ci| uri | string | Yes | [URI](../apis-arkts/js-apis-uri.md#uri) to normalize. | 1795e41f4b71Sopenharmony_ci| callback | AsyncCallback<string> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the normalized URI (if **null** is returned, URI normalization is not supported). Otherwise, **err** is an error object. | 1796e41f4b71Sopenharmony_ci 1797e41f4b71Sopenharmony_ci**Error codes** 1798e41f4b71Sopenharmony_ci 1799e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1800e41f4b71Sopenharmony_ci 1801e41f4b71Sopenharmony_ci| ID | Error Message | 1802e41f4b71Sopenharmony_ci| -------- | -------------------- | 1803e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1804e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1805e41f4b71Sopenharmony_ci 1806e41f4b71Sopenharmony_ci**Example** 1807e41f4b71Sopenharmony_ci 1808e41f4b71Sopenharmony_ci```ts 1809e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1810e41f4b71Sopenharmony_ci 1811e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1812e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 1813e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).normalizeUri(uri, (err: BusinessError, data: string) => { 1814e41f4b71Sopenharmony_ci if (err !== undefined) { 1815e41f4b71Sopenharmony_ci console.info("normalizeUri failed, error message : " + err); 1816e41f4b71Sopenharmony_ci } else { 1817e41f4b71Sopenharmony_ci console.info("normalizeUri = " + data); 1818e41f4b71Sopenharmony_ci } 1819e41f4b71Sopenharmony_ci }); 1820e41f4b71Sopenharmony_ci} 1821e41f4b71Sopenharmony_ci``` 1822e41f4b71Sopenharmony_ci 1823e41f4b71Sopenharmony_ci### normalizeUri 1824e41f4b71Sopenharmony_ci 1825e41f4b71Sopenharmony_cinormalizeUri(uri: string): Promise<string> 1826e41f4b71Sopenharmony_ci 1827e41f4b71Sopenharmony_ciNormalizes a **DataShare** URI. The **DataShare** URI can be used only by the local device, but the normalized URI can be used across devices. This API uses a promise to return the result. Silent access is not supported currently. 1828e41f4b71Sopenharmony_ci 1829e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1830e41f4b71Sopenharmony_ci 1831e41f4b71Sopenharmony_ci**Parameters** 1832e41f4b71Sopenharmony_ci 1833e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1834e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ----------------------------------------- | 1835e41f4b71Sopenharmony_ci| uri | string | Yes | [URI](../apis-arkts/js-apis-uri.md#uri) to normalize. | 1836e41f4b71Sopenharmony_ci 1837e41f4b71Sopenharmony_ci**Return value** 1838e41f4b71Sopenharmony_ci 1839e41f4b71Sopenharmony_ci| Type | Description | 1840e41f4b71Sopenharmony_ci| ---------------- | ---------------------------------------------- | 1841e41f4b71Sopenharmony_ci| Promise<string> | Promise used to return the result. If URI normalization is supported, the normalized URI is returned. Otherwise, **null** is returned. | 1842e41f4b71Sopenharmony_ci 1843e41f4b71Sopenharmony_ci**Error codes** 1844e41f4b71Sopenharmony_ci 1845e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1846e41f4b71Sopenharmony_ci 1847e41f4b71Sopenharmony_ci| ID | Error Message | 1848e41f4b71Sopenharmony_ci| -------- | -------------------- | 1849e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1850e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1851e41f4b71Sopenharmony_ci 1852e41f4b71Sopenharmony_ci**Example** 1853e41f4b71Sopenharmony_ci 1854e41f4b71Sopenharmony_ci```ts 1855e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1856e41f4b71Sopenharmony_ci 1857e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1858e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 1859e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).normalizeUri(uri).then((data: string) => { 1860e41f4b71Sopenharmony_ci console.info("normalizeUri = " + data); 1861e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1862e41f4b71Sopenharmony_ci console.info("normalizeUri failed, error message : " + err); 1863e41f4b71Sopenharmony_ci }); 1864e41f4b71Sopenharmony_ci} 1865e41f4b71Sopenharmony_ci``` 1866e41f4b71Sopenharmony_ci 1867e41f4b71Sopenharmony_ci### denormalizeUri 1868e41f4b71Sopenharmony_ci 1869e41f4b71Sopenharmony_cidenormalizeUri(uri: string, callback: AsyncCallback<string>): void 1870e41f4b71Sopenharmony_ci 1871e41f4b71Sopenharmony_ciDenormalizes a URI. This API uses an asynchronous callback to return the result. Silent access is not supported currently. 1872e41f4b71Sopenharmony_ci 1873e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1874e41f4b71Sopenharmony_ci 1875e41f4b71Sopenharmony_ci**Parameters** 1876e41f4b71Sopenharmony_ci 1877e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1878e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | --------------------------------------------------- | 1879e41f4b71Sopenharmony_ci| uri | string | Yes | [URI](../apis-arkts/js-apis-uri.md#uri) to denormalize. | 1880e41f4b71Sopenharmony_ci| callback | AsyncCallback<string> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the URI obtained. If the original URI is returned, denormalization is not required. If **null** is returned, denormalization is not supported. If the operation fails, **err** is an error object. | 1881e41f4b71Sopenharmony_ci 1882e41f4b71Sopenharmony_ci**Error codes** 1883e41f4b71Sopenharmony_ci 1884e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1885e41f4b71Sopenharmony_ci 1886e41f4b71Sopenharmony_ci| ID | Error Message | 1887e41f4b71Sopenharmony_ci| -------- | -------------------- | 1888e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1889e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1890e41f4b71Sopenharmony_ci 1891e41f4b71Sopenharmony_ci**Example** 1892e41f4b71Sopenharmony_ci 1893e41f4b71Sopenharmony_ci```ts 1894e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1895e41f4b71Sopenharmony_ci 1896e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1897e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 1898e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).denormalizeUri(uri, (err: BusinessError, data: string) => { 1899e41f4b71Sopenharmony_ci if (err !== undefined) { 1900e41f4b71Sopenharmony_ci console.error("denormalizeUri failed, error message : " + err); 1901e41f4b71Sopenharmony_ci } else { 1902e41f4b71Sopenharmony_ci console.info("denormalizeUri = " + data); 1903e41f4b71Sopenharmony_ci } 1904e41f4b71Sopenharmony_ci }); 1905e41f4b71Sopenharmony_ci} 1906e41f4b71Sopenharmony_ci``` 1907e41f4b71Sopenharmony_ci 1908e41f4b71Sopenharmony_ci### denormalizeUri 1909e41f4b71Sopenharmony_ci 1910e41f4b71Sopenharmony_cidenormalizeUri(uri: string): Promise<string> 1911e41f4b71Sopenharmony_ci 1912e41f4b71Sopenharmony_ciDenormalizes a URI. This API uses a promise to return the result. Silent access is not supported currently. 1913e41f4b71Sopenharmony_ci 1914e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1915e41f4b71Sopenharmony_ci 1916e41f4b71Sopenharmony_ci**Parameters** 1917e41f4b71Sopenharmony_ci 1918e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1919e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ------------------------------------------- | 1920e41f4b71Sopenharmony_ci| uri | string | Yes | [URI](../apis-arkts/js-apis-uri.md#uri) to denormalize. | 1921e41f4b71Sopenharmony_ci 1922e41f4b71Sopenharmony_ci**Return value** 1923e41f4b71Sopenharmony_ci 1924e41f4b71Sopenharmony_ci| Type | Description | 1925e41f4b71Sopenharmony_ci| ---------------- | ----------------------------------------- | 1926e41f4b71Sopenharmony_ci| Promise<string> | Promise used to return the result. If the denormalization is successful, the URI obtained is returned. If no operation is required, the original URI is returned. If denormalization is not supported, **null** is returned. | 1927e41f4b71Sopenharmony_ci 1928e41f4b71Sopenharmony_ci**Error codes** 1929e41f4b71Sopenharmony_ci 1930e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1931e41f4b71Sopenharmony_ci 1932e41f4b71Sopenharmony_ci| ID | Error Message | 1933e41f4b71Sopenharmony_ci| -------- | -------------------- | 1934e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 1935e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1936e41f4b71Sopenharmony_ci 1937e41f4b71Sopenharmony_ci**Example** 1938e41f4b71Sopenharmony_ci 1939e41f4b71Sopenharmony_ci```ts 1940e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit' 1941e41f4b71Sopenharmony_ci 1942e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1943e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 1944e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).denormalizeUri(uri).then((data: string) => { 1945e41f4b71Sopenharmony_ci console.info("denormalizeUri = " + data); 1946e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1947e41f4b71Sopenharmony_ci console.error("denormalizeUri failed, error message : " + err); 1948e41f4b71Sopenharmony_ci }); 1949e41f4b71Sopenharmony_ci} 1950e41f4b71Sopenharmony_ci``` 1951e41f4b71Sopenharmony_ci 1952e41f4b71Sopenharmony_ci### notifyChange 1953e41f4b71Sopenharmony_ci 1954e41f4b71Sopenharmony_cinotifyChange(uri: string, callback: AsyncCallback<void>): void 1955e41f4b71Sopenharmony_ci 1956e41f4b71Sopenharmony_ciNotifies the registered observer of data changes. This API uses an asynchronous callback to return the result. Silent access is not supported currently. 1957e41f4b71Sopenharmony_ci 1958e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1959e41f4b71Sopenharmony_ci 1960e41f4b71Sopenharmony_ci**Parameters** 1961e41f4b71Sopenharmony_ci 1962e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1963e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ------------------------ | 1964e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data.| 1965e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the observer is notified of the data changes, **err** is **undefined**. Otherwise, **err** is an error object. | 1966e41f4b71Sopenharmony_ci 1967e41f4b71Sopenharmony_ci**Error codes** 1968e41f4b71Sopenharmony_ci 1969e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 1970e41f4b71Sopenharmony_ci 1971e41f4b71Sopenharmony_ci| ID | Error Message | 1972e41f4b71Sopenharmony_ci| -------- | -------------------- | 1973e41f4b71Sopenharmony_ci| 401 | Parameter error.Mandatory parameters are left unspecified.| 1974e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 1975e41f4b71Sopenharmony_ci 1976e41f4b71Sopenharmony_ci**Example** 1977e41f4b71Sopenharmony_ci 1978e41f4b71Sopenharmony_ci```ts 1979e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 1980e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 1981e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).notifyChange(uri, () => { 1982e41f4b71Sopenharmony_ci console.info("***** notifyChange *****"); 1983e41f4b71Sopenharmony_ci }); 1984e41f4b71Sopenharmony_ci} 1985e41f4b71Sopenharmony_ci``` 1986e41f4b71Sopenharmony_ci 1987e41f4b71Sopenharmony_ci### notifyChange 1988e41f4b71Sopenharmony_ci 1989e41f4b71Sopenharmony_cinotifyChange(uri: string): Promise<void> 1990e41f4b71Sopenharmony_ci 1991e41f4b71Sopenharmony_ciNotifies the registered observer of data changes. This API uses a promise to return the result. Silent access is not supported currently. 1992e41f4b71Sopenharmony_ci 1993e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 1994e41f4b71Sopenharmony_ci 1995e41f4b71Sopenharmony_ci**Parameters** 1996e41f4b71Sopenharmony_ci 1997e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 1998e41f4b71Sopenharmony_ci| ---- | ------ | ---- | -------------------- | 1999e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the data. | 2000e41f4b71Sopenharmony_ci 2001e41f4b71Sopenharmony_ci**Return value** 2002e41f4b71Sopenharmony_ci 2003e41f4b71Sopenharmony_ci| Type | Description | 2004e41f4b71Sopenharmony_ci| -------------- | --------------------- | 2005e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value. | 2006e41f4b71Sopenharmony_ci 2007e41f4b71Sopenharmony_ci**Error codes** 2008e41f4b71Sopenharmony_ci 2009e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 2010e41f4b71Sopenharmony_ci 2011e41f4b71Sopenharmony_ci| ID | Error Message | 2012e41f4b71Sopenharmony_ci| -------- | -------------------- | 2013e41f4b71Sopenharmony_ci| 401 | Parameter error.Mandatory parameters are left unspecified.| 2014e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 2015e41f4b71Sopenharmony_ci 2016e41f4b71Sopenharmony_ci**Example** 2017e41f4b71Sopenharmony_ci 2018e41f4b71Sopenharmony_ci```ts 2019e41f4b71Sopenharmony_cilet uri = ("datashare:///com.samples.datasharetest.DataShare"); 2020e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 2021e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).notifyChange(uri); 2022e41f4b71Sopenharmony_ci} 2023e41f4b71Sopenharmony_ci``` 2024e41f4b71Sopenharmony_ci 2025e41f4b71Sopenharmony_ci### notifyChange<sup>12+</sup> 2026e41f4b71Sopenharmony_ci 2027e41f4b71Sopenharmony_cinotifyChange(data: ChangeInfo): Promise<void> 2028e41f4b71Sopenharmony_ci 2029e41f4b71Sopenharmony_ciNotifies the observer of the data change of the specified URI. This API uses a promise to return the result. Silent access is not supported. 2030e41f4b71Sopenharmony_ci 2031e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Consumer 2032e41f4b71Sopenharmony_ci 2033e41f4b71Sopenharmony_ci**Parameters** 2034e41f4b71Sopenharmony_ci 2035e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 2036e41f4b71Sopenharmony_ci| ---- | ------ | ---- | -------------------- | 2037e41f4b71Sopenharmony_ci| data | [ChangeInfo](#changeinfo12) | Yes | Information about the data change type, URI of the data changed, and changed data. | 2038e41f4b71Sopenharmony_ci 2039e41f4b71Sopenharmony_ci**Return value** 2040e41f4b71Sopenharmony_ci 2041e41f4b71Sopenharmony_ci| Type | Description | 2042e41f4b71Sopenharmony_ci| -------------- | --------------------- | 2043e41f4b71Sopenharmony_ci| Promise<void> | returns no value. | 2044e41f4b71Sopenharmony_ci 2045e41f4b71Sopenharmony_ci**Error codes** 2046e41f4b71Sopenharmony_ci 2047e41f4b71Sopenharmony_ciFor details about the error codes, see [DataShare Error Codes](errorcode-datashare.md) and [Universal Error Codes](../errorcode-universal.md). 2048e41f4b71Sopenharmony_ci 2049e41f4b71Sopenharmony_ci| ID | Error Message | 2050e41f4b71Sopenharmony_ci| -------- | -------------------- | 2051e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.| 2052e41f4b71Sopenharmony_ci| 15700013 | The DataShareHelper instance is already closed.| 2053e41f4b71Sopenharmony_ci 2054e41f4b71Sopenharmony_ci**Example** 2055e41f4b71Sopenharmony_ci 2056e41f4b71Sopenharmony_ci```ts 2057e41f4b71Sopenharmony_ciimport { ValuesBucket } from '@kit.ArkData' 2058e41f4b71Sopenharmony_ci 2059e41f4b71Sopenharmony_cilet dsUri = ("datashare:///com.acts.datasharetest"); 2060e41f4b71Sopenharmony_cilet bucket1: ValuesBucket = {"name": "LiSi"}; 2061e41f4b71Sopenharmony_cilet bucket2: ValuesBucket = {"name": "WangWu"}; 2062e41f4b71Sopenharmony_cilet bucket3: ValuesBucket = {"name": "ZhaoLiu"}; 2063e41f4b71Sopenharmony_cilet people: Array<ValuesBucket> = new Array(bucket1, bucket2, bucket3); 2064e41f4b71Sopenharmony_cilet changeData:dataShare.ChangeInfo= { type:dataShare.ChangeType.INSERT, uri:dsUri, values:people}; 2065e41f4b71Sopenharmony_ciif (dataShareHelper != undefined) { 2066e41f4b71Sopenharmony_ci (dataShareHelper as dataShare.DataShareHelper).notifyChange(changeData); 2067e41f4b71Sopenharmony_ci} 2068e41f4b71Sopenharmony_ci``` 2069