1e41f4b71Sopenharmony_ci# @ohos.data.distributedKVStore (Distributed KV Store) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **distributedKVStore** module implements collaboration between databases for different devices that forms a Super Device. You can use the APIs provided by this module to save application data to a distributed key-value (KV) store and perform operations, such as adding, deleting, modifying, querying, and synchronizing data in distributed KV stores. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThe **distributedKVStore** module provides the following functionalities: 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci- [KVManager](#kvmanager): provides a **KVManager** instance to obtain KV store information. 8e41f4b71Sopenharmony_ci- [KVStoreResultSet](#kvstoreresultset): provides APIs for accessing the results obtained from a KV store. 9e41f4b71Sopenharmony_ci- [Query](#query): provides APIs for setting predicates for data query. 10e41f4b71Sopenharmony_ci- [SingleKVStore](#singlekvstore): provides APIs for querying and synchronizing data in single KV stores. The single KV stores manage data without distinguishing devices. 11e41f4b71Sopenharmony_ci- [DeviceKVStore](#devicekvstore): provides APIs for querying and synchronizing data in device KV stores. This class inherits from [SingleKVStore](#singlekvstore). The device KV stores manage data by device. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci> **NOTE** 14e41f4b71Sopenharmony_ci> 15e41f4b71Sopenharmony_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. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Modules to Import 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci```ts 20e41f4b71Sopenharmony_ciimport { distributedKVStore } from '@kit.ArkData'; 21e41f4b71Sopenharmony_ci``` 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## KVManagerConfig 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciProvides the **KVManager** instance configuration, including the bundle name of the invoker and the application context. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 30e41f4b71Sopenharmony_ci| ---------- | --------------------- | ---- | ------------------------------------------------------------ | 31e41f4b71Sopenharmony_ci| context | BaseContext | Yes |Application context.<br>For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).<br>Since API version 10, the parameter type of context is [BaseContext](../apis-ability-kit/js-apis-inner-application-baseContext.md).| 32e41f4b71Sopenharmony_ci| bundleName | string | Yes | Bundle name. | 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci## Constants 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ciProvides constants of the distributed KV store. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci| Name | Value | Description | 41e41f4b71Sopenharmony_ci| --------------------- | ------- | --------------------------------------- | 42e41f4b71Sopenharmony_ci| MAX_KEY_LENGTH | 1024 | Maximum length of a key in a distributed KV store, in bytes. | 43e41f4b71Sopenharmony_ci| MAX_VALUE_LENGTH | 4194303 | Maximum length of a value in a distributed KV store, in bytes.| 44e41f4b71Sopenharmony_ci| MAX_KEY_LENGTH_DEVICE | 896 | Maximum length of a key in a device KV store, in bytes.| 45e41f4b71Sopenharmony_ci| MAX_STORE_ID_LENGTH | 128 | Maximum length of a KV store ID, in bytes. | 46e41f4b71Sopenharmony_ci| MAX_QUERY_LENGTH | 512000 | Maximum query length, in bytes. | 47e41f4b71Sopenharmony_ci| MAX_BATCH_SIZE | 128 | Maximum number of batch operations. | 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci## ValueType 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ciEnumerates the types of the value in a KV pair. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci| Name | Description | 56e41f4b71Sopenharmony_ci| ---------- | ---------------------- | 57e41f4b71Sopenharmony_ci| STRING | String. | 58e41f4b71Sopenharmony_ci| INTEGER | Integer. | 59e41f4b71Sopenharmony_ci| FLOAT | Float (single-precision floating point). | 60e41f4b71Sopenharmony_ci| BYTE_ARRAY | Byte array.| 61e41f4b71Sopenharmony_ci| BOOLEAN | Boolean. | 62e41f4b71Sopenharmony_ci| DOUBLE | Double (double-precision floating point).| 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci## Value 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ciDefines the **value** object in a KV store. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci| Name | Type |Mandatory | Description | 71e41f4b71Sopenharmony_ci| ----- | ------- |-----|------------------------ | 72e41f4b71Sopenharmony_ci| type | [ValueType](#valuetype) | Yes|Type of the value. | 73e41f4b71Sopenharmony_ci| value | Uint8Array \| string \| number \| boolean| Yes|Value of the KV pair. | 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci## Entry 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ciDefines the KV pairs in a KV store. 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 82e41f4b71Sopenharmony_ci| ----- | --------------- | ---- | -------- | 83e41f4b71Sopenharmony_ci| key | string | Yes | Key of the KV pair. | 84e41f4b71Sopenharmony_ci| value | [Value](#value) | Yes | Value object of the KV pair.| 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci## ChangeNotification 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ciDefines the content of a data change notification, including inserted data, updated data, deleted data, and device ID. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 93e41f4b71Sopenharmony_ci| ------------- | ----------------- | ---- | ------------------------ | 94e41f4b71Sopenharmony_ci| insertEntries | [Entry](#entry)[] | Yes | Data inserted. | 95e41f4b71Sopenharmony_ci| updateEntries | [Entry](#entry)[] | Yes | Data updated. | 96e41f4b71Sopenharmony_ci| deleteEntries | [Entry](#entry)[] | Yes | Data deleted. | 97e41f4b71Sopenharmony_ci| deviceId | string | Yes | UUID of the device.| 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci## SyncMode 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ciEnumerates the sync modes. 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci| Name | Description | 106e41f4b71Sopenharmony_ci| --------- | ---------------------------------------------------- | 107e41f4b71Sopenharmony_ci| PULL_ONLY | Pull data from the peer end to the local end only. | 108e41f4b71Sopenharmony_ci| PUSH_ONLY | Push data from the local end to the peer end only. | 109e41f4b71Sopenharmony_ci| PUSH_PULL | Push data from the local end to the peer end and then pull data from the peer end to the local end.| 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci## SubscribeType 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ciEnumerates the subscription types. 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci| Name | Description | 118e41f4b71Sopenharmony_ci| --------------------- | ---------------------------- | 119e41f4b71Sopenharmony_ci| SUBSCRIBE_TYPE_LOCAL | Local data changes. | 120e41f4b71Sopenharmony_ci| SUBSCRIBE_TYPE_REMOTE | Remote data changes. | 121e41f4b71Sopenharmony_ci| SUBSCRIBE_TYPE_ALL | Local and remote data changes.| 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci## KVStoreType 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ciEnumerates the distributed KV store types. 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci| Name | Description | 128e41f4b71Sopenharmony_ci| -------------------- | ------------------------------------------------------------ | 129e41f4b71Sopenharmony_ci| DEVICE_COLLABORATION | Device KV store.<br> The device KV store manages data by device, which eliminates conflicts. Data can be queried by device.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore| 130e41f4b71Sopenharmony_ci| SINGLE_VERSION | Single KV store.<br> The single KV store does not differentiate data by device. If entries with the same key are modified on different devices, the value will be overwritten.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core| 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci## SecurityLevel 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ciEnumerates the KV store security levels. 135e41f4b71Sopenharmony_ci> **NOTE** 136e41f4b71Sopenharmony_ci> 137e41f4b71Sopenharmony_ci> For the scenarios involving a single device, you can upgrade the security level of a KV store by modifying the **securityLevel** parameter. When upgrading the database security level, observe the following: 138e41f4b71Sopenharmony_ci> * This operation does not apply to the databases that require cross-device sync. Data cannot be synced between databases of different security levels. If you want to upgrade the security level of a database that requires cross-device sync, you are advised to create a database of a higher security level. 139e41f4b71Sopenharmony_ci> * You need to close the database before modifying the **securityLevel** parameter, and open it after the security level is upgraded. 140e41f4b71Sopenharmony_ci> * You cannot downgrade the database security level. For example, you can change the database security level from S2 to S3, but cannot change it from S3 to S2. 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci| Name | Description | 145e41f4b71Sopenharmony_ci| -------: | ------------------------------------------------------------ | 146e41f4b71Sopenharmony_ci| S1 | Low security level. Disclosure, tampering, corruption, or loss of the data may cause minor impact on an individual or group.<br>Examples: gender and nationality information, and user application records| 147e41f4b71Sopenharmony_ci| S2 | Medium security level. Disclosure, tampering, corruption, or loss of the data may cause major impact on an individual or group.<br>Examples: individual mailing addresses and nicknames| 148e41f4b71Sopenharmony_ci| S3 | High security level. Disclosure, tampering, corruption, or loss of the data may cause critical impact on an individual or group.<br>Examples: real-time precise positioning information and movement trajectory | 149e41f4b71Sopenharmony_ci| S4 | Critical security level. Disclosure, tampering, corruption, or loss of the data may cause significant adverse impact on an individual or group.<br>Examples: political opinions, religious and philosophical belief, trade union membership, genetic data, biological information, health and sexual life status, sexual orientation, device authentication, and personal credit card information| 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci## Options 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ciProvides KV store configuration. 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 156e41f4b71Sopenharmony_ci| --------------- | -------------- | ---- | -------------------------| 157e41f4b71Sopenharmony_ci| createIfMissing | boolean | No | Whether to create a KV store if the database file does not exist. The default value is **true**, which means to create a KV store.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core| 158e41f4b71Sopenharmony_ci| encrypt | boolean | No | Whether to encrypt the KV store. The default value is **false**, which means the KV store is not encrypted.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core| 159e41f4b71Sopenharmony_ci| backup | boolean | No | Whether to back up the KV store. The default value is **true**, which means to back up the KV store.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core| 160e41f4b71Sopenharmony_ci| autoSync | boolean | No | Whether to enable auto sync across devices. The default value is **false**, indicating that only manual sync is supported. If this parameter is set to **true**, <!--RP1-->it takes effect only in [device collaboration using cross-device calls](../../application-models/hop-multi-device-collaboration.md#using-cross-device-call).<!--RP1End--><br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core<br>**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC| 161e41f4b71Sopenharmony_ci| kvStoreType | [KVStoreType](#kvstoretype) | No | Type of the KV store to create. The default value is **DEVICE_COLLABORATION**, which indicates a device KV store.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core| 162e41f4b71Sopenharmony_ci| securityLevel | [SecurityLevel](#securitylevel) | Yes | Security level of the KV store.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core| 163e41f4b71Sopenharmony_ci| schema | [Schema](#schema) | No | Schema that defines the values stored in the KV store. The default value is **undefined**, which means no schema is used.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore| 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci## Schema 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ciDefines the schema of a KV store. You can create a **Schema** object and pass it in [Options](#options) when creating or opening a KV store. 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 172e41f4b71Sopenharmony_ci| ------- | ----------------------- | ---- | ---- | -------------------------- | 173e41f4b71Sopenharmony_ci| root | [FieldNode](#fieldnode) | Yes | Yes | Definitions of all the fields in **Value**.| 174e41f4b71Sopenharmony_ci| indexes | Array\<string> | Yes | Yes | Indexes of the fields in **Value**. Indexes are created only for **FieldNode** with this parameter specified. If no index needs to be created, this parameter can be left empty. <br>Format: `'$.field1'`, `'$.field2'`| 175e41f4b71Sopenharmony_ci| mode | number | Yes | Yes | Schema mode, which can be **0** (compatible mode) or **1** (strict mode).| 176e41f4b71Sopenharmony_ci| skip | number | Yes | Yes | Number of bytes that can be skipped during the value check. The value range is [0, 4M-2].| 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ciStrict mode: In this mode, the format of the value to be inserted must strictly match the schema defined, and the number of fields cannot be more or less than that defined in the schema. Otherwise, an error will be returned. 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ciCompatible mode: In this mode, the value check is successful as long as the value has the characteristics defined in the schema. Extra fields are allowed. For example, if **id** and **name** are defined, more fields such as **id**, **name**, and **age** can be inserted. 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci### constructor 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ciconstructor() 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ciA constructor used to create a **Schema** instance. 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci**Example** 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci```ts 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_cilet child1 = new distributedKVStore.FieldNode('id'); 195e41f4b71Sopenharmony_cichild1.type = distributedKVStore.ValueType.INTEGER; 196e41f4b71Sopenharmony_cichild1.nullable = false; 197e41f4b71Sopenharmony_cichild1.default = '1'; 198e41f4b71Sopenharmony_cilet child2 = new distributedKVStore.FieldNode('name'); 199e41f4b71Sopenharmony_cichild2.type = distributedKVStore.ValueType.STRING; 200e41f4b71Sopenharmony_cichild2.nullable = false; 201e41f4b71Sopenharmony_cichild2.default = 'zhangsan'; 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_cilet schema = new distributedKVStore.Schema(); 204e41f4b71Sopenharmony_cischema.root.appendChild(child1); 205e41f4b71Sopenharmony_cischema.root.appendChild(child2); 206e41f4b71Sopenharmony_cischema.indexes = ['$.id', '$.name']; 207e41f4b71Sopenharmony_cischema.mode = 1; 208e41f4b71Sopenharmony_cischema.skip = 0; 209e41f4b71Sopenharmony_ci``` 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci## FieldNode 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ciRepresents a **Schema** instance, which provides the methods for defining the values stored in a KV store. 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 218e41f4b71Sopenharmony_ci| -------- | ------- | ---- | ---- | ------------------------------ | 219e41f4b71Sopenharmony_ci| nullable | boolean | Yes | Yes | Whether the field can be null. The value **true** means the node field can be null; the value **false** means the opposite.| 220e41f4b71Sopenharmony_ci| default | string | Yes | Yes | Default value of **FieldNode**. | 221e41f4b71Sopenharmony_ci| type | number | Yes | Yes | **FieldNode** data type, which is a value of [ValueType](#valuetype). Currently, the BYTE_ARRAY type is not supported. Using this type may cause a failure in calling [getKVStore](#getkvstore).| 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci### constructor 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ciconstructor(name: string) 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ciA constructor used to create a **FieldNode** instance with a string field. 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci**Parameters** 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 234e41f4b71Sopenharmony_ci| ------ | -------- | ---- | --------------- | 235e41f4b71Sopenharmony_ci| name | string | Yes | Value of **FieldNode**, which cannot be left empty.| 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci**Error codes** 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci| ID| **Error Message** | 242e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 243e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed. | 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci### appendChild 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ciappendChild(child: FieldNode): boolean 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ciAppends a child node to this **FieldNode**. 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci**Parameters** 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 256e41f4b71Sopenharmony_ci| ------ | ----------------------- | ---- | ---------------- | 257e41f4b71Sopenharmony_ci| child | [FieldNode](#fieldnode) | Yes | Child node to append.| 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci**Return value** 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci| Type | Description | 262e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | 263e41f4b71Sopenharmony_ci| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci**Error codes** 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci| ID| **Error Message** | 270e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 271e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci**Example** 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci```ts 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_citry { 278e41f4b71Sopenharmony_ci let node: distributedKVStore.FieldNode | null = new distributedKVStore.FieldNode("root"); 279e41f4b71Sopenharmony_ci let child1: distributedKVStore.FieldNode | null = new distributedKVStore.FieldNode("child1"); 280e41f4b71Sopenharmony_ci let child2: distributedKVStore.FieldNode | null = new distributedKVStore.FieldNode("child2"); 281e41f4b71Sopenharmony_ci let child3: distributedKVStore.FieldNode | null = new distributedKVStore.FieldNode("child3"); 282e41f4b71Sopenharmony_ci node.appendChild(child1); 283e41f4b71Sopenharmony_ci node.appendChild(child2); 284e41f4b71Sopenharmony_ci node.appendChild(child3); 285e41f4b71Sopenharmony_ci console.info("appendNode " + JSON.stringify(node)); 286e41f4b71Sopenharmony_ci child1 = null; 287e41f4b71Sopenharmony_ci child2 = null; 288e41f4b71Sopenharmony_ci child3 = null; 289e41f4b71Sopenharmony_ci node = null; 290e41f4b71Sopenharmony_ci} catch (e) { 291e41f4b71Sopenharmony_ci console.error("AppendChild " + e); 292e41f4b71Sopenharmony_ci} 293e41f4b71Sopenharmony_ci``` 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci## distributedKVStore.createKVManager 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_cicreateKVManager(config: KVManagerConfig): KVManager 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ciCreates a **KVManager** instance for KV store management. 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci**Parameters** 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 306e41f4b71Sopenharmony_ci| ------ | ----------------------------- | ---- | --------------------------------------------------------- | 307e41f4b71Sopenharmony_ci| config | [KVManagerConfig](#kvmanagerconfig) | Yes | **KVManager** instance Configuration, including the bundle name (cannot be empty) and user information of the caller. | 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci**Return value** 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci| Type | Description | 312e41f4b71Sopenharmony_ci| -------------------------------------- | ------------------------------------------ | 313e41f4b71Sopenharmony_ci| [KVManager](#kvmanager) | **KVManager** instance created.| 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci**Error codes** 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ci| ID| **Error Message** | 320e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 321e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci**Example** 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ciStage model: 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci```ts 328e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 329e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_cilet kvManager: distributedKVStore.KVManager; 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 334e41f4b71Sopenharmony_ci onCreate() { 335e41f4b71Sopenharmony_ci console.info("MyAbilityStage onCreate") 336e41f4b71Sopenharmony_ci let context = this.context 337e41f4b71Sopenharmony_ci const kvManagerConfig: distributedKVStore.KVManagerConfig = { 338e41f4b71Sopenharmony_ci context: context, 339e41f4b71Sopenharmony_ci bundleName: 'com.example.datamanagertest', 340e41f4b71Sopenharmony_ci } 341e41f4b71Sopenharmony_ci try { 342e41f4b71Sopenharmony_ci kvManager = distributedKVStore.createKVManager(kvManagerConfig); 343e41f4b71Sopenharmony_ci console.info("Succeeded in creating KVManager"); 344e41f4b71Sopenharmony_ci } catch (e) { 345e41f4b71Sopenharmony_ci let error = e as BusinessError; 346e41f4b71Sopenharmony_ci console.error(`Failed to create KVManager.code is ${error.code},message is ${error.message}`); 347e41f4b71Sopenharmony_ci } 348e41f4b71Sopenharmony_ci if (kvManager !== undefined) { 349e41f4b71Sopenharmony_ci kvManager = kvManager as distributedKVStore.KVManager; 350e41f4b71Sopenharmony_ci // Perform subsequent operations such as creating a KV store. 351e41f4b71Sopenharmony_ci // ... 352e41f4b71Sopenharmony_ci } 353e41f4b71Sopenharmony_ci } 354e41f4b71Sopenharmony_ci} 355e41f4b71Sopenharmony_ci``` 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ciFA model: 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci<!--code_no_check_fa--> 360e41f4b71Sopenharmony_ci```ts 361e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 362e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_cilet kvManager: distributedKVStore.KVManager; 365e41f4b71Sopenharmony_cilet context = featureAbility.getContext() 366e41f4b71Sopenharmony_ciconst kvManagerConfig: distributedKVStore.KVManagerConfig = { 367e41f4b71Sopenharmony_ci context: context, 368e41f4b71Sopenharmony_ci bundleName: 'com.example.datamanagertest', 369e41f4b71Sopenharmony_ci} 370e41f4b71Sopenharmony_citry { 371e41f4b71Sopenharmony_ci kvManager = distributedKVStore.createKVManager(kvManagerConfig); 372e41f4b71Sopenharmony_ci console.info("Succeeded in creating KVManager"); 373e41f4b71Sopenharmony_ci} catch (e) { 374e41f4b71Sopenharmony_ci let error = e as BusinessError; 375e41f4b71Sopenharmony_ci console.error(`Failed to create KVManager.code is ${error.code},message is ${error.message}`); 376e41f4b71Sopenharmony_ci} 377e41f4b71Sopenharmony_ciif (kvManager !== undefined) { 378e41f4b71Sopenharmony_ci kvManager = kvManager as distributedKVStore.KVManager; 379e41f4b71Sopenharmony_ci // Perform subsequent operations such as creating a KV store. 380e41f4b71Sopenharmony_ci // ... 381e41f4b71Sopenharmony_ci} 382e41f4b71Sopenharmony_ci``` 383e41f4b71Sopenharmony_ci 384e41f4b71Sopenharmony_ci## KVManager 385e41f4b71Sopenharmony_ci 386e41f4b71Sopenharmony_ciProvides an instance to obtain information about a distributed KV store. Before calling any API in **KVManager**, you must use [createKVManager](#distributedkvstorecreatekvmanager) to create a **KVManager** instance. 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ci### getKVStore 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_cigetKVStore<T>(storeId: string, options: Options, callback: AsyncCallback<T>): void 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ciCreates and obtains a distributed KV store based on the specified **options** and **storeId**. This API uses an asynchronous callback to return the result. 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci> **NOTE** 395e41f4b71Sopenharmony_ci> 396e41f4b71Sopenharmony_ci> If the database file is corrupted, the auto rebuild logic will be triggered and the newly created distributed KV store instance will be returned. The database file corruption may be caused by abnormal behaviors, such as the operation for clearing data or a failure in querying data. Back up data in a timely manner to prevent data loss. 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci**Parameters** 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 403e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 404e41f4b71Sopenharmony_ci| storeId | string | Yes | Unique identifier of the KV store. The KV store ID allows only letters, digits, and underscores (_), and cannot exceed [MAX_STORE_ID_LENGTH](#constants) in length.| 405e41f4b71Sopenharmony_ci| options | [Options](#options) | Yes | Configuration of the KV store to create. | 406e41f4b71Sopenharmony_ci| callback | AsyncCallback<T> | Yes | Callback used to return the **SingleKVStore** or **DeviceKVStore** instance created.| 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci**Error codes** 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci| ID| **Error Message** | 413e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 414e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 415e41f4b71Sopenharmony_ci| 15100002 | Open existed database with changed options. | 416e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci**Example** 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci```ts 421e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_cilet kvStore: distributedKVStore.SingleKVStore | null = null; 424e41f4b71Sopenharmony_citry { 425e41f4b71Sopenharmony_ci const options: distributedKVStore.Options = { 426e41f4b71Sopenharmony_ci createIfMissing: true, 427e41f4b71Sopenharmony_ci encrypt: false, 428e41f4b71Sopenharmony_ci backup: false, 429e41f4b71Sopenharmony_ci autoSync: false, 430e41f4b71Sopenharmony_ci kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, 431e41f4b71Sopenharmony_ci securityLevel: distributedKVStore.SecurityLevel.S3, 432e41f4b71Sopenharmony_ci }; 433e41f4b71Sopenharmony_ci kvManager.getKVStore('storeId', options, (err: BusinessError, store: distributedKVStore.SingleKVStore) => { 434e41f4b71Sopenharmony_ci if (err) { 435e41f4b71Sopenharmony_ci console.error(`Failed to get KVStore.code is ${err.code},message is ${err.message}`); 436e41f4b71Sopenharmony_ci return; 437e41f4b71Sopenharmony_ci } 438e41f4b71Sopenharmony_ci console.info("Succeeded in getting KVStore"); 439e41f4b71Sopenharmony_ci kvStore = store; 440e41f4b71Sopenharmony_ci }); 441e41f4b71Sopenharmony_ci} catch (e) { 442e41f4b71Sopenharmony_ci let error = e as BusinessError; 443e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 444e41f4b71Sopenharmony_ci} 445e41f4b71Sopenharmony_ciif (kvStore !== null) { 446e41f4b71Sopenharmony_ci kvStore = kvStore as distributedKVStore.SingleKVStore; 447e41f4b71Sopenharmony_ci // Perform subsequent data operations, such as adding, deleting, modifying, and querying data, and subscribing to data changes. 448e41f4b71Sopenharmony_ci // ... 449e41f4b71Sopenharmony_ci} 450e41f4b71Sopenharmony_ci``` 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci### getKVStore 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_cigetKVStore<T>(storeId: string, options: Options): Promise<T> 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ciCreates and obtains a distributed KV store based on the specified **options** and **storeId**. This API uses a promise to return the result. 457e41f4b71Sopenharmony_ci 458e41f4b71Sopenharmony_ci> **NOTE** 459e41f4b71Sopenharmony_ci> 460e41f4b71Sopenharmony_ci> If the database file is corrupted, the auto rebuild logic will be triggered and the newly created distributed KV store instance will be returned. The database file corruption may be caused by abnormal behaviors, such as the operation for clearing data or a failure in querying data. Back up data in a timely manner to prevent data loss. 461e41f4b71Sopenharmony_ci 462e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ci**Parameters** 465e41f4b71Sopenharmony_ci 466e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 467e41f4b71Sopenharmony_ci| ------- | ------------------- | ---- | ------------------------------------------------------------ | 468e41f4b71Sopenharmony_ci| storeId | string | Yes | Unique identifier of the KV store. The KV store ID allows only letters, digits, and underscores (_), and cannot exceed [MAX_STORE_ID_LENGTH](#constants) in length.| 469e41f4b71Sopenharmony_ci| options | [Options](#options) | Yes | Configuration of the distributed KV store to create. | 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci**Return value** 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci| Type | Description | 474e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 475e41f4b71Sopenharmony_ci| Promise<T> | Promise used to return the **SingleKVStore** or **DeviceKVStore** instance created.| 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci**Error codes** 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci| ID| **Error Message** | 482e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 483e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed.| 484e41f4b71Sopenharmony_ci| 15100002 | Open existed database with changed options. | 485e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci**Example** 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci```ts 490e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 491e41f4b71Sopenharmony_ci 492e41f4b71Sopenharmony_cilet kvStore: distributedKVStore.SingleKVStore | null = null; 493e41f4b71Sopenharmony_citry { 494e41f4b71Sopenharmony_ci const options: distributedKVStore.Options = { 495e41f4b71Sopenharmony_ci createIfMissing: true, 496e41f4b71Sopenharmony_ci encrypt: false, 497e41f4b71Sopenharmony_ci backup: false, 498e41f4b71Sopenharmony_ci autoSync: false, 499e41f4b71Sopenharmony_ci kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, 500e41f4b71Sopenharmony_ci securityLevel: distributedKVStore.SecurityLevel.S3, 501e41f4b71Sopenharmony_ci }; 502e41f4b71Sopenharmony_ci kvManager.getKVStore<distributedKVStore.SingleKVStore>('storeId', options).then((store: distributedKVStore.SingleKVStore) => { 503e41f4b71Sopenharmony_ci console.info("Succeeded in getting KVStore"); 504e41f4b71Sopenharmony_ci kvStore = store; 505e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 506e41f4b71Sopenharmony_ci console.error(`Failed to get KVStore.code is ${err.code},message is ${err.message}`); 507e41f4b71Sopenharmony_ci }); 508e41f4b71Sopenharmony_ci} catch (e) { 509e41f4b71Sopenharmony_ci let error = e as BusinessError; 510e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 511e41f4b71Sopenharmony_ci} 512e41f4b71Sopenharmony_ci``` 513e41f4b71Sopenharmony_ci 514e41f4b71Sopenharmony_ci### closeKVStore 515e41f4b71Sopenharmony_ci 516e41f4b71Sopenharmony_cicloseKVStore(appId: string, storeId: string, callback: AsyncCallback<void>): void 517e41f4b71Sopenharmony_ci 518e41f4b71Sopenharmony_ciCloses a distributed KV store. This API uses an asynchronous callback to return the result. 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci**Parameters** 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 525e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 526e41f4b71Sopenharmony_ci| appId | string | Yes | Bundle name of the application. The value cannot be empty or exceed 256 bytes. | 527e41f4b71Sopenharmony_ci| storeId | string | Yes | Unique identifier of the KV store to close. The KV store ID allows only letters, digits, and underscores (_), and cannot exceed [MAX_STORE_ID_LENGTH](#constants) in length.| 528e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ci**Error codes** 531e41f4b71Sopenharmony_ci 532e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 533e41f4b71Sopenharmony_ci 534e41f4b71Sopenharmony_ci| ID| **Error Message** | 535e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 536e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed.| 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ci**Example** 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ci```ts 541e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 542e41f4b71Sopenharmony_ci 543e41f4b71Sopenharmony_cilet kvStore: distributedKVStore.SingleKVStore | null = null; 544e41f4b71Sopenharmony_ciconst options: distributedKVStore.Options = { 545e41f4b71Sopenharmony_ci createIfMissing: true, 546e41f4b71Sopenharmony_ci encrypt: false, 547e41f4b71Sopenharmony_ci backup: false, 548e41f4b71Sopenharmony_ci autoSync: false, 549e41f4b71Sopenharmony_ci kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, 550e41f4b71Sopenharmony_ci schema: undefined, 551e41f4b71Sopenharmony_ci securityLevel: distributedKVStore.SecurityLevel.S3, 552e41f4b71Sopenharmony_ci} 553e41f4b71Sopenharmony_citry { 554e41f4b71Sopenharmony_ci kvManager.getKVStore('storeId', options, async (err: BusinessError, store: distributedKVStore.SingleKVStore | null) => { 555e41f4b71Sopenharmony_ci if (err != undefined) { 556e41f4b71Sopenharmony_ci console.error(`Failed to get KVStore.code is ${err.code},message is ${err.message}`); 557e41f4b71Sopenharmony_ci return; 558e41f4b71Sopenharmony_ci } 559e41f4b71Sopenharmony_ci console.info('Succeeded in getting KVStore'); 560e41f4b71Sopenharmony_ci kvStore = store; 561e41f4b71Sopenharmony_ci kvStore = null; 562e41f4b71Sopenharmony_ci store = null; 563e41f4b71Sopenharmony_ci kvManager.closeKVStore('appId', 'storeId', (err: BusinessError)=> { 564e41f4b71Sopenharmony_ci if (err != undefined) { 565e41f4b71Sopenharmony_ci console.error(`Failed to close KVStore.code is ${err.code},message is ${err.message}`); 566e41f4b71Sopenharmony_ci return; 567e41f4b71Sopenharmony_ci } 568e41f4b71Sopenharmony_ci console.info('Succeeded in closing KVStore'); 569e41f4b71Sopenharmony_ci }); 570e41f4b71Sopenharmony_ci }); 571e41f4b71Sopenharmony_ci} catch (e) { 572e41f4b71Sopenharmony_ci let error = e as BusinessError; 573e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 574e41f4b71Sopenharmony_ci} 575e41f4b71Sopenharmony_ci``` 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci### closeKVStore 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_cicloseKVStore(appId: string, storeId: string): Promise<void> 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ciCloses a distributed KV store. This API uses a promise to return the result. 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 584e41f4b71Sopenharmony_ci 585e41f4b71Sopenharmony_ci**Parameters** 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 588e41f4b71Sopenharmony_ci| ------- | -------- | ---- | ------------------------------------------------------------ | 589e41f4b71Sopenharmony_ci| appId | string | Yes | Bundle name of the application. The value cannot be empty or exceed 256 bytes. | 590e41f4b71Sopenharmony_ci| storeId | string | Yes | Unique identifier of the KV store to close. The KV store ID allows only letters, digits, and underscores (_), and cannot exceed [MAX_STORE_ID_LENGTH](#constants) in length.| 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ci**Return value** 593e41f4b71Sopenharmony_ci 594e41f4b71Sopenharmony_ci| Type | Description | 595e41f4b71Sopenharmony_ci| -------------- | ------------------------- | 596e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value.| 597e41f4b71Sopenharmony_ci 598e41f4b71Sopenharmony_ci**Error codes** 599e41f4b71Sopenharmony_ci 600e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 601e41f4b71Sopenharmony_ci 602e41f4b71Sopenharmony_ci| ID| **Error Message** | 603e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | 604e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed.| 605e41f4b71Sopenharmony_ci 606e41f4b71Sopenharmony_ci**Example** 607e41f4b71Sopenharmony_ci 608e41f4b71Sopenharmony_ci```ts 609e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_cilet kvStore: distributedKVStore.SingleKVStore | null = null; 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ciconst options: distributedKVStore.Options = { 614e41f4b71Sopenharmony_ci createIfMissing: true, 615e41f4b71Sopenharmony_ci encrypt: false, 616e41f4b71Sopenharmony_ci backup: false, 617e41f4b71Sopenharmony_ci autoSync: false, 618e41f4b71Sopenharmony_ci kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, 619e41f4b71Sopenharmony_ci schema: undefined, 620e41f4b71Sopenharmony_ci securityLevel: distributedKVStore.SecurityLevel.S3, 621e41f4b71Sopenharmony_ci} 622e41f4b71Sopenharmony_citry { 623e41f4b71Sopenharmony_ci kvManager.getKVStore<distributedKVStore.SingleKVStore>('storeId', options).then(async (store: distributedKVStore.SingleKVStore | null) => { 624e41f4b71Sopenharmony_ci console.info('Succeeded in getting KVStore'); 625e41f4b71Sopenharmony_ci kvStore = store; 626e41f4b71Sopenharmony_ci kvStore = null; 627e41f4b71Sopenharmony_ci store = null; 628e41f4b71Sopenharmony_ci kvManager.closeKVStore('appId', 'storeId').then(() => { 629e41f4b71Sopenharmony_ci console.info('Succeeded in closing KVStore'); 630e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 631e41f4b71Sopenharmony_ci console.error(`Failed to close KVStore.code is ${err.code},message is ${err.message}`); 632e41f4b71Sopenharmony_ci }); 633e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 634e41f4b71Sopenharmony_ci console.error(`Failed to get KVStore.code is ${err.code},message is ${err.message}`); 635e41f4b71Sopenharmony_ci }); 636e41f4b71Sopenharmony_ci} catch (e) { 637e41f4b71Sopenharmony_ci let error = e as BusinessError; 638e41f4b71Sopenharmony_ci console.error(`Failed to close KVStore.code is ${error.code},message is ${error.message}`); 639e41f4b71Sopenharmony_ci} 640e41f4b71Sopenharmony_ci``` 641e41f4b71Sopenharmony_ci 642e41f4b71Sopenharmony_ci### deleteKVStore 643e41f4b71Sopenharmony_ci 644e41f4b71Sopenharmony_cideleteKVStore(appId: string, storeId: string, callback: AsyncCallback<void>): void 645e41f4b71Sopenharmony_ci 646e41f4b71Sopenharmony_ciDeletes a distributed KV store. This API uses an asynchronous callback to return the result. 647e41f4b71Sopenharmony_ci 648e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 649e41f4b71Sopenharmony_ci 650e41f4b71Sopenharmony_ci**Parameters** 651e41f4b71Sopenharmony_ci 652e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 653e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 654e41f4b71Sopenharmony_ci| appId | string | Yes | Bundle name of the application. The value cannot be empty or exceed 256 bytes. | 655e41f4b71Sopenharmony_ci| storeId | string | Yes | Unique identifier of the KV store to delete. The KV store ID allows only letters, digits, and underscores (_), and cannot exceed [MAX_STORE_ID_LENGTH](#constants) in length.| 656e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 657e41f4b71Sopenharmony_ci 658e41f4b71Sopenharmony_ci**Error codes** 659e41f4b71Sopenharmony_ci 660e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 661e41f4b71Sopenharmony_ci 662e41f4b71Sopenharmony_ci| ID| **Error Message**| 663e41f4b71Sopenharmony_ci| ------------ | ------------ | 664e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed.| 665e41f4b71Sopenharmony_ci| 15100004 | Not found. | 666e41f4b71Sopenharmony_ci 667e41f4b71Sopenharmony_ci**Example** 668e41f4b71Sopenharmony_ci 669e41f4b71Sopenharmony_ci```ts 670e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 671e41f4b71Sopenharmony_ci 672e41f4b71Sopenharmony_cilet kvStore: distributedKVStore.SingleKVStore | null = null; 673e41f4b71Sopenharmony_ci 674e41f4b71Sopenharmony_ciconst options: distributedKVStore.Options = { 675e41f4b71Sopenharmony_ci createIfMissing: true, 676e41f4b71Sopenharmony_ci encrypt: false, 677e41f4b71Sopenharmony_ci backup: false, 678e41f4b71Sopenharmony_ci autoSync: false, 679e41f4b71Sopenharmony_ci kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, 680e41f4b71Sopenharmony_ci schema: undefined, 681e41f4b71Sopenharmony_ci securityLevel: distributedKVStore.SecurityLevel.S3, 682e41f4b71Sopenharmony_ci} 683e41f4b71Sopenharmony_citry { 684e41f4b71Sopenharmony_ci kvManager.getKVStore('store', options, async (err: BusinessError, store: distributedKVStore.SingleKVStore | null) => { 685e41f4b71Sopenharmony_ci if (err != undefined) { 686e41f4b71Sopenharmony_ci console.error(`Failed to get KVStore.code is ${err.code},message is ${err.message}`); 687e41f4b71Sopenharmony_ci return; 688e41f4b71Sopenharmony_ci } 689e41f4b71Sopenharmony_ci console.info('Succeeded in getting KVStore'); 690e41f4b71Sopenharmony_ci kvStore = store; 691e41f4b71Sopenharmony_ci kvStore = null; 692e41f4b71Sopenharmony_ci store = null; 693e41f4b71Sopenharmony_ci kvManager.deleteKVStore('appId', 'storeId', (err: BusinessError) => { 694e41f4b71Sopenharmony_ci if (err != undefined) { 695e41f4b71Sopenharmony_ci console.error(`Failed to delete KVStore.code is ${err.code},message is ${err.message}`); 696e41f4b71Sopenharmony_ci return; 697e41f4b71Sopenharmony_ci } 698e41f4b71Sopenharmony_ci console.info(`Succeeded in deleting KVStore`); 699e41f4b71Sopenharmony_ci }); 700e41f4b71Sopenharmony_ci }); 701e41f4b71Sopenharmony_ci} catch (e) { 702e41f4b71Sopenharmony_ci let error = e as BusinessError; 703e41f4b71Sopenharmony_ci console.error(`Failed to delete KVStore.code is ${error.code},message is ${error.message}`); 704e41f4b71Sopenharmony_ci} 705e41f4b71Sopenharmony_ci``` 706e41f4b71Sopenharmony_ci 707e41f4b71Sopenharmony_ci### deleteKVStore 708e41f4b71Sopenharmony_ci 709e41f4b71Sopenharmony_cideleteKVStore(appId: string, storeId: string): Promise<void> 710e41f4b71Sopenharmony_ci 711e41f4b71Sopenharmony_ciDeletes a distributed KV store. This API uses a promise to return the result. 712e41f4b71Sopenharmony_ci 713e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 714e41f4b71Sopenharmony_ci 715e41f4b71Sopenharmony_ci**Parameters** 716e41f4b71Sopenharmony_ci 717e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 718e41f4b71Sopenharmony_ci| ------- | -------- | ---- | ------------------------------------------------------------ | 719e41f4b71Sopenharmony_ci| appId | string | Yes | Bundle name of the application. The value cannot be empty or exceed 256 bytes. | 720e41f4b71Sopenharmony_ci| storeId | string | Yes | Unique identifier of the KV store to delete. The KV store ID allows only letters, digits, and underscores (_), and cannot exceed [MAX_STORE_ID_LENGTH](#constants) in length.| 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_ci**Return value** 723e41f4b71Sopenharmony_ci 724e41f4b71Sopenharmony_ci| Type | Description | 725e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 726e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 727e41f4b71Sopenharmony_ci 728e41f4b71Sopenharmony_ci**Error codes** 729e41f4b71Sopenharmony_ci 730e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 731e41f4b71Sopenharmony_ci 732e41f4b71Sopenharmony_ci| ID| **Error Message**| 733e41f4b71Sopenharmony_ci| ------------ | ------------ | 734e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed.| 735e41f4b71Sopenharmony_ci| 15100004 | Not found. | 736e41f4b71Sopenharmony_ci 737e41f4b71Sopenharmony_ci**Example** 738e41f4b71Sopenharmony_ci 739e41f4b71Sopenharmony_ci```ts 740e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 741e41f4b71Sopenharmony_ci 742e41f4b71Sopenharmony_cilet kvStore: distributedKVStore.SingleKVStore | null = null; 743e41f4b71Sopenharmony_ci 744e41f4b71Sopenharmony_ciconst options: distributedKVStore.Options = { 745e41f4b71Sopenharmony_ci createIfMissing: true, 746e41f4b71Sopenharmony_ci encrypt: false, 747e41f4b71Sopenharmony_ci backup: false, 748e41f4b71Sopenharmony_ci autoSync: false, 749e41f4b71Sopenharmony_ci kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, 750e41f4b71Sopenharmony_ci schema: undefined, 751e41f4b71Sopenharmony_ci securityLevel: distributedKVStore.SecurityLevel.S3, 752e41f4b71Sopenharmony_ci} 753e41f4b71Sopenharmony_citry { 754e41f4b71Sopenharmony_ci kvManager.getKVStore<distributedKVStore.SingleKVStore>('storeId', options).then(async (store: distributedKVStore.SingleKVStore | null) => { 755e41f4b71Sopenharmony_ci console.info('Succeeded in getting KVStore'); 756e41f4b71Sopenharmony_ci kvStore = store; 757e41f4b71Sopenharmony_ci kvStore = null; 758e41f4b71Sopenharmony_ci store = null; 759e41f4b71Sopenharmony_ci kvManager.deleteKVStore('appId', 'storeId').then(() => { 760e41f4b71Sopenharmony_ci console.info('Succeeded in deleting KVStore'); 761e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 762e41f4b71Sopenharmony_ci console.error(`Failed to delete KVStore.code is ${err.code},message is ${err.message}`); 763e41f4b71Sopenharmony_ci }); 764e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 765e41f4b71Sopenharmony_ci console.error(`Failed to get KVStore.code is ${err.code},message is ${err.message}`); 766e41f4b71Sopenharmony_ci }); 767e41f4b71Sopenharmony_ci} catch (e) { 768e41f4b71Sopenharmony_ci let error = e as BusinessError; 769e41f4b71Sopenharmony_ci console.error(`Failed to delete KVStore.code is ${error.code},message is ${error.message}`); 770e41f4b71Sopenharmony_ci} 771e41f4b71Sopenharmony_ci``` 772e41f4b71Sopenharmony_ci 773e41f4b71Sopenharmony_ci### getAllKVStoreId 774e41f4b71Sopenharmony_ci 775e41f4b71Sopenharmony_cigetAllKVStoreId(appId: string, callback: AsyncCallback<string[]>): void 776e41f4b71Sopenharmony_ci 777e41f4b71Sopenharmony_ciObtains the IDs of all distributed KV stores that are created by [getKVStore](#getkvstore) and have not been deleted by [deleteKVStore](#deletekvstore). This API uses an asynchronous callback to return the result. 778e41f4b71Sopenharmony_ci 779e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 780e41f4b71Sopenharmony_ci 781e41f4b71Sopenharmony_ci**Parameters** 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 784e41f4b71Sopenharmony_ci| -------- | ----------------------------- | ---- | --------------------------------------------------- | 785e41f4b71Sopenharmony_ci| appId | string | Yes | Bundle name of the application. The value cannot be empty or exceed 256 bytes. | 786e41f4b71Sopenharmony_ci| callback | AsyncCallback<string[]> | Yes | Callback used to return the IDs of all the distributed KV stores created.| 787e41f4b71Sopenharmony_ci 788e41f4b71Sopenharmony_ci**Error codes** 789e41f4b71Sopenharmony_ci 790e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 791e41f4b71Sopenharmony_ci 792e41f4b71Sopenharmony_ci| ID| **Error Message**| 793e41f4b71Sopenharmony_ci| ------------ | ------------ | 794e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed.| 795e41f4b71Sopenharmony_ci 796e41f4b71Sopenharmony_ci**Example** 797e41f4b71Sopenharmony_ci 798e41f4b71Sopenharmony_ci```ts 799e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 800e41f4b71Sopenharmony_ci 801e41f4b71Sopenharmony_citry { 802e41f4b71Sopenharmony_ci kvManager.getAllKVStoreId('appId', (err: BusinessError, data: string[]) => { 803e41f4b71Sopenharmony_ci if (err != undefined) { 804e41f4b71Sopenharmony_ci console.error(`Failed to get AllKVStoreId.code is ${err.code},message is ${err.message}`); 805e41f4b71Sopenharmony_ci return; 806e41f4b71Sopenharmony_ci } 807e41f4b71Sopenharmony_ci console.info('Succeeded in getting AllKVStoreId'); 808e41f4b71Sopenharmony_ci console.info(`GetAllKVStoreId size = ${data.length}`); 809e41f4b71Sopenharmony_ci }); 810e41f4b71Sopenharmony_ci} catch (e) { 811e41f4b71Sopenharmony_ci let error = e as BusinessError; 812e41f4b71Sopenharmony_ci console.error(`Failed to get AllKVStoreId.code is ${error.code},message is ${error.message}`); 813e41f4b71Sopenharmony_ci} 814e41f4b71Sopenharmony_ci``` 815e41f4b71Sopenharmony_ci 816e41f4b71Sopenharmony_ci### getAllKVStoreId 817e41f4b71Sopenharmony_ci 818e41f4b71Sopenharmony_cigetAllKVStoreId(appId: string): Promise<string[]> 819e41f4b71Sopenharmony_ci 820e41f4b71Sopenharmony_ciObtains the IDs of all distributed KV stores that are created by [getKVStore](#getkvstore) and have not been deleted by [deleteKVStore](#deletekvstore). This API uses a promise to return the result. 821e41f4b71Sopenharmony_ci 822e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 823e41f4b71Sopenharmony_ci 824e41f4b71Sopenharmony_ci**Parameters** 825e41f4b71Sopenharmony_ci 826e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 827e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ---------------------- | 828e41f4b71Sopenharmony_ci| appId | string | Yes | Bundle name of the application. The value cannot be empty or exceed 256 bytes.| 829e41f4b71Sopenharmony_ci 830e41f4b71Sopenharmony_ci**Return value** 831e41f4b71Sopenharmony_ci 832e41f4b71Sopenharmony_ci| Type | Description | 833e41f4b71Sopenharmony_ci| ----------------------- | ------------------------------------------------------ | 834e41f4b71Sopenharmony_ci| Promise<string[]> | Promise used to return the IDs of all the distributed KV stores created.| 835e41f4b71Sopenharmony_ci 836e41f4b71Sopenharmony_ci**Error codes** 837e41f4b71Sopenharmony_ci 838e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 839e41f4b71Sopenharmony_ci 840e41f4b71Sopenharmony_ci| ID| **Error Message**| 841e41f4b71Sopenharmony_ci| ------------ | ------------ | 842e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed.| 843e41f4b71Sopenharmony_ci 844e41f4b71Sopenharmony_ci**Example** 845e41f4b71Sopenharmony_ci 846e41f4b71Sopenharmony_ci```ts 847e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 848e41f4b71Sopenharmony_ci 849e41f4b71Sopenharmony_citry { 850e41f4b71Sopenharmony_ci console.info('GetAllKVStoreId'); 851e41f4b71Sopenharmony_ci kvManager.getAllKVStoreId('appId').then((data: string[]) => { 852e41f4b71Sopenharmony_ci console.info('Succeeded in getting AllKVStoreId'); 853e41f4b71Sopenharmony_ci console.info(`GetAllKVStoreId size = ${data.length}`); 854e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 855e41f4b71Sopenharmony_ci console.error(`Failed to get AllKVStoreId.code is ${err.code},message is ${err.message}`); 856e41f4b71Sopenharmony_ci }); 857e41f4b71Sopenharmony_ci} catch (e) { 858e41f4b71Sopenharmony_ci let error = e as BusinessError; 859e41f4b71Sopenharmony_ci console.error(`Failed to get AllKVStoreId.code is ${error.code},message is ${error.message}`); 860e41f4b71Sopenharmony_ci} 861e41f4b71Sopenharmony_ci``` 862e41f4b71Sopenharmony_ci 863e41f4b71Sopenharmony_ci### on('distributedDataServiceDie') 864e41f4b71Sopenharmony_ci 865e41f4b71Sopenharmony_cion(event: 'distributedDataServiceDie', deathCallback: Callback<void>): void 866e41f4b71Sopenharmony_ci 867e41f4b71Sopenharmony_ciSubscribes to the termination (death) of the distributed data service. If the service is terminated, you need to register the callbacks for data change notifications and sync complete notifications again. In addition, an error will be returned for a sync operation. 868e41f4b71Sopenharmony_ci 869e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 870e41f4b71Sopenharmony_ci 871e41f4b71Sopenharmony_ci**Parameters** 872e41f4b71Sopenharmony_ci 873e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 874e41f4b71Sopenharmony_ci| ------------- | -------------------- | ---- | ------------------------------------------------------------ | 875e41f4b71Sopenharmony_ci| event | string | Yes | Event type. The value is **distributedDataServiceDie**, which indicates the termination of the distributed data service.| 876e41f4b71Sopenharmony_ci| deathCallback | Callback<void> | Yes | Callback used to return the result. If the subscription is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 877e41f4b71Sopenharmony_ci 878e41f4b71Sopenharmony_ci**Error codes** 879e41f4b71Sopenharmony_ci 880e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 881e41f4b71Sopenharmony_ci 882e41f4b71Sopenharmony_ci| ID| **Error Message**| 883e41f4b71Sopenharmony_ci| ------------ | ------------ | 884e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed.| 885e41f4b71Sopenharmony_ci 886e41f4b71Sopenharmony_ci**Example** 887e41f4b71Sopenharmony_ci 888e41f4b71Sopenharmony_ci```ts 889e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 890e41f4b71Sopenharmony_ci 891e41f4b71Sopenharmony_citry { 892e41f4b71Sopenharmony_ci console.info('KVManagerOn'); 893e41f4b71Sopenharmony_ci const deathCallback = () => { 894e41f4b71Sopenharmony_ci console.info('death callback call'); 895e41f4b71Sopenharmony_ci } 896e41f4b71Sopenharmony_ci kvManager.on('distributedDataServiceDie', deathCallback); 897e41f4b71Sopenharmony_ci} catch (e) { 898e41f4b71Sopenharmony_ci let error = e as BusinessError; 899e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 900e41f4b71Sopenharmony_ci} 901e41f4b71Sopenharmony_ci``` 902e41f4b71Sopenharmony_ci 903e41f4b71Sopenharmony_ci### off('distributedDataServiceDie') 904e41f4b71Sopenharmony_ci 905e41f4b71Sopenharmony_cioff(event: 'distributedDataServiceDie', deathCallback?: Callback<void>): void 906e41f4b71Sopenharmony_ci 907e41f4b71Sopenharmony_ciUnsubscribes from the termination (death) of the distributed data service. The **deathCallback** parameter must be a callback registered for subscribing to the termination of the distributed data service. Otherwise, the unsubscription will fail. 908e41f4b71Sopenharmony_ci 909e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 910e41f4b71Sopenharmony_ci 911e41f4b71Sopenharmony_ci**Parameters** 912e41f4b71Sopenharmony_ci 913e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 914e41f4b71Sopenharmony_ci| ------------- | -------------------- | ---- | ------------------------------------------------------------ | 915e41f4b71Sopenharmony_ci| event | string | Yes | Event type. The value is **distributedDataServiceDie**, which indicates the termination of the distributed data service.| 916e41f4b71Sopenharmony_ci| deathCallback | Callback<void> | No | Callback to unregister. If this parameter is not specified, this API unregisters all callbacks for the **distributedDataServiceDie** event. | 917e41f4b71Sopenharmony_ci 918e41f4b71Sopenharmony_ci**Error codes** 919e41f4b71Sopenharmony_ci 920e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 921e41f4b71Sopenharmony_ci 922e41f4b71Sopenharmony_ci| ID| **Error Message**| 923e41f4b71Sopenharmony_ci| ------------ | ------------ | 924e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 925e41f4b71Sopenharmony_ci 926e41f4b71Sopenharmony_ci**Example** 927e41f4b71Sopenharmony_ci 928e41f4b71Sopenharmony_ci```ts 929e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 930e41f4b71Sopenharmony_ci 931e41f4b71Sopenharmony_citry { 932e41f4b71Sopenharmony_ci console.info('KVManagerOff'); 933e41f4b71Sopenharmony_ci const deathCallback = () => { 934e41f4b71Sopenharmony_ci console.info('death callback call'); 935e41f4b71Sopenharmony_ci } 936e41f4b71Sopenharmony_ci kvManager.off('distributedDataServiceDie', deathCallback); 937e41f4b71Sopenharmony_ci} catch (e) { 938e41f4b71Sopenharmony_ci let error = e as BusinessError; 939e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 940e41f4b71Sopenharmony_ci} 941e41f4b71Sopenharmony_ci``` 942e41f4b71Sopenharmony_ci 943e41f4b71Sopenharmony_ci## KVStoreResultSet 944e41f4b71Sopenharmony_ci 945e41f4b71Sopenharmony_ciProvides APIs for obtaining the distributed KV store result sets. A maximum of eight result sets can be opened at a time. 946e41f4b71Sopenharmony_ci 947e41f4b71Sopenharmony_ciBefore calling any API in **KVStoreResultSet**, you must use **[getKVStore](#getkvstore)** to construct a **SingleKVStore** or **DeviceKVStore** instance. 948e41f4b71Sopenharmony_ci 949e41f4b71Sopenharmony_ci### getCount 950e41f4b71Sopenharmony_ci 951e41f4b71Sopenharmony_cigetCount(): number 952e41f4b71Sopenharmony_ci 953e41f4b71Sopenharmony_ciObtains the total number of rows in the result set. 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ci**Return value** 958e41f4b71Sopenharmony_ci 959e41f4b71Sopenharmony_ci| Type | Description | 960e41f4b71Sopenharmony_ci| ------ | ------------------ | 961e41f4b71Sopenharmony_ci| number | Total number of rows obtained.| 962e41f4b71Sopenharmony_ci 963e41f4b71Sopenharmony_ci**Example** 964e41f4b71Sopenharmony_ci 965e41f4b71Sopenharmony_ci```ts 966e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 967e41f4b71Sopenharmony_ci 968e41f4b71Sopenharmony_citry { 969e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 970e41f4b71Sopenharmony_ci let count: number; 971e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 972e41f4b71Sopenharmony_ci console.info('getResultSet succeed.'); 973e41f4b71Sopenharmony_ci resultSet = result; 974e41f4b71Sopenharmony_ci count = resultSet.getCount(); 975e41f4b71Sopenharmony_ci console.info("getCount succeed:" + count); 976e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 977e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 978e41f4b71Sopenharmony_ci }); 979e41f4b71Sopenharmony_ci} catch (e) { 980e41f4b71Sopenharmony_ci console.error("getCount failed: " + e); 981e41f4b71Sopenharmony_ci} 982e41f4b71Sopenharmony_ci``` 983e41f4b71Sopenharmony_ci 984e41f4b71Sopenharmony_ci### getPosition 985e41f4b71Sopenharmony_ci 986e41f4b71Sopenharmony_cigetPosition(): number 987e41f4b71Sopenharmony_ci 988e41f4b71Sopenharmony_ciObtains the current data read position (position from which data is read) in the result set. The read position changes with the operations, such as [moveToFirst](#movetofirst) and [moveToLast](#movetolast). 989e41f4b71Sopenharmony_ci 990e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 991e41f4b71Sopenharmony_ci 992e41f4b71Sopenharmony_ci**Return value** 993e41f4b71Sopenharmony_ci 994e41f4b71Sopenharmony_ci| Type | Description | 995e41f4b71Sopenharmony_ci| ------ | ------------------ | 996e41f4b71Sopenharmony_ci| number | Current data read position obtained. The value must be greater than or equal to **-1**. The value **-1** means no data is read; the value **0** indicates the first row.| 997e41f4b71Sopenharmony_ci 998e41f4b71Sopenharmony_ci**Example** 999e41f4b71Sopenharmony_ci 1000e41f4b71Sopenharmony_ci```ts 1001e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1002e41f4b71Sopenharmony_ci 1003e41f4b71Sopenharmony_citry { 1004e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1005e41f4b71Sopenharmony_ci let position: number; 1006e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1007e41f4b71Sopenharmony_ci console.info('getResultSet succeeded.'); 1008e41f4b71Sopenharmony_ci resultSet = result; 1009e41f4b71Sopenharmony_ci position = resultSet.getPosition(); 1010e41f4b71Sopenharmony_ci console.info("getPosition succeed:" + position); 1011e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1012e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 1013e41f4b71Sopenharmony_ci }); 1014e41f4b71Sopenharmony_ci} catch (e) { 1015e41f4b71Sopenharmony_ci console.error("getPosition failed: " + e); 1016e41f4b71Sopenharmony_ci} 1017e41f4b71Sopenharmony_ci``` 1018e41f4b71Sopenharmony_ci 1019e41f4b71Sopenharmony_ci### moveToFirst 1020e41f4b71Sopenharmony_ci 1021e41f4b71Sopenharmony_cimoveToFirst(): boolean 1022e41f4b71Sopenharmony_ci 1023e41f4b71Sopenharmony_ciMoves the data read position to the first row. If the result set is empty, **false** will be returned. 1024e41f4b71Sopenharmony_ci 1025e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1026e41f4b71Sopenharmony_ci 1027e41f4b71Sopenharmony_ci**Return value** 1028e41f4b71Sopenharmony_ci 1029e41f4b71Sopenharmony_ci| Type | Description | 1030e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | 1031e41f4b71Sopenharmony_ci| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1032e41f4b71Sopenharmony_ci 1033e41f4b71Sopenharmony_ci**Example** 1034e41f4b71Sopenharmony_ci 1035e41f4b71Sopenharmony_ci```ts 1036e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1037e41f4b71Sopenharmony_ci 1038e41f4b71Sopenharmony_citry { 1039e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1040e41f4b71Sopenharmony_ci let moved: boolean; 1041e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1042e41f4b71Sopenharmony_ci console.info('getResultSet succeed.'); 1043e41f4b71Sopenharmony_ci resultSet = result; 1044e41f4b71Sopenharmony_ci moved = resultSet.moveToFirst(); 1045e41f4b71Sopenharmony_ci console.info("moveToFirst succeed: " + moved); 1046e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1047e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 1048e41f4b71Sopenharmony_ci }); 1049e41f4b71Sopenharmony_ci} catch (e) { 1050e41f4b71Sopenharmony_ci console.error("moveToFirst failed " + e); 1051e41f4b71Sopenharmony_ci} 1052e41f4b71Sopenharmony_ci``` 1053e41f4b71Sopenharmony_ci 1054e41f4b71Sopenharmony_ci### moveToLast 1055e41f4b71Sopenharmony_ci 1056e41f4b71Sopenharmony_cimoveToLast(): boolean 1057e41f4b71Sopenharmony_ci 1058e41f4b71Sopenharmony_ciMoves the data read position to the last row. If the result set is empty, **false** will be returned. 1059e41f4b71Sopenharmony_ci 1060e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1061e41f4b71Sopenharmony_ci 1062e41f4b71Sopenharmony_ci**Return value** 1063e41f4b71Sopenharmony_ci 1064e41f4b71Sopenharmony_ci| Type | Description | 1065e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | 1066e41f4b71Sopenharmony_ci| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1067e41f4b71Sopenharmony_ci 1068e41f4b71Sopenharmony_ci**Example** 1069e41f4b71Sopenharmony_ci 1070e41f4b71Sopenharmony_ci```ts 1071e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1072e41f4b71Sopenharmony_ci 1073e41f4b71Sopenharmony_citry { 1074e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1075e41f4b71Sopenharmony_ci let moved: boolean; 1076e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1077e41f4b71Sopenharmony_ci console.info('getResultSet succeed.'); 1078e41f4b71Sopenharmony_ci resultSet = result; 1079e41f4b71Sopenharmony_ci moved = resultSet.moveToLast(); 1080e41f4b71Sopenharmony_ci console.info("moveToLast succeed:" + moved); 1081e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1082e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 1083e41f4b71Sopenharmony_ci }); 1084e41f4b71Sopenharmony_ci} catch (e) { 1085e41f4b71Sopenharmony_ci console.error("moveToLast failed: " + e); 1086e41f4b71Sopenharmony_ci} 1087e41f4b71Sopenharmony_ci``` 1088e41f4b71Sopenharmony_ci 1089e41f4b71Sopenharmony_ci### moveToNext 1090e41f4b71Sopenharmony_ci 1091e41f4b71Sopenharmony_cimoveToNext(): boolean 1092e41f4b71Sopenharmony_ci 1093e41f4b71Sopenharmony_ciMoves the data read position to the next row. If the result set is empty, **false** will be returned. This API applies when the whole result set is obtained. 1094e41f4b71Sopenharmony_ci 1095e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1096e41f4b71Sopenharmony_ci 1097e41f4b71Sopenharmony_ci**Return value** 1098e41f4b71Sopenharmony_ci 1099e41f4b71Sopenharmony_ci| Type | Description | 1100e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | 1101e41f4b71Sopenharmony_ci| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1102e41f4b71Sopenharmony_ci 1103e41f4b71Sopenharmony_ci**Example** 1104e41f4b71Sopenharmony_ci 1105e41f4b71Sopenharmony_ci```ts 1106e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1107e41f4b71Sopenharmony_ci 1108e41f4b71Sopenharmony_citry { 1109e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1110e41f4b71Sopenharmony_ci let moved: boolean; 1111e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1112e41f4b71Sopenharmony_ci console.info('getResultSet succeed.'); 1113e41f4b71Sopenharmony_ci resultSet = result; 1114e41f4b71Sopenharmony_ci do { 1115e41f4b71Sopenharmony_ci moved = resultSet.moveToNext(); 1116e41f4b71Sopenharmony_ci console.info("moveToNext succeed: " + moved); 1117e41f4b71Sopenharmony_ci } while (moved) 1118e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1119e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 1120e41f4b71Sopenharmony_ci }); 1121e41f4b71Sopenharmony_ci} catch (e) { 1122e41f4b71Sopenharmony_ci console.error("moveToNext failed: " + e); 1123e41f4b71Sopenharmony_ci} 1124e41f4b71Sopenharmony_ci``` 1125e41f4b71Sopenharmony_ci 1126e41f4b71Sopenharmony_ci### moveToPrevious 1127e41f4b71Sopenharmony_ci 1128e41f4b71Sopenharmony_cimoveToPrevious(): boolean 1129e41f4b71Sopenharmony_ci 1130e41f4b71Sopenharmony_ciMoves the data read position to the previous row. If the result set is empty, **false** will be returned. This API applies when the whole result set is obtained. 1131e41f4b71Sopenharmony_ci 1132e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1133e41f4b71Sopenharmony_ci 1134e41f4b71Sopenharmony_ci**Return value** 1135e41f4b71Sopenharmony_ci 1136e41f4b71Sopenharmony_ci| Type | Description | 1137e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | 1138e41f4b71Sopenharmony_ci| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1139e41f4b71Sopenharmony_ci 1140e41f4b71Sopenharmony_ci**Example** 1141e41f4b71Sopenharmony_ci 1142e41f4b71Sopenharmony_ci```ts 1143e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1144e41f4b71Sopenharmony_ci 1145e41f4b71Sopenharmony_citry { 1146e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1147e41f4b71Sopenharmony_ci let moved: boolean; 1148e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1149e41f4b71Sopenharmony_ci console.info('getResultSet succeed.'); 1150e41f4b71Sopenharmony_ci resultSet = result; 1151e41f4b71Sopenharmony_ci moved = resultSet.moveToLast(); 1152e41f4b71Sopenharmony_ci moved = resultSet.moveToPrevious(); 1153e41f4b71Sopenharmony_ci console.info("moveToPrevious succeed:" + moved); 1154e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1155e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 1156e41f4b71Sopenharmony_ci }); 1157e41f4b71Sopenharmony_ci} catch (e) { 1158e41f4b71Sopenharmony_ci console.error("moveToPrevious failed: " + e); 1159e41f4b71Sopenharmony_ci} 1160e41f4b71Sopenharmony_ci``` 1161e41f4b71Sopenharmony_ci 1162e41f4b71Sopenharmony_ci### move 1163e41f4b71Sopenharmony_ci 1164e41f4b71Sopenharmony_cimove(offset: number): boolean 1165e41f4b71Sopenharmony_ci 1166e41f4b71Sopenharmony_ciMoves the data read position with the specified offset from the current position. That is, moves the number of rows specified by **offset** from the current position. 1167e41f4b71Sopenharmony_ci 1168e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1169e41f4b71Sopenharmony_ci 1170e41f4b71Sopenharmony_ci**Parameters** 1171e41f4b71Sopenharmony_ci 1172e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 1173e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------------------------------------------------------ | 1174e41f4b71Sopenharmony_ci| offset | number | Yes | Offset to move the data read position. A negative value means to move backward, and a positive value means to move forward.| 1175e41f4b71Sopenharmony_ci 1176e41f4b71Sopenharmony_ci**Return value** 1177e41f4b71Sopenharmony_ci 1178e41f4b71Sopenharmony_ci| Type | Description | 1179e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | 1180e41f4b71Sopenharmony_ci| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1181e41f4b71Sopenharmony_ci 1182e41f4b71Sopenharmony_ci**Error codes** 1183e41f4b71Sopenharmony_ci 1184e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1185e41f4b71Sopenharmony_ci 1186e41f4b71Sopenharmony_ci| ID| **Error Message**| 1187e41f4b71Sopenharmony_ci| ------------ | ------------ | 1188e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1189e41f4b71Sopenharmony_ci 1190e41f4b71Sopenharmony_ci**Example** 1191e41f4b71Sopenharmony_ci 1192e41f4b71Sopenharmony_ci```ts 1193e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1194e41f4b71Sopenharmony_ci 1195e41f4b71Sopenharmony_citry { 1196e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1197e41f4b71Sopenharmony_ci let moved: boolean; 1198e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1199e41f4b71Sopenharmony_ci console.info('Succeeded in getting resultSet'); 1200e41f4b71Sopenharmony_ci resultSet = result; 1201e41f4b71Sopenharmony_ci moved = resultSet.move(2); // If the current position is 0, move the read position forward by two rows, that is, move to row 3. 1202e41f4b71Sopenharmony_ci console.info(`Succeeded in moving.moved = ${moved}`); 1203e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1204e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 1205e41f4b71Sopenharmony_ci }); 1206e41f4b71Sopenharmony_ci} catch (e) { 1207e41f4b71Sopenharmony_ci let error = e as BusinessError; 1208e41f4b71Sopenharmony_ci console.error(`Failed to move.code is ${error.code},message is ${error.message}`); 1209e41f4b71Sopenharmony_ci} 1210e41f4b71Sopenharmony_ci``` 1211e41f4b71Sopenharmony_ci 1212e41f4b71Sopenharmony_ci### moveToPosition 1213e41f4b71Sopenharmony_ci 1214e41f4b71Sopenharmony_cimoveToPosition(position: number): boolean 1215e41f4b71Sopenharmony_ci 1216e41f4b71Sopenharmony_ciMoves the data read position from 0 to an absolute position. 1217e41f4b71Sopenharmony_ci 1218e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1219e41f4b71Sopenharmony_ci 1220e41f4b71Sopenharmony_ci**Parameters** 1221e41f4b71Sopenharmony_ci 1222e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 1223e41f4b71Sopenharmony_ci| -------- | -------- | ---- | -------------- | 1224e41f4b71Sopenharmony_ci| position | number | Yes | Absolute position to move to.| 1225e41f4b71Sopenharmony_ci 1226e41f4b71Sopenharmony_ci**Return value** 1227e41f4b71Sopenharmony_ci 1228e41f4b71Sopenharmony_ci| Type | Description | 1229e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | 1230e41f4b71Sopenharmony_ci| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1231e41f4b71Sopenharmony_ci 1232e41f4b71Sopenharmony_ci**Error codes** 1233e41f4b71Sopenharmony_ci 1234e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1235e41f4b71Sopenharmony_ci 1236e41f4b71Sopenharmony_ci| ID| **Error Message**| 1237e41f4b71Sopenharmony_ci| ------------ | ------------ | 1238e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 1239e41f4b71Sopenharmony_ci 1240e41f4b71Sopenharmony_ci**Example** 1241e41f4b71Sopenharmony_ci 1242e41f4b71Sopenharmony_ci```ts 1243e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1244e41f4b71Sopenharmony_ci 1245e41f4b71Sopenharmony_citry { 1246e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1247e41f4b71Sopenharmony_ci let moved: boolean; 1248e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1249e41f4b71Sopenharmony_ci console.info('Succeeded in getting resultSet'); 1250e41f4b71Sopenharmony_ci resultSet = result; 1251e41f4b71Sopenharmony_ci moved = resultSet.moveToPosition(1); 1252e41f4b71Sopenharmony_ci console.info(`Succeeded in moving to position.moved=${moved}`); 1253e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1254e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 1255e41f4b71Sopenharmony_ci }); 1256e41f4b71Sopenharmony_ci} catch (e) { 1257e41f4b71Sopenharmony_ci let error = e as BusinessError; 1258e41f4b71Sopenharmony_ci console.error(`Failed to move to position.code is ${error.code},message is ${error.message}`); 1259e41f4b71Sopenharmony_ci} 1260e41f4b71Sopenharmony_ci``` 1261e41f4b71Sopenharmony_ci 1262e41f4b71Sopenharmony_ci### isFirst 1263e41f4b71Sopenharmony_ci 1264e41f4b71Sopenharmony_ciisFirst(): boolean 1265e41f4b71Sopenharmony_ci 1266e41f4b71Sopenharmony_ciChecks whether the data read position is the first row. 1267e41f4b71Sopenharmony_ci 1268e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1269e41f4b71Sopenharmony_ci 1270e41f4b71Sopenharmony_ci**Return value** 1271e41f4b71Sopenharmony_ci 1272e41f4b71Sopenharmony_ci| Type | Description | 1273e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | 1274e41f4b71Sopenharmony_ci| boolean | Returns **true** if the first row is being read; returns **false** otherwise.| 1275e41f4b71Sopenharmony_ci 1276e41f4b71Sopenharmony_ci**Example** 1277e41f4b71Sopenharmony_ci 1278e41f4b71Sopenharmony_ci```ts 1279e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1280e41f4b71Sopenharmony_ci 1281e41f4b71Sopenharmony_citry { 1282e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1283e41f4b71Sopenharmony_ci let isfirst: boolean; 1284e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1285e41f4b71Sopenharmony_ci console.info('getResultSet succeed.'); 1286e41f4b71Sopenharmony_ci resultSet = result; 1287e41f4b71Sopenharmony_ci isfirst = resultSet.isFirst(); 1288e41f4b71Sopenharmony_ci console.info("Check isFirst succeed:" + isfirst); 1289e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1290e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 1291e41f4b71Sopenharmony_ci }); 1292e41f4b71Sopenharmony_ci} catch (e) { 1293e41f4b71Sopenharmony_ci console.error("Check isFirst failed: " + e); 1294e41f4b71Sopenharmony_ci} 1295e41f4b71Sopenharmony_ci``` 1296e41f4b71Sopenharmony_ci 1297e41f4b71Sopenharmony_ci### isLast 1298e41f4b71Sopenharmony_ci 1299e41f4b71Sopenharmony_ciisLast(): boolean 1300e41f4b71Sopenharmony_ci 1301e41f4b71Sopenharmony_ciChecks whether the data read position is the last row. 1302e41f4b71Sopenharmony_ci 1303e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1304e41f4b71Sopenharmony_ci 1305e41f4b71Sopenharmony_ci**Return value** 1306e41f4b71Sopenharmony_ci 1307e41f4b71Sopenharmony_ci| Type | Description | 1308e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | 1309e41f4b71Sopenharmony_ci| boolean | Returns **true** if the last row is being read; returns **false** otherwise.| 1310e41f4b71Sopenharmony_ci 1311e41f4b71Sopenharmony_ci**Example** 1312e41f4b71Sopenharmony_ci 1313e41f4b71Sopenharmony_ci```ts 1314e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1315e41f4b71Sopenharmony_ci 1316e41f4b71Sopenharmony_citry { 1317e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1318e41f4b71Sopenharmony_ci let islast: boolean; 1319e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1320e41f4b71Sopenharmony_ci console.info('getResultSet succeed.'); 1321e41f4b71Sopenharmony_ci resultSet = result; 1322e41f4b71Sopenharmony_ci islast = resultSet.isLast(); 1323e41f4b71Sopenharmony_ci console.info("Check isLast succeed: " + islast); 1324e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1325e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 1326e41f4b71Sopenharmony_ci }); 1327e41f4b71Sopenharmony_ci} catch (e) { 1328e41f4b71Sopenharmony_ci console.error("Check isLast failed: " + e); 1329e41f4b71Sopenharmony_ci} 1330e41f4b71Sopenharmony_ci``` 1331e41f4b71Sopenharmony_ci 1332e41f4b71Sopenharmony_ci### isBeforeFirst 1333e41f4b71Sopenharmony_ci 1334e41f4b71Sopenharmony_ciisBeforeFirst(): boolean 1335e41f4b71Sopenharmony_ci 1336e41f4b71Sopenharmony_ciChecks whether the data read position is before the first row. 1337e41f4b71Sopenharmony_ci 1338e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1339e41f4b71Sopenharmony_ci 1340e41f4b71Sopenharmony_ci**Return value** 1341e41f4b71Sopenharmony_ci 1342e41f4b71Sopenharmony_ci| Type | Description | 1343e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | 1344e41f4b71Sopenharmony_ci| boolean | Returns **true** if the data read position is before the first row; returns **false** otherwise.| 1345e41f4b71Sopenharmony_ci 1346e41f4b71Sopenharmony_ci**Example** 1347e41f4b71Sopenharmony_ci 1348e41f4b71Sopenharmony_ci```ts 1349e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1350e41f4b71Sopenharmony_ci 1351e41f4b71Sopenharmony_citry { 1352e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1353e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1354e41f4b71Sopenharmony_ci console.info('getResultSet succeed.'); 1355e41f4b71Sopenharmony_ci resultSet = result; 1356e41f4b71Sopenharmony_ci let isbeforefirst = resultSet.isBeforeFirst(); 1357e41f4b71Sopenharmony_ci console.info("Check isBeforeFirst succeed: " + isbeforefirst); 1358e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1359e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 1360e41f4b71Sopenharmony_ci }); 1361e41f4b71Sopenharmony_ci} catch (e) { 1362e41f4b71Sopenharmony_ci console.error("Check isBeforeFirst failed: " + e); 1363e41f4b71Sopenharmony_ci} 1364e41f4b71Sopenharmony_ci``` 1365e41f4b71Sopenharmony_ci 1366e41f4b71Sopenharmony_ci### isAfterLast 1367e41f4b71Sopenharmony_ci 1368e41f4b71Sopenharmony_ciisAfterLast(): boolean 1369e41f4b71Sopenharmony_ci 1370e41f4b71Sopenharmony_ciChecks whether the data read position is after the last row. 1371e41f4b71Sopenharmony_ci 1372e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1373e41f4b71Sopenharmony_ci 1374e41f4b71Sopenharmony_ci**Return value** 1375e41f4b71Sopenharmony_ci 1376e41f4b71Sopenharmony_ci| Type | Description | 1377e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | 1378e41f4b71Sopenharmony_ci| boolean | Returns **true** if the data read position is after the last row; returns **false** otherwise.| 1379e41f4b71Sopenharmony_ci 1380e41f4b71Sopenharmony_ci**Example** 1381e41f4b71Sopenharmony_ci 1382e41f4b71Sopenharmony_ci```ts 1383e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1384e41f4b71Sopenharmony_ci 1385e41f4b71Sopenharmony_citry { 1386e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1387e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1388e41f4b71Sopenharmony_ci console.info('getResultSet succeed.'); 1389e41f4b71Sopenharmony_ci resultSet = result; 1390e41f4b71Sopenharmony_ci let isafterlast = resultSet.isAfterLast(); 1391e41f4b71Sopenharmony_ci console.info("Check isAfterLast succeed:" + isafterlast); 1392e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1393e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 1394e41f4b71Sopenharmony_ci }); 1395e41f4b71Sopenharmony_ci} catch (e) { 1396e41f4b71Sopenharmony_ci console.error("Check isAfterLast failed: " + e); 1397e41f4b71Sopenharmony_ci} 1398e41f4b71Sopenharmony_ci``` 1399e41f4b71Sopenharmony_ci 1400e41f4b71Sopenharmony_ci### getEntry 1401e41f4b71Sopenharmony_ci 1402e41f4b71Sopenharmony_cigetEntry(): Entry 1403e41f4b71Sopenharmony_ci 1404e41f4b71Sopenharmony_ciObtains the KV pair from the current position. 1405e41f4b71Sopenharmony_ci 1406e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1407e41f4b71Sopenharmony_ci 1408e41f4b71Sopenharmony_ci**Return value** 1409e41f4b71Sopenharmony_ci 1410e41f4b71Sopenharmony_ci| Type | Description | 1411e41f4b71Sopenharmony_ci| --------------- | ------------ | 1412e41f4b71Sopenharmony_ci| [Entry](#entry) | KV pair obtained.| 1413e41f4b71Sopenharmony_ci 1414e41f4b71Sopenharmony_ci**Example** 1415e41f4b71Sopenharmony_ci 1416e41f4b71Sopenharmony_ci```ts 1417e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1418e41f4b71Sopenharmony_ci 1419e41f4b71Sopenharmony_citry { 1420e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 1421e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 1422e41f4b71Sopenharmony_ci console.info('getResultSet succeed.'); 1423e41f4b71Sopenharmony_ci resultSet = result; 1424e41f4b71Sopenharmony_ci let entry = resultSet.getEntry(); 1425e41f4b71Sopenharmony_ci console.info("getEntry succeed:" + JSON.stringify(entry)); 1426e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1427e41f4b71Sopenharmony_ci console.error('getResultSet failed: ' + err); 1428e41f4b71Sopenharmony_ci }); 1429e41f4b71Sopenharmony_ci} catch (e) { 1430e41f4b71Sopenharmony_ci console.error("getEntry failed: " + e); 1431e41f4b71Sopenharmony_ci} 1432e41f4b71Sopenharmony_ci``` 1433e41f4b71Sopenharmony_ci 1434e41f4b71Sopenharmony_ci## Query 1435e41f4b71Sopenharmony_ci 1436e41f4b71Sopenharmony_ciProvides methods to create a **Query** object, which defines different data query criteria. A **Query** object supports a maximum of 256 predicates. 1437e41f4b71Sopenharmony_ci 1438e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1439e41f4b71Sopenharmony_ci 1440e41f4b71Sopenharmony_ci### constructor 1441e41f4b71Sopenharmony_ci 1442e41f4b71Sopenharmony_ciconstructor() 1443e41f4b71Sopenharmony_ci 1444e41f4b71Sopenharmony_ciA constructor used to create a **Query** instance. 1445e41f4b71Sopenharmony_ci 1446e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1447e41f4b71Sopenharmony_ci 1448e41f4b71Sopenharmony_ci### reset 1449e41f4b71Sopenharmony_ci 1450e41f4b71Sopenharmony_cireset(): Query 1451e41f4b71Sopenharmony_ci 1452e41f4b71Sopenharmony_ciResets the **Query** object. 1453e41f4b71Sopenharmony_ci 1454e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1455e41f4b71Sopenharmony_ci 1456e41f4b71Sopenharmony_ci**Return value** 1457e41f4b71Sopenharmony_ci 1458e41f4b71Sopenharmony_ci| Type | Description | 1459e41f4b71Sopenharmony_ci| -------------- | --------------------- | 1460e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object reset.| 1461e41f4b71Sopenharmony_ci 1462e41f4b71Sopenharmony_ci**Example** 1463e41f4b71Sopenharmony_ci 1464e41f4b71Sopenharmony_ci```ts 1465e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1466e41f4b71Sopenharmony_ci 1467e41f4b71Sopenharmony_citry { 1468e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1469e41f4b71Sopenharmony_ci query.equalTo("key", "value"); 1470e41f4b71Sopenharmony_ci console.info("query is " + query.getSqlLike()); 1471e41f4b71Sopenharmony_ci query.reset(); 1472e41f4b71Sopenharmony_ci console.info("query is " + query.getSqlLike()); 1473e41f4b71Sopenharmony_ci query = null; 1474e41f4b71Sopenharmony_ci} catch (e) { 1475e41f4b71Sopenharmony_ci console.error("simply calls should be ok :" + e); 1476e41f4b71Sopenharmony_ci} 1477e41f4b71Sopenharmony_ci``` 1478e41f4b71Sopenharmony_ci 1479e41f4b71Sopenharmony_ci### equalTo 1480e41f4b71Sopenharmony_ci 1481e41f4b71Sopenharmony_ciequalTo(field: string, value: number|string|boolean): Query 1482e41f4b71Sopenharmony_ci 1483e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is equal to the given value. 1484e41f4b71Sopenharmony_ci 1485e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1486e41f4b71Sopenharmony_ci 1487e41f4b71Sopenharmony_ci**Parameters** 1488e41f4b71Sopenharmony_ci 1489e41f4b71Sopenharmony_ci| Name | Type| Mandatory | Description | 1490e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ----------------------- | 1491e41f4b71Sopenharmony_ci| field | string | Yes |Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 1492e41f4b71Sopenharmony_ci| value | number\|string\|boolean | Yes | Value specified.| 1493e41f4b71Sopenharmony_ci 1494e41f4b71Sopenharmony_ci**Return value** 1495e41f4b71Sopenharmony_ci 1496e41f4b71Sopenharmony_ci| Type | Description | 1497e41f4b71Sopenharmony_ci| -------------- | --------------- | 1498e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1499e41f4b71Sopenharmony_ci 1500e41f4b71Sopenharmony_ci**Error codes** 1501e41f4b71Sopenharmony_ci 1502e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1503e41f4b71Sopenharmony_ci 1504e41f4b71Sopenharmony_ci| ID| **Error Message**| 1505e41f4b71Sopenharmony_ci| ------------ | ------------ | 1506e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1507e41f4b71Sopenharmony_ci 1508e41f4b71Sopenharmony_ci**Example** 1509e41f4b71Sopenharmony_ci 1510e41f4b71Sopenharmony_ci```ts 1511e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1512e41f4b71Sopenharmony_ci 1513e41f4b71Sopenharmony_citry { 1514e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1515e41f4b71Sopenharmony_ci query.equalTo("field", "value"); 1516e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1517e41f4b71Sopenharmony_ci query = null; 1518e41f4b71Sopenharmony_ci} catch (e) { 1519e41f4b71Sopenharmony_ci let error = e as BusinessError; 1520e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1521e41f4b71Sopenharmony_ci} 1522e41f4b71Sopenharmony_ci``` 1523e41f4b71Sopenharmony_ci 1524e41f4b71Sopenharmony_ci### notEqualTo 1525e41f4b71Sopenharmony_ci 1526e41f4b71Sopenharmony_cinotEqualTo(field: string, value: number|string|boolean): Query 1527e41f4b71Sopenharmony_ci 1528e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is not equal to the specified value. 1529e41f4b71Sopenharmony_ci 1530e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1531e41f4b71Sopenharmony_ci 1532e41f4b71Sopenharmony_ci**Parameters** 1533e41f4b71Sopenharmony_ci 1534e41f4b71Sopenharmony_ci| Name | Type| Mandatory | Description | 1535e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ----------------------- | 1536e41f4b71Sopenharmony_ci| field | string | Yes |Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned. | 1537e41f4b71Sopenharmony_ci| value | number\|string\|boolean | Yes | Value specified.| 1538e41f4b71Sopenharmony_ci 1539e41f4b71Sopenharmony_ci**Return value** 1540e41f4b71Sopenharmony_ci 1541e41f4b71Sopenharmony_ci| Type | Description | 1542e41f4b71Sopenharmony_ci| -------------- | --------------- | 1543e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1544e41f4b71Sopenharmony_ci 1545e41f4b71Sopenharmony_ci**Error codes** 1546e41f4b71Sopenharmony_ci 1547e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1548e41f4b71Sopenharmony_ci 1549e41f4b71Sopenharmony_ci| ID| **Error Message**| 1550e41f4b71Sopenharmony_ci| ------------ | ------------ | 1551e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1552e41f4b71Sopenharmony_ci 1553e41f4b71Sopenharmony_ci**Example** 1554e41f4b71Sopenharmony_ci 1555e41f4b71Sopenharmony_ci```ts 1556e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1557e41f4b71Sopenharmony_ci 1558e41f4b71Sopenharmony_citry { 1559e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1560e41f4b71Sopenharmony_ci query.notEqualTo("field", "value"); 1561e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1562e41f4b71Sopenharmony_ci query = null; 1563e41f4b71Sopenharmony_ci} catch (e) { 1564e41f4b71Sopenharmony_ci let error = e as BusinessError; 1565e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1566e41f4b71Sopenharmony_ci} 1567e41f4b71Sopenharmony_ci``` 1568e41f4b71Sopenharmony_ci 1569e41f4b71Sopenharmony_ci### greaterThan 1570e41f4b71Sopenharmony_ci 1571e41f4b71Sopenharmony_cigreaterThan(field: string, value: number|string|boolean): Query 1572e41f4b71Sopenharmony_ci 1573e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is greater than the specified value. 1574e41f4b71Sopenharmony_ci 1575e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1576e41f4b71Sopenharmony_ci 1577e41f4b71Sopenharmony_ci**Parameters** 1578e41f4b71Sopenharmony_ci| Name | Type| Mandatory | Description | 1579e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ----------------------- | 1580e41f4b71Sopenharmony_ci| field | string | Yes |Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned. | 1581e41f4b71Sopenharmony_ci| value | number\|string\|boolean | Yes | Value specified.| 1582e41f4b71Sopenharmony_ci 1583e41f4b71Sopenharmony_ci**Return value** 1584e41f4b71Sopenharmony_ci 1585e41f4b71Sopenharmony_ci| Type | Description | 1586e41f4b71Sopenharmony_ci| -------------- | --------------- | 1587e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1588e41f4b71Sopenharmony_ci 1589e41f4b71Sopenharmony_ci**Error codes** 1590e41f4b71Sopenharmony_ci 1591e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1592e41f4b71Sopenharmony_ci 1593e41f4b71Sopenharmony_ci| ID| **Error Message**| 1594e41f4b71Sopenharmony_ci| ------------ | ------------ | 1595e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1596e41f4b71Sopenharmony_ci 1597e41f4b71Sopenharmony_ci**Example** 1598e41f4b71Sopenharmony_ci 1599e41f4b71Sopenharmony_ci```ts 1600e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1601e41f4b71Sopenharmony_ci 1602e41f4b71Sopenharmony_citry { 1603e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1604e41f4b71Sopenharmony_ci query.greaterThan("field", "value"); 1605e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1606e41f4b71Sopenharmony_ci query = null; 1607e41f4b71Sopenharmony_ci} catch (e) { 1608e41f4b71Sopenharmony_ci let error = e as BusinessError; 1609e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1610e41f4b71Sopenharmony_ci} 1611e41f4b71Sopenharmony_ci``` 1612e41f4b71Sopenharmony_ci 1613e41f4b71Sopenharmony_ci### lessThan 1614e41f4b71Sopenharmony_ci 1615e41f4b71Sopenharmony_cilessThan(field: string, value: number|string): Query 1616e41f4b71Sopenharmony_ci 1617e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is less than the specified value. 1618e41f4b71Sopenharmony_ci 1619e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1620e41f4b71Sopenharmony_ci 1621e41f4b71Sopenharmony_ci**Parameters** 1622e41f4b71Sopenharmony_ci 1623e41f4b71Sopenharmony_ci 1624e41f4b71Sopenharmony_ci| Name | Type| Mandatory | Description | 1625e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ----------------------- | 1626e41f4b71Sopenharmony_ci| field | string | Yes |Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned. | 1627e41f4b71Sopenharmony_ci| value | number\|string | Yes | Value specified.| 1628e41f4b71Sopenharmony_ci 1629e41f4b71Sopenharmony_ci**Return value** 1630e41f4b71Sopenharmony_ci 1631e41f4b71Sopenharmony_ci| Type | Description | 1632e41f4b71Sopenharmony_ci| -------------- | --------------- | 1633e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1634e41f4b71Sopenharmony_ci 1635e41f4b71Sopenharmony_ci**Error codes** 1636e41f4b71Sopenharmony_ci 1637e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1638e41f4b71Sopenharmony_ci 1639e41f4b71Sopenharmony_ci| ID| **Error Message**| 1640e41f4b71Sopenharmony_ci| ------------ | ------------ | 1641e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1642e41f4b71Sopenharmony_ci 1643e41f4b71Sopenharmony_ci**Example** 1644e41f4b71Sopenharmony_ci 1645e41f4b71Sopenharmony_ci```ts 1646e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1647e41f4b71Sopenharmony_ci 1648e41f4b71Sopenharmony_citry { 1649e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1650e41f4b71Sopenharmony_ci query.lessThan("field", "value"); 1651e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1652e41f4b71Sopenharmony_ci query = null; 1653e41f4b71Sopenharmony_ci} catch (e) { 1654e41f4b71Sopenharmony_ci let error = e as BusinessError; 1655e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1656e41f4b71Sopenharmony_ci} 1657e41f4b71Sopenharmony_ci``` 1658e41f4b71Sopenharmony_ci 1659e41f4b71Sopenharmony_ci### greaterThanOrEqualTo 1660e41f4b71Sopenharmony_ci 1661e41f4b71Sopenharmony_cigreaterThanOrEqualTo(field: string, value: number|string): Query 1662e41f4b71Sopenharmony_ci 1663e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is greater than or equal to the specified value. 1664e41f4b71Sopenharmony_ci 1665e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1666e41f4b71Sopenharmony_ci 1667e41f4b71Sopenharmony_ci**Parameters** 1668e41f4b71Sopenharmony_ci 1669e41f4b71Sopenharmony_ci 1670e41f4b71Sopenharmony_ci| Name | Type| Mandatory | Description | 1671e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ----------------------- | 1672e41f4b71Sopenharmony_ci| field | string | Yes |Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned. | 1673e41f4b71Sopenharmony_ci| value | number\|string | Yes | Value specified.| 1674e41f4b71Sopenharmony_ci 1675e41f4b71Sopenharmony_ci**Return value** 1676e41f4b71Sopenharmony_ci 1677e41f4b71Sopenharmony_ci| Type | Description | 1678e41f4b71Sopenharmony_ci| -------------- | --------------- | 1679e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1680e41f4b71Sopenharmony_ci 1681e41f4b71Sopenharmony_ci**Error codes** 1682e41f4b71Sopenharmony_ci 1683e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1684e41f4b71Sopenharmony_ci 1685e41f4b71Sopenharmony_ci| ID| **Error Message**| 1686e41f4b71Sopenharmony_ci| ------------ | ------------ | 1687e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1688e41f4b71Sopenharmony_ci 1689e41f4b71Sopenharmony_ci**Example** 1690e41f4b71Sopenharmony_ci 1691e41f4b71Sopenharmony_ci```ts 1692e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1693e41f4b71Sopenharmony_ci 1694e41f4b71Sopenharmony_citry { 1695e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1696e41f4b71Sopenharmony_ci query.greaterThanOrEqualTo("field", "value"); 1697e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1698e41f4b71Sopenharmony_ci query = null; 1699e41f4b71Sopenharmony_ci} catch (e) { 1700e41f4b71Sopenharmony_ci let error = e as BusinessError; 1701e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1702e41f4b71Sopenharmony_ci} 1703e41f4b71Sopenharmony_ci``` 1704e41f4b71Sopenharmony_ci 1705e41f4b71Sopenharmony_ci### lessThanOrEqualTo 1706e41f4b71Sopenharmony_ci 1707e41f4b71Sopenharmony_cilessThanOrEqualTo(field: string, value: number|string): Query 1708e41f4b71Sopenharmony_ci 1709e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is less than or equal to the specified value. 1710e41f4b71Sopenharmony_ci 1711e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1712e41f4b71Sopenharmony_ci 1713e41f4b71Sopenharmony_ci**Parameters** 1714e41f4b71Sopenharmony_ci 1715e41f4b71Sopenharmony_ci 1716e41f4b71Sopenharmony_ci| Name | Type| Mandatory | Description | 1717e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ----------------------- | 1718e41f4b71Sopenharmony_ci| field | string | Yes |Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned. | 1719e41f4b71Sopenharmony_ci| value | number\|string | Yes | Value specified.| 1720e41f4b71Sopenharmony_ci 1721e41f4b71Sopenharmony_ci**Return value** 1722e41f4b71Sopenharmony_ci 1723e41f4b71Sopenharmony_ci| Type | Description | 1724e41f4b71Sopenharmony_ci| -------------- | --------------- | 1725e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1726e41f4b71Sopenharmony_ci 1727e41f4b71Sopenharmony_ci**Error codes** 1728e41f4b71Sopenharmony_ci 1729e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1730e41f4b71Sopenharmony_ci 1731e41f4b71Sopenharmony_ci| ID| **Error Message**| 1732e41f4b71Sopenharmony_ci| ------------ | ------------ | 1733e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1734e41f4b71Sopenharmony_ci 1735e41f4b71Sopenharmony_ci**Example** 1736e41f4b71Sopenharmony_ci 1737e41f4b71Sopenharmony_ci```ts 1738e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1739e41f4b71Sopenharmony_ci 1740e41f4b71Sopenharmony_citry { 1741e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1742e41f4b71Sopenharmony_ci query.lessThanOrEqualTo("field", "value"); 1743e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1744e41f4b71Sopenharmony_ci query = null; 1745e41f4b71Sopenharmony_ci} catch (e) { 1746e41f4b71Sopenharmony_ci let error = e as BusinessError; 1747e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1748e41f4b71Sopenharmony_ci} 1749e41f4b71Sopenharmony_ci``` 1750e41f4b71Sopenharmony_ci 1751e41f4b71Sopenharmony_ci### isNull 1752e41f4b71Sopenharmony_ci 1753e41f4b71Sopenharmony_ciisNull(field: string): Query 1754e41f4b71Sopenharmony_ci 1755e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is **null**. 1756e41f4b71Sopenharmony_ci 1757e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1758e41f4b71Sopenharmony_ci 1759e41f4b71Sopenharmony_ci**Parameters** 1760e41f4b71Sopenharmony_ci 1761e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 1762e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ----------------------------- | 1763e41f4b71Sopenharmony_ci| field | string | Yes | Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 1764e41f4b71Sopenharmony_ci 1765e41f4b71Sopenharmony_ci**Return value** 1766e41f4b71Sopenharmony_ci 1767e41f4b71Sopenharmony_ci| Type | Description | 1768e41f4b71Sopenharmony_ci| -------------- | --------------- | 1769e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1770e41f4b71Sopenharmony_ci 1771e41f4b71Sopenharmony_ci**Error codes** 1772e41f4b71Sopenharmony_ci 1773e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1774e41f4b71Sopenharmony_ci 1775e41f4b71Sopenharmony_ci| ID| **Error Message**| 1776e41f4b71Sopenharmony_ci| ------------ | ------------ | 1777e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1778e41f4b71Sopenharmony_ci 1779e41f4b71Sopenharmony_ci**Example** 1780e41f4b71Sopenharmony_ci 1781e41f4b71Sopenharmony_ci```ts 1782e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1783e41f4b71Sopenharmony_ci 1784e41f4b71Sopenharmony_citry { 1785e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1786e41f4b71Sopenharmony_ci query.isNull("field"); 1787e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1788e41f4b71Sopenharmony_ci query = null; 1789e41f4b71Sopenharmony_ci} catch (e) { 1790e41f4b71Sopenharmony_ci let error = e as BusinessError; 1791e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1792e41f4b71Sopenharmony_ci} 1793e41f4b71Sopenharmony_ci``` 1794e41f4b71Sopenharmony_ci 1795e41f4b71Sopenharmony_ci### inNumber 1796e41f4b71Sopenharmony_ci 1797e41f4b71Sopenharmony_ciinNumber(field: string, valueList: number[]): Query 1798e41f4b71Sopenharmony_ci 1799e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is within the specified list of numbers. 1800e41f4b71Sopenharmony_ci 1801e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1802e41f4b71Sopenharmony_ci 1803e41f4b71Sopenharmony_ci**Parameters** 1804e41f4b71Sopenharmony_ci 1805e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 1806e41f4b71Sopenharmony_ci| --------- | -------- | ---- | ----------------------------- | 1807e41f4b71Sopenharmony_ci| field | string | Yes | Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 1808e41f4b71Sopenharmony_ci| valueList | number[] | Yes | List of numbers. | 1809e41f4b71Sopenharmony_ci 1810e41f4b71Sopenharmony_ci**Return value** 1811e41f4b71Sopenharmony_ci 1812e41f4b71Sopenharmony_ci| Type | Description | 1813e41f4b71Sopenharmony_ci| -------------- | --------------- | 1814e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1815e41f4b71Sopenharmony_ci 1816e41f4b71Sopenharmony_ci**Error codes** 1817e41f4b71Sopenharmony_ci 1818e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1819e41f4b71Sopenharmony_ci 1820e41f4b71Sopenharmony_ci| ID| **Error Message**| 1821e41f4b71Sopenharmony_ci| ------------ | ------------ | 1822e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1823e41f4b71Sopenharmony_ci 1824e41f4b71Sopenharmony_ci**Example** 1825e41f4b71Sopenharmony_ci 1826e41f4b71Sopenharmony_ci```ts 1827e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1828e41f4b71Sopenharmony_ci 1829e41f4b71Sopenharmony_citry { 1830e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1831e41f4b71Sopenharmony_ci query.inNumber("field", [0, 1]); 1832e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1833e41f4b71Sopenharmony_ci query = null; 1834e41f4b71Sopenharmony_ci} catch (e) { 1835e41f4b71Sopenharmony_ci let error = e as BusinessError; 1836e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1837e41f4b71Sopenharmony_ci} 1838e41f4b71Sopenharmony_ci``` 1839e41f4b71Sopenharmony_ci 1840e41f4b71Sopenharmony_ci### inString 1841e41f4b71Sopenharmony_ci 1842e41f4b71Sopenharmony_ciinString(field: string, valueList: string[]): Query 1843e41f4b71Sopenharmony_ci 1844e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is within the specified list of strings. 1845e41f4b71Sopenharmony_ci 1846e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1847e41f4b71Sopenharmony_ci 1848e41f4b71Sopenharmony_ci**Parameters** 1849e41f4b71Sopenharmony_ci 1850e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 1851e41f4b71Sopenharmony_ci| --------- | -------- | ---- | ----------------------------- | 1852e41f4b71Sopenharmony_ci| field | string | Yes | Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 1853e41f4b71Sopenharmony_ci| valueList | string[] | Yes | List of strings. | 1854e41f4b71Sopenharmony_ci 1855e41f4b71Sopenharmony_ci**Return value** 1856e41f4b71Sopenharmony_ci 1857e41f4b71Sopenharmony_ci| Type | Description | 1858e41f4b71Sopenharmony_ci| -------------- | --------------- | 1859e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1860e41f4b71Sopenharmony_ci 1861e41f4b71Sopenharmony_ci**Error codes** 1862e41f4b71Sopenharmony_ci 1863e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1864e41f4b71Sopenharmony_ci 1865e41f4b71Sopenharmony_ci| ID| **Error Message**| 1866e41f4b71Sopenharmony_ci| ------------ | ------------ | 1867e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1868e41f4b71Sopenharmony_ci 1869e41f4b71Sopenharmony_ci**Example** 1870e41f4b71Sopenharmony_ci 1871e41f4b71Sopenharmony_ci```ts 1872e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1873e41f4b71Sopenharmony_ci 1874e41f4b71Sopenharmony_citry { 1875e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1876e41f4b71Sopenharmony_ci query.inString("field", ['test1', 'test2']); 1877e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1878e41f4b71Sopenharmony_ci query = null; 1879e41f4b71Sopenharmony_ci} catch (e) { 1880e41f4b71Sopenharmony_ci let error = e as BusinessError; 1881e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1882e41f4b71Sopenharmony_ci} 1883e41f4b71Sopenharmony_ci``` 1884e41f4b71Sopenharmony_ci 1885e41f4b71Sopenharmony_ci### notInNumber 1886e41f4b71Sopenharmony_ci 1887e41f4b71Sopenharmony_cinotInNumber(field: string, valueList: number[]): Query 1888e41f4b71Sopenharmony_ci 1889e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is not within the specified list of numbers. 1890e41f4b71Sopenharmony_ci 1891e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1892e41f4b71Sopenharmony_ci 1893e41f4b71Sopenharmony_ci**Parameters** 1894e41f4b71Sopenharmony_ci 1895e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 1896e41f4b71Sopenharmony_ci| --------- | -------- | ---- | ----------------------------- | 1897e41f4b71Sopenharmony_ci| field | string | Yes | Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 1898e41f4b71Sopenharmony_ci| valueList | number[] | Yes | List of numbers. | 1899e41f4b71Sopenharmony_ci 1900e41f4b71Sopenharmony_ci**Return value** 1901e41f4b71Sopenharmony_ci 1902e41f4b71Sopenharmony_ci| Type | Description | 1903e41f4b71Sopenharmony_ci| -------------- | --------------- | 1904e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1905e41f4b71Sopenharmony_ci 1906e41f4b71Sopenharmony_ci**Error codes** 1907e41f4b71Sopenharmony_ci 1908e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1909e41f4b71Sopenharmony_ci 1910e41f4b71Sopenharmony_ci| ID| **Error Message**| 1911e41f4b71Sopenharmony_ci| ------------ | ------------ | 1912e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1913e41f4b71Sopenharmony_ci 1914e41f4b71Sopenharmony_ci**Example** 1915e41f4b71Sopenharmony_ci 1916e41f4b71Sopenharmony_ci```ts 1917e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1918e41f4b71Sopenharmony_ci 1919e41f4b71Sopenharmony_citry { 1920e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1921e41f4b71Sopenharmony_ci query.notInNumber("field", [0, 1]); 1922e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1923e41f4b71Sopenharmony_ci query = null; 1924e41f4b71Sopenharmony_ci} catch (e) { 1925e41f4b71Sopenharmony_ci let error = e as BusinessError; 1926e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1927e41f4b71Sopenharmony_ci} 1928e41f4b71Sopenharmony_ci``` 1929e41f4b71Sopenharmony_ci 1930e41f4b71Sopenharmony_ci### notInString 1931e41f4b71Sopenharmony_ci 1932e41f4b71Sopenharmony_cinotInString(field: string, valueList: string[]): Query 1933e41f4b71Sopenharmony_ci 1934e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is not within the specified list of strings. 1935e41f4b71Sopenharmony_ci 1936e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1937e41f4b71Sopenharmony_ci 1938e41f4b71Sopenharmony_ci**Parameters** 1939e41f4b71Sopenharmony_ci 1940e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 1941e41f4b71Sopenharmony_ci| --------- | -------- | ---- | ----------------------------- | 1942e41f4b71Sopenharmony_ci| field | string | Yes | Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 1943e41f4b71Sopenharmony_ci| valueList | string[] | Yes | List of strings. | 1944e41f4b71Sopenharmony_ci 1945e41f4b71Sopenharmony_ci**Return value** 1946e41f4b71Sopenharmony_ci 1947e41f4b71Sopenharmony_ci| Type | Description | 1948e41f4b71Sopenharmony_ci| -------------- | --------------- | 1949e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1950e41f4b71Sopenharmony_ci 1951e41f4b71Sopenharmony_ci**Error codes** 1952e41f4b71Sopenharmony_ci 1953e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1954e41f4b71Sopenharmony_ci 1955e41f4b71Sopenharmony_ci| ID| **Error Message**| 1956e41f4b71Sopenharmony_ci| ------------ | ------------ | 1957e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 1958e41f4b71Sopenharmony_ci 1959e41f4b71Sopenharmony_ci**Example** 1960e41f4b71Sopenharmony_ci 1961e41f4b71Sopenharmony_ci```ts 1962e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1963e41f4b71Sopenharmony_ci 1964e41f4b71Sopenharmony_citry { 1965e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 1966e41f4b71Sopenharmony_ci query.notInString("field", ['test1', 'test2']); 1967e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 1968e41f4b71Sopenharmony_ci query = null; 1969e41f4b71Sopenharmony_ci} catch (e) { 1970e41f4b71Sopenharmony_ci let error = e as BusinessError; 1971e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 1972e41f4b71Sopenharmony_ci} 1973e41f4b71Sopenharmony_ci``` 1974e41f4b71Sopenharmony_ci 1975e41f4b71Sopenharmony_ci### like 1976e41f4b71Sopenharmony_ci 1977e41f4b71Sopenharmony_cilike(field: string, value: string): Query 1978e41f4b71Sopenharmony_ci 1979e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is similar to the specified string. 1980e41f4b71Sopenharmony_ci 1981e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 1982e41f4b71Sopenharmony_ci 1983e41f4b71Sopenharmony_ci**Parameters** 1984e41f4b71Sopenharmony_ci 1985e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 1986e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ----------------------------- | 1987e41f4b71Sopenharmony_ci| field | string | Yes | Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 1988e41f4b71Sopenharmony_ci| value | string | Yes | String specified. | 1989e41f4b71Sopenharmony_ci 1990e41f4b71Sopenharmony_ci**Return value** 1991e41f4b71Sopenharmony_ci 1992e41f4b71Sopenharmony_ci| Type | Description | 1993e41f4b71Sopenharmony_ci| -------------- | --------------- | 1994e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 1995e41f4b71Sopenharmony_ci 1996e41f4b71Sopenharmony_ci**Error codes** 1997e41f4b71Sopenharmony_ci 1998e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 1999e41f4b71Sopenharmony_ci 2000e41f4b71Sopenharmony_ci| ID| **Error Message**| 2001e41f4b71Sopenharmony_ci| ------------ | ------------ | 2002e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 2003e41f4b71Sopenharmony_ci 2004e41f4b71Sopenharmony_ci**Example** 2005e41f4b71Sopenharmony_ci 2006e41f4b71Sopenharmony_ci```ts 2007e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2008e41f4b71Sopenharmony_ci 2009e41f4b71Sopenharmony_citry { 2010e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2011e41f4b71Sopenharmony_ci query.like("field", "value"); 2012e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 2013e41f4b71Sopenharmony_ci query = null; 2014e41f4b71Sopenharmony_ci} catch (e) { 2015e41f4b71Sopenharmony_ci let error = e as BusinessError; 2016e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 2017e41f4b71Sopenharmony_ci} 2018e41f4b71Sopenharmony_ci``` 2019e41f4b71Sopenharmony_ci 2020e41f4b71Sopenharmony_ci### unlike 2021e41f4b71Sopenharmony_ci 2022e41f4b71Sopenharmony_ciunlike(field: string, value: string): Query 2023e41f4b71Sopenharmony_ci 2024e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is not similar to the specified string. 2025e41f4b71Sopenharmony_ci 2026e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2027e41f4b71Sopenharmony_ci 2028e41f4b71Sopenharmony_ci**Parameters** 2029e41f4b71Sopenharmony_ci 2030e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 2031e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ----------------------------- | 2032e41f4b71Sopenharmony_ci| field | string | Yes | Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 2033e41f4b71Sopenharmony_ci| value | string | Yes | String specified. | 2034e41f4b71Sopenharmony_ci 2035e41f4b71Sopenharmony_ci**Return value** 2036e41f4b71Sopenharmony_ci 2037e41f4b71Sopenharmony_ci| Type | Description | 2038e41f4b71Sopenharmony_ci| -------------- | --------------- | 2039e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2040e41f4b71Sopenharmony_ci 2041e41f4b71Sopenharmony_ci**Error codes** 2042e41f4b71Sopenharmony_ci 2043e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2044e41f4b71Sopenharmony_ci 2045e41f4b71Sopenharmony_ci| ID| **Error Message**| 2046e41f4b71Sopenharmony_ci| ------------ | ------------ | 2047e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 2048e41f4b71Sopenharmony_ci 2049e41f4b71Sopenharmony_ci**Example** 2050e41f4b71Sopenharmony_ci 2051e41f4b71Sopenharmony_ci```ts 2052e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2053e41f4b71Sopenharmony_ci 2054e41f4b71Sopenharmony_citry { 2055e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2056e41f4b71Sopenharmony_ci query.unlike("field", "value"); 2057e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 2058e41f4b71Sopenharmony_ci query = null; 2059e41f4b71Sopenharmony_ci} catch (e) { 2060e41f4b71Sopenharmony_ci let error = e as BusinessError; 2061e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 2062e41f4b71Sopenharmony_ci} 2063e41f4b71Sopenharmony_ci``` 2064e41f4b71Sopenharmony_ci 2065e41f4b71Sopenharmony_ci### and 2066e41f4b71Sopenharmony_ci 2067e41f4b71Sopenharmony_ciand(): Query 2068e41f4b71Sopenharmony_ci 2069e41f4b71Sopenharmony_ciCreates a **Query** object with the AND condition. 2070e41f4b71Sopenharmony_ci 2071e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2072e41f4b71Sopenharmony_ci 2073e41f4b71Sopenharmony_ci**Return value** 2074e41f4b71Sopenharmony_ci 2075e41f4b71Sopenharmony_ci| Type | Description | 2076e41f4b71Sopenharmony_ci| -------------- | -------------- | 2077e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2078e41f4b71Sopenharmony_ci 2079e41f4b71Sopenharmony_ci**Example** 2080e41f4b71Sopenharmony_ci 2081e41f4b71Sopenharmony_ci```ts 2082e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2083e41f4b71Sopenharmony_ci 2084e41f4b71Sopenharmony_citry { 2085e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2086e41f4b71Sopenharmony_ci query.notEqualTo("field", "value1"); 2087e41f4b71Sopenharmony_ci query.and(); 2088e41f4b71Sopenharmony_ci query.notEqualTo("field", "value2"); 2089e41f4b71Sopenharmony_ci console.info("query is " + query.getSqlLike()); 2090e41f4b71Sopenharmony_ci query = null; 2091e41f4b71Sopenharmony_ci} catch (e) { 2092e41f4b71Sopenharmony_ci console.error("duplicated calls should be ok :" + e); 2093e41f4b71Sopenharmony_ci} 2094e41f4b71Sopenharmony_ci``` 2095e41f4b71Sopenharmony_ci 2096e41f4b71Sopenharmony_ci### or 2097e41f4b71Sopenharmony_ci 2098e41f4b71Sopenharmony_cior(): Query 2099e41f4b71Sopenharmony_ci 2100e41f4b71Sopenharmony_ciCreates a **Query** object with the OR condition. 2101e41f4b71Sopenharmony_ci 2102e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2103e41f4b71Sopenharmony_ci 2104e41f4b71Sopenharmony_ci**Return value** 2105e41f4b71Sopenharmony_ci 2106e41f4b71Sopenharmony_ci| Type | Description | 2107e41f4b71Sopenharmony_ci| -------------- | -------------- | 2108e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2109e41f4b71Sopenharmony_ci 2110e41f4b71Sopenharmony_ci**Example** 2111e41f4b71Sopenharmony_ci 2112e41f4b71Sopenharmony_ci```ts 2113e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2114e41f4b71Sopenharmony_ci 2115e41f4b71Sopenharmony_citry { 2116e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2117e41f4b71Sopenharmony_ci query.notEqualTo("field", "value1"); 2118e41f4b71Sopenharmony_ci query.or(); 2119e41f4b71Sopenharmony_ci query.notEqualTo("field", "value2"); 2120e41f4b71Sopenharmony_ci console.info("query is " + query.getSqlLike()); 2121e41f4b71Sopenharmony_ci query = null; 2122e41f4b71Sopenharmony_ci} catch (e) { 2123e41f4b71Sopenharmony_ci console.error("duplicated calls should be ok :" + e); 2124e41f4b71Sopenharmony_ci} 2125e41f4b71Sopenharmony_ci``` 2126e41f4b71Sopenharmony_ci 2127e41f4b71Sopenharmony_ci### orderByAsc 2128e41f4b71Sopenharmony_ci 2129e41f4b71Sopenharmony_ciorderByAsc(field: string): Query 2130e41f4b71Sopenharmony_ci 2131e41f4b71Sopenharmony_ciCreates a **Query** object to sort the query results in ascending order. 2132e41f4b71Sopenharmony_ci 2133e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2134e41f4b71Sopenharmony_ci 2135e41f4b71Sopenharmony_ci**Parameters** 2136e41f4b71Sopenharmony_ci 2137e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 2138e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ----------------------------- | 2139e41f4b71Sopenharmony_ci| field | string | Yes | Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 2140e41f4b71Sopenharmony_ci 2141e41f4b71Sopenharmony_ci**Return value** 2142e41f4b71Sopenharmony_ci 2143e41f4b71Sopenharmony_ci| Type | Description | 2144e41f4b71Sopenharmony_ci| -------------- | --------------- | 2145e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2146e41f4b71Sopenharmony_ci 2147e41f4b71Sopenharmony_ci**Error codes** 2148e41f4b71Sopenharmony_ci 2149e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2150e41f4b71Sopenharmony_ci 2151e41f4b71Sopenharmony_ci| ID| **Error Message**| 2152e41f4b71Sopenharmony_ci| ------------ | ------------ | 2153e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 2154e41f4b71Sopenharmony_ci 2155e41f4b71Sopenharmony_ci**Example** 2156e41f4b71Sopenharmony_ci 2157e41f4b71Sopenharmony_ci```ts 2158e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2159e41f4b71Sopenharmony_ci 2160e41f4b71Sopenharmony_citry { 2161e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2162e41f4b71Sopenharmony_ci query.notEqualTo("field", "value"); 2163e41f4b71Sopenharmony_ci query.orderByAsc("field"); 2164e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 2165e41f4b71Sopenharmony_ci query = null; 2166e41f4b71Sopenharmony_ci} catch (e) { 2167e41f4b71Sopenharmony_ci let error = e as BusinessError; 2168e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 2169e41f4b71Sopenharmony_ci} 2170e41f4b71Sopenharmony_ci``` 2171e41f4b71Sopenharmony_ci 2172e41f4b71Sopenharmony_ci### orderByDesc 2173e41f4b71Sopenharmony_ci 2174e41f4b71Sopenharmony_ciorderByDesc(field: string): Query 2175e41f4b71Sopenharmony_ci 2176e41f4b71Sopenharmony_ciCreates a **Query** object to sort the query results in descending order. 2177e41f4b71Sopenharmony_ci 2178e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2179e41f4b71Sopenharmony_ci 2180e41f4b71Sopenharmony_ci**Parameters** 2181e41f4b71Sopenharmony_ci 2182e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 2183e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ----------------------------- | 2184e41f4b71Sopenharmony_ci| field | string | Yes | Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 2185e41f4b71Sopenharmony_ci 2186e41f4b71Sopenharmony_ci**Return value** 2187e41f4b71Sopenharmony_ci 2188e41f4b71Sopenharmony_ci| Type | Description | 2189e41f4b71Sopenharmony_ci| -------------- | --------------- | 2190e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2191e41f4b71Sopenharmony_ci 2192e41f4b71Sopenharmony_ci**Error codes** 2193e41f4b71Sopenharmony_ci 2194e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2195e41f4b71Sopenharmony_ci 2196e41f4b71Sopenharmony_ci| ID| **Error Message**| 2197e41f4b71Sopenharmony_ci| ------------ | ------------ | 2198e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 2199e41f4b71Sopenharmony_ci 2200e41f4b71Sopenharmony_ci**Example** 2201e41f4b71Sopenharmony_ci 2202e41f4b71Sopenharmony_ci```ts 2203e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2204e41f4b71Sopenharmony_ci 2205e41f4b71Sopenharmony_citry { 2206e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2207e41f4b71Sopenharmony_ci query.notEqualTo("field", "value"); 2208e41f4b71Sopenharmony_ci query.orderByDesc("field"); 2209e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 2210e41f4b71Sopenharmony_ci query = null; 2211e41f4b71Sopenharmony_ci} catch (e) { 2212e41f4b71Sopenharmony_ci let error = e as BusinessError; 2213e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 2214e41f4b71Sopenharmony_ci} 2215e41f4b71Sopenharmony_ci``` 2216e41f4b71Sopenharmony_ci 2217e41f4b71Sopenharmony_ci### limit 2218e41f4b71Sopenharmony_ci 2219e41f4b71Sopenharmony_cilimit(total: number, offset: number): Query 2220e41f4b71Sopenharmony_ci 2221e41f4b71Sopenharmony_ciCreates a **Query** object to specify the number of records of the query result and where to start. This API must be called after the invocation of the **orderByAsc()**, **orderByDesc()**, and the query APIs of the **Query** object. 2222e41f4b71Sopenharmony_ci 2223e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2224e41f4b71Sopenharmony_ci 2225e41f4b71Sopenharmony_ci**Parameters** 2226e41f4b71Sopenharmony_ci 2227e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 2228e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------------ | 2229e41f4b71Sopenharmony_ci| total | number | Yes | Number of results to query.| 2230e41f4b71Sopenharmony_ci| offset | number | Yes | Start position for query. | 2231e41f4b71Sopenharmony_ci 2232e41f4b71Sopenharmony_ci**Return value** 2233e41f4b71Sopenharmony_ci 2234e41f4b71Sopenharmony_ci| Type | Description | 2235e41f4b71Sopenharmony_ci| -------------- | --------------- | 2236e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2237e41f4b71Sopenharmony_ci 2238e41f4b71Sopenharmony_ci**Error codes** 2239e41f4b71Sopenharmony_ci 2240e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2241e41f4b71Sopenharmony_ci 2242e41f4b71Sopenharmony_ci| ID| **Error Message**| 2243e41f4b71Sopenharmony_ci| ------------ | ------------ | 2244e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 2245e41f4b71Sopenharmony_ci 2246e41f4b71Sopenharmony_ci**Example** 2247e41f4b71Sopenharmony_ci 2248e41f4b71Sopenharmony_ci```ts 2249e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2250e41f4b71Sopenharmony_ci 2251e41f4b71Sopenharmony_cilet total = 10; 2252e41f4b71Sopenharmony_cilet offset = 1; 2253e41f4b71Sopenharmony_citry { 2254e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2255e41f4b71Sopenharmony_ci query.notEqualTo("field", "value"); 2256e41f4b71Sopenharmony_ci query.limit(total, offset); 2257e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 2258e41f4b71Sopenharmony_ci query = null; 2259e41f4b71Sopenharmony_ci} catch (e) { 2260e41f4b71Sopenharmony_ci let error = e as BusinessError; 2261e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 2262e41f4b71Sopenharmony_ci} 2263e41f4b71Sopenharmony_ci``` 2264e41f4b71Sopenharmony_ci 2265e41f4b71Sopenharmony_ci### isNotNull 2266e41f4b71Sopenharmony_ci 2267e41f4b71Sopenharmony_ciisNotNull(field: string): Query 2268e41f4b71Sopenharmony_ci 2269e41f4b71Sopenharmony_ciCreates a **Query** object to match the specified field whose value is not **null**. 2270e41f4b71Sopenharmony_ci 2271e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2272e41f4b71Sopenharmony_ci 2273e41f4b71Sopenharmony_ci**Parameters** 2274e41f4b71Sopenharmony_ci 2275e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 2276e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ----------------------------- | 2277e41f4b71Sopenharmony_ci| field | string | Yes | Field to match. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 2278e41f4b71Sopenharmony_ci 2279e41f4b71Sopenharmony_ci**Return value** 2280e41f4b71Sopenharmony_ci 2281e41f4b71Sopenharmony_ci| Type | Description | 2282e41f4b71Sopenharmony_ci| -------------- | --------------- | 2283e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2284e41f4b71Sopenharmony_ci 2285e41f4b71Sopenharmony_ci**Error codes** 2286e41f4b71Sopenharmony_ci 2287e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2288e41f4b71Sopenharmony_ci 2289e41f4b71Sopenharmony_ci| ID| **Error Message**| 2290e41f4b71Sopenharmony_ci| ------------ | ------------ | 2291e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2292e41f4b71Sopenharmony_ci 2293e41f4b71Sopenharmony_ci**Example** 2294e41f4b71Sopenharmony_ci 2295e41f4b71Sopenharmony_ci```ts 2296e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2297e41f4b71Sopenharmony_ci 2298e41f4b71Sopenharmony_citry { 2299e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2300e41f4b71Sopenharmony_ci query.isNotNull("field"); 2301e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 2302e41f4b71Sopenharmony_ci query = null; 2303e41f4b71Sopenharmony_ci} catch (e) { 2304e41f4b71Sopenharmony_ci let error = e as BusinessError; 2305e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 2306e41f4b71Sopenharmony_ci} 2307e41f4b71Sopenharmony_ci``` 2308e41f4b71Sopenharmony_ci 2309e41f4b71Sopenharmony_ci### beginGroup 2310e41f4b71Sopenharmony_ci 2311e41f4b71Sopenharmony_cibeginGroup(): Query 2312e41f4b71Sopenharmony_ci 2313e41f4b71Sopenharmony_ciCreates a **Query** object for a query condition group with a left parenthesis. 2314e41f4b71Sopenharmony_ci 2315e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2316e41f4b71Sopenharmony_ci 2317e41f4b71Sopenharmony_ci**Return value** 2318e41f4b71Sopenharmony_ci 2319e41f4b71Sopenharmony_ci| Type | Description | 2320e41f4b71Sopenharmony_ci| -------------- | --------------- | 2321e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2322e41f4b71Sopenharmony_ci 2323e41f4b71Sopenharmony_ci**Example** 2324e41f4b71Sopenharmony_ci 2325e41f4b71Sopenharmony_ci```ts 2326e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2327e41f4b71Sopenharmony_ci 2328e41f4b71Sopenharmony_citry { 2329e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2330e41f4b71Sopenharmony_ci query.beginGroup(); 2331e41f4b71Sopenharmony_ci query.isNotNull("field"); 2332e41f4b71Sopenharmony_ci query.endGroup(); 2333e41f4b71Sopenharmony_ci console.info("query is " + query.getSqlLike()); 2334e41f4b71Sopenharmony_ci query = null; 2335e41f4b71Sopenharmony_ci} catch (e) { 2336e41f4b71Sopenharmony_ci console.error("duplicated calls should be ok :" + e); 2337e41f4b71Sopenharmony_ci} 2338e41f4b71Sopenharmony_ci``` 2339e41f4b71Sopenharmony_ci 2340e41f4b71Sopenharmony_ci### endGroup 2341e41f4b71Sopenharmony_ci 2342e41f4b71Sopenharmony_ciendGroup(): Query 2343e41f4b71Sopenharmony_ci 2344e41f4b71Sopenharmony_ciCreates a **Query** object for a query condition group with a right parenthesis. 2345e41f4b71Sopenharmony_ci 2346e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2347e41f4b71Sopenharmony_ci 2348e41f4b71Sopenharmony_ci**Return value** 2349e41f4b71Sopenharmony_ci 2350e41f4b71Sopenharmony_ci| Type | Description | 2351e41f4b71Sopenharmony_ci| -------------- | --------------- | 2352e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2353e41f4b71Sopenharmony_ci 2354e41f4b71Sopenharmony_ci**Example** 2355e41f4b71Sopenharmony_ci 2356e41f4b71Sopenharmony_ci```ts 2357e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2358e41f4b71Sopenharmony_ci 2359e41f4b71Sopenharmony_citry { 2360e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2361e41f4b71Sopenharmony_ci query.beginGroup(); 2362e41f4b71Sopenharmony_ci query.isNotNull("field"); 2363e41f4b71Sopenharmony_ci query.endGroup(); 2364e41f4b71Sopenharmony_ci console.info("query is " + query.getSqlLike()); 2365e41f4b71Sopenharmony_ci query = null; 2366e41f4b71Sopenharmony_ci} catch (e) { 2367e41f4b71Sopenharmony_ci console.error("duplicated calls should be ok :" + e); 2368e41f4b71Sopenharmony_ci} 2369e41f4b71Sopenharmony_ci``` 2370e41f4b71Sopenharmony_ci 2371e41f4b71Sopenharmony_ci### prefixKey 2372e41f4b71Sopenharmony_ci 2373e41f4b71Sopenharmony_ciprefixKey(prefix: string): Query 2374e41f4b71Sopenharmony_ci 2375e41f4b71Sopenharmony_ciCreates a **Query** object with a specified key prefix. 2376e41f4b71Sopenharmony_ci 2377e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2378e41f4b71Sopenharmony_ci 2379e41f4b71Sopenharmony_ci**Parameters** 2380e41f4b71Sopenharmony_ci 2381e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 2382e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------------ | 2383e41f4b71Sopenharmony_ci| prefix | string | Yes | Key prefix, which cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 2384e41f4b71Sopenharmony_ci 2385e41f4b71Sopenharmony_ci**Return value** 2386e41f4b71Sopenharmony_ci 2387e41f4b71Sopenharmony_ci| Type | Description | 2388e41f4b71Sopenharmony_ci| -------------- | --------------- | 2389e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2390e41f4b71Sopenharmony_ci 2391e41f4b71Sopenharmony_ci**Error codes** 2392e41f4b71Sopenharmony_ci 2393e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2394e41f4b71Sopenharmony_ci 2395e41f4b71Sopenharmony_ci| ID| **Error Message**| 2396e41f4b71Sopenharmony_ci| ------------ | ------------ | 2397e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 2398e41f4b71Sopenharmony_ci 2399e41f4b71Sopenharmony_ci**Example** 2400e41f4b71Sopenharmony_ci 2401e41f4b71Sopenharmony_ci```ts 2402e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2403e41f4b71Sopenharmony_ci 2404e41f4b71Sopenharmony_citry { 2405e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2406e41f4b71Sopenharmony_ci query.prefixKey("$.name"); 2407e41f4b71Sopenharmony_ci query.prefixKey("0"); 2408e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 2409e41f4b71Sopenharmony_ci query = null; 2410e41f4b71Sopenharmony_ci} catch (e) { 2411e41f4b71Sopenharmony_ci let error = e as BusinessError; 2412e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 2413e41f4b71Sopenharmony_ci} 2414e41f4b71Sopenharmony_ci``` 2415e41f4b71Sopenharmony_ci 2416e41f4b71Sopenharmony_ci### setSuggestIndex 2417e41f4b71Sopenharmony_ci 2418e41f4b71Sopenharmony_cisetSuggestIndex(index: string): Query 2419e41f4b71Sopenharmony_ci 2420e41f4b71Sopenharmony_ciCreates a **Query** object with an index preferentially used for query. 2421e41f4b71Sopenharmony_ci 2422e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2423e41f4b71Sopenharmony_ci 2424e41f4b71Sopenharmony_ci**Parameters** 2425e41f4b71Sopenharmony_ci 2426e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 2427e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------------ | 2428e41f4b71Sopenharmony_ci| index | string | Yes | Index preferentially used for query. It cannot contain '^'. If the value contains '^', the predicate becomes invalid and all data in the KV store will be returned.| 2429e41f4b71Sopenharmony_ci 2430e41f4b71Sopenharmony_ci**Return value** 2431e41f4b71Sopenharmony_ci 2432e41f4b71Sopenharmony_ci| Type | Description | 2433e41f4b71Sopenharmony_ci| -------------- | --------------- | 2434e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2435e41f4b71Sopenharmony_ci 2436e41f4b71Sopenharmony_ci**Error codes** 2437e41f4b71Sopenharmony_ci 2438e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2439e41f4b71Sopenharmony_ci 2440e41f4b71Sopenharmony_ci| ID| **Error Message**| 2441e41f4b71Sopenharmony_ci| ------------ | ------------ | 2442e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 2443e41f4b71Sopenharmony_ci 2444e41f4b71Sopenharmony_ci**Example** 2445e41f4b71Sopenharmony_ci 2446e41f4b71Sopenharmony_ci```ts 2447e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2448e41f4b71Sopenharmony_ci 2449e41f4b71Sopenharmony_citry { 2450e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2451e41f4b71Sopenharmony_ci query.setSuggestIndex("$.name"); 2452e41f4b71Sopenharmony_ci query.setSuggestIndex("0"); 2453e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 2454e41f4b71Sopenharmony_ci query = null; 2455e41f4b71Sopenharmony_ci} catch (e) { 2456e41f4b71Sopenharmony_ci let error = e as BusinessError; 2457e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 2458e41f4b71Sopenharmony_ci} 2459e41f4b71Sopenharmony_ci``` 2460e41f4b71Sopenharmony_ci 2461e41f4b71Sopenharmony_ci### deviceId 2462e41f4b71Sopenharmony_ci 2463e41f4b71Sopenharmony_cideviceId(deviceId:string):Query 2464e41f4b71Sopenharmony_ci 2465e41f4b71Sopenharmony_ciCreates a **Query** object with the device ID as the key prefix. 2466e41f4b71Sopenharmony_ci> **NOTE** 2467e41f4b71Sopenharmony_ci> 2468e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 2469e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 2470e41f4b71Sopenharmony_ci 2471e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2472e41f4b71Sopenharmony_ci 2473e41f4b71Sopenharmony_ci**Parameters** 2474e41f4b71Sopenharmony_ci 2475e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 2476e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ------------------ | 2477e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the device to be queried. This parameter cannot be left empty.| 2478e41f4b71Sopenharmony_ci 2479e41f4b71Sopenharmony_ci**Return value** 2480e41f4b71Sopenharmony_ci 2481e41f4b71Sopenharmony_ci| Type | Description | 2482e41f4b71Sopenharmony_ci| -------------- | --------------- | 2483e41f4b71Sopenharmony_ci| [Query](#query) | **Query** object created.| 2484e41f4b71Sopenharmony_ci 2485e41f4b71Sopenharmony_ci**Error codes** 2486e41f4b71Sopenharmony_ci 2487e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 2488e41f4b71Sopenharmony_ci 2489e41f4b71Sopenharmony_ci| ID| **Error Message**| 2490e41f4b71Sopenharmony_ci| ------------ | ------------ | 2491e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 2492e41f4b71Sopenharmony_ci 2493e41f4b71Sopenharmony_ci**Example** 2494e41f4b71Sopenharmony_ci 2495e41f4b71Sopenharmony_ci```ts 2496e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2497e41f4b71Sopenharmony_ci 2498e41f4b71Sopenharmony_citry { 2499e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2500e41f4b71Sopenharmony_ci query.deviceId("deviceId"); 2501e41f4b71Sopenharmony_ci console.info(`query is ${query.getSqlLike()}`); 2502e41f4b71Sopenharmony_ci} catch (e) { 2503e41f4b71Sopenharmony_ci let error = e as BusinessError; 2504e41f4b71Sopenharmony_ci console.error(`duplicated calls should be ok.code is ${error.code},message is ${error.message}`); 2505e41f4b71Sopenharmony_ci} 2506e41f4b71Sopenharmony_ci``` 2507e41f4b71Sopenharmony_ci 2508e41f4b71Sopenharmony_ci### getSqlLike 2509e41f4b71Sopenharmony_ci 2510e41f4b71Sopenharmony_cigetSqlLike():string 2511e41f4b71Sopenharmony_ci 2512e41f4b71Sopenharmony_ciObtains the query statement of the **Query** object. 2513e41f4b71Sopenharmony_ci 2514e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2515e41f4b71Sopenharmony_ci 2516e41f4b71Sopenharmony_ci**Return value** 2517e41f4b71Sopenharmony_ci 2518e41f4b71Sopenharmony_ci| Type | Description | 2519e41f4b71Sopenharmony_ci| ------ | ------------------------------------ | 2520e41f4b71Sopenharmony_ci| string | Returns the query statement obtained.| 2521e41f4b71Sopenharmony_ci 2522e41f4b71Sopenharmony_ci**Example** 2523e41f4b71Sopenharmony_ci 2524e41f4b71Sopenharmony_ci```ts 2525e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2526e41f4b71Sopenharmony_ci 2527e41f4b71Sopenharmony_citry { 2528e41f4b71Sopenharmony_ci let query: distributedKVStore.Query | null = new distributedKVStore.Query(); 2529e41f4b71Sopenharmony_ci let sql1 = query.getSqlLike(); 2530e41f4b71Sopenharmony_ci console.info(`GetSqlLike sql= ${sql1}`); 2531e41f4b71Sopenharmony_ci} catch (e) { 2532e41f4b71Sopenharmony_ci console.error("duplicated calls should be ok : " + e); 2533e41f4b71Sopenharmony_ci} 2534e41f4b71Sopenharmony_ci``` 2535e41f4b71Sopenharmony_ci 2536e41f4b71Sopenharmony_ci## SingleKVStore 2537e41f4b71Sopenharmony_ci 2538e41f4b71Sopenharmony_ciImplements data management in a single KV store, such as adding data, deleting data, and subscribing to data changes or data sync completion. 2539e41f4b71Sopenharmony_ci 2540e41f4b71Sopenharmony_ciBefore calling any method in **SingleKVStore**, you must use [getKVStore](#getkvstore) to obtain a **SingleKVStore** instance. 2541e41f4b71Sopenharmony_ci 2542e41f4b71Sopenharmony_ci### put 2543e41f4b71Sopenharmony_ci 2544e41f4b71Sopenharmony_ciput(key: string, value: Uint8Array | string | number | boolean, callback: AsyncCallback<void>): void 2545e41f4b71Sopenharmony_ci 2546e41f4b71Sopenharmony_ciAdds a KV pair of the specified type to this KV store. This API uses an asynchronous callback to return the result. 2547e41f4b71Sopenharmony_ci 2548e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2549e41f4b71Sopenharmony_ci 2550e41f4b71Sopenharmony_ci**Parameters** 2551e41f4b71Sopenharmony_ci 2552e41f4b71Sopenharmony_ci| Name | Type| Mandatory | Description | 2553e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ----------------------- | 2554e41f4b71Sopenharmony_ci| key | string | Yes |Key of the KV pair to add. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants). | 2555e41f4b71Sopenharmony_ci| value | Uint8Array \| string \| number \| boolean | Yes |Value of the KV pair to add. The value type can be Uint8Array, number, string, or boolean. A value of the Uint8Array or string type cannot exceed [MAX_VALUE_LENGTH](#constants). | 2556e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes |Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 2557e41f4b71Sopenharmony_ci 2558e41f4b71Sopenharmony_ci**Error codes** 2559e41f4b71Sopenharmony_ci 2560e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 2561e41f4b71Sopenharmony_ci 2562e41f4b71Sopenharmony_ci| ID| **Error Message** | 2563e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- | 2564e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 2565e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 2566e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 2567e41f4b71Sopenharmony_ci 2568e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md). 2569e41f4b71Sopenharmony_ci 2570e41f4b71Sopenharmony_ci| ID| **Error Message** | 2571e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- | 2572e41f4b71Sopenharmony_ci| 14800047 | The WAL file size exceeds the default limit. | 2573e41f4b71Sopenharmony_ci 2574e41f4b71Sopenharmony_ci**Example** 2575e41f4b71Sopenharmony_ci 2576e41f4b71Sopenharmony_ci```ts 2577e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2578e41f4b71Sopenharmony_ci 2579e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string'; 2580e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 2581e41f4b71Sopenharmony_citry { 2582e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err: BusinessError) => { 2583e41f4b71Sopenharmony_ci if (err != undefined) { 2584e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 2585e41f4b71Sopenharmony_ci return; 2586e41f4b71Sopenharmony_ci } 2587e41f4b71Sopenharmony_ci console.info("Succeeded in putting"); 2588e41f4b71Sopenharmony_ci }); 2589e41f4b71Sopenharmony_ci} catch (e) { 2590e41f4b71Sopenharmony_ci let error = e as BusinessError; 2591e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 2592e41f4b71Sopenharmony_ci} 2593e41f4b71Sopenharmony_ci``` 2594e41f4b71Sopenharmony_ci 2595e41f4b71Sopenharmony_ci### put 2596e41f4b71Sopenharmony_ci 2597e41f4b71Sopenharmony_ciput(key: string, value: Uint8Array | string | number | boolean): Promise<void> 2598e41f4b71Sopenharmony_ci 2599e41f4b71Sopenharmony_ciAdds a KV pair of the specified type to this KV store. This API uses a promise to return the result. 2600e41f4b71Sopenharmony_ci 2601e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2602e41f4b71Sopenharmony_ci 2603e41f4b71Sopenharmony_ci**Parameters** 2604e41f4b71Sopenharmony_ci 2605e41f4b71Sopenharmony_ci| Name | Type| Mandatory | Description | 2606e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ----------------------- | 2607e41f4b71Sopenharmony_ci| key | string | Yes |Key of the KV pair to add. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants). | 2608e41f4b71Sopenharmony_ci| value | Uint8Array \| string \| number \| boolean | Yes |Value of the KV pair to add. The value type can be Uint8Array, number, string, or boolean. A value of the Uint8Array or string type cannot exceed [MAX_VALUE_LENGTH](#constants). | 2609e41f4b71Sopenharmony_ci 2610e41f4b71Sopenharmony_ci**Return value** 2611e41f4b71Sopenharmony_ci 2612e41f4b71Sopenharmony_ci| Type | Description | 2613e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 2614e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 2615e41f4b71Sopenharmony_ci 2616e41f4b71Sopenharmony_ci**Error codes** 2617e41f4b71Sopenharmony_ci 2618e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 2619e41f4b71Sopenharmony_ci 2620e41f4b71Sopenharmony_ci| ID| **Error Message** | 2621e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- | 2622e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 2623e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 2624e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 2625e41f4b71Sopenharmony_ci 2626e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md). 2627e41f4b71Sopenharmony_ci 2628e41f4b71Sopenharmony_ci| ID| **Error Message** | 2629e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- | 2630e41f4b71Sopenharmony_ci| 14800047 | The WAL file size exceeds the default limit. | 2631e41f4b71Sopenharmony_ci 2632e41f4b71Sopenharmony_ci**Example** 2633e41f4b71Sopenharmony_ci 2634e41f4b71Sopenharmony_ci```ts 2635e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2636e41f4b71Sopenharmony_ci 2637e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string'; 2638e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 2639e41f4b71Sopenharmony_citry { 2640e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(() => { 2641e41f4b71Sopenharmony_ci console.info(`Succeeded in putting data`); 2642e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2643e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 2644e41f4b71Sopenharmony_ci }); 2645e41f4b71Sopenharmony_ci} catch (e) { 2646e41f4b71Sopenharmony_ci let error = e as BusinessError; 2647e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 2648e41f4b71Sopenharmony_ci} 2649e41f4b71Sopenharmony_ci``` 2650e41f4b71Sopenharmony_ci 2651e41f4b71Sopenharmony_ci### putBatch 2652e41f4b71Sopenharmony_ci 2653e41f4b71Sopenharmony_ciputBatch(entries: Entry[], callback: AsyncCallback<void>): void 2654e41f4b71Sopenharmony_ci 2655e41f4b71Sopenharmony_ciBatch inserts KV pairs to this single KV store. This API uses an asynchronous callback to return the result. 2656e41f4b71Sopenharmony_ci 2657e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2658e41f4b71Sopenharmony_ci 2659e41f4b71Sopenharmony_ci**Parameters** 2660e41f4b71Sopenharmony_ci 2661e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2662e41f4b71Sopenharmony_ci| -------- | ------------------------ | ---- | ------------------------ | 2663e41f4b71Sopenharmony_ci| entries | [Entry](#entry)[] | Yes | KV pairs to insert, which cannot exceed 512 MB.| 2664e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 2665e41f4b71Sopenharmony_ci 2666e41f4b71Sopenharmony_ci**Error codes** 2667e41f4b71Sopenharmony_ci 2668e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 2669e41f4b71Sopenharmony_ci 2670e41f4b71Sopenharmony_ci| ID| **Error Message** | 2671e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- | 2672e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 2673e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 2674e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 2675e41f4b71Sopenharmony_ci 2676e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md). 2677e41f4b71Sopenharmony_ci 2678e41f4b71Sopenharmony_ci| ID| **Error Message** | 2679e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- | 2680e41f4b71Sopenharmony_ci| 14800047 | The WAL file size exceeds the default limit. | 2681e41f4b71Sopenharmony_ci 2682e41f4b71Sopenharmony_ci**Example** 2683e41f4b71Sopenharmony_ci 2684e41f4b71Sopenharmony_ci```ts 2685e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2686e41f4b71Sopenharmony_ci 2687e41f4b71Sopenharmony_citry { 2688e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 2689e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 2690e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 2691e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 2692e41f4b71Sopenharmony_ci key: key + i, 2693e41f4b71Sopenharmony_ci value: { 2694e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 2695e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 2696e41f4b71Sopenharmony_ci } 2697e41f4b71Sopenharmony_ci } 2698e41f4b71Sopenharmony_ci entries.push(entry); 2699e41f4b71Sopenharmony_ci } 2700e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 2701e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 2702e41f4b71Sopenharmony_ci if (err != undefined) { 2703e41f4b71Sopenharmony_ci console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 2704e41f4b71Sopenharmony_ci return; 2705e41f4b71Sopenharmony_ci } 2706e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 2707e41f4b71Sopenharmony_ci if (kvStore != null) { 2708e41f4b71Sopenharmony_ci kvStore.getEntries('batch_test_string_key', (err: BusinessError, entries: distributedKVStore.Entry[]) => { 2709e41f4b71Sopenharmony_ci if (err != undefined) { 2710e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 2711e41f4b71Sopenharmony_ci } 2712e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 2713e41f4b71Sopenharmony_ci console.info(`entries.length: ${entries.length}`); 2714e41f4b71Sopenharmony_ci console.info(`entries[0]: ${entries[0]}`); 2715e41f4b71Sopenharmony_ci }); 2716e41f4b71Sopenharmony_ci } else { 2717e41f4b71Sopenharmony_ci console.error('KvStore is null'); // The subsequent sample code is the same as the code here. 2718e41f4b71Sopenharmony_ci } 2719e41f4b71Sopenharmony_ci }); 2720e41f4b71Sopenharmony_ci} catch (e) { 2721e41f4b71Sopenharmony_ci let error = e as BusinessError; 2722e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message} `); 2723e41f4b71Sopenharmony_ci} 2724e41f4b71Sopenharmony_ci``` 2725e41f4b71Sopenharmony_ci 2726e41f4b71Sopenharmony_ci### putBatch 2727e41f4b71Sopenharmony_ci 2728e41f4b71Sopenharmony_ciputBatch(entries: Entry[]): Promise<void> 2729e41f4b71Sopenharmony_ci 2730e41f4b71Sopenharmony_ciBatch inserts KV pairs to this single KV store. This API uses a promise to return the result. 2731e41f4b71Sopenharmony_ci 2732e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2733e41f4b71Sopenharmony_ci 2734e41f4b71Sopenharmony_ci**Parameters** 2735e41f4b71Sopenharmony_ci 2736e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2737e41f4b71Sopenharmony_ci| ------- | ----------------- | ---- | ------------------------ | 2738e41f4b71Sopenharmony_ci| entries | [Entry](#entry)[] | Yes | KV pairs to insert, which cannot exceed 512 MB.| 2739e41f4b71Sopenharmony_ci 2740e41f4b71Sopenharmony_ci**Return value** 2741e41f4b71Sopenharmony_ci 2742e41f4b71Sopenharmony_ci| Type | Description | 2743e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 2744e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 2745e41f4b71Sopenharmony_ci 2746e41f4b71Sopenharmony_ci**Error codes** 2747e41f4b71Sopenharmony_ci 2748e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 2749e41f4b71Sopenharmony_ci 2750e41f4b71Sopenharmony_ci| ID| **Error Message** | 2751e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- | 2752e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 2753e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 2754e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 2755e41f4b71Sopenharmony_ci 2756e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md). 2757e41f4b71Sopenharmony_ci 2758e41f4b71Sopenharmony_ci| ID| **Error Message** | 2759e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- | 2760e41f4b71Sopenharmony_ci| 14800047 | The WAL file size exceeds the default limit. | 2761e41f4b71Sopenharmony_ci 2762e41f4b71Sopenharmony_ci**Example** 2763e41f4b71Sopenharmony_ci 2764e41f4b71Sopenharmony_ci```ts 2765e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2766e41f4b71Sopenharmony_ci 2767e41f4b71Sopenharmony_citry { 2768e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 2769e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 2770e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 2771e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 2772e41f4b71Sopenharmony_ci key: key + i, 2773e41f4b71Sopenharmony_ci value: { 2774e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 2775e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 2776e41f4b71Sopenharmony_ci } 2777e41f4b71Sopenharmony_ci } 2778e41f4b71Sopenharmony_ci entries.push(entry); 2779e41f4b71Sopenharmony_ci } 2780e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 2781e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 2782e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 2783e41f4b71Sopenharmony_ci if (kvStore != null) { 2784e41f4b71Sopenharmony_ci kvStore.getEntries('batch_test_string_key').then((entries: distributedKVStore.Entry[]) => { 2785e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 2786e41f4b71Sopenharmony_ci console.info(`PutBatch ${entries}`); 2787e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2788e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 2789e41f4b71Sopenharmony_ci }); 2790e41f4b71Sopenharmony_ci } 2791e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2792e41f4b71Sopenharmony_ci console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 2793e41f4b71Sopenharmony_ci }); 2794e41f4b71Sopenharmony_ci} catch (e) { 2795e41f4b71Sopenharmony_ci let error = e as BusinessError; 2796e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message} `); 2797e41f4b71Sopenharmony_ci} 2798e41f4b71Sopenharmony_ci``` 2799e41f4b71Sopenharmony_ci 2800e41f4b71Sopenharmony_ci### delete 2801e41f4b71Sopenharmony_ci 2802e41f4b71Sopenharmony_cidelete(key: string, callback: AsyncCallback<void>): void 2803e41f4b71Sopenharmony_ci 2804e41f4b71Sopenharmony_ciDeletes a KV pair from this KV store. This API uses an asynchronous callback to return the result. 2805e41f4b71Sopenharmony_ci 2806e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2807e41f4b71Sopenharmony_ci 2808e41f4b71Sopenharmony_ci**Parameters** 2809e41f4b71Sopenharmony_ci 2810e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2811e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 2812e41f4b71Sopenharmony_ci| key | string | Yes | Key of the KV pair to delete. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants).| 2813e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 2814e41f4b71Sopenharmony_ci 2815e41f4b71Sopenharmony_ci**Error codes** 2816e41f4b71Sopenharmony_ci 2817e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 2818e41f4b71Sopenharmony_ci 2819e41f4b71Sopenharmony_ci| ID| **Error Message** | 2820e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 2821e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 2822e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 2823e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 2824e41f4b71Sopenharmony_ci 2825e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md). 2826e41f4b71Sopenharmony_ci 2827e41f4b71Sopenharmony_ci| ID| **Error Message** | 2828e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- | 2829e41f4b71Sopenharmony_ci| 14800047 | The WAL file size exceeds the default limit. | 2830e41f4b71Sopenharmony_ci 2831e41f4b71Sopenharmony_ci**Example** 2832e41f4b71Sopenharmony_ci 2833e41f4b71Sopenharmony_ci```ts 2834e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2835e41f4b71Sopenharmony_ci 2836e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string'; 2837e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 2838e41f4b71Sopenharmony_citry { 2839e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err: BusinessError) => { 2840e41f4b71Sopenharmony_ci if (err != undefined) { 2841e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 2842e41f4b71Sopenharmony_ci return; 2843e41f4b71Sopenharmony_ci } 2844e41f4b71Sopenharmony_ci console.info('Succeeded in putting'); 2845e41f4b71Sopenharmony_ci if (kvStore != null) { 2846e41f4b71Sopenharmony_ci kvStore.delete(KEY_TEST_STRING_ELEMENT, (err: BusinessError) => { 2847e41f4b71Sopenharmony_ci if (err != undefined) { 2848e41f4b71Sopenharmony_ci console.error(`Failed to delete.code is ${err.code},message is ${err.message}`); 2849e41f4b71Sopenharmony_ci return; 2850e41f4b71Sopenharmony_ci } 2851e41f4b71Sopenharmony_ci console.info('Succeeded in deleting'); 2852e41f4b71Sopenharmony_ci }); 2853e41f4b71Sopenharmony_ci } 2854e41f4b71Sopenharmony_ci }); 2855e41f4b71Sopenharmony_ci} catch (e) { 2856e41f4b71Sopenharmony_ci let error = e as BusinessError; 2857e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 2858e41f4b71Sopenharmony_ci} 2859e41f4b71Sopenharmony_ci``` 2860e41f4b71Sopenharmony_ci 2861e41f4b71Sopenharmony_ci### delete 2862e41f4b71Sopenharmony_ci 2863e41f4b71Sopenharmony_cidelete(key: string): Promise<void> 2864e41f4b71Sopenharmony_ci 2865e41f4b71Sopenharmony_ciDeletes a KV pair from this KV store. This API uses a promise to return the result. 2866e41f4b71Sopenharmony_ci 2867e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2868e41f4b71Sopenharmony_ci 2869e41f4b71Sopenharmony_ci**Parameters** 2870e41f4b71Sopenharmony_ci 2871e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 2872e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------------------------------------------------------ | 2873e41f4b71Sopenharmony_ci| key | string | Yes | Key of the KV pair to delete. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants).| 2874e41f4b71Sopenharmony_ci 2875e41f4b71Sopenharmony_ci**Return value** 2876e41f4b71Sopenharmony_ci 2877e41f4b71Sopenharmony_ci| Type | Description | 2878e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 2879e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 2880e41f4b71Sopenharmony_ci 2881e41f4b71Sopenharmony_ci**Error codes** 2882e41f4b71Sopenharmony_ci 2883e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 2884e41f4b71Sopenharmony_ci 2885e41f4b71Sopenharmony_ci| ID| **Error Message** | 2886e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- | 2887e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 2888e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 2889e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 2890e41f4b71Sopenharmony_ci 2891e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md). 2892e41f4b71Sopenharmony_ci 2893e41f4b71Sopenharmony_ci| ID| **Error Message** | 2894e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- | 2895e41f4b71Sopenharmony_ci| 14800047 | The WAL file size exceeds the default limit. | 2896e41f4b71Sopenharmony_ci 2897e41f4b71Sopenharmony_ci**Example** 2898e41f4b71Sopenharmony_ci 2899e41f4b71Sopenharmony_ci```ts 2900e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2901e41f4b71Sopenharmony_ci 2902e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string'; 2903e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 2904e41f4b71Sopenharmony_citry { 2905e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(() => { 2906e41f4b71Sopenharmony_ci console.info(`Succeeded in putting data`); 2907e41f4b71Sopenharmony_ci if (kvStore != null) { 2908e41f4b71Sopenharmony_ci kvStore.delete(KEY_TEST_STRING_ELEMENT).then(() => { 2909e41f4b71Sopenharmony_ci console.info('Succeeded in deleting'); 2910e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2911e41f4b71Sopenharmony_ci console.error(`Failed to delete.code is ${err.code},message is ${err.message}`); 2912e41f4b71Sopenharmony_ci }); 2913e41f4b71Sopenharmony_ci } 2914e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2915e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 2916e41f4b71Sopenharmony_ci }); 2917e41f4b71Sopenharmony_ci} catch (e) { 2918e41f4b71Sopenharmony_ci let error = e as BusinessError; 2919e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 2920e41f4b71Sopenharmony_ci} 2921e41f4b71Sopenharmony_ci``` 2922e41f4b71Sopenharmony_ci 2923e41f4b71Sopenharmony_ci### deleteBatch 2924e41f4b71Sopenharmony_ci 2925e41f4b71Sopenharmony_cideleteBatch(keys: string[], callback: AsyncCallback<void>): void 2926e41f4b71Sopenharmony_ci 2927e41f4b71Sopenharmony_ciBatch deletes KV pairs from this single KV store. This API uses an asynchronous callback to return the result. 2928e41f4b71Sopenharmony_ci 2929e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 2930e41f4b71Sopenharmony_ci 2931e41f4b71Sopenharmony_ci**Parameters** 2932e41f4b71Sopenharmony_ci 2933e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2934e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------ | 2935e41f4b71Sopenharmony_ci| keys | string[] | Yes | KV pairs to delete. This parameter cannot be empty.| 2936e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 2937e41f4b71Sopenharmony_ci 2938e41f4b71Sopenharmony_ci**Error codes** 2939e41f4b71Sopenharmony_ci 2940e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 2941e41f4b71Sopenharmony_ci 2942e41f4b71Sopenharmony_ci| ID| **Error Message** | 2943e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- | 2944e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 2945e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 2946e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 2947e41f4b71Sopenharmony_ci 2948e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md). 2949e41f4b71Sopenharmony_ci 2950e41f4b71Sopenharmony_ci| ID| **Error Message** | 2951e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- | 2952e41f4b71Sopenharmony_ci| 14800047 | The WAL file size exceeds the default limit. | 2953e41f4b71Sopenharmony_ci 2954e41f4b71Sopenharmony_ci**Example** 2955e41f4b71Sopenharmony_ci 2956e41f4b71Sopenharmony_ci```ts 2957e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2958e41f4b71Sopenharmony_ci 2959e41f4b71Sopenharmony_citry { 2960e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 2961e41f4b71Sopenharmony_ci let keys: string[] = []; 2962e41f4b71Sopenharmony_ci for (let i = 0; i < 5; i++) { 2963e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 2964e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 2965e41f4b71Sopenharmony_ci key: key + i, 2966e41f4b71Sopenharmony_ci value: { 2967e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 2968e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 2969e41f4b71Sopenharmony_ci } 2970e41f4b71Sopenharmony_ci } 2971e41f4b71Sopenharmony_ci entries.push(entry); 2972e41f4b71Sopenharmony_ci keys.push(key + i); 2973e41f4b71Sopenharmony_ci } 2974e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 2975e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 2976e41f4b71Sopenharmony_ci if (err != undefined) { 2977e41f4b71Sopenharmony_ci console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 2978e41f4b71Sopenharmony_ci return; 2979e41f4b71Sopenharmony_ci } 2980e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 2981e41f4b71Sopenharmony_ci if (kvStore != null) { 2982e41f4b71Sopenharmony_ci kvStore.deleteBatch(keys, async (err: BusinessError) => { 2983e41f4b71Sopenharmony_ci if (err != undefined) { 2984e41f4b71Sopenharmony_ci console.error(`Failed to delete Batch.code is ${err.code},message is ${err.message}`); 2985e41f4b71Sopenharmony_ci return; 2986e41f4b71Sopenharmony_ci } 2987e41f4b71Sopenharmony_ci console.info('Succeeded in deleting Batch'); 2988e41f4b71Sopenharmony_ci }); 2989e41f4b71Sopenharmony_ci } 2990e41f4b71Sopenharmony_ci }); 2991e41f4b71Sopenharmony_ci} catch (e) { 2992e41f4b71Sopenharmony_ci let error = e as BusinessError; 2993e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 2994e41f4b71Sopenharmony_ci} 2995e41f4b71Sopenharmony_ci``` 2996e41f4b71Sopenharmony_ci 2997e41f4b71Sopenharmony_ci### deleteBatch 2998e41f4b71Sopenharmony_ci 2999e41f4b71Sopenharmony_cideleteBatch(keys: string[]): Promise<void> 3000e41f4b71Sopenharmony_ci 3001e41f4b71Sopenharmony_ciBatch deletes KV pairs from this single KV store. This API uses a promise to return the result. 3002e41f4b71Sopenharmony_ci 3003e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3004e41f4b71Sopenharmony_ci 3005e41f4b71Sopenharmony_ci**Parameters** 3006e41f4b71Sopenharmony_ci 3007e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 3008e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------------------ | 3009e41f4b71Sopenharmony_ci| keys | string[] | Yes | KV pairs to delete. This parameter cannot be empty.| 3010e41f4b71Sopenharmony_ci 3011e41f4b71Sopenharmony_ci**Return value** 3012e41f4b71Sopenharmony_ci 3013e41f4b71Sopenharmony_ci| Type | Description | 3014e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 3015e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 3016e41f4b71Sopenharmony_ci 3017e41f4b71Sopenharmony_ci**Error codes** 3018e41f4b71Sopenharmony_ci 3019e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3020e41f4b71Sopenharmony_ci 3021e41f4b71Sopenharmony_ci| ID| **Error Message** | 3022e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- | 3023e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 3024e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3025e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3026e41f4b71Sopenharmony_ci 3027e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md). 3028e41f4b71Sopenharmony_ci 3029e41f4b71Sopenharmony_ci| ID| **Error Message** | 3030e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- | 3031e41f4b71Sopenharmony_ci| 14800047 | The WAL file size exceeds the default limit. | 3032e41f4b71Sopenharmony_ci 3033e41f4b71Sopenharmony_ci**Example** 3034e41f4b71Sopenharmony_ci 3035e41f4b71Sopenharmony_ci```ts 3036e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3037e41f4b71Sopenharmony_ci 3038e41f4b71Sopenharmony_citry { 3039e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 3040e41f4b71Sopenharmony_ci let keys: string[] = []; 3041e41f4b71Sopenharmony_ci for (let i = 0; i < 5; i++) { 3042e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 3043e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 3044e41f4b71Sopenharmony_ci key: key + i, 3045e41f4b71Sopenharmony_ci value: { 3046e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 3047e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 3048e41f4b71Sopenharmony_ci } 3049e41f4b71Sopenharmony_ci } 3050e41f4b71Sopenharmony_ci entries.push(entry); 3051e41f4b71Sopenharmony_ci keys.push(key + i); 3052e41f4b71Sopenharmony_ci } 3053e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 3054e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 3055e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 3056e41f4b71Sopenharmony_ci if (kvStore != null) { 3057e41f4b71Sopenharmony_ci kvStore.deleteBatch(keys).then(() => { 3058e41f4b71Sopenharmony_ci console.info('Succeeded in deleting Batch'); 3059e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3060e41f4b71Sopenharmony_ci console.error(`Failed to delete Batch.code is ${err.code},message is ${err.message}`); 3061e41f4b71Sopenharmony_ci }); 3062e41f4b71Sopenharmony_ci } 3063e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3064e41f4b71Sopenharmony_ci console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 3065e41f4b71Sopenharmony_ci }); 3066e41f4b71Sopenharmony_ci} catch (e) { 3067e41f4b71Sopenharmony_ci let error = e as BusinessError; 3068e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 3069e41f4b71Sopenharmony_ci} 3070e41f4b71Sopenharmony_ci``` 3071e41f4b71Sopenharmony_ci 3072e41f4b71Sopenharmony_ci### removeDeviceData 3073e41f4b71Sopenharmony_ci 3074e41f4b71Sopenharmony_ciremoveDeviceData(deviceId: string, callback: AsyncCallback<void>): void 3075e41f4b71Sopenharmony_ci 3076e41f4b71Sopenharmony_ciDeletes data of a device. This API uses an asynchronous callback to return the result. 3077e41f4b71Sopenharmony_ci> **NOTE** 3078e41f4b71Sopenharmony_ci> 3079e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 3080e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 3081e41f4b71Sopenharmony_ci 3082e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 3083e41f4b71Sopenharmony_ci 3084e41f4b71Sopenharmony_ci**Parameters** 3085e41f4b71Sopenharmony_ci 3086e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3087e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------------------- | 3088e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the target device.| 3089e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 3090e41f4b71Sopenharmony_ci 3091e41f4b71Sopenharmony_ci**Error codes** 3092e41f4b71Sopenharmony_ci 3093e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3094e41f4b71Sopenharmony_ci 3095e41f4b71Sopenharmony_ci| ID| **Error Message** | 3096e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3097e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed. | 3098e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3099e41f4b71Sopenharmony_ci 3100e41f4b71Sopenharmony_ci**Example** 3101e41f4b71Sopenharmony_ci 3102e41f4b71Sopenharmony_ci```ts 3103e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3104e41f4b71Sopenharmony_ci 3105e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string_2'; 3106e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-string-002'; 3107e41f4b71Sopenharmony_citry { 3108e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, async (err: BusinessError) => { 3109e41f4b71Sopenharmony_ci console.info('Succeeded in putting data'); 3110e41f4b71Sopenharmony_ci const deviceid = 'no_exist_device_id'; 3111e41f4b71Sopenharmony_ci if (kvStore != null) { 3112e41f4b71Sopenharmony_ci kvStore.removeDeviceData(deviceid, async (err: BusinessError) => { 3113e41f4b71Sopenharmony_ci if (err == undefined) { 3114e41f4b71Sopenharmony_ci console.info('succeeded in removing device data'); 3115e41f4b71Sopenharmony_ci } else { 3116e41f4b71Sopenharmony_ci console.error(`Failed to remove device data.code is ${err.code},message is ${err.message} `); 3117e41f4b71Sopenharmony_ci if (kvStore != null) { 3118e41f4b71Sopenharmony_ci kvStore.get(KEY_TEST_STRING_ELEMENT, async (err: BusinessError, data: boolean | string | number | Uint8Array) => { 3119e41f4b71Sopenharmony_ci console.info('Succeeded in getting data'); 3120e41f4b71Sopenharmony_ci }); 3121e41f4b71Sopenharmony_ci } 3122e41f4b71Sopenharmony_ci } 3123e41f4b71Sopenharmony_ci }); 3124e41f4b71Sopenharmony_ci } 3125e41f4b71Sopenharmony_ci }); 3126e41f4b71Sopenharmony_ci} catch (e) { 3127e41f4b71Sopenharmony_ci let error = e as BusinessError; 3128e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`) 3129e41f4b71Sopenharmony_ci} 3130e41f4b71Sopenharmony_ci``` 3131e41f4b71Sopenharmony_ci 3132e41f4b71Sopenharmony_ci### removeDeviceData 3133e41f4b71Sopenharmony_ci 3134e41f4b71Sopenharmony_ciremoveDeviceData(deviceId: string): Promise<void> 3135e41f4b71Sopenharmony_ci 3136e41f4b71Sopenharmony_ciDeletes data of a device. This API uses a promise to return the result. 3137e41f4b71Sopenharmony_ci> **NOTE** 3138e41f4b71Sopenharmony_ci> 3139e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 3140e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 3141e41f4b71Sopenharmony_ci 3142e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 3143e41f4b71Sopenharmony_ci 3144e41f4b71Sopenharmony_ci**Parameters** 3145e41f4b71Sopenharmony_ci 3146e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 3147e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ---------------------- | 3148e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the target device.| 3149e41f4b71Sopenharmony_ci 3150e41f4b71Sopenharmony_ci**Return value** 3151e41f4b71Sopenharmony_ci 3152e41f4b71Sopenharmony_ci| Type | Description | 3153e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 3154e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 3155e41f4b71Sopenharmony_ci 3156e41f4b71Sopenharmony_ci**Error codes** 3157e41f4b71Sopenharmony_ci 3158e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3159e41f4b71Sopenharmony_ci 3160e41f4b71Sopenharmony_ci| ID| **Error Message** | 3161e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3162e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed. | 3163e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3164e41f4b71Sopenharmony_ci 3165e41f4b71Sopenharmony_ci**Example** 3166e41f4b71Sopenharmony_ci 3167e41f4b71Sopenharmony_ci```ts 3168e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3169e41f4b71Sopenharmony_ci 3170e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string_2'; 3171e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-string-001'; 3172e41f4b71Sopenharmony_citry { 3173e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(() => { 3174e41f4b71Sopenharmony_ci console.info('Succeeded in putting data'); 3175e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3176e41f4b71Sopenharmony_ci console.error(`Failed to put data.code is ${err.code},message is ${err.message} `); 3177e41f4b71Sopenharmony_ci }); 3178e41f4b71Sopenharmony_ci const deviceid = 'no_exist_device_id'; 3179e41f4b71Sopenharmony_ci kvStore.removeDeviceData(deviceid).then(() => { 3180e41f4b71Sopenharmony_ci console.info('succeeded in removing device data'); 3181e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3182e41f4b71Sopenharmony_ci console.error(`Failed to remove device data.code is ${err.code},message is ${err.message} `); 3183e41f4b71Sopenharmony_ci }); 3184e41f4b71Sopenharmony_ci kvStore.get(KEY_TEST_STRING_ELEMENT).then((data: boolean | string | number | Uint8Array) => { 3185e41f4b71Sopenharmony_ci console.info('Succeeded in getting data'); 3186e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3187e41f4b71Sopenharmony_ci console.error(`Failed to get data.code is ${err.code},message is ${err.message} `); 3188e41f4b71Sopenharmony_ci }); 3189e41f4b71Sopenharmony_ci} catch (e) { 3190e41f4b71Sopenharmony_ci let error = e as BusinessError; 3191e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`) 3192e41f4b71Sopenharmony_ci} 3193e41f4b71Sopenharmony_ci``` 3194e41f4b71Sopenharmony_ci 3195e41f4b71Sopenharmony_ci### get 3196e41f4b71Sopenharmony_ci 3197e41f4b71Sopenharmony_ciget(key: string, callback: AsyncCallback<boolean | string | number | Uint8Array>): void 3198e41f4b71Sopenharmony_ci 3199e41f4b71Sopenharmony_ciObtains the value of the specified key. This API uses an asynchronous callback to return the result. 3200e41f4b71Sopenharmony_ci 3201e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3202e41f4b71Sopenharmony_ci 3203e41f4b71Sopenharmony_ci**Parameters** 3204e41f4b71Sopenharmony_ci 3205e41f4b71Sopenharmony_ci| Name | Type| Mandatory | Description | 3206e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ----------------------- | 3207e41f4b71Sopenharmony_ci| key |string | Yes |Key of the value to obtain. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants). | 3208e41f4b71Sopenharmony_ci| callback |AsyncCallback<boolean \| string \| number \| Uint8Array> | Yes |Callback used to return the value obtained. | 3209e41f4b71Sopenharmony_ci 3210e41f4b71Sopenharmony_ci**Error codes** 3211e41f4b71Sopenharmony_ci 3212e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3213e41f4b71Sopenharmony_ci 3214e41f4b71Sopenharmony_ci| ID| **Error Message** | 3215e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3216e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 3217e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3218e41f4b71Sopenharmony_ci| 15100004 | Not found. | 3219e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3220e41f4b71Sopenharmony_ci 3221e41f4b71Sopenharmony_ci**Example** 3222e41f4b71Sopenharmony_ci 3223e41f4b71Sopenharmony_ci```ts 3224e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3225e41f4b71Sopenharmony_ci 3226e41f4b71Sopenharmony_ci 3227e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string'; 3228e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 3229e41f4b71Sopenharmony_citry { 3230e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err: BusinessError) => { 3231e41f4b71Sopenharmony_ci if (err != undefined) { 3232e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 3233e41f4b71Sopenharmony_ci return; 3234e41f4b71Sopenharmony_ci } 3235e41f4b71Sopenharmony_ci console.info("Succeeded in putting"); 3236e41f4b71Sopenharmony_ci if (kvStore != null) { 3237e41f4b71Sopenharmony_ci kvStore.get(KEY_TEST_STRING_ELEMENT, (err: BusinessError, data: boolean | string | number | Uint8Array) => { 3238e41f4b71Sopenharmony_ci if (err != undefined) { 3239e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 3240e41f4b71Sopenharmony_ci return; 3241e41f4b71Sopenharmony_ci } 3242e41f4b71Sopenharmony_ci console.info(`Succeeded in getting data.data=${data}`); 3243e41f4b71Sopenharmony_ci }); 3244e41f4b71Sopenharmony_ci } 3245e41f4b71Sopenharmony_ci }); 3246e41f4b71Sopenharmony_ci} catch (e) { 3247e41f4b71Sopenharmony_ci let error = e as BusinessError; 3248e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 3249e41f4b71Sopenharmony_ci} 3250e41f4b71Sopenharmony_ci``` 3251e41f4b71Sopenharmony_ci 3252e41f4b71Sopenharmony_ci### get 3253e41f4b71Sopenharmony_ci 3254e41f4b71Sopenharmony_ciget(key: string): Promise<boolean | string | number | Uint8Array> 3255e41f4b71Sopenharmony_ci 3256e41f4b71Sopenharmony_ciObtains the value of the specified key. This API uses a promise to return the result. 3257e41f4b71Sopenharmony_ci 3258e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3259e41f4b71Sopenharmony_ci 3260e41f4b71Sopenharmony_ci**Parameters** 3261e41f4b71Sopenharmony_ci 3262e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 3263e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------------------------------------------------------ | 3264e41f4b71Sopenharmony_ci| key | string | Yes | Key of the value to obtain. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants).| 3265e41f4b71Sopenharmony_ci 3266e41f4b71Sopenharmony_ci**Return value** 3267e41f4b71Sopenharmony_ci 3268e41f4b71Sopenharmony_ci| Type | Description | 3269e41f4b71Sopenharmony_ci| ------ | ------- | 3270e41f4b71Sopenharmony_ci|Promise<Uint8Array \| string \| boolean \| number> |Promise used to return the value obtained.| 3271e41f4b71Sopenharmony_ci 3272e41f4b71Sopenharmony_ci**Error codes** 3273e41f4b71Sopenharmony_ci 3274e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3275e41f4b71Sopenharmony_ci 3276e41f4b71Sopenharmony_ci| ID| **Error Message** | 3277e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3278e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 3279e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3280e41f4b71Sopenharmony_ci| 15100004 | Not found. | 3281e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3282e41f4b71Sopenharmony_ci 3283e41f4b71Sopenharmony_ci**Example** 3284e41f4b71Sopenharmony_ci 3285e41f4b71Sopenharmony_ci```ts 3286e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3287e41f4b71Sopenharmony_ci 3288e41f4b71Sopenharmony_ci 3289e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string'; 3290e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 3291e41f4b71Sopenharmony_citry { 3292e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(() => { 3293e41f4b71Sopenharmony_ci console.info(`Succeeded in putting data`); 3294e41f4b71Sopenharmony_ci if (kvStore != null) { 3295e41f4b71Sopenharmony_ci kvStore.get(KEY_TEST_STRING_ELEMENT).then((data: boolean | string | number | Uint8Array) => { 3296e41f4b71Sopenharmony_ci console.info(`Succeeded in getting data.data=${data}`); 3297e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3298e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 3299e41f4b71Sopenharmony_ci }); 3300e41f4b71Sopenharmony_ci } 3301e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3302e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 3303e41f4b71Sopenharmony_ci }); 3304e41f4b71Sopenharmony_ci} catch (e) { 3305e41f4b71Sopenharmony_ci let error = e as BusinessError; 3306e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 3307e41f4b71Sopenharmony_ci} 3308e41f4b71Sopenharmony_ci``` 3309e41f4b71Sopenharmony_ci 3310e41f4b71Sopenharmony_ci### getEntries 3311e41f4b71Sopenharmony_ci 3312e41f4b71Sopenharmony_cigetEntries(keyPrefix: string, callback: AsyncCallback<Entry[]>): void 3313e41f4b71Sopenharmony_ci 3314e41f4b71Sopenharmony_ciObtains all KV pairs that match the specified key prefix. This API uses an asynchronous callback to return the result. 3315e41f4b71Sopenharmony_ci 3316e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3317e41f4b71Sopenharmony_ci 3318e41f4b71Sopenharmony_ci**Parameters** 3319e41f4b71Sopenharmony_ci 3320e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3321e41f4b71Sopenharmony_ci| --------- | -------------------------------------- | ---- | ---------------------------------------- | 3322e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 3323e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Entry](#entry)[]> | Yes | Callback used to return the KV pairs that match the specified prefix.| 3324e41f4b71Sopenharmony_ci 3325e41f4b71Sopenharmony_ci**Error codes** 3326e41f4b71Sopenharmony_ci 3327e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3328e41f4b71Sopenharmony_ci 3329e41f4b71Sopenharmony_ci| ID| **Error Message** | 3330e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3331e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 3332e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3333e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3334e41f4b71Sopenharmony_ci 3335e41f4b71Sopenharmony_ci**Example** 3336e41f4b71Sopenharmony_ci 3337e41f4b71Sopenharmony_ci```ts 3338e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3339e41f4b71Sopenharmony_ci 3340e41f4b71Sopenharmony_citry { 3341e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 3342e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 3343e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 3344e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 3345e41f4b71Sopenharmony_ci key: key + i, 3346e41f4b71Sopenharmony_ci value: { 3347e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 3348e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 3349e41f4b71Sopenharmony_ci } 3350e41f4b71Sopenharmony_ci } 3351e41f4b71Sopenharmony_ci entries.push(entry); 3352e41f4b71Sopenharmony_ci } 3353e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 3354e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 3355e41f4b71Sopenharmony_ci if (err != undefined) { 3356e41f4b71Sopenharmony_ci console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 3357e41f4b71Sopenharmony_ci return; 3358e41f4b71Sopenharmony_ci } 3359e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 3360e41f4b71Sopenharmony_ci if (kvStore != null) { 3361e41f4b71Sopenharmony_ci kvStore.getEntries('batch_test_string_key', (err: BusinessError, entries: distributedKVStore.Entry[]) => { 3362e41f4b71Sopenharmony_ci if (err != undefined) { 3363e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 3364e41f4b71Sopenharmony_ci return; 3365e41f4b71Sopenharmony_ci } 3366e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 3367e41f4b71Sopenharmony_ci console.info(`entries.length: ${entries.length}`); 3368e41f4b71Sopenharmony_ci console.info(`entries[0]: ${entries[0]}`); 3369e41f4b71Sopenharmony_ci }); 3370e41f4b71Sopenharmony_ci } 3371e41f4b71Sopenharmony_ci }); 3372e41f4b71Sopenharmony_ci} catch (e) { 3373e41f4b71Sopenharmony_ci let error = e as BusinessError; 3374e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message} `); 3375e41f4b71Sopenharmony_ci} 3376e41f4b71Sopenharmony_ci``` 3377e41f4b71Sopenharmony_ci 3378e41f4b71Sopenharmony_ci### getEntries 3379e41f4b71Sopenharmony_ci 3380e41f4b71Sopenharmony_cigetEntries(keyPrefix: string): Promise<Entry[]> 3381e41f4b71Sopenharmony_ci 3382e41f4b71Sopenharmony_ciObtains all KV pairs that match the specified key prefix. This API uses a promise to return the result. 3383e41f4b71Sopenharmony_ci 3384e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3385e41f4b71Sopenharmony_ci 3386e41f4b71Sopenharmony_ci**Parameters** 3387e41f4b71Sopenharmony_ci 3388e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 3389e41f4b71Sopenharmony_ci| --------- | -------- | ---- | -------------------- | 3390e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 3391e41f4b71Sopenharmony_ci 3392e41f4b71Sopenharmony_ci**Return value** 3393e41f4b71Sopenharmony_ci 3394e41f4b71Sopenharmony_ci| Type | Description | 3395e41f4b71Sopenharmony_ci| -------------------------------- | ------------------------------------------- | 3396e41f4b71Sopenharmony_ci| Promise<[Entry](#entry)[]> | Promise used to return the KV pairs that match the specified prefix.| 3397e41f4b71Sopenharmony_ci 3398e41f4b71Sopenharmony_ci**Error codes** 3399e41f4b71Sopenharmony_ci 3400e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3401e41f4b71Sopenharmony_ci 3402e41f4b71Sopenharmony_ci| ID| **Error Message** | 3403e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3404e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 3405e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3406e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3407e41f4b71Sopenharmony_ci 3408e41f4b71Sopenharmony_ci**Example** 3409e41f4b71Sopenharmony_ci 3410e41f4b71Sopenharmony_ci```ts 3411e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3412e41f4b71Sopenharmony_ci 3413e41f4b71Sopenharmony_ci 3414e41f4b71Sopenharmony_citry { 3415e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 3416e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 3417e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 3418e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 3419e41f4b71Sopenharmony_ci key: key + i, 3420e41f4b71Sopenharmony_ci value: { 3421e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 3422e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 3423e41f4b71Sopenharmony_ci } 3424e41f4b71Sopenharmony_ci } 3425e41f4b71Sopenharmony_ci entries.push(entry); 3426e41f4b71Sopenharmony_ci } 3427e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 3428e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 3429e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 3430e41f4b71Sopenharmony_ci if (kvStore != null) { 3431e41f4b71Sopenharmony_ci kvStore.getEntries('batch_test_string_key').then((entries: distributedKVStore.Entry[]) => { 3432e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 3433e41f4b71Sopenharmony_ci console.info(`PutBatch ${entries}`); 3434e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3435e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 3436e41f4b71Sopenharmony_ci }); 3437e41f4b71Sopenharmony_ci } 3438e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3439e41f4b71Sopenharmony_ci console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 3440e41f4b71Sopenharmony_ci }); 3441e41f4b71Sopenharmony_ci} catch (e) { 3442e41f4b71Sopenharmony_ci let error = e as BusinessError; 3443e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message} `); 3444e41f4b71Sopenharmony_ci} 3445e41f4b71Sopenharmony_ci``` 3446e41f4b71Sopenharmony_ci 3447e41f4b71Sopenharmony_ci### getEntries 3448e41f4b71Sopenharmony_ci 3449e41f4b71Sopenharmony_cigetEntries(query: Query, callback: AsyncCallback<Entry[]>): void 3450e41f4b71Sopenharmony_ci 3451e41f4b71Sopenharmony_ciObtains the KV pairs that match the specified **Query** object. This API uses an asynchronous callback to return the result. 3452e41f4b71Sopenharmony_ci 3453e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3454e41f4b71Sopenharmony_ci 3455e41f4b71Sopenharmony_ci**Parameters** 3456e41f4b71Sopenharmony_ci 3457e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3458e41f4b71Sopenharmony_ci| -------- | -------------------------------------- | ---- | ----------------------------------------------- | 3459e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | Key prefix to match. | 3460e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Entry](#entry)[]> | Yes | Callback used to return the KV pairs that match the specified **Query** object.| 3461e41f4b71Sopenharmony_ci 3462e41f4b71Sopenharmony_ci**Error codes** 3463e41f4b71Sopenharmony_ci 3464e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3465e41f4b71Sopenharmony_ci 3466e41f4b71Sopenharmony_ci| ID| **Error Message** | 3467e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3468e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 3469e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3470e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3471e41f4b71Sopenharmony_ci 3472e41f4b71Sopenharmony_ci**Example** 3473e41f4b71Sopenharmony_ci 3474e41f4b71Sopenharmony_ci```ts 3475e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3476e41f4b71Sopenharmony_ci 3477e41f4b71Sopenharmony_citry { 3478e41f4b71Sopenharmony_ci let arr = new Uint8Array([21, 31]); 3479e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 3480e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 3481e41f4b71Sopenharmony_ci let key = 'batch_test_bool_key'; 3482e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 3483e41f4b71Sopenharmony_ci key: key + i, 3484e41f4b71Sopenharmony_ci value: { 3485e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.BYTE_ARRAY, 3486e41f4b71Sopenharmony_ci value: arr 3487e41f4b71Sopenharmony_ci } 3488e41f4b71Sopenharmony_ci } 3489e41f4b71Sopenharmony_ci entries.push(entry); 3490e41f4b71Sopenharmony_ci } 3491e41f4b71Sopenharmony_ci console.info(`entries: {entries}`); 3492e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 3493e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 3494e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 3495e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 3496e41f4b71Sopenharmony_ci if (kvStore != null) { 3497e41f4b71Sopenharmony_ci kvStore.getEntries(query, (err: BusinessError, entries: distributedKVStore.Entry[]) => { 3498e41f4b71Sopenharmony_ci if (err != undefined) { 3499e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 3500e41f4b71Sopenharmony_ci return; 3501e41f4b71Sopenharmony_ci } 3502e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 3503e41f4b71Sopenharmony_ci console.info(`entries.length: ${entries.length}`); 3504e41f4b71Sopenharmony_ci console.info(`entries[0]: ${entries[0]}`); 3505e41f4b71Sopenharmony_ci }); 3506e41f4b71Sopenharmony_ci } 3507e41f4b71Sopenharmony_ci }); 3508e41f4b71Sopenharmony_ci} catch (e) { 3509e41f4b71Sopenharmony_ci let error = e as BusinessError; 3510e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${error.code},message is ${error.message}`); 3511e41f4b71Sopenharmony_ci} 3512e41f4b71Sopenharmony_ci``` 3513e41f4b71Sopenharmony_ci 3514e41f4b71Sopenharmony_ci### getEntries 3515e41f4b71Sopenharmony_ci 3516e41f4b71Sopenharmony_cigetEntries(query: Query): Promise<Entry[]> 3517e41f4b71Sopenharmony_ci 3518e41f4b71Sopenharmony_ciObtains the KV pairs that match the specified **Query** object. This API uses a promise to return the result. 3519e41f4b71Sopenharmony_ci 3520e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3521e41f4b71Sopenharmony_ci 3522e41f4b71Sopenharmony_ci**Parameters** 3523e41f4b71Sopenharmony_ci 3524e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 3525e41f4b71Sopenharmony_ci| ------ | -------------- | ---- | -------------- | 3526e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match.| 3527e41f4b71Sopenharmony_ci 3528e41f4b71Sopenharmony_ci**Return value** 3529e41f4b71Sopenharmony_ci 3530e41f4b71Sopenharmony_ci| Type | Description | 3531e41f4b71Sopenharmony_ci| -------------------------------- | -------------------------------------------------- | 3532e41f4b71Sopenharmony_ci| Promise<[Entry](#entry)[]> | Promise used to return the KV pairs that match the specified **Query** object.| 3533e41f4b71Sopenharmony_ci 3534e41f4b71Sopenharmony_ci**Error codes** 3535e41f4b71Sopenharmony_ci 3536e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3537e41f4b71Sopenharmony_ci 3538e41f4b71Sopenharmony_ci| ID| **Error Message** | 3539e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3540e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 3541e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3542e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3543e41f4b71Sopenharmony_ci 3544e41f4b71Sopenharmony_ci**Example** 3545e41f4b71Sopenharmony_ci 3546e41f4b71Sopenharmony_ci```ts 3547e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3548e41f4b71Sopenharmony_ci 3549e41f4b71Sopenharmony_citry { 3550e41f4b71Sopenharmony_ci let arr = new Uint8Array([21, 31]); 3551e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 3552e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 3553e41f4b71Sopenharmony_ci let key = 'batch_test_bool_key'; 3554e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 3555e41f4b71Sopenharmony_ci key: key + i, 3556e41f4b71Sopenharmony_ci value: { 3557e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.BYTE_ARRAY, 3558e41f4b71Sopenharmony_ci value: arr 3559e41f4b71Sopenharmony_ci } 3560e41f4b71Sopenharmony_ci } 3561e41f4b71Sopenharmony_ci entries.push(entry); 3562e41f4b71Sopenharmony_ci } 3563e41f4b71Sopenharmony_ci console.info(`entries: {entries}`); 3564e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 3565e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 3566e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 3567e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 3568e41f4b71Sopenharmony_ci if (kvStore != null) { 3569e41f4b71Sopenharmony_ci kvStore.getEntries(query).then((entries: distributedKVStore.Entry[]) => { 3570e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 3571e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3572e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 3573e41f4b71Sopenharmony_ci }); 3574e41f4b71Sopenharmony_ci } 3575e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3576e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`) 3577e41f4b71Sopenharmony_ci }); 3578e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 3579e41f4b71Sopenharmony_ci} catch (e) { 3580e41f4b71Sopenharmony_ci let error = e as BusinessError; 3581e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${error.code},message is ${error.message}`); 3582e41f4b71Sopenharmony_ci} 3583e41f4b71Sopenharmony_ci``` 3584e41f4b71Sopenharmony_ci 3585e41f4b71Sopenharmony_ci### getResultSet 3586e41f4b71Sopenharmony_ci 3587e41f4b71Sopenharmony_cigetResultSet(keyPrefix: string, callback: AsyncCallback<KVStoreResultSet>): void 3588e41f4b71Sopenharmony_ci 3589e41f4b71Sopenharmony_ciObtains a result set with the specified prefix from this single KV store. This API uses an asynchronous callback to return the result. 3590e41f4b71Sopenharmony_ci 3591e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3592e41f4b71Sopenharmony_ci 3593e41f4b71Sopenharmony_ci**Parameters** 3594e41f4b71Sopenharmony_ci 3595e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3596e41f4b71Sopenharmony_ci| --------- | ---------------------------------------------------------- | ---- | ------------------------------------ | 3597e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned. | 3598e41f4b71Sopenharmony_ci| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | Yes | Callback used to return the result set with the specified prefix.| 3599e41f4b71Sopenharmony_ci 3600e41f4b71Sopenharmony_ci**Error codes** 3601e41f4b71Sopenharmony_ci 3602e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3603e41f4b71Sopenharmony_ci 3604e41f4b71Sopenharmony_ci| ID| **Error Message** | 3605e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3606e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 3607e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 3608e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3609e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3610e41f4b71Sopenharmony_ci 3611e41f4b71Sopenharmony_ci 3612e41f4b71Sopenharmony_ci**Example** 3613e41f4b71Sopenharmony_ci 3614e41f4b71Sopenharmony_ci```ts 3615e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3616e41f4b71Sopenharmony_ci 3617e41f4b71Sopenharmony_citry { 3618e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 3619e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 3620e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 3621e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 3622e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 3623e41f4b71Sopenharmony_ci key: key + i, 3624e41f4b71Sopenharmony_ci value: { 3625e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 3626e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 3627e41f4b71Sopenharmony_ci } 3628e41f4b71Sopenharmony_ci } 3629e41f4b71Sopenharmony_ci entries.push(entry); 3630e41f4b71Sopenharmony_ci } 3631e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 3632e41f4b71Sopenharmony_ci if (err != undefined) { 3633e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 3634e41f4b71Sopenharmony_ci return; 3635e41f4b71Sopenharmony_ci } 3636e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 3637e41f4b71Sopenharmony_ci if (kvStore != null) { 3638e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key', async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 3639e41f4b71Sopenharmony_ci if (err != undefined) { 3640e41f4b71Sopenharmony_ci console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3641e41f4b71Sopenharmony_ci return; 3642e41f4b71Sopenharmony_ci } 3643e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set'); 3644e41f4b71Sopenharmony_ci resultSet = result; 3645e41f4b71Sopenharmony_ci if (kvStore != null) { 3646e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet, (err :BusinessError) => { 3647e41f4b71Sopenharmony_ci if (err != undefined) { 3648e41f4b71Sopenharmony_ci console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 3649e41f4b71Sopenharmony_ci return; 3650e41f4b71Sopenharmony_ci } 3651e41f4b71Sopenharmony_ci console.info('Succeeded in closing result set'); 3652e41f4b71Sopenharmony_ci }); 3653e41f4b71Sopenharmony_ci } 3654e41f4b71Sopenharmony_ci }); 3655e41f4b71Sopenharmony_ci } 3656e41f4b71Sopenharmony_ci }); 3657e41f4b71Sopenharmony_ci} catch (e) { 3658e41f4b71Sopenharmony_ci let error = e as BusinessError; 3659e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 3660e41f4b71Sopenharmony_ci} 3661e41f4b71Sopenharmony_ci``` 3662e41f4b71Sopenharmony_ci 3663e41f4b71Sopenharmony_ci### getResultSet 3664e41f4b71Sopenharmony_ci 3665e41f4b71Sopenharmony_cigetResultSet(keyPrefix: string): Promise<KVStoreResultSet> 3666e41f4b71Sopenharmony_ci 3667e41f4b71Sopenharmony_ciObtains a result set with the specified prefix from this single KV store. This API uses a promise to return the result. 3668e41f4b71Sopenharmony_ci 3669e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3670e41f4b71Sopenharmony_ci 3671e41f4b71Sopenharmony_ci**Parameters** 3672e41f4b71Sopenharmony_ci 3673e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 3674e41f4b71Sopenharmony_ci| --------- | -------- | ---- | -------------------- | 3675e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 3676e41f4b71Sopenharmony_ci 3677e41f4b71Sopenharmony_ci**Return value** 3678e41f4b71Sopenharmony_ci 3679e41f4b71Sopenharmony_ci| Type | Description | 3680e41f4b71Sopenharmony_ci| ---------------------------------------------------- | --------------------------------------- | 3681e41f4b71Sopenharmony_ci| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise used to return the result set with the specified prefix.| 3682e41f4b71Sopenharmony_ci 3683e41f4b71Sopenharmony_ci**Error codes** 3684e41f4b71Sopenharmony_ci 3685e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3686e41f4b71Sopenharmony_ci 3687e41f4b71Sopenharmony_ci| ID| **Error Message** | 3688e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3689e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 3690e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 3691e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3692e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3693e41f4b71Sopenharmony_ci 3694e41f4b71Sopenharmony_ci**Example** 3695e41f4b71Sopenharmony_ci 3696e41f4b71Sopenharmony_ci```ts 3697e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3698e41f4b71Sopenharmony_ci 3699e41f4b71Sopenharmony_citry { 3700e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 3701e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 3702e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 3703e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 3704e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 3705e41f4b71Sopenharmony_ci key: key + i, 3706e41f4b71Sopenharmony_ci value: { 3707e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 3708e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 3709e41f4b71Sopenharmony_ci } 3710e41f4b71Sopenharmony_ci } 3711e41f4b71Sopenharmony_ci entries.push(entry); 3712e41f4b71Sopenharmony_ci } 3713e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 3714e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 3715e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3716e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 3717e41f4b71Sopenharmony_ci }); 3718e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 3719e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set'); 3720e41f4b71Sopenharmony_ci resultSet = result; 3721e41f4b71Sopenharmony_ci if (kvStore != null) { 3722e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet).then(() => { 3723e41f4b71Sopenharmony_ci console.info('Succeeded in closing result set'); 3724e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3725e41f4b71Sopenharmony_ci console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 3726e41f4b71Sopenharmony_ci }); 3727e41f4b71Sopenharmony_ci } 3728e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3729e41f4b71Sopenharmony_ci console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3730e41f4b71Sopenharmony_ci }); 3731e41f4b71Sopenharmony_ci} catch (e) { 3732e41f4b71Sopenharmony_ci let error = e as BusinessError; 3733e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 3734e41f4b71Sopenharmony_ci} 3735e41f4b71Sopenharmony_ci``` 3736e41f4b71Sopenharmony_ci 3737e41f4b71Sopenharmony_ci### getResultSet 3738e41f4b71Sopenharmony_ci 3739e41f4b71Sopenharmony_cigetResultSet(query: Query, callback: AsyncCallback<KVStoreResultSet>): void 3740e41f4b71Sopenharmony_ci 3741e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified **Query** object. This API uses an asynchronous callback to return the result. 3742e41f4b71Sopenharmony_ci 3743e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3744e41f4b71Sopenharmony_ci 3745e41f4b71Sopenharmony_ci**Parameters** 3746e41f4b71Sopenharmony_ci 3747e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3748e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------------- | ---- | --------------------------------------------------------- | 3749e41f4b71Sopenharmony_ci| query | Query | Yes | **Query** object to match. | 3750e41f4b71Sopenharmony_ci| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | Yes | Callback used to return the **KVStoreResultSet** object obtained.| 3751e41f4b71Sopenharmony_ci 3752e41f4b71Sopenharmony_ci**Error codes** 3753e41f4b71Sopenharmony_ci 3754e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3755e41f4b71Sopenharmony_ci 3756e41f4b71Sopenharmony_ci| ID| **Error Message** | 3757e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3758e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 3759e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 3760e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3761e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3762e41f4b71Sopenharmony_ci 3763e41f4b71Sopenharmony_ci**Example** 3764e41f4b71Sopenharmony_ci 3765e41f4b71Sopenharmony_ci```ts 3766e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3767e41f4b71Sopenharmony_ci 3768e41f4b71Sopenharmony_citry { 3769e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 3770e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 3771e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 3772e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 3773e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 3774e41f4b71Sopenharmony_ci key: key + i, 3775e41f4b71Sopenharmony_ci value: { 3776e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 3777e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 3778e41f4b71Sopenharmony_ci } 3779e41f4b71Sopenharmony_ci } 3780e41f4b71Sopenharmony_ci entries.push(entry); 3781e41f4b71Sopenharmony_ci } 3782e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 3783e41f4b71Sopenharmony_ci if (err != undefined) { 3784e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 3785e41f4b71Sopenharmony_ci return; 3786e41f4b71Sopenharmony_ci } 3787e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 3788e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 3789e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 3790e41f4b71Sopenharmony_ci if (kvStore != null) { 3791e41f4b71Sopenharmony_ci kvStore.getResultSet(query, async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 3792e41f4b71Sopenharmony_ci if (err != undefined) { 3793e41f4b71Sopenharmony_ci console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3794e41f4b71Sopenharmony_ci return; 3795e41f4b71Sopenharmony_ci } 3796e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set'); 3797e41f4b71Sopenharmony_ci }); 3798e41f4b71Sopenharmony_ci } 3799e41f4b71Sopenharmony_ci }); 3800e41f4b71Sopenharmony_ci} catch (e) { 3801e41f4b71Sopenharmony_ci let error = e as BusinessError; 3802e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 3803e41f4b71Sopenharmony_ci} 3804e41f4b71Sopenharmony_ci``` 3805e41f4b71Sopenharmony_ci 3806e41f4b71Sopenharmony_ci### getResultSet 3807e41f4b71Sopenharmony_ci 3808e41f4b71Sopenharmony_cigetResultSet(query: Query): Promise<KVStoreResultSet> 3809e41f4b71Sopenharmony_ci 3810e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified **Query** object. This API uses a promise to return the result. 3811e41f4b71Sopenharmony_ci 3812e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3813e41f4b71Sopenharmony_ci 3814e41f4b71Sopenharmony_ci**Parameters** 3815e41f4b71Sopenharmony_ci 3816e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 3817e41f4b71Sopenharmony_ci| ------ | -------------- | ---- | -------------- | 3818e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match.| 3819e41f4b71Sopenharmony_ci 3820e41f4b71Sopenharmony_ci**Return value** 3821e41f4b71Sopenharmony_ci 3822e41f4b71Sopenharmony_ci| Type | Description | 3823e41f4b71Sopenharmony_ci| ---------------------------------------------------- | ------------------------------------------------------------ | 3824e41f4b71Sopenharmony_ci| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise used to return the **KVStoreResultSet** object obtained.| 3825e41f4b71Sopenharmony_ci 3826e41f4b71Sopenharmony_ci**Error codes** 3827e41f4b71Sopenharmony_ci 3828e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 3829e41f4b71Sopenharmony_ci 3830e41f4b71Sopenharmony_ci| ID| **Error Message** | 3831e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3832e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 3833e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 3834e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 3835e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 3836e41f4b71Sopenharmony_ci 3837e41f4b71Sopenharmony_ci**Example** 3838e41f4b71Sopenharmony_ci 3839e41f4b71Sopenharmony_ci```ts 3840e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3841e41f4b71Sopenharmony_ci 3842e41f4b71Sopenharmony_citry { 3843e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 3844e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 3845e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 3846e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 3847e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 3848e41f4b71Sopenharmony_ci key: key + i, 3849e41f4b71Sopenharmony_ci value: { 3850e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 3851e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 3852e41f4b71Sopenharmony_ci } 3853e41f4b71Sopenharmony_ci } 3854e41f4b71Sopenharmony_ci entries.push(entry); 3855e41f4b71Sopenharmony_ci } 3856e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 3857e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 3858e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3859e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 3860e41f4b71Sopenharmony_ci }); 3861e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 3862e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 3863e41f4b71Sopenharmony_ci kvStore.getResultSet(query).then((result: distributedKVStore.KVStoreResultSet) => { 3864e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set'); 3865e41f4b71Sopenharmony_ci resultSet = result; 3866e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3867e41f4b71Sopenharmony_ci console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3868e41f4b71Sopenharmony_ci }); 3869e41f4b71Sopenharmony_ci} catch (e) { 3870e41f4b71Sopenharmony_ci let error = e as BusinessError; 3871e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 3872e41f4b71Sopenharmony_ci} 3873e41f4b71Sopenharmony_ci``` 3874e41f4b71Sopenharmony_ci 3875e41f4b71Sopenharmony_ci### closeResultSet 3876e41f4b71Sopenharmony_ci 3877e41f4b71Sopenharmony_cicloseResultSet(resultSet: KVStoreResultSet, callback: AsyncCallback<void>): void 3878e41f4b71Sopenharmony_ci 3879e41f4b71Sopenharmony_ciCloses the **KVStoreResultSet** object returned by [SingleKvStore.getResultSet](#getresultset-1). This API uses an asynchronous callback to return the result. 3880e41f4b71Sopenharmony_ci 3881e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3882e41f4b71Sopenharmony_ci 3883e41f4b71Sopenharmony_ci**Parameters** 3884e41f4b71Sopenharmony_ci 3885e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3886e41f4b71Sopenharmony_ci| --------- | ------------------------------------- | ---- | ---------------------------------- | 3887e41f4b71Sopenharmony_ci| resultSet | [KVStoreResultSet](#kvstoreresultset) | Yes | **KVStoreResultSet** object to close.| 3888e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 3889e41f4b71Sopenharmony_ci 3890e41f4b71Sopenharmony_ci**Error codes** 3891e41f4b71Sopenharmony_ci 3892e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 3893e41f4b71Sopenharmony_ci 3894e41f4b71Sopenharmony_ci| ID| **Error Message** | 3895e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3896e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 3897e41f4b71Sopenharmony_ci 3898e41f4b71Sopenharmony_ci**Example** 3899e41f4b71Sopenharmony_ci 3900e41f4b71Sopenharmony_ci```ts 3901e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3902e41f4b71Sopenharmony_ci 3903e41f4b71Sopenharmony_cilet resultSet: distributedKVStore.KVStoreResultSet; 3904e41f4b71Sopenharmony_citry { 3905e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key', async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 3906e41f4b71Sopenharmony_ci if (err != undefined) { 3907e41f4b71Sopenharmony_ci console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3908e41f4b71Sopenharmony_ci return; 3909e41f4b71Sopenharmony_ci } 3910e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set'); 3911e41f4b71Sopenharmony_ci resultSet = result; 3912e41f4b71Sopenharmony_ci if (kvStore != null) { 3913e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet, (err: BusinessError) => { 3914e41f4b71Sopenharmony_ci if (err != undefined) { 3915e41f4b71Sopenharmony_ci console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 3916e41f4b71Sopenharmony_ci return; 3917e41f4b71Sopenharmony_ci } 3918e41f4b71Sopenharmony_ci console.info('Succeeded in closing result set'); 3919e41f4b71Sopenharmony_ci }) 3920e41f4b71Sopenharmony_ci } 3921e41f4b71Sopenharmony_ci }); 3922e41f4b71Sopenharmony_ci} catch (e) { 3923e41f4b71Sopenharmony_ci let error = e as BusinessError; 3924e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 3925e41f4b71Sopenharmony_ci} 3926e41f4b71Sopenharmony_ci 3927e41f4b71Sopenharmony_ci``` 3928e41f4b71Sopenharmony_ci 3929e41f4b71Sopenharmony_ci### closeResultSet 3930e41f4b71Sopenharmony_ci 3931e41f4b71Sopenharmony_cicloseResultSet(resultSet: KVStoreResultSet): Promise<void> 3932e41f4b71Sopenharmony_ci 3933e41f4b71Sopenharmony_ciCloses the **KVStoreResultSet** object returned by [SingleKvStore.getResultSet](#getresultset-1). This API uses a promise to return the result. 3934e41f4b71Sopenharmony_ci 3935e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3936e41f4b71Sopenharmony_ci 3937e41f4b71Sopenharmony_ci**Parameters** 3938e41f4b71Sopenharmony_ci 3939e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3940e41f4b71Sopenharmony_ci| --------- | ------------------------------------- | ---- | ---------------------------------- | 3941e41f4b71Sopenharmony_ci| resultSet | [KVStoreResultSet](#kvstoreresultset) | Yes | **KVStoreResultSet** object to close.| 3942e41f4b71Sopenharmony_ci 3943e41f4b71Sopenharmony_ci**Return value** 3944e41f4b71Sopenharmony_ci 3945e41f4b71Sopenharmony_ci| Type | Description | 3946e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 3947e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 3948e41f4b71Sopenharmony_ci 3949e41f4b71Sopenharmony_ci**Error codes** 3950e41f4b71Sopenharmony_ci 3951e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 3952e41f4b71Sopenharmony_ci 3953e41f4b71Sopenharmony_ci| ID| **Error Message** | 3954e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 3955e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 3956e41f4b71Sopenharmony_ci 3957e41f4b71Sopenharmony_ci**Example** 3958e41f4b71Sopenharmony_ci 3959e41f4b71Sopenharmony_ci```ts 3960e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 3961e41f4b71Sopenharmony_ci 3962e41f4b71Sopenharmony_cilet resultSet: distributedKVStore.KVStoreResultSet; 3963e41f4b71Sopenharmony_citry { 3964e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 3965e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set'); 3966e41f4b71Sopenharmony_ci resultSet = result; 3967e41f4b71Sopenharmony_ci if (kvStore != null) { 3968e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet).then(() => { 3969e41f4b71Sopenharmony_ci console.info('Succeeded in closing result set'); 3970e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3971e41f4b71Sopenharmony_ci console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 3972e41f4b71Sopenharmony_ci }); 3973e41f4b71Sopenharmony_ci } 3974e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 3975e41f4b71Sopenharmony_ci console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3976e41f4b71Sopenharmony_ci }); 3977e41f4b71Sopenharmony_ci 3978e41f4b71Sopenharmony_ci} catch (e) { 3979e41f4b71Sopenharmony_ci let error = e as BusinessError; 3980e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 3981e41f4b71Sopenharmony_ci} 3982e41f4b71Sopenharmony_ci``` 3983e41f4b71Sopenharmony_ci 3984e41f4b71Sopenharmony_ci### getResultSize 3985e41f4b71Sopenharmony_ci 3986e41f4b71Sopenharmony_cigetResultSize(query: Query, callback: AsyncCallback<number>): void 3987e41f4b71Sopenharmony_ci 3988e41f4b71Sopenharmony_ciObtains the number of results that matches the specified **Query** object. This API uses an asynchronous callback to return the result. 3989e41f4b71Sopenharmony_ci 3990e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 3991e41f4b71Sopenharmony_ci 3992e41f4b71Sopenharmony_ci**Parameters** 3993e41f4b71Sopenharmony_ci 3994e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3995e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ------------------------------------------- | 3996e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match. | 3997e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the number of results obtained.| 3998e41f4b71Sopenharmony_ci 3999e41f4b71Sopenharmony_ci**Error codes** 4000e41f4b71Sopenharmony_ci 4001e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 4002e41f4b71Sopenharmony_ci 4003e41f4b71Sopenharmony_ci| ID| **Error Message** | 4004e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4005e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 4006e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 4007e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4008e41f4b71Sopenharmony_ci 4009e41f4b71Sopenharmony_ci**Example** 4010e41f4b71Sopenharmony_ci 4011e41f4b71Sopenharmony_ci```ts 4012e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4013e41f4b71Sopenharmony_ci 4014e41f4b71Sopenharmony_citry { 4015e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 4016e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 4017e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 4018e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 4019e41f4b71Sopenharmony_ci key: key + i, 4020e41f4b71Sopenharmony_ci value: { 4021e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 4022e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 4023e41f4b71Sopenharmony_ci } 4024e41f4b71Sopenharmony_ci } 4025e41f4b71Sopenharmony_ci entries.push(entry); 4026e41f4b71Sopenharmony_ci } 4027e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 4028e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 4029e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 4030e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 4031e41f4b71Sopenharmony_ci if (kvStore != null) { 4032e41f4b71Sopenharmony_ci kvStore.getResultSize(query, async (err: BusinessError, resultSize: number) => { 4033e41f4b71Sopenharmony_ci if (err != undefined) { 4034e41f4b71Sopenharmony_ci console.error(`Failed to get result size.code is ${err.code},message is ${err.message}`); 4035e41f4b71Sopenharmony_ci return; 4036e41f4b71Sopenharmony_ci } 4037e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set size'); 4038e41f4b71Sopenharmony_ci }); 4039e41f4b71Sopenharmony_ci } 4040e41f4b71Sopenharmony_ci }); 4041e41f4b71Sopenharmony_ci} catch (e) { 4042e41f4b71Sopenharmony_ci let error = e as BusinessError; 4043e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 4044e41f4b71Sopenharmony_ci} 4045e41f4b71Sopenharmony_ci``` 4046e41f4b71Sopenharmony_ci 4047e41f4b71Sopenharmony_ci### getResultSize 4048e41f4b71Sopenharmony_ci 4049e41f4b71Sopenharmony_cigetResultSize(query: Query): Promise<number> 4050e41f4b71Sopenharmony_ci 4051e41f4b71Sopenharmony_ciObtains the number of results that matches the specified **Query** object. This API uses a promise to return the result. 4052e41f4b71Sopenharmony_ci 4053e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4054e41f4b71Sopenharmony_ci 4055e41f4b71Sopenharmony_ci**Parameters** 4056e41f4b71Sopenharmony_ci 4057e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 4058e41f4b71Sopenharmony_ci| ------ | -------------- | ---- | -------------- | 4059e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match.| 4060e41f4b71Sopenharmony_ci 4061e41f4b71Sopenharmony_ci**Return value** 4062e41f4b71Sopenharmony_ci 4063e41f4b71Sopenharmony_ci| Type | Description | 4064e41f4b71Sopenharmony_ci| --------------------- | ----------------------------------------------- | 4065e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the number of results obtained.| 4066e41f4b71Sopenharmony_ci 4067e41f4b71Sopenharmony_ci**Error codes** 4068e41f4b71Sopenharmony_ci 4069e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 4070e41f4b71Sopenharmony_ci 4071e41f4b71Sopenharmony_ci| ID| **Error Message** | 4072e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4073e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 4074e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 4075e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4076e41f4b71Sopenharmony_ci 4077e41f4b71Sopenharmony_ci**Example** 4078e41f4b71Sopenharmony_ci 4079e41f4b71Sopenharmony_ci```ts 4080e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4081e41f4b71Sopenharmony_ci 4082e41f4b71Sopenharmony_citry { 4083e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 4084e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 4085e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 4086e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 4087e41f4b71Sopenharmony_ci key: key + i, 4088e41f4b71Sopenharmony_ci value: { 4089e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 4090e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 4091e41f4b71Sopenharmony_ci } 4092e41f4b71Sopenharmony_ci } 4093e41f4b71Sopenharmony_ci entries.push(entry); 4094e41f4b71Sopenharmony_ci } 4095e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 4096e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 4097e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4098e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 4099e41f4b71Sopenharmony_ci }); 4100e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 4101e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 4102e41f4b71Sopenharmony_ci kvStore.getResultSize(query).then((resultSize: number) => { 4103e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set size'); 4104e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4105e41f4b71Sopenharmony_ci console.error(`Failed to get result size.code is ${err.code},message is ${err.message}`); 4106e41f4b71Sopenharmony_ci }); 4107e41f4b71Sopenharmony_ci} catch (e) { 4108e41f4b71Sopenharmony_ci let error = e as BusinessError; 4109e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 4110e41f4b71Sopenharmony_ci} 4111e41f4b71Sopenharmony_ci``` 4112e41f4b71Sopenharmony_ci 4113e41f4b71Sopenharmony_ci### backup 4114e41f4b71Sopenharmony_ci 4115e41f4b71Sopenharmony_cibackup(file:string, callback: AsyncCallback<void>):void 4116e41f4b71Sopenharmony_ci 4117e41f4b71Sopenharmony_ciBacks up a distributed KV store. This API uses an asynchronous callback to return the result. 4118e41f4b71Sopenharmony_ci 4119e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4120e41f4b71Sopenharmony_ci 4121e41f4b71Sopenharmony_ci**Parameters** 4122e41f4b71Sopenharmony_ci 4123e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4124e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 4125e41f4b71Sopenharmony_ci| file | string | Yes | Name of the KV store. The value cannot be empty or exceed [MAX_KEY_LENGTH](#constants).| 4126e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 4127e41f4b71Sopenharmony_ci 4128e41f4b71Sopenharmony_ci**Error codes** 4129e41f4b71Sopenharmony_ci 4130e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 4131e41f4b71Sopenharmony_ci 4132e41f4b71Sopenharmony_ci| ID| **Error Message** | 4133e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4134e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed. | 4135e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4136e41f4b71Sopenharmony_ci 4137e41f4b71Sopenharmony_ci**Example** 4138e41f4b71Sopenharmony_ci 4139e41f4b71Sopenharmony_ci```ts 4140e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4141e41f4b71Sopenharmony_ci 4142e41f4b71Sopenharmony_cilet backupFile = "BK001"; 4143e41f4b71Sopenharmony_citry { 4144e41f4b71Sopenharmony_ci kvStore.backup(backupFile, (err: BusinessError) => { 4145e41f4b71Sopenharmony_ci if (err) { 4146e41f4b71Sopenharmony_ci console.error(`Failed to backup.code is ${err.code},message is ${err.message} `); 4147e41f4b71Sopenharmony_ci } else { 4148e41f4b71Sopenharmony_ci console.info(`Succeeded in backupping data`); 4149e41f4b71Sopenharmony_ci } 4150e41f4b71Sopenharmony_ci }); 4151e41f4b71Sopenharmony_ci} catch (e) { 4152e41f4b71Sopenharmony_ci let error = e as BusinessError; 4153e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4154e41f4b71Sopenharmony_ci} 4155e41f4b71Sopenharmony_ci``` 4156e41f4b71Sopenharmony_ci 4157e41f4b71Sopenharmony_ci### backup 4158e41f4b71Sopenharmony_ci 4159e41f4b71Sopenharmony_cibackup(file:string): Promise<void> 4160e41f4b71Sopenharmony_ci 4161e41f4b71Sopenharmony_ciBacks up an RDB store. This API uses a promise to return the result. 4162e41f4b71Sopenharmony_ci 4163e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4164e41f4b71Sopenharmony_ci 4165e41f4b71Sopenharmony_ci**Parameters** 4166e41f4b71Sopenharmony_ci 4167e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 4168e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------------------------------------------------------ | 4169e41f4b71Sopenharmony_ci| file | string | Yes | Name of the KV store. The value cannot be empty or exceed [MAX_KEY_LENGTH](#constants).| 4170e41f4b71Sopenharmony_ci 4171e41f4b71Sopenharmony_ci**Return value** 4172e41f4b71Sopenharmony_ci 4173e41f4b71Sopenharmony_ci| Type | Description | 4174e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 4175e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 4176e41f4b71Sopenharmony_ci 4177e41f4b71Sopenharmony_ci**Error codes** 4178e41f4b71Sopenharmony_ci 4179e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 4180e41f4b71Sopenharmony_ci 4181e41f4b71Sopenharmony_ci| ID| **Error Message** | 4182e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4183e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed. | 4184e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4185e41f4b71Sopenharmony_ci 4186e41f4b71Sopenharmony_ci**Example** 4187e41f4b71Sopenharmony_ci 4188e41f4b71Sopenharmony_ci```ts 4189e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4190e41f4b71Sopenharmony_ci 4191e41f4b71Sopenharmony_cilet backupFile = "BK001"; 4192e41f4b71Sopenharmony_citry { 4193e41f4b71Sopenharmony_ci kvStore.backup(backupFile).then(() => { 4194e41f4b71Sopenharmony_ci console.info(`Succeeded in backupping data`); 4195e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4196e41f4b71Sopenharmony_ci console.error(`Failed to backup.code is ${err.code},message is ${err.message}`); 4197e41f4b71Sopenharmony_ci }); 4198e41f4b71Sopenharmony_ci} catch (e) { 4199e41f4b71Sopenharmony_ci let error = e as BusinessError; 4200e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4201e41f4b71Sopenharmony_ci} 4202e41f4b71Sopenharmony_ci``` 4203e41f4b71Sopenharmony_ci 4204e41f4b71Sopenharmony_ci### restore 4205e41f4b71Sopenharmony_ci 4206e41f4b71Sopenharmony_cirestore(file:string, callback: AsyncCallback<void>):void 4207e41f4b71Sopenharmony_ci 4208e41f4b71Sopenharmony_ciRestores a distributed KV store from a database file. This API uses an asynchronous callback to return the result. 4209e41f4b71Sopenharmony_ci 4210e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4211e41f4b71Sopenharmony_ci 4212e41f4b71Sopenharmony_ci**Parameters** 4213e41f4b71Sopenharmony_ci 4214e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4215e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 4216e41f4b71Sopenharmony_ci| file | string | Yes | Name of the database file. The value cannot be empty or exceed [MAX_KEY_LENGTH](#constants).| 4217e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 4218e41f4b71Sopenharmony_ci 4219e41f4b71Sopenharmony_ci**Error codes** 4220e41f4b71Sopenharmony_ci 4221e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 4222e41f4b71Sopenharmony_ci 4223e41f4b71Sopenharmony_ci| ID| **Error Message** | 4224e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4225e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed. | 4226e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4227e41f4b71Sopenharmony_ci 4228e41f4b71Sopenharmony_ci**Example** 4229e41f4b71Sopenharmony_ci 4230e41f4b71Sopenharmony_ci```ts 4231e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4232e41f4b71Sopenharmony_ci 4233e41f4b71Sopenharmony_cilet backupFile = "BK001"; 4234e41f4b71Sopenharmony_citry { 4235e41f4b71Sopenharmony_ci kvStore.restore(backupFile, (err: BusinessError) => { 4236e41f4b71Sopenharmony_ci if (err) { 4237e41f4b71Sopenharmony_ci console.error(`Failed to restore.code is ${err.code},message is ${err.message}`); 4238e41f4b71Sopenharmony_ci } else { 4239e41f4b71Sopenharmony_ci console.info(`Succeeded in restoring data`); 4240e41f4b71Sopenharmony_ci } 4241e41f4b71Sopenharmony_ci }); 4242e41f4b71Sopenharmony_ci} catch (e) { 4243e41f4b71Sopenharmony_ci let error = e as BusinessError; 4244e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4245e41f4b71Sopenharmony_ci} 4246e41f4b71Sopenharmony_ci``` 4247e41f4b71Sopenharmony_ci 4248e41f4b71Sopenharmony_ci### restore 4249e41f4b71Sopenharmony_ci 4250e41f4b71Sopenharmony_cirestore(file:string): Promise<void> 4251e41f4b71Sopenharmony_ci 4252e41f4b71Sopenharmony_ciRestores a distributed KV store from a database file. This API uses a promise to return the result. 4253e41f4b71Sopenharmony_ci 4254e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4255e41f4b71Sopenharmony_ci 4256e41f4b71Sopenharmony_ci**Parameters** 4257e41f4b71Sopenharmony_ci 4258e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 4259e41f4b71Sopenharmony_ci| ------ | -------- | ---- | ------------------------------------------------------------ | 4260e41f4b71Sopenharmony_ci| file | string | Yes | Name of the database file. The value cannot be empty or exceed [MAX_KEY_LENGTH](#constants).| 4261e41f4b71Sopenharmony_ci 4262e41f4b71Sopenharmony_ci**Return value** 4263e41f4b71Sopenharmony_ci 4264e41f4b71Sopenharmony_ci| Type | Description | 4265e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 4266e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 4267e41f4b71Sopenharmony_ci 4268e41f4b71Sopenharmony_ci**Error codes** 4269e41f4b71Sopenharmony_ci 4270e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 4271e41f4b71Sopenharmony_ci 4272e41f4b71Sopenharmony_ci| ID| **Error Message** | 4273e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4274e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed. | 4275e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4276e41f4b71Sopenharmony_ci 4277e41f4b71Sopenharmony_ci**Example** 4278e41f4b71Sopenharmony_ci 4279e41f4b71Sopenharmony_ci```ts 4280e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4281e41f4b71Sopenharmony_ci 4282e41f4b71Sopenharmony_cilet backupFile = "BK001"; 4283e41f4b71Sopenharmony_citry { 4284e41f4b71Sopenharmony_ci kvStore.restore(backupFile).then(() => { 4285e41f4b71Sopenharmony_ci console.info(`Succeeded in restoring data`); 4286e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4287e41f4b71Sopenharmony_ci console.error(`Failed to restore.code is ${err.code},message is ${err.message}`); 4288e41f4b71Sopenharmony_ci }); 4289e41f4b71Sopenharmony_ci} catch (e) { 4290e41f4b71Sopenharmony_ci let error = e as BusinessError; 4291e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4292e41f4b71Sopenharmony_ci} 4293e41f4b71Sopenharmony_ci``` 4294e41f4b71Sopenharmony_ci 4295e41f4b71Sopenharmony_ci### deleteBackup 4296e41f4b71Sopenharmony_ci 4297e41f4b71Sopenharmony_cideleteBackup(files:Array<string>, callback: AsyncCallback<Array<[string, number]>>):void 4298e41f4b71Sopenharmony_ci 4299e41f4b71Sopenharmony_ciDeletes a backup file. This API uses an asynchronous callback to return the result. 4300e41f4b71Sopenharmony_ci 4301e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4302e41f4b71Sopenharmony_ci 4303e41f4b71Sopenharmony_ci**Parameters** 4304e41f4b71Sopenharmony_ci 4305e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4306e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ | 4307e41f4b71Sopenharmony_ci| files | Array<string> | Yes | Name of the backup file to delete. The value cannot be empty or exceed [MAX_KEY_LENGTH](#constants).| 4308e41f4b71Sopenharmony_ci| callback | AsyncCallback<Array<[string, number]>> | Yes | Callback used to return the name of the backup file deleted and the operation result. | 4309e41f4b71Sopenharmony_ci 4310e41f4b71Sopenharmony_ci**Error codes** 4311e41f4b71Sopenharmony_ci 4312e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4313e41f4b71Sopenharmony_ci 4314e41f4b71Sopenharmony_ci| ID| **Error Message** | 4315e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4316e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed. | 4317e41f4b71Sopenharmony_ci 4318e41f4b71Sopenharmony_ci**Example** 4319e41f4b71Sopenharmony_ci 4320e41f4b71Sopenharmony_ci```ts 4321e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4322e41f4b71Sopenharmony_ci 4323e41f4b71Sopenharmony_cilet files = ["BK001", "BK002"]; 4324e41f4b71Sopenharmony_citry { 4325e41f4b71Sopenharmony_ci kvStore.deleteBackup(files, (err: BusinessError, data: [string, number][]) => { 4326e41f4b71Sopenharmony_ci if (err) { 4327e41f4b71Sopenharmony_ci console.error(`Failed to delete Backup.code is ${err.code},message is ${err.message}`); 4328e41f4b71Sopenharmony_ci } else { 4329e41f4b71Sopenharmony_ci console.info(`Succeed in deleting Backup.data=${data}`); 4330e41f4b71Sopenharmony_ci } 4331e41f4b71Sopenharmony_ci }); 4332e41f4b71Sopenharmony_ci} catch (e) { 4333e41f4b71Sopenharmony_ci let error = e as BusinessError; 4334e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4335e41f4b71Sopenharmony_ci} 4336e41f4b71Sopenharmony_ci``` 4337e41f4b71Sopenharmony_ci 4338e41f4b71Sopenharmony_ci### deleteBackup 4339e41f4b71Sopenharmony_ci 4340e41f4b71Sopenharmony_cideleteBackup(files:Array<string>): Promise<Array<[string, number]>> 4341e41f4b71Sopenharmony_ci 4342e41f4b71Sopenharmony_ciDeletes a backup file. This API uses a promise to return the result. 4343e41f4b71Sopenharmony_ci 4344e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4345e41f4b71Sopenharmony_ci 4346e41f4b71Sopenharmony_ci**Parameters** 4347e41f4b71Sopenharmony_ci 4348e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 4349e41f4b71Sopenharmony_ci| ------ | ------------------- | ---- | ------------------------------------------------------------ | 4350e41f4b71Sopenharmony_ci| files | Array<string> | Yes | Name of the backup file to delete. The value cannot be empty or exceed [MAX_KEY_LENGTH](#constants).| 4351e41f4b71Sopenharmony_ci 4352e41f4b71Sopenharmony_ci**Return value** 4353e41f4b71Sopenharmony_ci 4354e41f4b71Sopenharmony_ci| Type | Description | 4355e41f4b71Sopenharmony_ci| -------------------------------------------- | ----------------------------------------------- | 4356e41f4b71Sopenharmony_ci| Promise<Array<[string, number]>> | Promise used to return the name of the backup file deleted and the operation result.| 4357e41f4b71Sopenharmony_ci 4358e41f4b71Sopenharmony_ci**Error codes** 4359e41f4b71Sopenharmony_ci 4360e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4361e41f4b71Sopenharmony_ci 4362e41f4b71Sopenharmony_ci| ID| **Error Message** | 4363e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4364e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Parameter verification failed. | 4365e41f4b71Sopenharmony_ci 4366e41f4b71Sopenharmony_ci**Example** 4367e41f4b71Sopenharmony_ci 4368e41f4b71Sopenharmony_ci```ts 4369e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4370e41f4b71Sopenharmony_ci 4371e41f4b71Sopenharmony_cilet files = ["BK001", "BK002"]; 4372e41f4b71Sopenharmony_citry { 4373e41f4b71Sopenharmony_ci kvStore.deleteBackup(files).then((data: [string, number][]) => { 4374e41f4b71Sopenharmony_ci console.info(`Succeed in deleting Backup.data=${data}`); 4375e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4376e41f4b71Sopenharmony_ci console.error(`Failed to delete Backup.code is ${err.code},message is ${err.message}`); 4377e41f4b71Sopenharmony_ci }) 4378e41f4b71Sopenharmony_ci} catch (e) { 4379e41f4b71Sopenharmony_ci let error = e as BusinessError; 4380e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4381e41f4b71Sopenharmony_ci} 4382e41f4b71Sopenharmony_ci``` 4383e41f4b71Sopenharmony_ci 4384e41f4b71Sopenharmony_ci### startTransaction 4385e41f4b71Sopenharmony_ci 4386e41f4b71Sopenharmony_cistartTransaction(callback: AsyncCallback<void>): void 4387e41f4b71Sopenharmony_ci 4388e41f4b71Sopenharmony_ciStarts the transaction in this single KV store. This API uses an asynchronous callback to return the result. 4389e41f4b71Sopenharmony_ci 4390e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4391e41f4b71Sopenharmony_ci 4392e41f4b71Sopenharmony_ci**Parameters** 4393e41f4b71Sopenharmony_ci 4394e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4395e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------- | 4396e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 4397e41f4b71Sopenharmony_ci 4398e41f4b71Sopenharmony_ci**Error codes** 4399e41f4b71Sopenharmony_ci 4400e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md). 4401e41f4b71Sopenharmony_ci 4402e41f4b71Sopenharmony_ci| ID| **Error Message** | 4403e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- | 4404e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4405e41f4b71Sopenharmony_ci 4406e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md). 4407e41f4b71Sopenharmony_ci 4408e41f4b71Sopenharmony_ci| ID| **Error Message** | 4409e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- | 4410e41f4b71Sopenharmony_ci| 14800047 | The WAL file size exceeds the default limit. | 4411e41f4b71Sopenharmony_ci 4412e41f4b71Sopenharmony_ci**Example** 4413e41f4b71Sopenharmony_ci 4414e41f4b71Sopenharmony_ci```ts 4415e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4416e41f4b71Sopenharmony_ci 4417e41f4b71Sopenharmony_cifunction putBatchString(len: number, prefix: string) { 4418e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 4419e41f4b71Sopenharmony_ci for (let i = 0; i < len; i++) { 4420e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 4421e41f4b71Sopenharmony_ci key: prefix + i, 4422e41f4b71Sopenharmony_ci value: { 4423e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 4424e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 4425e41f4b71Sopenharmony_ci } 4426e41f4b71Sopenharmony_ci } 4427e41f4b71Sopenharmony_ci entries.push(entry); 4428e41f4b71Sopenharmony_ci } 4429e41f4b71Sopenharmony_ci return entries; 4430e41f4b71Sopenharmony_ci} // Custom function used outside the scope to prevent syntax check errors. 4431e41f4b71Sopenharmony_ci 4432e41f4b71Sopenharmony_citry { 4433e41f4b71Sopenharmony_ci let count = 0; 4434e41f4b71Sopenharmony_ci kvStore.on('dataChange', 0, (data: distributedKVStore.ChangeNotification) => { 4435e41f4b71Sopenharmony_ci console.info(`startTransaction 0 ${data}`); 4436e41f4b71Sopenharmony_ci count++; 4437e41f4b71Sopenharmony_ci }); 4438e41f4b71Sopenharmony_ci kvStore.startTransaction(async (err: BusinessError) => { 4439e41f4b71Sopenharmony_ci if (err != undefined) { 4440e41f4b71Sopenharmony_ci console.error(`Failed to start Transaction.code is ${err.code},message is ${err.message}`); 4441e41f4b71Sopenharmony_ci return; 4442e41f4b71Sopenharmony_ci } 4443e41f4b71Sopenharmony_ci console.info('Succeeded in starting Transaction'); 4444e41f4b71Sopenharmony_ci let entries = putBatchString(10, 'batch_test_string_key'); 4445e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 4446e41f4b71Sopenharmony_ci if (kvStore != null) { 4447e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 4448e41f4b71Sopenharmony_ci if (err != undefined) { 4449e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 4450e41f4b71Sopenharmony_ci return; 4451e41f4b71Sopenharmony_ci } 4452e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 4453e41f4b71Sopenharmony_ci }); 4454e41f4b71Sopenharmony_ci } 4455e41f4b71Sopenharmony_ci }); 4456e41f4b71Sopenharmony_ci} catch (e) { 4457e41f4b71Sopenharmony_ci let error = e as BusinessError; 4458e41f4b71Sopenharmony_ci console.error(`Failed to start Transaction.code is ${error.code},message is ${error.message}`); 4459e41f4b71Sopenharmony_ci} 4460e41f4b71Sopenharmony_ci``` 4461e41f4b71Sopenharmony_ci 4462e41f4b71Sopenharmony_ci### startTransaction 4463e41f4b71Sopenharmony_ci 4464e41f4b71Sopenharmony_cistartTransaction(): Promise<void> 4465e41f4b71Sopenharmony_ci 4466e41f4b71Sopenharmony_ciStarts the transaction in this single KV store. This API uses a promise to return the result. 4467e41f4b71Sopenharmony_ci 4468e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4469e41f4b71Sopenharmony_ci 4470e41f4b71Sopenharmony_ci**Return value** 4471e41f4b71Sopenharmony_ci 4472e41f4b71Sopenharmony_ci| Type | Description | 4473e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 4474e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 4475e41f4b71Sopenharmony_ci 4476e41f4b71Sopenharmony_ci**Error codes** 4477e41f4b71Sopenharmony_ci 4478e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md). 4479e41f4b71Sopenharmony_ci 4480e41f4b71Sopenharmony_ci| ID| **Error Message** | 4481e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- | 4482e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4483e41f4b71Sopenharmony_ci 4484e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md). 4485e41f4b71Sopenharmony_ci 4486e41f4b71Sopenharmony_ci| ID| **Error Message** | 4487e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- | 4488e41f4b71Sopenharmony_ci| 14800047 | The WAL file size exceeds the default limit. | 4489e41f4b71Sopenharmony_ci 4490e41f4b71Sopenharmony_ci**Example** 4491e41f4b71Sopenharmony_ci 4492e41f4b71Sopenharmony_ci```ts 4493e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4494e41f4b71Sopenharmony_ci 4495e41f4b71Sopenharmony_citry { 4496e41f4b71Sopenharmony_ci let count = 0; 4497e41f4b71Sopenharmony_ci kvStore.on('dataChange', distributedKVStore.SubscribeType.SUBSCRIBE_TYPE_ALL, (data: distributedKVStore.ChangeNotification) => { 4498e41f4b71Sopenharmony_ci console.info(`startTransaction 0 ${data}`); 4499e41f4b71Sopenharmony_ci count++; 4500e41f4b71Sopenharmony_ci }); 4501e41f4b71Sopenharmony_ci kvStore.startTransaction().then(async () => { 4502e41f4b71Sopenharmony_ci console.info('Succeeded in starting Transaction'); 4503e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4504e41f4b71Sopenharmony_ci console.error(`Failed to start Transaction.code is ${err.code},message is ${err.message}`); 4505e41f4b71Sopenharmony_ci }); 4506e41f4b71Sopenharmony_ci} catch (e) { 4507e41f4b71Sopenharmony_ci let error = e as BusinessError; 4508e41f4b71Sopenharmony_ci console.error(`Failed to start Transaction.code is ${error.code},message is ${error.message}`); 4509e41f4b71Sopenharmony_ci} 4510e41f4b71Sopenharmony_ci``` 4511e41f4b71Sopenharmony_ci 4512e41f4b71Sopenharmony_ci### commit 4513e41f4b71Sopenharmony_ci 4514e41f4b71Sopenharmony_cicommit(callback: AsyncCallback<void>): void 4515e41f4b71Sopenharmony_ci 4516e41f4b71Sopenharmony_ciCommits the transaction in this single KV store. This API uses an asynchronous callback to return the result. 4517e41f4b71Sopenharmony_ci 4518e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4519e41f4b71Sopenharmony_ci 4520e41f4b71Sopenharmony_ci**Parameters** 4521e41f4b71Sopenharmony_ci 4522e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4523e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------- | 4524e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 4525e41f4b71Sopenharmony_ci 4526e41f4b71Sopenharmony_ci**Error codes** 4527e41f4b71Sopenharmony_ci 4528e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md). 4529e41f4b71Sopenharmony_ci 4530e41f4b71Sopenharmony_ci| ID| **Error Message** | 4531e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4532e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4533e41f4b71Sopenharmony_ci 4534e41f4b71Sopenharmony_ci**Example** 4535e41f4b71Sopenharmony_ci 4536e41f4b71Sopenharmony_ci```ts 4537e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4538e41f4b71Sopenharmony_ci 4539e41f4b71Sopenharmony_citry { 4540e41f4b71Sopenharmony_ci kvStore.commit((err: BusinessError) => { 4541e41f4b71Sopenharmony_ci if (err == undefined) { 4542e41f4b71Sopenharmony_ci console.info('Succeeded in committing'); 4543e41f4b71Sopenharmony_ci } else { 4544e41f4b71Sopenharmony_ci console.error(`Failed to commit.code is ${err.code},message is ${err.message}`); 4545e41f4b71Sopenharmony_ci } 4546e41f4b71Sopenharmony_ci }); 4547e41f4b71Sopenharmony_ci} catch (e) { 4548e41f4b71Sopenharmony_ci let error = e as BusinessError; 4549e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4550e41f4b71Sopenharmony_ci} 4551e41f4b71Sopenharmony_ci``` 4552e41f4b71Sopenharmony_ci 4553e41f4b71Sopenharmony_ci### commit 4554e41f4b71Sopenharmony_ci 4555e41f4b71Sopenharmony_cicommit(): Promise<void> 4556e41f4b71Sopenharmony_ci 4557e41f4b71Sopenharmony_ciCommits the transaction in this single KV store. This API uses a promise to return the result. 4558e41f4b71Sopenharmony_ci 4559e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4560e41f4b71Sopenharmony_ci 4561e41f4b71Sopenharmony_ci**Return value** 4562e41f4b71Sopenharmony_ci 4563e41f4b71Sopenharmony_ci| Type | Description | 4564e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 4565e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 4566e41f4b71Sopenharmony_ci 4567e41f4b71Sopenharmony_ci**Error codes** 4568e41f4b71Sopenharmony_ci 4569e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md). 4570e41f4b71Sopenharmony_ci 4571e41f4b71Sopenharmony_ci| ID| **Error Message** | 4572e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4573e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4574e41f4b71Sopenharmony_ci 4575e41f4b71Sopenharmony_ci**Example** 4576e41f4b71Sopenharmony_ci 4577e41f4b71Sopenharmony_ci```ts 4578e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4579e41f4b71Sopenharmony_ci 4580e41f4b71Sopenharmony_citry { 4581e41f4b71Sopenharmony_ci kvStore.commit().then(async () => { 4582e41f4b71Sopenharmony_ci console.info('Succeeded in committing'); 4583e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4584e41f4b71Sopenharmony_ci console.error(`Failed to commit.code is ${err.code},message is ${err.message}`); 4585e41f4b71Sopenharmony_ci }); 4586e41f4b71Sopenharmony_ci} catch (e) { 4587e41f4b71Sopenharmony_ci let error = e as BusinessError; 4588e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4589e41f4b71Sopenharmony_ci} 4590e41f4b71Sopenharmony_ci``` 4591e41f4b71Sopenharmony_ci 4592e41f4b71Sopenharmony_ci### rollback 4593e41f4b71Sopenharmony_ci 4594e41f4b71Sopenharmony_cirollback(callback: AsyncCallback<void>): void 4595e41f4b71Sopenharmony_ci 4596e41f4b71Sopenharmony_ciRolls back the transaction in this single KV store. This API uses an asynchronous callback to return the result. 4597e41f4b71Sopenharmony_ci 4598e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4599e41f4b71Sopenharmony_ci 4600e41f4b71Sopenharmony_ci**Parameters** 4601e41f4b71Sopenharmony_ci 4602e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4603e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | ---------- | 4604e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 4605e41f4b71Sopenharmony_ci 4606e41f4b71Sopenharmony_ci**Error codes** 4607e41f4b71Sopenharmony_ci 4608e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md). 4609e41f4b71Sopenharmony_ci 4610e41f4b71Sopenharmony_ci| ID| **Error Message** | 4611e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4612e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4613e41f4b71Sopenharmony_ci 4614e41f4b71Sopenharmony_ci**Example** 4615e41f4b71Sopenharmony_ci 4616e41f4b71Sopenharmony_ci```ts 4617e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4618e41f4b71Sopenharmony_ci 4619e41f4b71Sopenharmony_citry { 4620e41f4b71Sopenharmony_ci kvStore.rollback((err: BusinessError) => { 4621e41f4b71Sopenharmony_ci if (err == undefined) { 4622e41f4b71Sopenharmony_ci console.info('Succeeded in rolling back'); 4623e41f4b71Sopenharmony_ci } else { 4624e41f4b71Sopenharmony_ci console.error(`Failed to rollback.code is ${err.code},message is ${err.message}`); 4625e41f4b71Sopenharmony_ci } 4626e41f4b71Sopenharmony_ci }); 4627e41f4b71Sopenharmony_ci} catch (e) { 4628e41f4b71Sopenharmony_ci let error = e as BusinessError; 4629e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4630e41f4b71Sopenharmony_ci} 4631e41f4b71Sopenharmony_ci``` 4632e41f4b71Sopenharmony_ci 4633e41f4b71Sopenharmony_ci### rollback 4634e41f4b71Sopenharmony_ci 4635e41f4b71Sopenharmony_cirollback(): Promise<void> 4636e41f4b71Sopenharmony_ci 4637e41f4b71Sopenharmony_ciRolls back the transaction in this single KV store. This API uses a promise to return the result. 4638e41f4b71Sopenharmony_ci 4639e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4640e41f4b71Sopenharmony_ci 4641e41f4b71Sopenharmony_ci**Return value** 4642e41f4b71Sopenharmony_ci 4643e41f4b71Sopenharmony_ci| Type | Description | 4644e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 4645e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 4646e41f4b71Sopenharmony_ci 4647e41f4b71Sopenharmony_ci**Error codes** 4648e41f4b71Sopenharmony_ci 4649e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md). 4650e41f4b71Sopenharmony_ci 4651e41f4b71Sopenharmony_ci| ID| **Error Message** | 4652e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4653e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 4654e41f4b71Sopenharmony_ci 4655e41f4b71Sopenharmony_ci**Example** 4656e41f4b71Sopenharmony_ci 4657e41f4b71Sopenharmony_ci```ts 4658e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4659e41f4b71Sopenharmony_ci 4660e41f4b71Sopenharmony_citry { 4661e41f4b71Sopenharmony_ci kvStore.rollback().then(async () => { 4662e41f4b71Sopenharmony_ci console.info('Succeeded in rolling back'); 4663e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4664e41f4b71Sopenharmony_ci console.error(`Failed to rollback.code is ${err.code},message is ${err.message}`); 4665e41f4b71Sopenharmony_ci }); 4666e41f4b71Sopenharmony_ci} catch (e) { 4667e41f4b71Sopenharmony_ci let error = e as BusinessError; 4668e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4669e41f4b71Sopenharmony_ci} 4670e41f4b71Sopenharmony_ci``` 4671e41f4b71Sopenharmony_ci 4672e41f4b71Sopenharmony_ci### enableSync 4673e41f4b71Sopenharmony_ci 4674e41f4b71Sopenharmony_cienableSync(enabled: boolean, callback: AsyncCallback<void>): void 4675e41f4b71Sopenharmony_ci 4676e41f4b71Sopenharmony_ciSets data sync, which can be enabled or disabled. This API uses an asynchronous callback to return the result. 4677e41f4b71Sopenharmony_ci 4678e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4679e41f4b71Sopenharmony_ci 4680e41f4b71Sopenharmony_ci**Parameters** 4681e41f4b71Sopenharmony_ci 4682e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4683e41f4b71Sopenharmony_ci| -------- | ------------------------- | ---- | --------------------------------------------------------- | 4684e41f4b71Sopenharmony_ci| enabled | boolean | Yes | Whether to enable data sync. The value **true** means to enable data sync, and **false** means the opposite.| 4685e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 4686e41f4b71Sopenharmony_ci 4687e41f4b71Sopenharmony_ci**Error codes** 4688e41f4b71Sopenharmony_ci 4689e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4690e41f4b71Sopenharmony_ci 4691e41f4b71Sopenharmony_ci| ID| **Error Message** | 4692e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4693e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameters types. | 4694e41f4b71Sopenharmony_ci 4695e41f4b71Sopenharmony_ci**Example** 4696e41f4b71Sopenharmony_ci 4697e41f4b71Sopenharmony_ci```ts 4698e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4699e41f4b71Sopenharmony_ci 4700e41f4b71Sopenharmony_citry { 4701e41f4b71Sopenharmony_ci kvStore.enableSync(true, (err: BusinessError) => { 4702e41f4b71Sopenharmony_ci if (err == undefined) { 4703e41f4b71Sopenharmony_ci console.info('Succeeded in enabling sync'); 4704e41f4b71Sopenharmony_ci } else { 4705e41f4b71Sopenharmony_ci console.error(`Failed to enable sync.code is ${err.code},message is ${err.message}`); 4706e41f4b71Sopenharmony_ci } 4707e41f4b71Sopenharmony_ci }); 4708e41f4b71Sopenharmony_ci} catch (e) { 4709e41f4b71Sopenharmony_ci let error = e as BusinessError; 4710e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4711e41f4b71Sopenharmony_ci} 4712e41f4b71Sopenharmony_ci``` 4713e41f4b71Sopenharmony_ci 4714e41f4b71Sopenharmony_ci### enableSync 4715e41f4b71Sopenharmony_ci 4716e41f4b71Sopenharmony_cienableSync(enabled: boolean): Promise<void> 4717e41f4b71Sopenharmony_ci 4718e41f4b71Sopenharmony_ciSets data sync, which can be enabled or disabled. This API uses a promise to return the result. 4719e41f4b71Sopenharmony_ci 4720e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4721e41f4b71Sopenharmony_ci 4722e41f4b71Sopenharmony_ci**Parameters** 4723e41f4b71Sopenharmony_ci 4724e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 4725e41f4b71Sopenharmony_ci| ------- | -------- | ---- | --------------------------------------------------------- | 4726e41f4b71Sopenharmony_ci| enabled | boolean | Yes | Whether to enable data sync. The value **true** means to enable data sync, and **false** means the opposite.| 4727e41f4b71Sopenharmony_ci 4728e41f4b71Sopenharmony_ci**Return value** 4729e41f4b71Sopenharmony_ci 4730e41f4b71Sopenharmony_ci| Type | Description | 4731e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 4732e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 4733e41f4b71Sopenharmony_ci 4734e41f4b71Sopenharmony_ci**Error codes** 4735e41f4b71Sopenharmony_ci 4736e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4737e41f4b71Sopenharmony_ci 4738e41f4b71Sopenharmony_ci| ID| **Error Message** | 4739e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 4740e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 4741e41f4b71Sopenharmony_ci 4742e41f4b71Sopenharmony_ci**Example** 4743e41f4b71Sopenharmony_ci 4744e41f4b71Sopenharmony_ci```ts 4745e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4746e41f4b71Sopenharmony_ci 4747e41f4b71Sopenharmony_citry { 4748e41f4b71Sopenharmony_ci kvStore.enableSync(true).then(() => { 4749e41f4b71Sopenharmony_ci console.info('Succeeded in enabling sync'); 4750e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4751e41f4b71Sopenharmony_ci console.error(`Failed to enable sync.code is ${err.code},message is ${err.message}`); 4752e41f4b71Sopenharmony_ci }); 4753e41f4b71Sopenharmony_ci} catch (e) { 4754e41f4b71Sopenharmony_ci let error = e as BusinessError; 4755e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4756e41f4b71Sopenharmony_ci} 4757e41f4b71Sopenharmony_ci``` 4758e41f4b71Sopenharmony_ci 4759e41f4b71Sopenharmony_ci### setSyncRange 4760e41f4b71Sopenharmony_ci 4761e41f4b71Sopenharmony_cisetSyncRange(localLabels: string[], remoteSupportLabels: string[], callback: AsyncCallback<void>): void 4762e41f4b71Sopenharmony_ci 4763e41f4b71Sopenharmony_ciSets the data sync range. This API uses an asynchronous callback to return the result. 4764e41f4b71Sopenharmony_ci 4765e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4766e41f4b71Sopenharmony_ci 4767e41f4b71Sopenharmony_ci**Parameters** 4768e41f4b71Sopenharmony_ci 4769e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4770e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | ---- | -------------------------------- | 4771e41f4b71Sopenharmony_ci| localLabels | string[] | Yes | Sync labels set for the local device. | 4772e41f4b71Sopenharmony_ci| remoteSupportLabels | string[] | Yes | Sync labels set for remote devices.| 4773e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 4774e41f4b71Sopenharmony_ci 4775e41f4b71Sopenharmony_ci**Error codes** 4776e41f4b71Sopenharmony_ci 4777e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4778e41f4b71Sopenharmony_ci 4779e41f4b71Sopenharmony_ci| ID| **Error Message**| 4780e41f4b71Sopenharmony_ci| ------------ | ------------ | 4781e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 4782e41f4b71Sopenharmony_ci 4783e41f4b71Sopenharmony_ci**Example** 4784e41f4b71Sopenharmony_ci 4785e41f4b71Sopenharmony_ci```ts 4786e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4787e41f4b71Sopenharmony_ci 4788e41f4b71Sopenharmony_citry { 4789e41f4b71Sopenharmony_ci const localLabels = ['A', 'B']; 4790e41f4b71Sopenharmony_ci const remoteSupportLabels = ['C', 'D']; 4791e41f4b71Sopenharmony_ci kvStore.setSyncRange(localLabels, remoteSupportLabels, (err: BusinessError) => { 4792e41f4b71Sopenharmony_ci if (err != undefined) { 4793e41f4b71Sopenharmony_ci console.error(`Failed to set syncRange.code is ${err.code},message is ${err.message}`); 4794e41f4b71Sopenharmony_ci return; 4795e41f4b71Sopenharmony_ci } 4796e41f4b71Sopenharmony_ci console.info('Succeeded in setting syncRange'); 4797e41f4b71Sopenharmony_ci }); 4798e41f4b71Sopenharmony_ci} catch (e) { 4799e41f4b71Sopenharmony_ci let error = e as BusinessError; 4800e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4801e41f4b71Sopenharmony_ci} 4802e41f4b71Sopenharmony_ci``` 4803e41f4b71Sopenharmony_ci 4804e41f4b71Sopenharmony_ci### setSyncRange 4805e41f4b71Sopenharmony_ci 4806e41f4b71Sopenharmony_cisetSyncRange(localLabels: string[], remoteSupportLabels: string[]): Promise<void> 4807e41f4b71Sopenharmony_ci 4808e41f4b71Sopenharmony_ciSets the data sync range. This API uses a promise to return the result. 4809e41f4b71Sopenharmony_ci 4810e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4811e41f4b71Sopenharmony_ci 4812e41f4b71Sopenharmony_ci**Parameters** 4813e41f4b71Sopenharmony_ci 4814e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 4815e41f4b71Sopenharmony_ci| ------------------- | -------- | ---- | -------------------------------- | 4816e41f4b71Sopenharmony_ci| localLabels | string[] | Yes | Sync labels set for the local device. | 4817e41f4b71Sopenharmony_ci| remoteSupportLabels | string[] | Yes | Sync labels set for remote devices.| 4818e41f4b71Sopenharmony_ci 4819e41f4b71Sopenharmony_ci**Return value** 4820e41f4b71Sopenharmony_ci 4821e41f4b71Sopenharmony_ci| Type | Description | 4822e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 4823e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 4824e41f4b71Sopenharmony_ci 4825e41f4b71Sopenharmony_ci**Error codes** 4826e41f4b71Sopenharmony_ci 4827e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4828e41f4b71Sopenharmony_ci 4829e41f4b71Sopenharmony_ci| ID| **Error Message**| 4830e41f4b71Sopenharmony_ci| ------------ | ------------ | 4831e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 4832e41f4b71Sopenharmony_ci 4833e41f4b71Sopenharmony_ci**Example** 4834e41f4b71Sopenharmony_ci 4835e41f4b71Sopenharmony_ci```ts 4836e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4837e41f4b71Sopenharmony_ci 4838e41f4b71Sopenharmony_citry { 4839e41f4b71Sopenharmony_ci const localLabels = ['A', 'B']; 4840e41f4b71Sopenharmony_ci const remoteSupportLabels = ['C', 'D']; 4841e41f4b71Sopenharmony_ci kvStore.setSyncRange(localLabels, remoteSupportLabels).then(() => { 4842e41f4b71Sopenharmony_ci console.info('Succeeded in setting syncRange'); 4843e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4844e41f4b71Sopenharmony_ci console.error(`Failed to set syncRange.code is ${err.code},message is ${err.message}`); 4845e41f4b71Sopenharmony_ci }); 4846e41f4b71Sopenharmony_ci} catch (e) { 4847e41f4b71Sopenharmony_ci let error = e as BusinessError; 4848e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4849e41f4b71Sopenharmony_ci} 4850e41f4b71Sopenharmony_ci``` 4851e41f4b71Sopenharmony_ci 4852e41f4b71Sopenharmony_ci### setSyncParam 4853e41f4b71Sopenharmony_ci 4854e41f4b71Sopenharmony_cisetSyncParam(defaultAllowedDelayMs: number, callback: AsyncCallback<void>): void 4855e41f4b71Sopenharmony_ci 4856e41f4b71Sopenharmony_ciSets the default delay allowed for KV store sync. This API uses an asynchronous callback to return the result. 4857e41f4b71Sopenharmony_ci 4858e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4859e41f4b71Sopenharmony_ci 4860e41f4b71Sopenharmony_ci**Parameters** 4861e41f4b71Sopenharmony_ci 4862e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4863e41f4b71Sopenharmony_ci| --------------------- | ------------------------- | ---- | -------------------------------------------- | 4864e41f4b71Sopenharmony_ci| defaultAllowedDelayMs | number | Yes | Default delay allowed for database sync, in ms.| 4865e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 4866e41f4b71Sopenharmony_ci 4867e41f4b71Sopenharmony_ci**Error codes** 4868e41f4b71Sopenharmony_ci 4869e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4870e41f4b71Sopenharmony_ci 4871e41f4b71Sopenharmony_ci| ID| **Error Message**| 4872e41f4b71Sopenharmony_ci| ------------ | ------------ | 4873e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 4874e41f4b71Sopenharmony_ci 4875e41f4b71Sopenharmony_ci**Example** 4876e41f4b71Sopenharmony_ci 4877e41f4b71Sopenharmony_ci```ts 4878e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4879e41f4b71Sopenharmony_ci 4880e41f4b71Sopenharmony_citry { 4881e41f4b71Sopenharmony_ci const defaultAllowedDelayMs = 500; 4882e41f4b71Sopenharmony_ci kvStore.setSyncParam(defaultAllowedDelayMs, (err: BusinessError) => { 4883e41f4b71Sopenharmony_ci if (err != undefined) { 4884e41f4b71Sopenharmony_ci console.error(`Failed to set syncParam.code is ${err.code},message is ${err.message}`); 4885e41f4b71Sopenharmony_ci return; 4886e41f4b71Sopenharmony_ci } 4887e41f4b71Sopenharmony_ci console.info('Succeeded in setting syncParam'); 4888e41f4b71Sopenharmony_ci }); 4889e41f4b71Sopenharmony_ci} catch (e) { 4890e41f4b71Sopenharmony_ci let error = e as BusinessError; 4891e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4892e41f4b71Sopenharmony_ci} 4893e41f4b71Sopenharmony_ci``` 4894e41f4b71Sopenharmony_ci 4895e41f4b71Sopenharmony_ci### setSyncParam 4896e41f4b71Sopenharmony_ci 4897e41f4b71Sopenharmony_cisetSyncParam(defaultAllowedDelayMs: number): Promise<void> 4898e41f4b71Sopenharmony_ci 4899e41f4b71Sopenharmony_ciSets the default delay allowed for KV store sync. This API uses a promise to return the result. 4900e41f4b71Sopenharmony_ci 4901e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4902e41f4b71Sopenharmony_ci 4903e41f4b71Sopenharmony_ci**Parameters** 4904e41f4b71Sopenharmony_ci 4905e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 4906e41f4b71Sopenharmony_ci| --------------------- | -------- | ---- | -------------------------------------------- | 4907e41f4b71Sopenharmony_ci| defaultAllowedDelayMs | number | Yes | Default delay allowed for database sync, in ms.| 4908e41f4b71Sopenharmony_ci 4909e41f4b71Sopenharmony_ci**Return value** 4910e41f4b71Sopenharmony_ci 4911e41f4b71Sopenharmony_ci| Type | Description | 4912e41f4b71Sopenharmony_ci| ------------------- | ------------------------- | 4913e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 4914e41f4b71Sopenharmony_ci 4915e41f4b71Sopenharmony_ci**Error codes** 4916e41f4b71Sopenharmony_ci 4917e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 4918e41f4b71Sopenharmony_ci 4919e41f4b71Sopenharmony_ci| ID| **Error Message**| 4920e41f4b71Sopenharmony_ci| ------------ | ------------ | 4921e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 4922e41f4b71Sopenharmony_ci 4923e41f4b71Sopenharmony_ci**Example** 4924e41f4b71Sopenharmony_ci 4925e41f4b71Sopenharmony_ci```ts 4926e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4927e41f4b71Sopenharmony_ci 4928e41f4b71Sopenharmony_citry { 4929e41f4b71Sopenharmony_ci const defaultAllowedDelayMs = 500; 4930e41f4b71Sopenharmony_ci kvStore.setSyncParam(defaultAllowedDelayMs).then(() => { 4931e41f4b71Sopenharmony_ci console.info('Succeeded in setting syncParam'); 4932e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 4933e41f4b71Sopenharmony_ci console.error(`Failed to set syncParam.code is ${err.code},message is ${err.message}`); 4934e41f4b71Sopenharmony_ci }); 4935e41f4b71Sopenharmony_ci} catch (e) { 4936e41f4b71Sopenharmony_ci let error = e as BusinessError; 4937e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4938e41f4b71Sopenharmony_ci} 4939e41f4b71Sopenharmony_ci``` 4940e41f4b71Sopenharmony_ci 4941e41f4b71Sopenharmony_ci### sync 4942e41f4b71Sopenharmony_ci 4943e41f4b71Sopenharmony_cisync(deviceIds: string[], mode: SyncMode, delayMs?: number): void 4944e41f4b71Sopenharmony_ci 4945e41f4b71Sopenharmony_ciSynchronizes the KV store manually. For details about the sync modes of KV stores, see [Cross-Device Synchronization of KV Stores](../../database/data-sync-of-kv-store.md). 4946e41f4b71Sopenharmony_ci> **NOTE** 4947e41f4b71Sopenharmony_ci> 4948e41f4b71Sopenharmony_ci> **deviceIds** is **networkId** in [DeviceBasicInfo](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#devicebasicinfo), which can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 4949e41f4b71Sopenharmony_ci 4950e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 4951e41f4b71Sopenharmony_ci 4952e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 4953e41f4b71Sopenharmony_ci 4954e41f4b71Sopenharmony_ci**Parameters** 4955e41f4b71Sopenharmony_ci 4956e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4957e41f4b71Sopenharmony_ci| --------- | --------------------- | ---- | ---------------------------------------------- | 4958e41f4b71Sopenharmony_ci| deviceIds | string[] | Yes | List of **networkId**s of the devices in the same networking environment to be synchronized.| 4959e41f4b71Sopenharmony_ci| mode | [SyncMode](#syncmode) | Yes | Sync mode. | 4960e41f4b71Sopenharmony_ci| delayMs | number | No | Delay time allowed, in ms. The default value is **0**. | 4961e41f4b71Sopenharmony_ci 4962e41f4b71Sopenharmony_ci**Error codes** 4963e41f4b71Sopenharmony_ci 4964e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 4965e41f4b71Sopenharmony_ci 4966e41f4b71Sopenharmony_ci| ID| **Error Message** | 4967e41f4b71Sopenharmony_ci| ------------ | ------------------- | 4968e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 4969e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 4970e41f4b71Sopenharmony_ci| 15100004 | Not found. | 4971e41f4b71Sopenharmony_ci 4972e41f4b71Sopenharmony_ci**Example** 4973e41f4b71Sopenharmony_ci 4974e41f4b71Sopenharmony_ci```ts 4975e41f4b71Sopenharmony_ciimport { distributedDeviceManager } from '@kit.DistributedServiceKit'; 4976e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 4977e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 4978e41f4b71Sopenharmony_ci 4979e41f4b71Sopenharmony_cilet devManager: distributedDeviceManager.DeviceManager; 4980e41f4b71Sopenharmony_ciconst KEY_TEST_SYNC_ELEMENT = 'key_test_sync'; 4981e41f4b71Sopenharmony_ciconst VALUE_TEST_SYNC_ELEMENT = 'value-string-001'; 4982e41f4b71Sopenharmony_ci// create deviceManager 4983e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 4984e41f4b71Sopenharmony_ci onCreate() { 4985e41f4b71Sopenharmony_ci let context = this.context; 4986e41f4b71Sopenharmony_ci try { 4987e41f4b71Sopenharmony_ci devManager = distributedDeviceManager.createDeviceManager(context.applicationInfo.name); 4988e41f4b71Sopenharmony_ci let deviceIds: string[] = []; 4989e41f4b71Sopenharmony_ci if (devManager != null) { 4990e41f4b71Sopenharmony_ci let devices = devManager.getAvailableDeviceListSync(); 4991e41f4b71Sopenharmony_ci for (let i = 0; i < devices.length; i++) { 4992e41f4b71Sopenharmony_ci deviceIds[i] = devices[i].networkId as string; 4993e41f4b71Sopenharmony_ci } 4994e41f4b71Sopenharmony_ci } 4995e41f4b71Sopenharmony_ci try { 4996e41f4b71Sopenharmony_ci if (kvStore != null) { 4997e41f4b71Sopenharmony_ci kvStore.on('syncComplete', (data: [string, number][]) => { 4998e41f4b71Sopenharmony_ci console.info('Sync dataChange'); 4999e41f4b71Sopenharmony_ci }); 5000e41f4b71Sopenharmony_ci if (kvStore != null) { 5001e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_SYNC_ELEMENT + 'testSync101', VALUE_TEST_SYNC_ELEMENT, (err: BusinessError) => { 5002e41f4b71Sopenharmony_ci if (err != undefined) { 5003e41f4b71Sopenharmony_ci console.error(`Failed to sync.code is ${err.code},message is ${err.message}`); 5004e41f4b71Sopenharmony_ci return; 5005e41f4b71Sopenharmony_ci } 5006e41f4b71Sopenharmony_ci console.info('Succeeded in putting data'); 5007e41f4b71Sopenharmony_ci const mode = distributedKVStore.SyncMode.PULL_ONLY; 5008e41f4b71Sopenharmony_ci if (kvStore != null) { 5009e41f4b71Sopenharmony_ci kvStore.sync(deviceIds, mode, 1000); 5010e41f4b71Sopenharmony_ci } 5011e41f4b71Sopenharmony_ci }); 5012e41f4b71Sopenharmony_ci } 5013e41f4b71Sopenharmony_ci } 5014e41f4b71Sopenharmony_ci } catch (e) { 5015e41f4b71Sopenharmony_ci let error = e as BusinessError; 5016e41f4b71Sopenharmony_ci console.error(`Failed to sync.code is ${error.code},message is ${error.message}`); 5017e41f4b71Sopenharmony_ci } 5018e41f4b71Sopenharmony_ci 5019e41f4b71Sopenharmony_ci } catch (err) { 5020e41f4b71Sopenharmony_ci let error = err as BusinessError; 5021e41f4b71Sopenharmony_ci console.error("createDeviceManager errCode:" + error.code + ",errMessage:" + error.message); 5022e41f4b71Sopenharmony_ci } 5023e41f4b71Sopenharmony_ci } 5024e41f4b71Sopenharmony_ci} 5025e41f4b71Sopenharmony_ci``` 5026e41f4b71Sopenharmony_ci 5027e41f4b71Sopenharmony_ci### sync 5028e41f4b71Sopenharmony_ci 5029e41f4b71Sopenharmony_cisync(deviceIds: string[], query: Query, mode: SyncMode, delayMs?: number): void 5030e41f4b71Sopenharmony_ci 5031e41f4b71Sopenharmony_ciSynchronizes the KV store manually. This API returns the result synchronously. For details about the sync modes of KV stores, see [Cross-Device Synchronization of KV Stores](../../database/data-sync-of-kv-store.md). 5032e41f4b71Sopenharmony_ci> **NOTE** 5033e41f4b71Sopenharmony_ci> 5034e41f4b71Sopenharmony_ci> **deviceIds** is **networkId** in [DeviceBasicInfo](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#devicebasicinfo), which can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 5035e41f4b71Sopenharmony_ci 5036e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 5037e41f4b71Sopenharmony_ci 5038e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5039e41f4b71Sopenharmony_ci 5040e41f4b71Sopenharmony_ci**Parameters** 5041e41f4b71Sopenharmony_ci 5042e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5043e41f4b71Sopenharmony_ci| --------- | --------------------- | ---- | ---------------------------------------------- | 5044e41f4b71Sopenharmony_ci| deviceIds | string[] | Yes | List of **networkId**s of the devices in the same networking environment to be synchronized.| 5045e41f4b71Sopenharmony_ci| mode | [SyncMode](#syncmode) | Yes | Sync mode. | 5046e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match. | 5047e41f4b71Sopenharmony_ci| delayMs | number | No | Delay time allowed, in ms. The default value is **0**. | 5048e41f4b71Sopenharmony_ci 5049e41f4b71Sopenharmony_ci**Error codes** 5050e41f4b71Sopenharmony_ci 5051e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5052e41f4b71Sopenharmony_ci 5053e41f4b71Sopenharmony_ci| ID| **Error Message** | 5054e41f4b71Sopenharmony_ci| ------------ | ------------------- | 5055e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 5056e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 5057e41f4b71Sopenharmony_ci| 15100004 | Not found. | 5058e41f4b71Sopenharmony_ci 5059e41f4b71Sopenharmony_ci**Example** 5060e41f4b71Sopenharmony_ci 5061e41f4b71Sopenharmony_ci```ts 5062e41f4b71Sopenharmony_ciimport { distributedDeviceManager } from '@kit.DistributedServiceKit'; 5063e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 5064e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5065e41f4b71Sopenharmony_ci 5066e41f4b71Sopenharmony_cilet devManager: distributedDeviceManager.DeviceManager; 5067e41f4b71Sopenharmony_ciconst KEY_TEST_SYNC_ELEMENT = 'key_test_sync'; 5068e41f4b71Sopenharmony_ciconst VALUE_TEST_SYNC_ELEMENT = 'value-string-001'; 5069e41f4b71Sopenharmony_ci// create deviceManager 5070e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 5071e41f4b71Sopenharmony_ci onCreate() { 5072e41f4b71Sopenharmony_ci let context = this.context; 5073e41f4b71Sopenharmony_ci try { 5074e41f4b71Sopenharmony_ci let devManager = distributedDeviceManager.createDeviceManager(context.applicationInfo.name); 5075e41f4b71Sopenharmony_ci let deviceIds: string[] = []; 5076e41f4b71Sopenharmony_ci if (devManager != null) { 5077e41f4b71Sopenharmony_ci let devices = devManager.getAvailableDeviceListSync(); 5078e41f4b71Sopenharmony_ci for (let i = 0; i < devices.length; i++) { 5079e41f4b71Sopenharmony_ci deviceIds[i] = devices[i].networkId as string; 5080e41f4b71Sopenharmony_ci } 5081e41f4b71Sopenharmony_ci } 5082e41f4b71Sopenharmony_ci try { 5083e41f4b71Sopenharmony_ci if (kvStore != null) { 5084e41f4b71Sopenharmony_ci kvStore.on('syncComplete', (data: [string, number][]) => { 5085e41f4b71Sopenharmony_ci console.info('Sync dataChange'); 5086e41f4b71Sopenharmony_ci }); 5087e41f4b71Sopenharmony_ci if (kvStore != null) { 5088e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_SYNC_ELEMENT + 'testSync101', VALUE_TEST_SYNC_ELEMENT, (err: BusinessError) => { 5089e41f4b71Sopenharmony_ci if (err != undefined) { 5090e41f4b71Sopenharmony_ci console.error(`Failed to sync.code is ${err.code},message is ${err.message}`); 5091e41f4b71Sopenharmony_ci return; 5092e41f4b71Sopenharmony_ci } 5093e41f4b71Sopenharmony_ci console.info('Succeeded in putting data'); 5094e41f4b71Sopenharmony_ci const mode = distributedKVStore.SyncMode.PULL_ONLY; 5095e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 5096e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 5097e41f4b71Sopenharmony_ci query.deviceId(devManager.getLocalDeviceNetworkId()); 5098e41f4b71Sopenharmony_ci if (kvStore != null) { 5099e41f4b71Sopenharmony_ci kvStore.sync(deviceIds, query, mode, 1000); 5100e41f4b71Sopenharmony_ci } 5101e41f4b71Sopenharmony_ci }); 5102e41f4b71Sopenharmony_ci } 5103e41f4b71Sopenharmony_ci } 5104e41f4b71Sopenharmony_ci } catch (e) { 5105e41f4b71Sopenharmony_ci let error = e as BusinessError; 5106e41f4b71Sopenharmony_ci console.error(`Failed to sync.code is ${error.code},message is ${error.message}`); 5107e41f4b71Sopenharmony_ci } 5108e41f4b71Sopenharmony_ci 5109e41f4b71Sopenharmony_ci } catch (err) { 5110e41f4b71Sopenharmony_ci let error = err as BusinessError; 5111e41f4b71Sopenharmony_ci console.error("createDeviceManager errCode:" + error.code + ",errMessage:" + error.message); 5112e41f4b71Sopenharmony_ci } 5113e41f4b71Sopenharmony_ci } 5114e41f4b71Sopenharmony_ci} 5115e41f4b71Sopenharmony_ci``` 5116e41f4b71Sopenharmony_ci 5117e41f4b71Sopenharmony_ci### on('dataChange') 5118e41f4b71Sopenharmony_ci 5119e41f4b71Sopenharmony_cion(event: 'dataChange', type: SubscribeType, listener: Callback<ChangeNotification>): void 5120e41f4b71Sopenharmony_ci 5121e41f4b71Sopenharmony_ciSubscribes to data changes of the specified type. 5122e41f4b71Sopenharmony_ci 5123e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5124e41f4b71Sopenharmony_ci 5125e41f4b71Sopenharmony_ci**Parameters** 5126e41f4b71Sopenharmony_ci 5127e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5128e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------------- | ---- | ---------------------------------------------------- | 5129e41f4b71Sopenharmony_ci| event | string | Yes | Event type. The value is **dataChange**, which indicates data changes.| 5130e41f4b71Sopenharmony_ci| type | [SubscribeType](#subscribetype) | Yes | Type of data change. | 5131e41f4b71Sopenharmony_ci| listener | Callback<[ChangeNotification](#changenotification)> | Yes | Callback used to return the data change. | 5132e41f4b71Sopenharmony_ci 5133e41f4b71Sopenharmony_ci**Error codes** 5134e41f4b71Sopenharmony_ci 5135e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5136e41f4b71Sopenharmony_ci 5137e41f4b71Sopenharmony_ci| ID| **Error Message** | 5138e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5139e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 5140e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 5141e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5142e41f4b71Sopenharmony_ci 5143e41f4b71Sopenharmony_ci**Example** 5144e41f4b71Sopenharmony_ci 5145e41f4b71Sopenharmony_ci```ts 5146e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5147e41f4b71Sopenharmony_ci 5148e41f4b71Sopenharmony_citry { 5149e41f4b71Sopenharmony_ci kvStore.on('dataChange', distributedKVStore.SubscribeType.SUBSCRIBE_TYPE_LOCAL, (data: distributedKVStore.ChangeNotification) => { 5150e41f4b71Sopenharmony_ci console.info(`dataChange callback call data: ${data}`); 5151e41f4b71Sopenharmony_ci }); 5152e41f4b71Sopenharmony_ci} catch (e) { 5153e41f4b71Sopenharmony_ci let error = e as BusinessError; 5154e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 5155e41f4b71Sopenharmony_ci} 5156e41f4b71Sopenharmony_ci``` 5157e41f4b71Sopenharmony_ci 5158e41f4b71Sopenharmony_ci### on('syncComplete') 5159e41f4b71Sopenharmony_ci 5160e41f4b71Sopenharmony_cion(event: 'syncComplete', syncCallback: Callback<Array<[string, number]>>): void 5161e41f4b71Sopenharmony_ci 5162e41f4b71Sopenharmony_ciSubscribes to sync complete events. 5163e41f4b71Sopenharmony_ci 5164e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5165e41f4b71Sopenharmony_ci 5166e41f4b71Sopenharmony_ci**Parameters** 5167e41f4b71Sopenharmony_ci 5168e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5169e41f4b71Sopenharmony_ci| ------------ | --------------------------------------------- | ---- | ------------------------------------------------------ | 5170e41f4b71Sopenharmony_ci| event | string | Yes | Event type. The value is **syncComplete**, which indicates a sync complete event.| 5171e41f4b71Sopenharmony_ci| syncCallback | Callback<Array<[string, number]>> | Yes | Callback used to return the sync complete event. | 5172e41f4b71Sopenharmony_ci 5173e41f4b71Sopenharmony_ci**Error codes** 5174e41f4b71Sopenharmony_ci 5175e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 5176e41f4b71Sopenharmony_ci 5177e41f4b71Sopenharmony_ci| ID| **Error Message** | 5178e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5179e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 5180e41f4b71Sopenharmony_ci 5181e41f4b71Sopenharmony_ci**Example** 5182e41f4b71Sopenharmony_ci 5183e41f4b71Sopenharmony_ci```ts 5184e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5185e41f4b71Sopenharmony_ci 5186e41f4b71Sopenharmony_ci 5187e41f4b71Sopenharmony_ciconst KEY_TEST_FLOAT_ELEMENT = 'key_test_float'; 5188e41f4b71Sopenharmony_ciconst VALUE_TEST_FLOAT_ELEMENT = 321.12; 5189e41f4b71Sopenharmony_citry { 5190e41f4b71Sopenharmony_ci kvStore.on('syncComplete', (data: [string, number][]) => { 5191e41f4b71Sopenharmony_ci console.info(`syncComplete ${data}`); 5192e41f4b71Sopenharmony_ci }); 5193e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_FLOAT_ELEMENT, VALUE_TEST_FLOAT_ELEMENT).then(() => { 5194e41f4b71Sopenharmony_ci console.info('succeeded in putting'); 5195e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5196e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 5197e41f4b71Sopenharmony_ci }); 5198e41f4b71Sopenharmony_ci} catch (e) { 5199e41f4b71Sopenharmony_ci let error = e as BusinessError; 5200e41f4b71Sopenharmony_ci console.error(`Failed to subscribe syncComplete.code is ${error.code},message is ${error.message}`); 5201e41f4b71Sopenharmony_ci} 5202e41f4b71Sopenharmony_ci``` 5203e41f4b71Sopenharmony_ci 5204e41f4b71Sopenharmony_ci### off('dataChange') 5205e41f4b71Sopenharmony_ci 5206e41f4b71Sopenharmony_cioff(event:'dataChange', listener?: Callback<ChangeNotification>): void 5207e41f4b71Sopenharmony_ci 5208e41f4b71Sopenharmony_ciUnsubscribes from data changes. 5209e41f4b71Sopenharmony_ci 5210e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5211e41f4b71Sopenharmony_ci 5212e41f4b71Sopenharmony_ci**Parameters** 5213e41f4b71Sopenharmony_ci 5214e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5215e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------------- | ---- | -------------------------------------------------------- | 5216e41f4b71Sopenharmony_ci| event | string | Yes | Event type. The value is **dataChange**, which indicates data changes.| 5217e41f4b71Sopenharmony_ci| listener | Callback<[ChangeNotification](#changenotification)> | No | Callback to unregister. If this parameter is not specified, this API unregisters all callbacks for data changes.| 5218e41f4b71Sopenharmony_ci 5219e41f4b71Sopenharmony_ci**Error codes** 5220e41f4b71Sopenharmony_ci 5221e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5222e41f4b71Sopenharmony_ci 5223e41f4b71Sopenharmony_ci| ID| **Error Message** | 5224e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5225e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 5226e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5227e41f4b71Sopenharmony_ci 5228e41f4b71Sopenharmony_ci**Example** 5229e41f4b71Sopenharmony_ci 5230e41f4b71Sopenharmony_ci```ts 5231e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5232e41f4b71Sopenharmony_ci 5233e41f4b71Sopenharmony_ciclass KvstoreModel { 5234e41f4b71Sopenharmony_ci call(data: distributedKVStore.ChangeNotification) { 5235e41f4b71Sopenharmony_ci console.info(`dataChange : ${data}`); 5236e41f4b71Sopenharmony_ci } 5237e41f4b71Sopenharmony_ci 5238e41f4b71Sopenharmony_ci subscribeDataChange() { 5239e41f4b71Sopenharmony_ci try { 5240e41f4b71Sopenharmony_ci if (kvStore != null) { 5241e41f4b71Sopenharmony_ci kvStore.on('dataChange', distributedKVStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, this.call); 5242e41f4b71Sopenharmony_ci } 5243e41f4b71Sopenharmony_ci } catch (err) { 5244e41f4b71Sopenharmony_ci let error = err as BusinessError; 5245e41f4b71Sopenharmony_ci console.error(`Failed to subscribeDataChange.code is ${error.code},message is ${error.message}`); 5246e41f4b71Sopenharmony_ci } 5247e41f4b71Sopenharmony_ci } 5248e41f4b71Sopenharmony_ci 5249e41f4b71Sopenharmony_ci unsubscribeDataChange() { 5250e41f4b71Sopenharmony_ci try { 5251e41f4b71Sopenharmony_ci if (kvStore != null) { 5252e41f4b71Sopenharmony_ci kvStore.off('dataChange', this.call); 5253e41f4b71Sopenharmony_ci } 5254e41f4b71Sopenharmony_ci } catch (err) { 5255e41f4b71Sopenharmony_ci let error = err as BusinessError; 5256e41f4b71Sopenharmony_ci console.error(`Failed to unsubscribeDataChange.code is ${error.code},message is ${error.message}`); 5257e41f4b71Sopenharmony_ci } 5258e41f4b71Sopenharmony_ci } 5259e41f4b71Sopenharmony_ci} 5260e41f4b71Sopenharmony_ci``` 5261e41f4b71Sopenharmony_ci 5262e41f4b71Sopenharmony_ci### off('syncComplete') 5263e41f4b71Sopenharmony_ci 5264e41f4b71Sopenharmony_cioff(event: 'syncComplete', syncCallback?: Callback<Array<[string, number]>>): void 5265e41f4b71Sopenharmony_ci 5266e41f4b71Sopenharmony_ciUnsubscribes from sync complete events. 5267e41f4b71Sopenharmony_ci 5268e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5269e41f4b71Sopenharmony_ci 5270e41f4b71Sopenharmony_ci**Parameters** 5271e41f4b71Sopenharmony_ci 5272e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5273e41f4b71Sopenharmony_ci| ------------ | --------------------------------------------- | ---- | ---------------------------------------------------------- | 5274e41f4b71Sopenharmony_ci| event | string | Yes | Event type. The value is **syncComplete**, which indicates a sync complete event.| 5275e41f4b71Sopenharmony_ci| syncCallback | Callback<Array<[string, number]>> | No | Callback to unregister. If this parameter is not specified, this API unregisters all the callbacks for the sync complete event. | 5276e41f4b71Sopenharmony_ci 5277e41f4b71Sopenharmony_ci**Error codes** 5278e41f4b71Sopenharmony_ci 5279e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 5280e41f4b71Sopenharmony_ci 5281e41f4b71Sopenharmony_ci| ID| **Error Message** | 5282e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5283e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 5284e41f4b71Sopenharmony_ci 5285e41f4b71Sopenharmony_ci**Example** 5286e41f4b71Sopenharmony_ci 5287e41f4b71Sopenharmony_ci```ts 5288e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5289e41f4b71Sopenharmony_ci 5290e41f4b71Sopenharmony_ciclass KvstoreModel { 5291e41f4b71Sopenharmony_ci call(data: [string, number][]) { 5292e41f4b71Sopenharmony_ci console.info(`syncComplete : ${data}`); 5293e41f4b71Sopenharmony_ci } 5294e41f4b71Sopenharmony_ci 5295e41f4b71Sopenharmony_ci subscribeDataChange() { 5296e41f4b71Sopenharmony_ci try { 5297e41f4b71Sopenharmony_ci if (kvStore != null) { 5298e41f4b71Sopenharmony_ci kvStore.on('syncComplete', this.call); 5299e41f4b71Sopenharmony_ci } 5300e41f4b71Sopenharmony_ci } catch (err) { 5301e41f4b71Sopenharmony_ci let error = err as BusinessError; 5302e41f4b71Sopenharmony_ci console.error(`Failed to subscribeDataChange.code is ${error.code},message is ${error.message}`); 5303e41f4b71Sopenharmony_ci } 5304e41f4b71Sopenharmony_ci } 5305e41f4b71Sopenharmony_ci 5306e41f4b71Sopenharmony_ci unsubscribeDataChange() { 5307e41f4b71Sopenharmony_ci try { 5308e41f4b71Sopenharmony_ci if (kvStore != null) { 5309e41f4b71Sopenharmony_ci kvStore.off('syncComplete', this.call); 5310e41f4b71Sopenharmony_ci } 5311e41f4b71Sopenharmony_ci } catch (err) { 5312e41f4b71Sopenharmony_ci let error = err as BusinessError; 5313e41f4b71Sopenharmony_ci console.error(`Failed to unsubscribeDataChange.code is ${error.code},message is ${error.message}`); 5314e41f4b71Sopenharmony_ci } 5315e41f4b71Sopenharmony_ci } 5316e41f4b71Sopenharmony_ci} 5317e41f4b71Sopenharmony_ci``` 5318e41f4b71Sopenharmony_ci 5319e41f4b71Sopenharmony_ci### getSecurityLevel 5320e41f4b71Sopenharmony_ci 5321e41f4b71Sopenharmony_cigetSecurityLevel(callback: AsyncCallback<SecurityLevel>): void 5322e41f4b71Sopenharmony_ci 5323e41f4b71Sopenharmony_ciObtains the security level of this KV store. This API uses an asynchronous callback to return the result. 5324e41f4b71Sopenharmony_ci 5325e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5326e41f4b71Sopenharmony_ci 5327e41f4b71Sopenharmony_ci**Parameters** 5328e41f4b71Sopenharmony_ci 5329e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5330e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | ---- | -------------------------------- | 5331e41f4b71Sopenharmony_ci| callback | AsyncCallback<[SecurityLevel](#securitylevel)> | Yes | Callback used to return the security level of the KV store.| 5332e41f4b71Sopenharmony_ci 5333e41f4b71Sopenharmony_ci**Error codes** 5334e41f4b71Sopenharmony_ci 5335e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md). 5336e41f4b71Sopenharmony_ci 5337e41f4b71Sopenharmony_ci| ID| **Error Message** | 5338e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5339e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5340e41f4b71Sopenharmony_ci 5341e41f4b71Sopenharmony_ci**Example** 5342e41f4b71Sopenharmony_ci 5343e41f4b71Sopenharmony_ci```ts 5344e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5345e41f4b71Sopenharmony_ci 5346e41f4b71Sopenharmony_citry { 5347e41f4b71Sopenharmony_ci kvStore.getSecurityLevel((err: BusinessError, data: distributedKVStore.SecurityLevel) => { 5348e41f4b71Sopenharmony_ci if (err != undefined) { 5349e41f4b71Sopenharmony_ci console.error(`Failed to get SecurityLevel.code is ${err.code},message is ${err.message}`); 5350e41f4b71Sopenharmony_ci return; 5351e41f4b71Sopenharmony_ci } 5352e41f4b71Sopenharmony_ci console.info('Succeeded in getting securityLevel'); 5353e41f4b71Sopenharmony_ci }); 5354e41f4b71Sopenharmony_ci} catch (e) { 5355e41f4b71Sopenharmony_ci let error = e as BusinessError; 5356e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 5357e41f4b71Sopenharmony_ci} 5358e41f4b71Sopenharmony_ci``` 5359e41f4b71Sopenharmony_ci 5360e41f4b71Sopenharmony_ci### getSecurityLevel 5361e41f4b71Sopenharmony_ci 5362e41f4b71Sopenharmony_cigetSecurityLevel(): Promise<SecurityLevel> 5363e41f4b71Sopenharmony_ci 5364e41f4b71Sopenharmony_ciObtains the security level of this KV store. This API uses a promise to return the result. 5365e41f4b71Sopenharmony_ci 5366e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5367e41f4b71Sopenharmony_ci 5368e41f4b71Sopenharmony_ci**Return value** 5369e41f4b71Sopenharmony_ci 5370e41f4b71Sopenharmony_ci| Type | Description | 5371e41f4b71Sopenharmony_ci| ---------------------------------------------- | ----------------------------------- | 5372e41f4b71Sopenharmony_ci| Promise<[SecurityLevel](#securitylevel)> | Promise used to return the security level of the KV store.| 5373e41f4b71Sopenharmony_ci 5374e41f4b71Sopenharmony_ci**Error codes** 5375e41f4b71Sopenharmony_ci 5376e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md). 5377e41f4b71Sopenharmony_ci 5378e41f4b71Sopenharmony_ci| ID| **Error Message** | 5379e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5380e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5381e41f4b71Sopenharmony_ci 5382e41f4b71Sopenharmony_ci**Example** 5383e41f4b71Sopenharmony_ci 5384e41f4b71Sopenharmony_ci```ts 5385e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5386e41f4b71Sopenharmony_ci 5387e41f4b71Sopenharmony_citry { 5388e41f4b71Sopenharmony_ci kvStore.getSecurityLevel().then((data: distributedKVStore.SecurityLevel) => { 5389e41f4b71Sopenharmony_ci console.info('Succeeded in getting securityLevel'); 5390e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5391e41f4b71Sopenharmony_ci console.error(`Failed to get SecurityLevel.code is ${err.code},message is ${err.message}`); 5392e41f4b71Sopenharmony_ci }); 5393e41f4b71Sopenharmony_ci} catch (e) { 5394e41f4b71Sopenharmony_ci let error = e as BusinessError; 5395e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 5396e41f4b71Sopenharmony_ci} 5397e41f4b71Sopenharmony_ci``` 5398e41f4b71Sopenharmony_ci 5399e41f4b71Sopenharmony_ci## DeviceKVStore 5400e41f4b71Sopenharmony_ci 5401e41f4b71Sopenharmony_ciProvides APIs for querying and synchronizing data in a device KV store. This class inherits from **SingleKVStore**. The **SingleKVStore** APIs such as **put** and **putBatch** can be used. 5402e41f4b71Sopenharmony_ci 5403e41f4b71Sopenharmony_ciData is distinguished by device in a device KV store. Each device can only write and modify its own data. Data of other devices is read-only and cannot be modified. 5404e41f4b71Sopenharmony_ci 5405e41f4b71Sopenharmony_ciFor example, a device KV store can be used to implement image sharing between devices. The images of other devices can be viewed, but not be modified or deleted. 5406e41f4b71Sopenharmony_ci 5407e41f4b71Sopenharmony_ciBefore calling any method in **DeviceKVStore**, you must use [getKVStore](#getkvstore) to obtain a **DeviceKVStore** object. 5408e41f4b71Sopenharmony_ci 5409e41f4b71Sopenharmony_ci### get 5410e41f4b71Sopenharmony_ci 5411e41f4b71Sopenharmony_ciget(key: string, callback: AsyncCallback<boolean | string | number | Uint8Array>): void 5412e41f4b71Sopenharmony_ci 5413e41f4b71Sopenharmony_ciObtains the value of the specified key for this device. This API uses an asynchronous callback to return the result. 5414e41f4b71Sopenharmony_ci 5415e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5416e41f4b71Sopenharmony_ci 5417e41f4b71Sopenharmony_ci**Parameters** 5418e41f4b71Sopenharmony_ci 5419e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5420e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5421e41f4b71Sopenharmony_ci| key | string | Yes | Key of the value to obtain. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants).| 5422e41f4b71Sopenharmony_ci| callback | AsyncCallback<boolean \| string \| number \| Uint8Array> | Yes | Callback used to return the value obtained. | 5423e41f4b71Sopenharmony_ci 5424e41f4b71Sopenharmony_ci**Error codes** 5425e41f4b71Sopenharmony_ci 5426e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5427e41f4b71Sopenharmony_ci 5428e41f4b71Sopenharmony_ci| ID| **Error Message** | 5429e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5430e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 5431e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 5432e41f4b71Sopenharmony_ci| 15100004 | Not found. | 5433e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5434e41f4b71Sopenharmony_ci 5435e41f4b71Sopenharmony_ci**Example** 5436e41f4b71Sopenharmony_ci 5437e41f4b71Sopenharmony_ci```ts 5438e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5439e41f4b71Sopenharmony_ci 5440e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string'; 5441e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 5442e41f4b71Sopenharmony_citry { 5443e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err: BusinessError) => { 5444e41f4b71Sopenharmony_ci if (err != undefined) { 5445e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 5446e41f4b71Sopenharmony_ci return; 5447e41f4b71Sopenharmony_ci } 5448e41f4b71Sopenharmony_ci console.info("Succeeded in putting"); 5449e41f4b71Sopenharmony_ci if (kvStore != null) { 5450e41f4b71Sopenharmony_ci kvStore.get(KEY_TEST_STRING_ELEMENT, (err: BusinessError, data: boolean | string | number | Uint8Array) => { 5451e41f4b71Sopenharmony_ci if (err != undefined) { 5452e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 5453e41f4b71Sopenharmony_ci return; 5454e41f4b71Sopenharmony_ci } 5455e41f4b71Sopenharmony_ci console.info(`Succeeded in getting data.data=${data}`); 5456e41f4b71Sopenharmony_ci }); 5457e41f4b71Sopenharmony_ci } 5458e41f4b71Sopenharmony_ci }); 5459e41f4b71Sopenharmony_ci} catch (e) { 5460e41f4b71Sopenharmony_ci let error = e as BusinessError; 5461e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 5462e41f4b71Sopenharmony_ci} 5463e41f4b71Sopenharmony_ci``` 5464e41f4b71Sopenharmony_ci 5465e41f4b71Sopenharmony_ci### get 5466e41f4b71Sopenharmony_ci 5467e41f4b71Sopenharmony_ciget(key: string): Promise<boolean | string | number | Uint8Array> 5468e41f4b71Sopenharmony_ci 5469e41f4b71Sopenharmony_ciObtains the value of the specified key for this device. This API uses a promise to return the result. 5470e41f4b71Sopenharmony_ci 5471e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5472e41f4b71Sopenharmony_ci 5473e41f4b71Sopenharmony_ci**Parameters** 5474e41f4b71Sopenharmony_ci 5475e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 5476e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 5477e41f4b71Sopenharmony_ci| key | string | Yes | Key of the value to obtain. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants).| 5478e41f4b71Sopenharmony_ci 5479e41f4b71Sopenharmony_ci**Return value** 5480e41f4b71Sopenharmony_ci 5481e41f4b71Sopenharmony_ci| Type | Description | 5482e41f4b71Sopenharmony_ci| -------------------------------------------------------- | ------------------------------- | 5483e41f4b71Sopenharmony_ci| Promise<Uint8Array \| string \| boolean \| number> | Promise used to return the value obtained.| 5484e41f4b71Sopenharmony_ci 5485e41f4b71Sopenharmony_ci**Error codes** 5486e41f4b71Sopenharmony_ci 5487e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5488e41f4b71Sopenharmony_ci 5489e41f4b71Sopenharmony_ci| ID| **Error Message** | 5490e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5491e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 5492e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 5493e41f4b71Sopenharmony_ci| 15100004 | Not found. | 5494e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5495e41f4b71Sopenharmony_ci 5496e41f4b71Sopenharmony_ci**Example** 5497e41f4b71Sopenharmony_ci 5498e41f4b71Sopenharmony_ci```ts 5499e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5500e41f4b71Sopenharmony_ci 5501e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string'; 5502e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 5503e41f4b71Sopenharmony_citry { 5504e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(() => { 5505e41f4b71Sopenharmony_ci console.info(`Succeeded in putting data`); 5506e41f4b71Sopenharmony_ci if (kvStore != null) { 5507e41f4b71Sopenharmony_ci kvStore.get(KEY_TEST_STRING_ELEMENT).then((data: boolean | string | number | Uint8Array) => { 5508e41f4b71Sopenharmony_ci console.info(`Succeeded in getting data.data=${data}`); 5509e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5510e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 5511e41f4b71Sopenharmony_ci }); 5512e41f4b71Sopenharmony_ci } 5513e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5514e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 5515e41f4b71Sopenharmony_ci }); 5516e41f4b71Sopenharmony_ci} catch (e) { 5517e41f4b71Sopenharmony_ci let error = e as BusinessError; 5518e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 5519e41f4b71Sopenharmony_ci} 5520e41f4b71Sopenharmony_ci``` 5521e41f4b71Sopenharmony_ci 5522e41f4b71Sopenharmony_ci### get 5523e41f4b71Sopenharmony_ci 5524e41f4b71Sopenharmony_ciget(deviceId: string, key: string, callback: AsyncCallback<boolean | string | number | Uint8Array>): void 5525e41f4b71Sopenharmony_ci 5526e41f4b71Sopenharmony_ciObtains a string value that matches the specified device ID and key. This API uses an asynchronous callback to return the result. 5527e41f4b71Sopenharmony_ci> **NOTE** 5528e41f4b71Sopenharmony_ci> 5529e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 5530e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 5531e41f4b71Sopenharmony_ci 5532e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5533e41f4b71Sopenharmony_ci 5534e41f4b71Sopenharmony_ci**Parameters** 5535e41f4b71Sopenharmony_ci 5536e41f4b71Sopenharmony_ci| Name | Type| Mandatory | Description | 5537e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ----------------------- | 5538e41f4b71Sopenharmony_ci| deviceId |string | Yes |ID of the target device. | 5539e41f4b71Sopenharmony_ci| key |string | Yes |Key to match. It cannot be empty or exceed [MAX_KEY_LENGTH](#constants). | 5540e41f4b71Sopenharmony_ci| callback |AsyncCallback<boolean\|string\|number\|Uint8Array> | Yes |Callback used to return the value obtained. | 5541e41f4b71Sopenharmony_ci 5542e41f4b71Sopenharmony_ci**Error codes** 5543e41f4b71Sopenharmony_ci 5544e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5545e41f4b71Sopenharmony_ci 5546e41f4b71Sopenharmony_ci| ID| **Error Message** | 5547e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5548e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 5549e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 5550e41f4b71Sopenharmony_ci| 15100004 | Not found. | 5551e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5552e41f4b71Sopenharmony_ci 5553e41f4b71Sopenharmony_ci**Example** 5554e41f4b71Sopenharmony_ci 5555e41f4b71Sopenharmony_ci```ts 5556e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5557e41f4b71Sopenharmony_ci 5558e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string_2'; 5559e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-string-002'; 5560e41f4b71Sopenharmony_citry { 5561e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, async (err: BusinessError) => { 5562e41f4b71Sopenharmony_ci if (err != undefined) { 5563e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 5564e41f4b71Sopenharmony_ci return; 5565e41f4b71Sopenharmony_ci } 5566e41f4b71Sopenharmony_ci console.info('Succeeded in putting'); 5567e41f4b71Sopenharmony_ci if (kvStore != null) { 5568e41f4b71Sopenharmony_ci kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT, (err: BusinessError, data: boolean | string | number | Uint8Array) => { 5569e41f4b71Sopenharmony_ci if (err != undefined) { 5570e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 5571e41f4b71Sopenharmony_ci return; 5572e41f4b71Sopenharmony_ci } 5573e41f4b71Sopenharmony_ci console.info('Succeeded in getting'); 5574e41f4b71Sopenharmony_ci }); 5575e41f4b71Sopenharmony_ci } 5576e41f4b71Sopenharmony_ci }) 5577e41f4b71Sopenharmony_ci} catch (e) { 5578e41f4b71Sopenharmony_ci let error = e as BusinessError; 5579e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 5580e41f4b71Sopenharmony_ci} 5581e41f4b71Sopenharmony_ci``` 5582e41f4b71Sopenharmony_ci 5583e41f4b71Sopenharmony_ci### get 5584e41f4b71Sopenharmony_ci 5585e41f4b71Sopenharmony_ciget(deviceId: string, key: string): Promise<boolean | string | number | Uint8Array> 5586e41f4b71Sopenharmony_ci 5587e41f4b71Sopenharmony_ciObtains a string value that matches the specified device ID and key. This API uses a promise to return the result. 5588e41f4b71Sopenharmony_ci> **NOTE** 5589e41f4b71Sopenharmony_ci> 5590e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 5591e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 5592e41f4b71Sopenharmony_ci 5593e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5594e41f4b71Sopenharmony_ci 5595e41f4b71Sopenharmony_ci**Parameters** 5596e41f4b71Sopenharmony_ci 5597e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 5598e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ------------------------ | 5599e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the target device.| 5600e41f4b71Sopenharmony_ci| key | string | Yes | Key to match. It cannot be empty or exceed [MAX_KEY_LENGTH](#constants). | 5601e41f4b71Sopenharmony_ci 5602e41f4b71Sopenharmony_ci**Return value** 5603e41f4b71Sopenharmony_ci 5604e41f4b71Sopenharmony_ci| Type | Description | 5605e41f4b71Sopenharmony_ci| ------ | ------- | 5606e41f4b71Sopenharmony_ci|Promise<boolean\|string\|number\|Uint8Array> |Promise used to return the string value that matches the given condition.| 5607e41f4b71Sopenharmony_ci 5608e41f4b71Sopenharmony_ci**Error codes** 5609e41f4b71Sopenharmony_ci 5610e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5611e41f4b71Sopenharmony_ci 5612e41f4b71Sopenharmony_ci| ID| **Error Message** | 5613e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5614e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. | 5615e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 5616e41f4b71Sopenharmony_ci| 15100004 | Not found. | 5617e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5618e41f4b71Sopenharmony_ci 5619e41f4b71Sopenharmony_ci**Example** 5620e41f4b71Sopenharmony_ci 5621e41f4b71Sopenharmony_ci```ts 5622e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5623e41f4b71Sopenharmony_ci 5624e41f4b71Sopenharmony_ciconst KEY_TEST_STRING_ELEMENT = 'key_test_string_2'; 5625e41f4b71Sopenharmony_ciconst VALUE_TEST_STRING_ELEMENT = 'value-string-002'; 5626e41f4b71Sopenharmony_citry { 5627e41f4b71Sopenharmony_ci kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(async () => { 5628e41f4b71Sopenharmony_ci console.info('Succeeded in putting'); 5629e41f4b71Sopenharmony_ci if (kvStore != null) { 5630e41f4b71Sopenharmony_ci kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT).then((data: boolean | string | number | Uint8Array) => { 5631e41f4b71Sopenharmony_ci console.info('Succeeded in getting'); 5632e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5633e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 5634e41f4b71Sopenharmony_ci }); 5635e41f4b71Sopenharmony_ci } 5636e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 5637e41f4b71Sopenharmony_ci console.error(`Failed to put.code is ${error.code},message is ${error.message}`); 5638e41f4b71Sopenharmony_ci }); 5639e41f4b71Sopenharmony_ci} catch (e) { 5640e41f4b71Sopenharmony_ci let error = e as BusinessError; 5641e41f4b71Sopenharmony_ci console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 5642e41f4b71Sopenharmony_ci} 5643e41f4b71Sopenharmony_ci``` 5644e41f4b71Sopenharmony_ci 5645e41f4b71Sopenharmony_ci### getEntries 5646e41f4b71Sopenharmony_ci 5647e41f4b71Sopenharmony_cigetEntries(keyPrefix: string, callback: AsyncCallback<Entry[]>): void 5648e41f4b71Sopenharmony_ci 5649e41f4b71Sopenharmony_ciObtains all KV pairs that match the specified key prefix for this device. This API uses an asynchronous callback to return the result. 5650e41f4b71Sopenharmony_ci 5651e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5652e41f4b71Sopenharmony_ci 5653e41f4b71Sopenharmony_ci**Parameters** 5654e41f4b71Sopenharmony_ci 5655e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5656e41f4b71Sopenharmony_ci| --------- | -------------------------------------- | ---- | ---------------------------------------- | 5657e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 5658e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Entry](#entry)[]> | Yes | Callback used to return the KV pairs that match the specified prefix.| 5659e41f4b71Sopenharmony_ci 5660e41f4b71Sopenharmony_ci**Error codes** 5661e41f4b71Sopenharmony_ci 5662e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5663e41f4b71Sopenharmony_ci 5664e41f4b71Sopenharmony_ci| ID| **Error Message** | 5665e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5666e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 5667e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 5668e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5669e41f4b71Sopenharmony_ci 5670e41f4b71Sopenharmony_ci**Example** 5671e41f4b71Sopenharmony_ci 5672e41f4b71Sopenharmony_ci```ts 5673e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5674e41f4b71Sopenharmony_ci 5675e41f4b71Sopenharmony_citry { 5676e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 5677e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 5678e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 5679e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 5680e41f4b71Sopenharmony_ci key: key + i, 5681e41f4b71Sopenharmony_ci value: { 5682e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 5683e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 5684e41f4b71Sopenharmony_ci } 5685e41f4b71Sopenharmony_ci } 5686e41f4b71Sopenharmony_ci entries.push(entry); 5687e41f4b71Sopenharmony_ci } 5688e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 5689e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 5690e41f4b71Sopenharmony_ci if (err != undefined) { 5691e41f4b71Sopenharmony_ci console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 5692e41f4b71Sopenharmony_ci return; 5693e41f4b71Sopenharmony_ci } 5694e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 5695e41f4b71Sopenharmony_ci if (kvStore != null) { 5696e41f4b71Sopenharmony_ci kvStore.getEntries('batch_test_string_key', (err: BusinessError, entries: distributedKVStore.Entry[]) => { 5697e41f4b71Sopenharmony_ci if (err != undefined) { 5698e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 5699e41f4b71Sopenharmony_ci return; 5700e41f4b71Sopenharmony_ci } 5701e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 5702e41f4b71Sopenharmony_ci console.info(`entries.length: ${entries.length}`); 5703e41f4b71Sopenharmony_ci console.info(`entries[0]: ${entries[0]}`); 5704e41f4b71Sopenharmony_ci }); 5705e41f4b71Sopenharmony_ci } 5706e41f4b71Sopenharmony_ci }); 5707e41f4b71Sopenharmony_ci} catch (e) { 5708e41f4b71Sopenharmony_ci let error = e as BusinessError; 5709e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message} `); 5710e41f4b71Sopenharmony_ci} 5711e41f4b71Sopenharmony_ci``` 5712e41f4b71Sopenharmony_ci 5713e41f4b71Sopenharmony_ci### getEntries 5714e41f4b71Sopenharmony_ci 5715e41f4b71Sopenharmony_cigetEntries(keyPrefix: string): Promise<Entry[]> 5716e41f4b71Sopenharmony_ci 5717e41f4b71Sopenharmony_ciObtains all KV pairs that match the specified key prefix for this device. This API uses a promise to return the result. 5718e41f4b71Sopenharmony_ci 5719e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5720e41f4b71Sopenharmony_ci 5721e41f4b71Sopenharmony_ci**Parameters** 5722e41f4b71Sopenharmony_ci 5723e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5724e41f4b71Sopenharmony_ci| --------- | ------ | ---- | -------------------- | 5725e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 5726e41f4b71Sopenharmony_ci 5727e41f4b71Sopenharmony_ci**Return value** 5728e41f4b71Sopenharmony_ci 5729e41f4b71Sopenharmony_ci| Type | Description | 5730e41f4b71Sopenharmony_ci| -------------------------------- | ------------------------------------------- | 5731e41f4b71Sopenharmony_ci| Promise<[Entry](#entry)[]> | Promise used to return the KV pairs that match the specified prefix.| 5732e41f4b71Sopenharmony_ci 5733e41f4b71Sopenharmony_ci**Error codes** 5734e41f4b71Sopenharmony_ci 5735e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5736e41f4b71Sopenharmony_ci 5737e41f4b71Sopenharmony_ci| ID| **Error Message** | 5738e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5739e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 5740e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 5741e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5742e41f4b71Sopenharmony_ci 5743e41f4b71Sopenharmony_ci**Example** 5744e41f4b71Sopenharmony_ci 5745e41f4b71Sopenharmony_ci```ts 5746e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5747e41f4b71Sopenharmony_ci 5748e41f4b71Sopenharmony_citry { 5749e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 5750e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 5751e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 5752e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 5753e41f4b71Sopenharmony_ci key: key + i, 5754e41f4b71Sopenharmony_ci value: { 5755e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 5756e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 5757e41f4b71Sopenharmony_ci } 5758e41f4b71Sopenharmony_ci } 5759e41f4b71Sopenharmony_ci entries.push(entry); 5760e41f4b71Sopenharmony_ci } 5761e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 5762e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 5763e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 5764e41f4b71Sopenharmony_ci if (kvStore != null) { 5765e41f4b71Sopenharmony_ci kvStore.getEntries('batch_test_string_key').then((entries: distributedKVStore.Entry[]) => { 5766e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 5767e41f4b71Sopenharmony_ci console.info(`PutBatch ${entries}`); 5768e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5769e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 5770e41f4b71Sopenharmony_ci }); 5771e41f4b71Sopenharmony_ci } 5772e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5773e41f4b71Sopenharmony_ci console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 5774e41f4b71Sopenharmony_ci }); 5775e41f4b71Sopenharmony_ci} catch (e) { 5776e41f4b71Sopenharmony_ci let error = e as BusinessError; 5777e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message} `); 5778e41f4b71Sopenharmony_ci} 5779e41f4b71Sopenharmony_ci``` 5780e41f4b71Sopenharmony_ci 5781e41f4b71Sopenharmony_ci### getEntries 5782e41f4b71Sopenharmony_ci 5783e41f4b71Sopenharmony_cigetEntries(deviceId: string, keyPrefix: string, callback: AsyncCallback<Entry[]>): void 5784e41f4b71Sopenharmony_ci 5785e41f4b71Sopenharmony_ciObtains all KV pairs that match the specified device ID and key prefix. This API uses an asynchronous callback to return the result. 5786e41f4b71Sopenharmony_ci> **NOTE** 5787e41f4b71Sopenharmony_ci> 5788e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 5789e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 5790e41f4b71Sopenharmony_ci 5791e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5792e41f4b71Sopenharmony_ci 5793e41f4b71Sopenharmony_ci**Parameters** 5794e41f4b71Sopenharmony_ci 5795e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5796e41f4b71Sopenharmony_ci| --------- | -------------------------------------- | ---- | ---------------------------------------------- | 5797e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the target device. | 5798e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 5799e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Entry](#entry)[]> | Yes | Callback used to return the KV pairs obtained.| 5800e41f4b71Sopenharmony_ci 5801e41f4b71Sopenharmony_ci**Error codes** 5802e41f4b71Sopenharmony_ci 5803e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5804e41f4b71Sopenharmony_ci 5805e41f4b71Sopenharmony_ci| ID| **Error Message** | 5806e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5807e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 5808e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 5809e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5810e41f4b71Sopenharmony_ci 5811e41f4b71Sopenharmony_ci**Example** 5812e41f4b71Sopenharmony_ci 5813e41f4b71Sopenharmony_ci```ts 5814e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5815e41f4b71Sopenharmony_ci 5816e41f4b71Sopenharmony_citry { 5817e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 5818e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 5819e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 5820e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 5821e41f4b71Sopenharmony_ci key: key + i, 5822e41f4b71Sopenharmony_ci value: { 5823e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 5824e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 5825e41f4b71Sopenharmony_ci } 5826e41f4b71Sopenharmony_ci } 5827e41f4b71Sopenharmony_ci entries.push(entry); 5828e41f4b71Sopenharmony_ci } 5829e41f4b71Sopenharmony_ci console.info(`entries : ${entries}`); 5830e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 5831e41f4b71Sopenharmony_ci if (err != undefined) { 5832e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 5833e41f4b71Sopenharmony_ci return; 5834e41f4b71Sopenharmony_ci } 5835e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 5836e41f4b71Sopenharmony_ci if (kvStore != null) { 5837e41f4b71Sopenharmony_ci kvStore.getEntries('localDeviceId', 'batch_test_string_key', (err: BusinessError, entries: distributedKVStore.Entry[]) => { 5838e41f4b71Sopenharmony_ci if (err != undefined) { 5839e41f4b71Sopenharmony_ci console.error(`Failed to get entries.code is ${err.code},message is ${err.message}`); 5840e41f4b71Sopenharmony_ci return; 5841e41f4b71Sopenharmony_ci } 5842e41f4b71Sopenharmony_ci console.info('Succeeded in getting entries'); 5843e41f4b71Sopenharmony_ci console.info(`entries.length: ${entries.length}`); 5844e41f4b71Sopenharmony_ci console.info(`entries[0]: ${entries[0]}`); 5845e41f4b71Sopenharmony_ci }); 5846e41f4b71Sopenharmony_ci } 5847e41f4b71Sopenharmony_ci }); 5848e41f4b71Sopenharmony_ci} catch (e) { 5849e41f4b71Sopenharmony_ci let error = e as BusinessError; 5850e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${error.code},message is ${error.message}`); 5851e41f4b71Sopenharmony_ci} 5852e41f4b71Sopenharmony_ci``` 5853e41f4b71Sopenharmony_ci 5854e41f4b71Sopenharmony_ci### getEntries 5855e41f4b71Sopenharmony_ci 5856e41f4b71Sopenharmony_cigetEntries(deviceId: string, keyPrefix: string): Promise<Entry[]> 5857e41f4b71Sopenharmony_ci 5858e41f4b71Sopenharmony_ciObtains all KV pairs that match the specified device ID and key prefix. This API uses a promise to return the result. 5859e41f4b71Sopenharmony_ci> **NOTE** 5860e41f4b71Sopenharmony_ci> 5861e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 5862e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 5863e41f4b71Sopenharmony_ci 5864e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5865e41f4b71Sopenharmony_ci 5866e41f4b71Sopenharmony_ci**Parameters** 5867e41f4b71Sopenharmony_ci 5868e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 5869e41f4b71Sopenharmony_ci| --------- | -------- | ---- | ------------------------ | 5870e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the target device.| 5871e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 5872e41f4b71Sopenharmony_ci 5873e41f4b71Sopenharmony_ci**Return value** 5874e41f4b71Sopenharmony_ci 5875e41f4b71Sopenharmony_ci| Type | Description | 5876e41f4b71Sopenharmony_ci| -------------------------------- | ------------------------------------------------- | 5877e41f4b71Sopenharmony_ci| Promise<[Entry](#entry)[]> | Promise used to return all the KV pairs that match the given condition.| 5878e41f4b71Sopenharmony_ci 5879e41f4b71Sopenharmony_ci**Error codes** 5880e41f4b71Sopenharmony_ci 5881e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5882e41f4b71Sopenharmony_ci 5883e41f4b71Sopenharmony_ci| ID| **Error Message** | 5884e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5885e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 5886e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 5887e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5888e41f4b71Sopenharmony_ci 5889e41f4b71Sopenharmony_ci**Example** 5890e41f4b71Sopenharmony_ci 5891e41f4b71Sopenharmony_ci<!--code_no_check--> 5892e41f4b71Sopenharmony_ci```ts 5893e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5894e41f4b71Sopenharmony_ci 5895e41f4b71Sopenharmony_citry { 5896e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 5897e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 5898e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 5899e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 5900e41f4b71Sopenharmony_ci key: key + i, 5901e41f4b71Sopenharmony_ci value: { 5902e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 5903e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 5904e41f4b71Sopenharmony_ci } 5905e41f4b71Sopenharmony_ci } 5906e41f4b71Sopenharmony_ci entries.push(entry); 5907e41f4b71Sopenharmony_ci } 5908e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 5909e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 5910e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 5911e41f4b71Sopenharmony_ci if (kvStore != null) { 5912e41f4b71Sopenharmony_ci kvStore.getEntries('localDeviceId', 'batch_test_string_key').then((entries: distributedKVStore.Entry[]) => { 5913e41f4b71Sopenharmony_ci console.info('Succeeded in getting entries'); 5914e41f4b71Sopenharmony_ci console.info(`entries.length: ${entries.length}`); 5915e41f4b71Sopenharmony_ci console.info(`entries[0]: ${entries[0]}`); 5916e41f4b71Sopenharmony_ci console.info(`entries[0].value: ${entries[0].value}`); 5917e41f4b71Sopenharmony_ci console.info(`entries[0].value.value: ${entries[0].value.value}`); 5918e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5919e41f4b71Sopenharmony_ci console.error(`Failed to get entries.code is ${err.code},message is ${err.message}`); 5920e41f4b71Sopenharmony_ci }); 5921e41f4b71Sopenharmony_ci } 5922e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 5923e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 5924e41f4b71Sopenharmony_ci }); 5925e41f4b71Sopenharmony_ci} catch (e) { 5926e41f4b71Sopenharmony_ci let error = e as BusinessError; 5927e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${error.code},message is ${error.message}`); 5928e41f4b71Sopenharmony_ci} 5929e41f4b71Sopenharmony_ci``` 5930e41f4b71Sopenharmony_ci 5931e41f4b71Sopenharmony_ci### getEntries 5932e41f4b71Sopenharmony_ci 5933e41f4b71Sopenharmony_cigetEntries(query: Query, callback: AsyncCallback<Entry[]>): void 5934e41f4b71Sopenharmony_ci 5935e41f4b71Sopenharmony_ciObtains all KV pairs that match the specified **Query** object for this device. This API uses an asynchronous callback to return the result. 5936e41f4b71Sopenharmony_ci 5937e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 5938e41f4b71Sopenharmony_ci 5939e41f4b71Sopenharmony_ci**Parameters** 5940e41f4b71Sopenharmony_ci 5941e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 5942e41f4b71Sopenharmony_ci| -------- | -------------------------------------- | ---- | ----------------------------------------------------- | 5943e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | Key prefix to match. | 5944e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Entry](#entry)[]> | Yes | Callback used to return the KV pairs that match the specified **Query** object on the local device.| 5945e41f4b71Sopenharmony_ci 5946e41f4b71Sopenharmony_ci**Error codes** 5947e41f4b71Sopenharmony_ci 5948e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 5949e41f4b71Sopenharmony_ci 5950e41f4b71Sopenharmony_ci| ID| **Error Message** | 5951e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 5952e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 5953e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 5954e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 5955e41f4b71Sopenharmony_ci 5956e41f4b71Sopenharmony_ci**Example** 5957e41f4b71Sopenharmony_ci 5958e41f4b71Sopenharmony_ci```ts 5959e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 5960e41f4b71Sopenharmony_ci 5961e41f4b71Sopenharmony_citry { 5962e41f4b71Sopenharmony_ci let arr = new Uint8Array([21, 31]); 5963e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 5964e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 5965e41f4b71Sopenharmony_ci let key = 'batch_test_bool_key'; 5966e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 5967e41f4b71Sopenharmony_ci key: key + i, 5968e41f4b71Sopenharmony_ci value: { 5969e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.BYTE_ARRAY, 5970e41f4b71Sopenharmony_ci value: arr 5971e41f4b71Sopenharmony_ci } 5972e41f4b71Sopenharmony_ci } 5973e41f4b71Sopenharmony_ci entries.push(entry); 5974e41f4b71Sopenharmony_ci } 5975e41f4b71Sopenharmony_ci console.info(`entries: {entries}`); 5976e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 5977e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 5978e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 5979e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 5980e41f4b71Sopenharmony_ci if (kvStore != null) { 5981e41f4b71Sopenharmony_ci kvStore.getEntries(query, (err: BusinessError, entries: distributedKVStore.Entry[]) => { 5982e41f4b71Sopenharmony_ci if (err != undefined) { 5983e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 5984e41f4b71Sopenharmony_ci return; 5985e41f4b71Sopenharmony_ci } 5986e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 5987e41f4b71Sopenharmony_ci console.info(`entries.length: ${entries.length}`); 5988e41f4b71Sopenharmony_ci console.info(`entries[0]: ${entries[0]}`); 5989e41f4b71Sopenharmony_ci }); 5990e41f4b71Sopenharmony_ci } 5991e41f4b71Sopenharmony_ci }); 5992e41f4b71Sopenharmony_ci} catch (e) { 5993e41f4b71Sopenharmony_ci let error = e as BusinessError; 5994e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${error.code},message is ${error.message}`); 5995e41f4b71Sopenharmony_ci} 5996e41f4b71Sopenharmony_ci``` 5997e41f4b71Sopenharmony_ci 5998e41f4b71Sopenharmony_ci### getEntries 5999e41f4b71Sopenharmony_ci 6000e41f4b71Sopenharmony_cigetEntries(query: Query): Promise<Entry[]> 6001e41f4b71Sopenharmony_ci 6002e41f4b71Sopenharmony_ciObtains all KV pairs that match the specified **Query** object for this device. This API uses a promise to return the result. 6003e41f4b71Sopenharmony_ci 6004e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 6005e41f4b71Sopenharmony_ci 6006e41f4b71Sopenharmony_ci**Parameters** 6007e41f4b71Sopenharmony_ci 6008e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 6009e41f4b71Sopenharmony_ci| ------ | -------------- | ---- | -------------- | 6010e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match.| 6011e41f4b71Sopenharmony_ci 6012e41f4b71Sopenharmony_ci**Return value** 6013e41f4b71Sopenharmony_ci 6014e41f4b71Sopenharmony_ci| Type | Description | 6015e41f4b71Sopenharmony_ci| -------------------------------- | -------------------------------------------------------- | 6016e41f4b71Sopenharmony_ci| Promise<[Entry](#entry)[]> | Promise used to return the KV pairs that match the specified **Query** object on the local device.| 6017e41f4b71Sopenharmony_ci 6018e41f4b71Sopenharmony_ci**Error codes** 6019e41f4b71Sopenharmony_ci 6020e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6021e41f4b71Sopenharmony_ci 6022e41f4b71Sopenharmony_ci| ID| **Error Message** | 6023e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6024e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6025e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6026e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6027e41f4b71Sopenharmony_ci 6028e41f4b71Sopenharmony_ci**Example** 6029e41f4b71Sopenharmony_ci 6030e41f4b71Sopenharmony_ci```ts 6031e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6032e41f4b71Sopenharmony_ci 6033e41f4b71Sopenharmony_citry { 6034e41f4b71Sopenharmony_ci let arr = new Uint8Array([21, 31]); 6035e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6036e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6037e41f4b71Sopenharmony_ci let key = 'batch_test_bool_key'; 6038e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6039e41f4b71Sopenharmony_ci key: key + i, 6040e41f4b71Sopenharmony_ci value: { 6041e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.BYTE_ARRAY, 6042e41f4b71Sopenharmony_ci value: arr 6043e41f4b71Sopenharmony_ci } 6044e41f4b71Sopenharmony_ci } 6045e41f4b71Sopenharmony_ci entries.push(entry); 6046e41f4b71Sopenharmony_ci } 6047e41f4b71Sopenharmony_ci console.info(`entries: {entries}`); 6048e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 6049e41f4b71Sopenharmony_ci console.info('Succeeded in putting Batch'); 6050e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 6051e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 6052e41f4b71Sopenharmony_ci if (kvStore != null) { 6053e41f4b71Sopenharmony_ci kvStore.getEntries(query).then((entries: distributedKVStore.Entry[]) => { 6054e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 6055e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6056e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 6057e41f4b71Sopenharmony_ci }); 6058e41f4b71Sopenharmony_ci } 6059e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6060e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`) 6061e41f4b71Sopenharmony_ci }); 6062e41f4b71Sopenharmony_ci console.info('Succeeded in getting Entries'); 6063e41f4b71Sopenharmony_ci} catch (e) { 6064e41f4b71Sopenharmony_ci let error = e as BusinessError; 6065e41f4b71Sopenharmony_ci console.error(`Failed to get Entries.code is ${error.code},message is ${error.message}`); 6066e41f4b71Sopenharmony_ci} 6067e41f4b71Sopenharmony_ci``` 6068e41f4b71Sopenharmony_ci 6069e41f4b71Sopenharmony_ci### getEntries 6070e41f4b71Sopenharmony_ci 6071e41f4b71Sopenharmony_cigetEntries(deviceId: string, query: Query, callback: AsyncCallback<Entry[]>): void 6072e41f4b71Sopenharmony_ci 6073e41f4b71Sopenharmony_ciObtains the KV pairs that match the specified device ID and **Query** object. This API uses an asynchronous callback to return the result. 6074e41f4b71Sopenharmony_ci> **NOTE** 6075e41f4b71Sopenharmony_ci> 6076e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 6077e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 6078e41f4b71Sopenharmony_ci 6079e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6080e41f4b71Sopenharmony_ci 6081e41f4b71Sopenharmony_ci**Parameters** 6082e41f4b71Sopenharmony_ci 6083e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 6084e41f4b71Sopenharmony_ci| -------- | -------------------------------------- | ---- | ------------------------------------------------------- | 6085e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the target device. | 6086e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match. | 6087e41f4b71Sopenharmony_ci| callback | AsyncCallback<[Entry](#entry)[]> | Yes | Callback used to return the KV pairs that match the specified device ID and **Query** object.| 6088e41f4b71Sopenharmony_ci 6089e41f4b71Sopenharmony_ci**Error codes** 6090e41f4b71Sopenharmony_ci 6091e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6092e41f4b71Sopenharmony_ci 6093e41f4b71Sopenharmony_ci| ID| **Error Message** | 6094e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6095e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6096e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6097e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6098e41f4b71Sopenharmony_ci 6099e41f4b71Sopenharmony_ci**Example** 6100e41f4b71Sopenharmony_ci 6101e41f4b71Sopenharmony_ci```ts 6102e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6103e41f4b71Sopenharmony_ci 6104e41f4b71Sopenharmony_citry { 6105e41f4b71Sopenharmony_ci let arr = new Uint8Array([21, 31]); 6106e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6107e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6108e41f4b71Sopenharmony_ci let key = 'batch_test_bool_key'; 6109e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6110e41f4b71Sopenharmony_ci key: key + i, 6111e41f4b71Sopenharmony_ci value: { 6112e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.BYTE_ARRAY, 6113e41f4b71Sopenharmony_ci value: arr 6114e41f4b71Sopenharmony_ci } 6115e41f4b71Sopenharmony_ci } 6116e41f4b71Sopenharmony_ci entries.push(entry); 6117e41f4b71Sopenharmony_ci } 6118e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 6119e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 6120e41f4b71Sopenharmony_ci if (err != undefined) { 6121e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6122e41f4b71Sopenharmony_ci return; 6123e41f4b71Sopenharmony_ci } 6124e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 6125e41f4b71Sopenharmony_ci let query = new distributedKVStore.Query(); 6126e41f4b71Sopenharmony_ci query.deviceId('localDeviceId'); 6127e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 6128e41f4b71Sopenharmony_ci if (kvStore != null) { 6129e41f4b71Sopenharmony_ci kvStore.getEntries('localDeviceId', query, (err: BusinessError, entries: distributedKVStore.Entry[]) => { 6130e41f4b71Sopenharmony_ci if (err != undefined) { 6131e41f4b71Sopenharmony_ci console.error(`Failed to get entries.code is ${err.code},message is ${err.message}`); 6132e41f4b71Sopenharmony_ci return; 6133e41f4b71Sopenharmony_ci } 6134e41f4b71Sopenharmony_ci console.info('Succeeded in getting entries'); 6135e41f4b71Sopenharmony_ci console.info(`entries.length: ${entries.length}`); 6136e41f4b71Sopenharmony_ci console.info(`entries[0]: ${entries[0]}`); 6137e41f4b71Sopenharmony_ci }) 6138e41f4b71Sopenharmony_ci } 6139e41f4b71Sopenharmony_ci }); 6140e41f4b71Sopenharmony_ci console.info('Succeeded in getting entries'); 6141e41f4b71Sopenharmony_ci} catch (e) { 6142e41f4b71Sopenharmony_ci let error = e as BusinessError; 6143e41f4b71Sopenharmony_ci console.error(`Failed to get entries.code is ${error.code},message is ${error.message}`); 6144e41f4b71Sopenharmony_ci} 6145e41f4b71Sopenharmony_ci``` 6146e41f4b71Sopenharmony_ci 6147e41f4b71Sopenharmony_ci### getEntries 6148e41f4b71Sopenharmony_ci 6149e41f4b71Sopenharmony_cigetEntries(deviceId: string, query: Query): Promise<Entry[]> 6150e41f4b71Sopenharmony_ci 6151e41f4b71Sopenharmony_ciObtains the KV pairs that match the specified device ID and **Query** object. This API uses a promise to return the result. 6152e41f4b71Sopenharmony_ci> **NOTE** 6153e41f4b71Sopenharmony_ci> 6154e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 6155e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 6156e41f4b71Sopenharmony_ci 6157e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6158e41f4b71Sopenharmony_ci 6159e41f4b71Sopenharmony_ci**Parameters** 6160e41f4b71Sopenharmony_ci 6161e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 6162e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | -------------------- | 6163e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the target device.| 6164e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match. | 6165e41f4b71Sopenharmony_ci 6166e41f4b71Sopenharmony_ci**Return value** 6167e41f4b71Sopenharmony_ci 6168e41f4b71Sopenharmony_ci| Type | Description | 6169e41f4b71Sopenharmony_ci| -------------------------------- | ---------------------------------------------------------- | 6170e41f4b71Sopenharmony_ci| Promise<[Entry](#entry)[]> | Promise used to return the KV pairs that match the specified device ID and **Query** object.| 6171e41f4b71Sopenharmony_ci 6172e41f4b71Sopenharmony_ci**Error codes** 6173e41f4b71Sopenharmony_ci 6174e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6175e41f4b71Sopenharmony_ci 6176e41f4b71Sopenharmony_ci| ID| **Error Message** | 6177e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6178e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6179e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6180e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6181e41f4b71Sopenharmony_ci 6182e41f4b71Sopenharmony_ci**Example** 6183e41f4b71Sopenharmony_ci 6184e41f4b71Sopenharmony_ci<!--code_no_check--> 6185e41f4b71Sopenharmony_ci```ts 6186e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6187e41f4b71Sopenharmony_ci 6188e41f4b71Sopenharmony_citry { 6189e41f4b71Sopenharmony_ci let arr = new Uint8Array([21, 31]); 6190e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6191e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6192e41f4b71Sopenharmony_ci let key = 'batch_test_bool_key'; 6193e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6194e41f4b71Sopenharmony_ci key: key + i, 6195e41f4b71Sopenharmony_ci value: { 6196e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.BYTE_ARRAY, 6197e41f4b71Sopenharmony_ci value: arr 6198e41f4b71Sopenharmony_ci } 6199e41f4b71Sopenharmony_ci } 6200e41f4b71Sopenharmony_ci entries.push(entry); 6201e41f4b71Sopenharmony_ci } 6202e41f4b71Sopenharmony_ci console.info(`entries: ${entries}`); 6203e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 6204e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 6205e41f4b71Sopenharmony_ci let query = new distributedKVStore.Query(); 6206e41f4b71Sopenharmony_ci query.deviceId('localDeviceId'); 6207e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 6208e41f4b71Sopenharmony_ci if (kvStore != null) { 6209e41f4b71Sopenharmony_ci kvStore.getEntries('localDeviceId', query).then((entries: distributedKVStore.Entry[]) => { 6210e41f4b71Sopenharmony_ci console.info('Succeeded in getting entries'); 6211e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6212e41f4b71Sopenharmony_ci console.error(`Failed to get entries.code is ${err.code},message is ${err.message}`); 6213e41f4b71Sopenharmony_ci }); 6214e41f4b71Sopenharmony_ci } 6215e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6216e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6217e41f4b71Sopenharmony_ci }); 6218e41f4b71Sopenharmony_ci console.info('Succeeded in getting entries'); 6219e41f4b71Sopenharmony_ci} catch (e) { 6220e41f4b71Sopenharmony_ci let error = e as BusinessError; 6221e41f4b71Sopenharmony_ci console.error(`Failed to get entries.code is ${error.code},message is ${error.message}`); 6222e41f4b71Sopenharmony_ci} 6223e41f4b71Sopenharmony_ci``` 6224e41f4b71Sopenharmony_ci 6225e41f4b71Sopenharmony_ci### getResultSet 6226e41f4b71Sopenharmony_ci 6227e41f4b71Sopenharmony_cigetResultSet(keyPrefix: string, callback: AsyncCallback<KVStoreResultSet>): void 6228e41f4b71Sopenharmony_ci 6229e41f4b71Sopenharmony_ciObtains a result set with the specified prefix for this device. This API uses an asynchronous callback to return the result. 6230e41f4b71Sopenharmony_ci 6231e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 6232e41f4b71Sopenharmony_ci 6233e41f4b71Sopenharmony_ci**Parameters** 6234e41f4b71Sopenharmony_ci 6235e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 6236e41f4b71Sopenharmony_ci| --------- | ---------------------------------------------------------- | ---- | ------------------------------------ | 6237e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 6238e41f4b71Sopenharmony_ci| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | Yes | Callback used to return the result set with the specified prefix.| 6239e41f4b71Sopenharmony_ci 6240e41f4b71Sopenharmony_ci**Error codes** 6241e41f4b71Sopenharmony_ci 6242e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6243e41f4b71Sopenharmony_ci 6244e41f4b71Sopenharmony_ci| ID| **Error Message** | 6245e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6246e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6247e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 6248e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6249e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6250e41f4b71Sopenharmony_ci 6251e41f4b71Sopenharmony_ci**Example** 6252e41f4b71Sopenharmony_ci 6253e41f4b71Sopenharmony_ci```ts 6254e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6255e41f4b71Sopenharmony_ci 6256e41f4b71Sopenharmony_citry { 6257e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 6258e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6259e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6260e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 6261e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6262e41f4b71Sopenharmony_ci key: key + i, 6263e41f4b71Sopenharmony_ci value: { 6264e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 6265e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 6266e41f4b71Sopenharmony_ci } 6267e41f4b71Sopenharmony_ci } 6268e41f4b71Sopenharmony_ci entries.push(entry); 6269e41f4b71Sopenharmony_ci } 6270e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 6271e41f4b71Sopenharmony_ci if (err != undefined) { 6272e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6273e41f4b71Sopenharmony_ci return; 6274e41f4b71Sopenharmony_ci } 6275e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 6276e41f4b71Sopenharmony_ci if (kvStore != null) { 6277e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key', async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 6278e41f4b71Sopenharmony_ci if (err != undefined) { 6279e41f4b71Sopenharmony_ci console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 6280e41f4b71Sopenharmony_ci return; 6281e41f4b71Sopenharmony_ci } 6282e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set'); 6283e41f4b71Sopenharmony_ci resultSet = result; 6284e41f4b71Sopenharmony_ci if (kvStore != null) { 6285e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet, (err: BusinessError) => { 6286e41f4b71Sopenharmony_ci if (err != undefined) { 6287e41f4b71Sopenharmony_ci console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 6288e41f4b71Sopenharmony_ci return; 6289e41f4b71Sopenharmony_ci } 6290e41f4b71Sopenharmony_ci console.info('Succeeded in closing result set'); 6291e41f4b71Sopenharmony_ci }) 6292e41f4b71Sopenharmony_ci } 6293e41f4b71Sopenharmony_ci }); 6294e41f4b71Sopenharmony_ci } 6295e41f4b71Sopenharmony_ci }); 6296e41f4b71Sopenharmony_ci} catch (e) { 6297e41f4b71Sopenharmony_ci let error = e as BusinessError; 6298e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 6299e41f4b71Sopenharmony_ci} 6300e41f4b71Sopenharmony_ci``` 6301e41f4b71Sopenharmony_ci 6302e41f4b71Sopenharmony_ci### getResultSet 6303e41f4b71Sopenharmony_ci 6304e41f4b71Sopenharmony_cigetResultSet(keyPrefix: string): Promise<KVStoreResultSet> 6305e41f4b71Sopenharmony_ci 6306e41f4b71Sopenharmony_ciObtains a result set with the specified prefix for this device. This API uses a promise to return the result. 6307e41f4b71Sopenharmony_ci 6308e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 6309e41f4b71Sopenharmony_ci 6310e41f4b71Sopenharmony_ci**Parameters** 6311e41f4b71Sopenharmony_ci 6312e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 6313e41f4b71Sopenharmony_ci| --------- | ------ | ---- | -------------------- | 6314e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 6315e41f4b71Sopenharmony_ci 6316e41f4b71Sopenharmony_ci**Return value** 6317e41f4b71Sopenharmony_ci 6318e41f4b71Sopenharmony_ci| Type | Description | 6319e41f4b71Sopenharmony_ci| ---------------------------------------------------- | --------------------------------------- | 6320e41f4b71Sopenharmony_ci| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise used to return the result set with the specified prefix.| 6321e41f4b71Sopenharmony_ci 6322e41f4b71Sopenharmony_ci**Error codes** 6323e41f4b71Sopenharmony_ci 6324e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6325e41f4b71Sopenharmony_ci 6326e41f4b71Sopenharmony_ci| ID| **Error Message** | 6327e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6328e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6329e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 6330e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6331e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6332e41f4b71Sopenharmony_ci 6333e41f4b71Sopenharmony_ci**Example** 6334e41f4b71Sopenharmony_ci 6335e41f4b71Sopenharmony_ci```ts 6336e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6337e41f4b71Sopenharmony_ci 6338e41f4b71Sopenharmony_citry { 6339e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 6340e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6341e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6342e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 6343e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6344e41f4b71Sopenharmony_ci key: key + i, 6345e41f4b71Sopenharmony_ci value: { 6346e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 6347e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 6348e41f4b71Sopenharmony_ci } 6349e41f4b71Sopenharmony_ci } 6350e41f4b71Sopenharmony_ci entries.push(entry); 6351e41f4b71Sopenharmony_ci } 6352e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 6353e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 6354e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6355e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6356e41f4b71Sopenharmony_ci }); 6357e41f4b71Sopenharmony_ci kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 6358e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set'); 6359e41f4b71Sopenharmony_ci resultSet = result; 6360e41f4b71Sopenharmony_ci if (kvStore != null) { 6361e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet).then(() => { 6362e41f4b71Sopenharmony_ci console.info('Succeeded in closing result set'); 6363e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6364e41f4b71Sopenharmony_ci console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 6365e41f4b71Sopenharmony_ci }); 6366e41f4b71Sopenharmony_ci } 6367e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6368e41f4b71Sopenharmony_ci console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 6369e41f4b71Sopenharmony_ci }); 6370e41f4b71Sopenharmony_ci} catch (e) { 6371e41f4b71Sopenharmony_ci let error = e as BusinessError; 6372e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 6373e41f4b71Sopenharmony_ci} 6374e41f4b71Sopenharmony_ci``` 6375e41f4b71Sopenharmony_ci 6376e41f4b71Sopenharmony_ci### getResultSet 6377e41f4b71Sopenharmony_ci 6378e41f4b71Sopenharmony_cigetResultSet(deviceId: string, keyPrefix: string, callback: AsyncCallback<KVStoreResultSet>): void 6379e41f4b71Sopenharmony_ci 6380e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified device ID and key prefix. This API uses an asynchronous callback to return the result. 6381e41f4b71Sopenharmony_ci> **NOTE** 6382e41f4b71Sopenharmony_ci> 6383e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 6384e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 6385e41f4b71Sopenharmony_ci 6386e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6387e41f4b71Sopenharmony_ci 6388e41f4b71Sopenharmony_ci**Parameters** 6389e41f4b71Sopenharmony_ci 6390e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 6391e41f4b71Sopenharmony_ci| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6392e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the target device. | 6393e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 6394e41f4b71Sopenharmony_ci| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | Yes | Callback used to return the **KVStoreResultSet** object obtained.| 6395e41f4b71Sopenharmony_ci 6396e41f4b71Sopenharmony_ci**Error codes** 6397e41f4b71Sopenharmony_ci 6398e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6399e41f4b71Sopenharmony_ci 6400e41f4b71Sopenharmony_ci| ID| **Error Message** | 6401e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6402e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6403e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 6404e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6405e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6406e41f4b71Sopenharmony_ci 6407e41f4b71Sopenharmony_ci**Example** 6408e41f4b71Sopenharmony_ci 6409e41f4b71Sopenharmony_ci```ts 6410e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6411e41f4b71Sopenharmony_ci 6412e41f4b71Sopenharmony_citry { 6413e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 6414e41f4b71Sopenharmony_ci kvStore.getResultSet('localDeviceId', 'batch_test_string_key', async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 6415e41f4b71Sopenharmony_ci if (err != undefined) { 6416e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 6417e41f4b71Sopenharmony_ci return; 6418e41f4b71Sopenharmony_ci } 6419e41f4b71Sopenharmony_ci console.info('Succeeded in getting resultSet'); 6420e41f4b71Sopenharmony_ci resultSet = result; 6421e41f4b71Sopenharmony_ci if (kvStore != null) { 6422e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet, (err: BusinessError) => { 6423e41f4b71Sopenharmony_ci if (err != undefined) { 6424e41f4b71Sopenharmony_ci console.error(`Failed to close resultSet.code is ${err.code},message is ${err.message}`); 6425e41f4b71Sopenharmony_ci return; 6426e41f4b71Sopenharmony_ci } 6427e41f4b71Sopenharmony_ci console.info('Succeeded in closing resultSet'); 6428e41f4b71Sopenharmony_ci }) 6429e41f4b71Sopenharmony_ci } 6430e41f4b71Sopenharmony_ci }); 6431e41f4b71Sopenharmony_ci} catch (e) { 6432e41f4b71Sopenharmony_ci let error = e as BusinessError; 6433e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${error.code},message is ${error.message}`); 6434e41f4b71Sopenharmony_ci} 6435e41f4b71Sopenharmony_ci``` 6436e41f4b71Sopenharmony_ci 6437e41f4b71Sopenharmony_ci### getResultSet 6438e41f4b71Sopenharmony_ci 6439e41f4b71Sopenharmony_cigetResultSet(deviceId: string, keyPrefix: string): Promise<KVStoreResultSet> 6440e41f4b71Sopenharmony_ci 6441e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified device ID and key prefix. This API uses a promise to return the result. 6442e41f4b71Sopenharmony_ci> **NOTE** 6443e41f4b71Sopenharmony_ci> 6444e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 6445e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 6446e41f4b71Sopenharmony_ci 6447e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6448e41f4b71Sopenharmony_ci 6449e41f4b71Sopenharmony_ci**Parameters** 6450e41f4b71Sopenharmony_ci 6451e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 6452e41f4b71Sopenharmony_ci| --------- | -------- | ---- | ------------------------ | 6453e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the target device.| 6454e41f4b71Sopenharmony_ci| keyPrefix | string | Yes | Key prefix to match. It cannot contain '^'; otherwise, the predicate becomes invalid and all data in the RDB store will be returned.| 6455e41f4b71Sopenharmony_ci 6456e41f4b71Sopenharmony_ci**Return value** 6457e41f4b71Sopenharmony_ci 6458e41f4b71Sopenharmony_ci| Type | Description | 6459e41f4b71Sopenharmony_ci| ------------------------------------------------------ | ------------------------------------------------------------ | 6460e41f4b71Sopenharmony_ci| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise used to return the **KVStoreResultSet** object obtained.| 6461e41f4b71Sopenharmony_ci 6462e41f4b71Sopenharmony_ci**Error codes** 6463e41f4b71Sopenharmony_ci 6464e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6465e41f4b71Sopenharmony_ci 6466e41f4b71Sopenharmony_ci| ID| **Error Message** | 6467e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6468e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6469e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 6470e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6471e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6472e41f4b71Sopenharmony_ci 6473e41f4b71Sopenharmony_ci**Example** 6474e41f4b71Sopenharmony_ci 6475e41f4b71Sopenharmony_ci<!--code_no_check--> 6476e41f4b71Sopenharmony_ci```ts 6477e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6478e41f4b71Sopenharmony_ci 6479e41f4b71Sopenharmony_citry { 6480e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 6481e41f4b71Sopenharmony_ci kvStore.getResultSet('localDeviceId', 'batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 6482e41f4b71Sopenharmony_ci console.info('Succeeded in getting resultSet'); 6483e41f4b71Sopenharmony_ci resultSet = result; 6484e41f4b71Sopenharmony_ci if (kvStore != null) { 6485e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet).then(() => { 6486e41f4b71Sopenharmony_ci console.info('Succeeded in closing resultSet'); 6487e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6488e41f4b71Sopenharmony_ci console.error(`Failed to close resultSet.code is ${err.code},message is ${err.message}`); 6489e41f4b71Sopenharmony_ci }); 6490e41f4b71Sopenharmony_ci } 6491e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6492e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 6493e41f4b71Sopenharmony_ci }); 6494e41f4b71Sopenharmony_ci} catch (e) { 6495e41f4b71Sopenharmony_ci let error = e as BusinessError; 6496e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${error.code},message is ${error.message}`); 6497e41f4b71Sopenharmony_ci} 6498e41f4b71Sopenharmony_ci``` 6499e41f4b71Sopenharmony_ci 6500e41f4b71Sopenharmony_ci### getResultSet 6501e41f4b71Sopenharmony_ci 6502e41f4b71Sopenharmony_cigetResultSet(deviceId: string, query: Query, callback: AsyncCallback<KVStoreResultSet>): void 6503e41f4b71Sopenharmony_ci 6504e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified device ID and **Query** object. This API uses an asynchronous callback to return the result. 6505e41f4b71Sopenharmony_ci> **NOTE** 6506e41f4b71Sopenharmony_ci> 6507e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 6508e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 6509e41f4b71Sopenharmony_ci 6510e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6511e41f4b71Sopenharmony_ci 6512e41f4b71Sopenharmony_ci**Parameters** 6513e41f4b71Sopenharmony_ci 6514e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 6515e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6516e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the device to which the **KVStoreResultSet** object belongs. | 6517e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match. | 6518e41f4b71Sopenharmony_ci| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | Yes | Callback used to return the **KVStoreResultSet** object that matches the specified device ID and **Query** object.| 6519e41f4b71Sopenharmony_ci 6520e41f4b71Sopenharmony_ci**Error codes** 6521e41f4b71Sopenharmony_ci 6522e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6523e41f4b71Sopenharmony_ci 6524e41f4b71Sopenharmony_ci| ID| **Error Message** | 6525e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6526e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6527e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 6528e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6529e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6530e41f4b71Sopenharmony_ci 6531e41f4b71Sopenharmony_ci**Example** 6532e41f4b71Sopenharmony_ci 6533e41f4b71Sopenharmony_ci```ts 6534e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6535e41f4b71Sopenharmony_ci 6536e41f4b71Sopenharmony_citry { 6537e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 6538e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6539e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6540e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 6541e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6542e41f4b71Sopenharmony_ci key: key + i, 6543e41f4b71Sopenharmony_ci value: { 6544e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 6545e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 6546e41f4b71Sopenharmony_ci } 6547e41f4b71Sopenharmony_ci } 6548e41f4b71Sopenharmony_ci entries.push(entry); 6549e41f4b71Sopenharmony_ci } 6550e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 6551e41f4b71Sopenharmony_ci if (err != undefined) { 6552e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6553e41f4b71Sopenharmony_ci return; 6554e41f4b71Sopenharmony_ci } 6555e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 6556e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 6557e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 6558e41f4b71Sopenharmony_ci if (kvStore != null) { 6559e41f4b71Sopenharmony_ci kvStore.getResultSet('localDeviceId', query, async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 6560e41f4b71Sopenharmony_ci if (err != undefined) { 6561e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 6562e41f4b71Sopenharmony_ci return; 6563e41f4b71Sopenharmony_ci } 6564e41f4b71Sopenharmony_ci console.info('Succeeded in getting resultSet'); 6565e41f4b71Sopenharmony_ci resultSet = result; 6566e41f4b71Sopenharmony_ci if (kvStore != null) { 6567e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet, (err: BusinessError) => { 6568e41f4b71Sopenharmony_ci if (err != undefined) { 6569e41f4b71Sopenharmony_ci console.error(`Failed to close resultSet.code is ${err.code},message is ${err.message}`); 6570e41f4b71Sopenharmony_ci return; 6571e41f4b71Sopenharmony_ci } 6572e41f4b71Sopenharmony_ci console.info('Succeeded in closing resultSet'); 6573e41f4b71Sopenharmony_ci }) 6574e41f4b71Sopenharmony_ci } 6575e41f4b71Sopenharmony_ci }); 6576e41f4b71Sopenharmony_ci } 6577e41f4b71Sopenharmony_ci }); 6578e41f4b71Sopenharmony_ci} catch (e) { 6579e41f4b71Sopenharmony_ci let error = e as BusinessError; 6580e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${error.code},message is ${error.message}`); 6581e41f4b71Sopenharmony_ci} 6582e41f4b71Sopenharmony_ci``` 6583e41f4b71Sopenharmony_ci 6584e41f4b71Sopenharmony_ci### getResultSet 6585e41f4b71Sopenharmony_ci 6586e41f4b71Sopenharmony_cigetResultSet(deviceId: string, query: Query): Promise<KVStoreResultSet> 6587e41f4b71Sopenharmony_ci 6588e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified device ID and **Query** object. This API uses a promise to return the result. 6589e41f4b71Sopenharmony_ci> **NOTE** 6590e41f4b71Sopenharmony_ci> 6591e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 6592e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 6593e41f4b71Sopenharmony_ci 6594e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6595e41f4b71Sopenharmony_ci 6596e41f4b71Sopenharmony_ci**Parameters** 6597e41f4b71Sopenharmony_ci 6598e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 6599e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---------------------------------- | 6600e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the device to which the **KVStoreResultSet** object belongs.| 6601e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match. | 6602e41f4b71Sopenharmony_ci 6603e41f4b71Sopenharmony_ci**Return value** 6604e41f4b71Sopenharmony_ci 6605e41f4b71Sopenharmony_ci| Type | Description | 6606e41f4b71Sopenharmony_ci| ------------------------------------------------------ | ------------------------------------------------------------ | 6607e41f4b71Sopenharmony_ci| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise used to return the **KVStoreResultSet** object that matches the specified device ID and **Query** object.| 6608e41f4b71Sopenharmony_ci 6609e41f4b71Sopenharmony_ci**Error codes** 6610e41f4b71Sopenharmony_ci 6611e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6612e41f4b71Sopenharmony_ci 6613e41f4b71Sopenharmony_ci| ID| **Error Message** | 6614e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6615e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6616e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 6617e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6618e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6619e41f4b71Sopenharmony_ci 6620e41f4b71Sopenharmony_ci**Example** 6621e41f4b71Sopenharmony_ci 6622e41f4b71Sopenharmony_ci<!--code_no_check--> 6623e41f4b71Sopenharmony_ci```ts 6624e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6625e41f4b71Sopenharmony_ci 6626e41f4b71Sopenharmony_citry { 6627e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 6628e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6629e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6630e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 6631e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6632e41f4b71Sopenharmony_ci key: key + i, 6633e41f4b71Sopenharmony_ci value: { 6634e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 6635e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 6636e41f4b71Sopenharmony_ci } 6637e41f4b71Sopenharmony_ci } 6638e41f4b71Sopenharmony_ci entries.push(entry); 6639e41f4b71Sopenharmony_ci } 6640e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 6641e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 6642e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6643e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6644e41f4b71Sopenharmony_ci }); 6645e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 6646e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 6647e41f4b71Sopenharmony_ci if (kvStore != null) { 6648e41f4b71Sopenharmony_ci kvStore.getResultSet('localDeviceId', query).then((result: distributedKVStore.KVStoreResultSet) => { 6649e41f4b71Sopenharmony_ci console.info('Succeeded in getting resultSet'); 6650e41f4b71Sopenharmony_ci resultSet = result; 6651e41f4b71Sopenharmony_ci if (kvStore != null) { 6652e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet).then(() => { 6653e41f4b71Sopenharmony_ci console.info('Succeeded in closing resultSet'); 6654e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6655e41f4b71Sopenharmony_ci console.error(`Failed to close resultSet.code is ${err.code},message is ${err.message}`); 6656e41f4b71Sopenharmony_ci }); 6657e41f4b71Sopenharmony_ci } 6658e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6659e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 6660e41f4b71Sopenharmony_ci }); 6661e41f4b71Sopenharmony_ci } 6662e41f4b71Sopenharmony_ci query.deviceId('localDeviceId'); 6663e41f4b71Sopenharmony_ci console.info("GetResultSet " + query.getSqlLike()); 6664e41f4b71Sopenharmony_ci 6665e41f4b71Sopenharmony_ci} catch (e) { 6666e41f4b71Sopenharmony_ci let error = e as BusinessError; 6667e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${error.code},message is ${error.message}`); 6668e41f4b71Sopenharmony_ci} 6669e41f4b71Sopenharmony_ci``` 6670e41f4b71Sopenharmony_ci 6671e41f4b71Sopenharmony_ci### getResultSet 6672e41f4b71Sopenharmony_ci 6673e41f4b71Sopenharmony_cigetResultSet(query: Query): Promise<KVStoreResultSet> 6674e41f4b71Sopenharmony_ci 6675e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified **Query** object for this device. This API uses a promise to return the result. 6676e41f4b71Sopenharmony_ci 6677e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 6678e41f4b71Sopenharmony_ci 6679e41f4b71Sopenharmony_ci**Parameters** 6680e41f4b71Sopenharmony_ci 6681e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 6682e41f4b71Sopenharmony_ci| ------ | -------------- | ---- | -------------- | 6683e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match.| 6684e41f4b71Sopenharmony_ci 6685e41f4b71Sopenharmony_ci**Return value** 6686e41f4b71Sopenharmony_ci 6687e41f4b71Sopenharmony_ci| Type | Description | 6688e41f4b71Sopenharmony_ci| ---------------------------------------------------- | ------------------------------------------------------------ | 6689e41f4b71Sopenharmony_ci| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise used to return the **KVStoreResultSet** object obtained.| 6690e41f4b71Sopenharmony_ci 6691e41f4b71Sopenharmony_ci**Error codes** 6692e41f4b71Sopenharmony_ci 6693e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6694e41f4b71Sopenharmony_ci 6695e41f4b71Sopenharmony_ci| ID| **Error Message** | 6696e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6697e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6698e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 6699e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6700e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6701e41f4b71Sopenharmony_ci 6702e41f4b71Sopenharmony_ci**Example** 6703e41f4b71Sopenharmony_ci 6704e41f4b71Sopenharmony_ci```ts 6705e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6706e41f4b71Sopenharmony_ci 6707e41f4b71Sopenharmony_citry { 6708e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 6709e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6710e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6711e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 6712e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6713e41f4b71Sopenharmony_ci key: key + i, 6714e41f4b71Sopenharmony_ci value: { 6715e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 6716e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 6717e41f4b71Sopenharmony_ci } 6718e41f4b71Sopenharmony_ci } 6719e41f4b71Sopenharmony_ci entries.push(entry); 6720e41f4b71Sopenharmony_ci } 6721e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 6722e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 6723e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6724e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6725e41f4b71Sopenharmony_ci }); 6726e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 6727e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 6728e41f4b71Sopenharmony_ci kvStore.getResultSet(query).then((result: distributedKVStore.KVStoreResultSet) => { 6729e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set'); 6730e41f4b71Sopenharmony_ci resultSet = result; 6731e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6732e41f4b71Sopenharmony_ci console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 6733e41f4b71Sopenharmony_ci }); 6734e41f4b71Sopenharmony_ci} catch (e) { 6735e41f4b71Sopenharmony_ci let error = e as BusinessError; 6736e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 6737e41f4b71Sopenharmony_ci} 6738e41f4b71Sopenharmony_ci``` 6739e41f4b71Sopenharmony_ci 6740e41f4b71Sopenharmony_ci### getResultSet 6741e41f4b71Sopenharmony_ci 6742e41f4b71Sopenharmony_cigetResultSet(query: Query, callback:AsyncCallback<KVStoreResultSet>): void 6743e41f4b71Sopenharmony_ci 6744e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified **Query** object for this device. This API uses an asynchronous callback to return the result. 6745e41f4b71Sopenharmony_ci> **NOTE** 6746e41f4b71Sopenharmony_ci> 6747e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 6748e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 6749e41f4b71Sopenharmony_ci 6750e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 6751e41f4b71Sopenharmony_ci 6752e41f4b71Sopenharmony_ci**Parameters** 6753e41f4b71Sopenharmony_ci 6754e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 6755e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---------------------------------- | 6756e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match. | 6757e41f4b71Sopenharmony_ci| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | Yes | Callback used to return the **KVStoreResultSet** object obtained. | 6758e41f4b71Sopenharmony_ci 6759e41f4b71Sopenharmony_ci 6760e41f4b71Sopenharmony_ci**Error codes** 6761e41f4b71Sopenharmony_ci 6762e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6763e41f4b71Sopenharmony_ci 6764e41f4b71Sopenharmony_ci| ID| **Error Message** | 6765e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6766e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types.| 6767e41f4b71Sopenharmony_ci| 15100001 | Over max limits. | 6768e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6769e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6770e41f4b71Sopenharmony_ci 6771e41f4b71Sopenharmony_ci**Example** 6772e41f4b71Sopenharmony_ci 6773e41f4b71Sopenharmony_ci```ts 6774e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6775e41f4b71Sopenharmony_ci 6776e41f4b71Sopenharmony_citry { 6777e41f4b71Sopenharmony_ci let resultSet: distributedKVStore.KVStoreResultSet; 6778e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6779e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6780e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 6781e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6782e41f4b71Sopenharmony_ci key: key + i, 6783e41f4b71Sopenharmony_ci value: { 6784e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 6785e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 6786e41f4b71Sopenharmony_ci } 6787e41f4b71Sopenharmony_ci } 6788e41f4b71Sopenharmony_ci entries.push(entry); 6789e41f4b71Sopenharmony_ci } 6790e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 6791e41f4b71Sopenharmony_ci if (err != undefined) { 6792e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6793e41f4b71Sopenharmony_ci return; 6794e41f4b71Sopenharmony_ci } 6795e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 6796e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 6797e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 6798e41f4b71Sopenharmony_ci if (kvStore != null) { 6799e41f4b71Sopenharmony_ci kvStore.getResultSet(query, async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 6800e41f4b71Sopenharmony_ci if (err != undefined) { 6801e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 6802e41f4b71Sopenharmony_ci return; 6803e41f4b71Sopenharmony_ci } 6804e41f4b71Sopenharmony_ci console.info('Succeeded in getting resultSet'); 6805e41f4b71Sopenharmony_ci resultSet = result; 6806e41f4b71Sopenharmony_ci if (kvStore != null) { 6807e41f4b71Sopenharmony_ci kvStore.closeResultSet(resultSet, (err: BusinessError) => { 6808e41f4b71Sopenharmony_ci if (err != undefined) { 6809e41f4b71Sopenharmony_ci console.error(`Failed to close resultSet.code is ${err.code},message is ${err.message}`); 6810e41f4b71Sopenharmony_ci return; 6811e41f4b71Sopenharmony_ci } 6812e41f4b71Sopenharmony_ci console.info('Succeeded in closing resultSet'); 6813e41f4b71Sopenharmony_ci }) 6814e41f4b71Sopenharmony_ci } 6815e41f4b71Sopenharmony_ci }); 6816e41f4b71Sopenharmony_ci } 6817e41f4b71Sopenharmony_ci }); 6818e41f4b71Sopenharmony_ci} catch (e) { 6819e41f4b71Sopenharmony_ci let error = e as BusinessError; 6820e41f4b71Sopenharmony_ci console.error(`Failed to get resultSet`); 6821e41f4b71Sopenharmony_ci} 6822e41f4b71Sopenharmony_ci``` 6823e41f4b71Sopenharmony_ci 6824e41f4b71Sopenharmony_ci### getResultSize 6825e41f4b71Sopenharmony_ci 6826e41f4b71Sopenharmony_cigetResultSize(query: Query, callback: AsyncCallback<number>): void 6827e41f4b71Sopenharmony_ci 6828e41f4b71Sopenharmony_ciObtains the number of results that match the specified **Query** object for this device. This API uses an asynchronous callback to return the result. 6829e41f4b71Sopenharmony_ci 6830e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 6831e41f4b71Sopenharmony_ci 6832e41f4b71Sopenharmony_ci**Parameters** 6833e41f4b71Sopenharmony_ci 6834e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 6835e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ------------------------------------------------- | 6836e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match. | 6837e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the number of results obtained.| 6838e41f4b71Sopenharmony_ci 6839e41f4b71Sopenharmony_ci**Error codes** 6840e41f4b71Sopenharmony_ci 6841e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6842e41f4b71Sopenharmony_ci 6843e41f4b71Sopenharmony_ci| ID| **Error Message** | 6844e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6845e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 6846e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6847e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6848e41f4b71Sopenharmony_ci 6849e41f4b71Sopenharmony_ci**Example** 6850e41f4b71Sopenharmony_ci 6851e41f4b71Sopenharmony_ci```ts 6852e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6853e41f4b71Sopenharmony_ci 6854e41f4b71Sopenharmony_citry { 6855e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6856e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6857e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 6858e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6859e41f4b71Sopenharmony_ci key: key + i, 6860e41f4b71Sopenharmony_ci value: { 6861e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 6862e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 6863e41f4b71Sopenharmony_ci } 6864e41f4b71Sopenharmony_ci } 6865e41f4b71Sopenharmony_ci entries.push(entry); 6866e41f4b71Sopenharmony_ci } 6867e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 6868e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 6869e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 6870e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 6871e41f4b71Sopenharmony_ci if (kvStore != null) { 6872e41f4b71Sopenharmony_ci kvStore.getResultSize(query, async (err: BusinessError, resultSize: number) => { 6873e41f4b71Sopenharmony_ci if (err != undefined) { 6874e41f4b71Sopenharmony_ci console.error(`Failed to get result size.code is ${err.code},message is ${err.message}`); 6875e41f4b71Sopenharmony_ci return; 6876e41f4b71Sopenharmony_ci } 6877e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set size'); 6878e41f4b71Sopenharmony_ci }); 6879e41f4b71Sopenharmony_ci } 6880e41f4b71Sopenharmony_ci }); 6881e41f4b71Sopenharmony_ci} catch (e) { 6882e41f4b71Sopenharmony_ci let error = e as BusinessError; 6883e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 6884e41f4b71Sopenharmony_ci} 6885e41f4b71Sopenharmony_ci``` 6886e41f4b71Sopenharmony_ci 6887e41f4b71Sopenharmony_ci### getResultSize 6888e41f4b71Sopenharmony_ci 6889e41f4b71Sopenharmony_cigetResultSize(query: Query): Promise<number> 6890e41f4b71Sopenharmony_ci 6891e41f4b71Sopenharmony_ciObtains the number of results that match the specified **Query** object for this device. This API uses a promise to return the result. 6892e41f4b71Sopenharmony_ci 6893e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core 6894e41f4b71Sopenharmony_ci 6895e41f4b71Sopenharmony_ci**Parameters** 6896e41f4b71Sopenharmony_ci 6897e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 6898e41f4b71Sopenharmony_ci| ------ | -------------- | ---- | -------------- | 6899e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match.| 6900e41f4b71Sopenharmony_ci 6901e41f4b71Sopenharmony_ci**Return value** 6902e41f4b71Sopenharmony_ci 6903e41f4b71Sopenharmony_ci| Type | Description | 6904e41f4b71Sopenharmony_ci| --------------------- | ---------------------------------------------------- | 6905e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the number of results obtained.| 6906e41f4b71Sopenharmony_ci 6907e41f4b71Sopenharmony_ci**Error codes** 6908e41f4b71Sopenharmony_ci 6909e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6910e41f4b71Sopenharmony_ci 6911e41f4b71Sopenharmony_ci| ID| **Error Message** | 6912e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6913e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 6914e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6915e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6916e41f4b71Sopenharmony_ci 6917e41f4b71Sopenharmony_ci**Example** 6918e41f4b71Sopenharmony_ci 6919e41f4b71Sopenharmony_ci```ts 6920e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6921e41f4b71Sopenharmony_ci 6922e41f4b71Sopenharmony_citry { 6923e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6924e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6925e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 6926e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6927e41f4b71Sopenharmony_ci key: key + i, 6928e41f4b71Sopenharmony_ci value: { 6929e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 6930e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 6931e41f4b71Sopenharmony_ci } 6932e41f4b71Sopenharmony_ci } 6933e41f4b71Sopenharmony_ci entries.push(entry); 6934e41f4b71Sopenharmony_ci } 6935e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 6936e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 6937e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6938e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6939e41f4b71Sopenharmony_ci }); 6940e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 6941e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 6942e41f4b71Sopenharmony_ci kvStore.getResultSize(query).then((resultSize: number) => { 6943e41f4b71Sopenharmony_ci console.info('Succeeded in getting result set size'); 6944e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 6945e41f4b71Sopenharmony_ci console.error(`Failed to get result size.code is ${err.code},message is ${err.message}`); 6946e41f4b71Sopenharmony_ci }); 6947e41f4b71Sopenharmony_ci} catch (e) { 6948e41f4b71Sopenharmony_ci let error = e as BusinessError; 6949e41f4b71Sopenharmony_ci console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 6950e41f4b71Sopenharmony_ci} 6951e41f4b71Sopenharmony_ci``` 6952e41f4b71Sopenharmony_ci 6953e41f4b71Sopenharmony_ci### getResultSize 6954e41f4b71Sopenharmony_ci 6955e41f4b71Sopenharmony_cigetResultSize(deviceId: string, query: Query, callback: AsyncCallback<number>): void; 6956e41f4b71Sopenharmony_ci 6957e41f4b71Sopenharmony_ciObtains the number of results that matches the specified device ID and **Query** object. This API uses an asynchronous callback to return the result. 6958e41f4b71Sopenharmony_ci> **NOTE** 6959e41f4b71Sopenharmony_ci> 6960e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 6961e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 6962e41f4b71Sopenharmony_ci 6963e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6964e41f4b71Sopenharmony_ci 6965e41f4b71Sopenharmony_ci**Parameters** 6966e41f4b71Sopenharmony_ci 6967e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 6968e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | --------------------------------------------------- | 6969e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the device to which the **KVStoreResultSet** object belongs. | 6970e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match. | 6971e41f4b71Sopenharmony_ci| callback | AsyncCallback<number> | Yes | Callback used to return the number of results obtained.| 6972e41f4b71Sopenharmony_ci 6973e41f4b71Sopenharmony_ci**Error codes** 6974e41f4b71Sopenharmony_ci 6975e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 6976e41f4b71Sopenharmony_ci 6977e41f4b71Sopenharmony_ci| ID| **Error Message** | 6978e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 6979e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 6980e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 6981e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 6982e41f4b71Sopenharmony_ci 6983e41f4b71Sopenharmony_ci**Example** 6984e41f4b71Sopenharmony_ci 6985e41f4b71Sopenharmony_ci```ts 6986e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 6987e41f4b71Sopenharmony_ci 6988e41f4b71Sopenharmony_citry { 6989e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 6990e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 6991e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 6992e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 6993e41f4b71Sopenharmony_ci key: key + i, 6994e41f4b71Sopenharmony_ci value: { 6995e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 6996e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 6997e41f4b71Sopenharmony_ci } 6998e41f4b71Sopenharmony_ci } 6999e41f4b71Sopenharmony_ci entries.push(entry); 7000e41f4b71Sopenharmony_ci } 7001e41f4b71Sopenharmony_ci kvStore.putBatch(entries, async (err: BusinessError) => { 7002e41f4b71Sopenharmony_ci if (err != undefined) { 7003e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 7004e41f4b71Sopenharmony_ci return; 7005e41f4b71Sopenharmony_ci } 7006e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 7007e41f4b71Sopenharmony_ci const query = new distributedKVStore.Query(); 7008e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 7009e41f4b71Sopenharmony_ci if (kvStore != null) { 7010e41f4b71Sopenharmony_ci kvStore.getResultSize('localDeviceId', query, async (err: BusinessError, resultSize: number) => { 7011e41f4b71Sopenharmony_ci if (err != undefined) { 7012e41f4b71Sopenharmony_ci console.error(`Failed to get resultSize.code is ${err.code},message is ${err.message}`); 7013e41f4b71Sopenharmony_ci return; 7014e41f4b71Sopenharmony_ci } 7015e41f4b71Sopenharmony_ci console.info('Succeeded in getting resultSize'); 7016e41f4b71Sopenharmony_ci }); 7017e41f4b71Sopenharmony_ci } 7018e41f4b71Sopenharmony_ci }); 7019e41f4b71Sopenharmony_ci} catch (e) { 7020e41f4b71Sopenharmony_ci let error = e as BusinessError; 7021e41f4b71Sopenharmony_ci console.error(`Failed to get resultSize.code is ${error.code},message is ${error.message}`); 7022e41f4b71Sopenharmony_ci} 7023e41f4b71Sopenharmony_ci``` 7024e41f4b71Sopenharmony_ci 7025e41f4b71Sopenharmony_ci### getResultSize 7026e41f4b71Sopenharmony_ci 7027e41f4b71Sopenharmony_cigetResultSize(deviceId: string, query: Query): Promise<number> 7028e41f4b71Sopenharmony_ci 7029e41f4b71Sopenharmony_ciObtains the number of results that matches the specified device ID and **Query** object. This API uses a promise to return the result. 7030e41f4b71Sopenharmony_ci> **NOTE** 7031e41f4b71Sopenharmony_ci> 7032e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 7033e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see [sync()](#sync). 7034e41f4b71Sopenharmony_ci 7035e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 7036e41f4b71Sopenharmony_ci 7037e41f4b71Sopenharmony_ci**Parameters** 7038e41f4b71Sopenharmony_ci 7039e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 7040e41f4b71Sopenharmony_ci| -------- | -------------- | ---- | ---------------------------------- | 7041e41f4b71Sopenharmony_ci| deviceId | string | Yes | ID of the device to which the **KVStoreResultSet** object belongs.| 7042e41f4b71Sopenharmony_ci| query | [Query](#query) | Yes | **Query** object to match. | 7043e41f4b71Sopenharmony_ci 7044e41f4b71Sopenharmony_ci**Return value** 7045e41f4b71Sopenharmony_ci 7046e41f4b71Sopenharmony_ci| Type | Description | 7047e41f4b71Sopenharmony_ci| --------------------- | ------------------------------------------------------ | 7048e41f4b71Sopenharmony_ci| Promise<number> | Promise used to return the number of results obtained. | 7049e41f4b71Sopenharmony_ci 7050e41f4b71Sopenharmony_ci**Error codes** 7051e41f4b71Sopenharmony_ci 7052e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md) and [Universal Error Codes](../errorcode-universal.md). 7053e41f4b71Sopenharmony_ci 7054e41f4b71Sopenharmony_ci| ID| **Error Message** | 7055e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | 7056e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types. | 7057e41f4b71Sopenharmony_ci| 15100003 | Database corrupted. | 7058e41f4b71Sopenharmony_ci| 15100005 | Database or result set already closed. | 7059e41f4b71Sopenharmony_ci 7060e41f4b71Sopenharmony_ci**Example** 7061e41f4b71Sopenharmony_ci 7062e41f4b71Sopenharmony_ci```ts 7063e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 7064e41f4b71Sopenharmony_ci 7065e41f4b71Sopenharmony_citry { 7066e41f4b71Sopenharmony_ci let entries: distributedKVStore.Entry[] = []; 7067e41f4b71Sopenharmony_ci for (let i = 0; i < 10; i++) { 7068e41f4b71Sopenharmony_ci let key = 'batch_test_string_key'; 7069e41f4b71Sopenharmony_ci let entry: distributedKVStore.Entry = { 7070e41f4b71Sopenharmony_ci key: key + i, 7071e41f4b71Sopenharmony_ci value: { 7072e41f4b71Sopenharmony_ci type: distributedKVStore.ValueType.STRING, 7073e41f4b71Sopenharmony_ci value: 'batch_test_string_value' 7074e41f4b71Sopenharmony_ci } 7075e41f4b71Sopenharmony_ci } 7076e41f4b71Sopenharmony_ci entries.push(entry); 7077e41f4b71Sopenharmony_ci } 7078e41f4b71Sopenharmony_ci kvStore.putBatch(entries).then(async () => { 7079e41f4b71Sopenharmony_ci console.info('Succeeded in putting batch'); 7080e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 7081e41f4b71Sopenharmony_ci console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 7082e41f4b71Sopenharmony_ci }); 7083e41f4b71Sopenharmony_ci let query = new distributedKVStore.Query(); 7084e41f4b71Sopenharmony_ci query.prefixKey("batch_test"); 7085e41f4b71Sopenharmony_ci kvStore.getResultSize('localDeviceId', query).then((resultSize: number) => { 7086e41f4b71Sopenharmony_ci console.info('Succeeded in getting resultSize'); 7087e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 7088e41f4b71Sopenharmony_ci console.error(`Failed to get resultSize.code is ${err.code},message is ${err.message}`); 7089e41f4b71Sopenharmony_ci }); 7090e41f4b71Sopenharmony_ci} catch (e) { 7091e41f4b71Sopenharmony_ci let error = e as BusinessError; 7092e41f4b71Sopenharmony_ci console.error(`Failed to get resultSize.code is ${error.code},message is ${error.message}`); 7093e41f4b71Sopenharmony_ci} 7094e41f4b71Sopenharmony_ci``` 7095