js-apis-distributed-data.md 165.5 KB
Newer Older
A
annie_wangli 已提交
1
# Distributed Data Management
Z
zengyawen 已提交
2

A
Annie_wang 已提交
3 4 5 6 7 8 9 10 11 12
The distributed data management module implements collaboration between databases of different devices for applications. The APIs provided by distributed data management can be used to save data to distributed databases and perform operations such as adding, deleting, modifying, querying, and synchronizing data in distributed databases.

This module provides the following functions:

- [KVManager](#kvmanager): provides a **KVManager** instance to manage key-value (KV) stores.
- [KvStoreResultSet<sup>8+</sup>](#kvstoreresultset8): provides methods to obtain the KV store result set and query or move the data read position.
- [Query<sup>8+</sup>](#query8): provides methods to query data from the database through a **Query** instance by using predicates.
- [KVStore](#kvstore): provides methods to add data, delete data, and observe data changes and data synchronization through a **KVStore** instance.
- [SingleKVStore](#singlekvstore): provides methods to query and synchronize data in a single KV store. This class inherits from [KVStore](#kvstore), and data is not distinguished by device.
- [DeviceKVStore<sup>8+</sup> ](#devicekvstore8): provides methods to query and synchronize data in a device KV store. This class inherits from [KVStore](#kvstore), and data is distinguished by device.
A
Annie_wang 已提交
13

A
Annie_wang 已提交
14
>**NOTE**<br/>
A
Annie_wang 已提交
15
>
Z
zengyawen 已提交
16 17
>The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

A
annie_wangli 已提交
18 19

## Modules to Import
Z
zengyawen 已提交
20

A
Annie_wang 已提交
21
```js
Z
zengyawen 已提交
22 23 24 25
import distributedData from '@ohos.data.distributedData';
```


A
annie_wangli 已提交
26
## distributedData.createKVManager
Z
zengyawen 已提交
27

A
annie_wangli 已提交
28 29
createKVManager(config: KVManagerConfig, callback: AsyncCallback&lt;KVManager&gt;): void

A
Annie_wang 已提交
30
Creates a **KVManager** instance to manage KV stores. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
31

A
annie_wangli 已提交
32 33 34
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**
A
annie_wangli 已提交
35 36 37

| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
A
Annie_wang 已提交
38 39
| config | [KVManagerConfig](#kvmanagerconfig) | Yes | Configuration of the **KVManager** instance, including the bundle name and user information of the caller.|
| callback | AsyncCallback&lt;[KVManager](#kvmanager)&gt; | Yes | Callback invoked to return the **KVManager** instance created.|
Z
zengyawen 已提交
40

A
annie_wangli 已提交
41
**Example**
A
Annie_wang 已提交
42
```js
A
annie_wangli 已提交
43 44 45 46 47 48 49
let kvManager;
try {
    const kvManagerConfig = {
        bundleName : 'com.example.datamanagertest',
        userInfo : {
            userId : '0',
            userType : distributedData.UserType.SAME_USER_ID
Z
zengyawen 已提交
50 51
        }
    }
A
annie_wangli 已提交
52 53 54 55 56 57 58 59 60 61 62 63
    distributedData.createKVManager(kvManagerConfig, function (err, manager) {
        if (err) {
            console.log("createKVManager err: "  + JSON.stringify(err));
            return;
        }
        console.log("createKVManager success");
        kvManager = manager;
    });
} catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
Z
zengyawen 已提交
64

A
annie_wangli 已提交
65
## distributedData.createKVManager
Z
zengyawen 已提交
66

A
annie_wangli 已提交
67
createKVManager(config: KVManagerConfig): Promise&lt;KVManager&gt;
Z
zengyawen 已提交
68

A
Annie_wang 已提交
69
Creates a **KVManager** instance to manage KV stores. This API uses a promise to return the result.
Z
zengyawen 已提交
70

A
annie_wangli 已提交
71 72 73
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**
A
annie_wangli 已提交
74

A
annie_wangli 已提交
75 76
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
A
Annie_wang 已提交
77
| config |[KVManagerConfig](#kvmanager) | Yes | Configuration of the **KVManager** instance, including the bundle name and user information of the caller.|
A
annie_wangli 已提交
78

A
annie_wangli 已提交
79
**Return value**
Z
zengyawen 已提交
80

A
annie_wangli 已提交
81 82
| Type| Description|
| -------- | -------- |
A
Annie_wang 已提交
83
| Promise&lt;[KVManager](#kvmanager)&gt; | Promise used to return the **KVManager** instance created.|
A
annie_wangli 已提交
84

A
annie_wangli 已提交
85
**Example**
Z
zengyawen 已提交
86

A
Annie_wang 已提交
87
```js
A
annie_wangli 已提交
88 89 90 91 92 93 94
let kvManager;
try {
    const kvManagerConfig = {
        bundleName : 'com.example.datamanagertest',
        userInfo : {
            userId : '0',
            userType : distributedData.UserType.SAME_USER_ID
Z
zengyawen 已提交
95 96
        }
    }
A
annie_wangli 已提交
97 98 99 100 101 102 103 104 105 106
    distributedData.createKVManager(kvManagerConfig).then((manager) => {
        console.log("createKVManager success");
        kvManager = manager;
    }).catch((err) => {
        console.log("createKVManager err: "  + JSON.stringify(err));
    });
} catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
Z
zengyawen 已提交
107

A
annie_wangli 已提交
108 109 110 111
## KVManagerConfig

Provides configuration of the **KVManager** object, including the bundle name and user information of the caller.

A
annie_wangli 已提交
112 113
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
annie_wangli 已提交
114 115
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
A
Annie_wang 已提交
116
| context | Context | Yes| Context of the application or function.<br>See [Context](js-apis-Context.md) for versions earlier than API version 9.<br>See [Context](js-apis-ability-context.md) for API version 9 or later.|
A
annie_wangli 已提交
117 118
| userInfo | [UserInfo](#userinfo) | Yes | User information.|
| bundleName | string | Yes | Bundle name.|
A
annie_wangli 已提交
119 120

## UserInfo
Z
zengyawen 已提交
121 122 123

Defines user information.

A
annie_wangli 已提交
124 125
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
annie_wangli 已提交
126 127
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
A
annie_wangli 已提交
128 129
| userId | string | Yes | User ID.|
| userType | [UserType](#usertype) | Yes | User type.|
A
annie_wangli 已提交
130 131 132


## UserType
Z
zengyawen 已提交
133

A
Annie_wang 已提交
134
Enumerates the user types.
Z
zengyawen 已提交
135

A
annie_wangli 已提交
136 137
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
Annie_wang 已提交
138
| Name| Value| Description|
A
annie_wangli 已提交
139
| ----- | ------ | ------ |
A
annie_wangli 已提交
140
| SAME_USER_ID | 0 | User who logs in to different devices using the same account.|
A
annie_wangli 已提交
141 142 143 144


## KVManager

A
Annie_wang 已提交
145
Creates a **KVManager** object to obtain KV store information. Before calling any method in **KVManager**, you must use [createKVManager](#distributeddatacreatekvmanager) to create a **KVManager** object.
A
annie_wangli 已提交
146 147 148

### getKVStore

A
annie_wangli 已提交
149
getKVStore&lt;T extends KVStore&gt;(storeId: string, options: Options, callback: AsyncCallback&lt;T&gt;): void
Z
zengyawen 已提交
150

A
Annie_wang 已提交
151
Creates and obtains a KV store. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
152

A
annie_wangli 已提交
153 154 155
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**
Z
zengyawen 已提交
156

A
annie_wangli 已提交
157 158
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
A
annie_wangli 已提交
159 160
| storeId | string | Yes | Unique identifier of the KV store. The length cannot exceed [MAX_STORE_ID_LENGTH](#constants).|
| options | [Options](#options) | Yes | Configuration of the KV store.|
A
Annie_wang 已提交
161
| callback | AsyncCallback&lt;T&gt; , &lt;T extends [KVStore](#kvstore)&gt;| Yes | Callback invoked to return the KV store created.|
Z
zengyawen 已提交
162

A
annie_wangli 已提交
163
**Example**
Z
zengyawen 已提交
164

A
Annie_wang 已提交
165
```js
A
annie_wangli 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
let kvStore;
let kvManager;
try {
    const options = {
        createIfMissing : true,
        encrypt : false,
        backup : false,
        autoSync : true,
        kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
        securityLevel : distributedData.SecurityLevel.S2,
    };
    kvManager.getKVStore('storeId', options, function (err, store) {
        if (err) {
            console.log("getKVStore err: "  + JSON.stringify(err));
            return;
        }
        console.log("getKVStore success");
        kvStore = store;
    });
} catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
Z
zengyawen 已提交
189 190


A
annie_wangli 已提交
191
### getKVStore
Z
zengyawen 已提交
192

A
annie_wangli 已提交
193
getKVStore&lt;T extends KVStore&gt;(storeId: string, options: Options): Promise&lt;T&gt;
Z
zengyawen 已提交
194

A
Annie_wang 已提交
195
Creates and obtains a KV store. This API uses a promise to return the result.
Z
zengyawen 已提交
196

A
annie_wangli 已提交
197
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
198

A
annie_wangli 已提交
199 200 201
**Parameters**

| Name  | Type               | Mandatory | Description   |
A
annie_wangli 已提交
202
| ------- | ---------------------- | ---- | -------------------- |
A
annie_wangli 已提交
203 204
| storeId  | string      | Yes  | Unique identifier of the KV store. The length cannot exceed [MAX_STORE_ID_LENGTH](#constants).|
| options  | [Options](#options)   | Yes  | Configuration of the KV store.|
A
annie_wangli 已提交
205 206


A
annie_wangli 已提交
207
**Return value**
A
annie_wangli 已提交
208

A
annie_wangli 已提交
209
| Type                                   | Description       |
A
annie_wangli 已提交
210
| -------------------------------------- | ------------------------ |
A
Annie_wang 已提交
211
| Promise&lt;T&gt;, &lt;T extends [KVStore](#kvstore)&gt; | Promise used to return the KV store created.|
A
annie_wangli 已提交
212

A
annie_wangli 已提交
213
**Example**
A
annie_wangli 已提交
214

A
Annie_wang 已提交
215
```js
A
annie_wangli 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
let kvStore;
let kvManager;
try {
    const options = {
        createIfMissing : true,
        encrypt : false,
        backup : false,
        autoSync : true,
        kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
        securityLevel : distributedData.SecurityLevel.S2,
    };
    kvManager.getKVStore('storeId', options).then((store) => {
        console.log("getKVStore success");
        kvStore = store;
    }).catch((err) => {
        console.log("getKVStore err: "  + JSON.stringify(err));
    });
} catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
237 238 239

### closeKVStore<sup>8+</sup> ###

A
annie_wangli 已提交
240
closeKVStore(appId: string, storeId: string, kvStore: KVStore, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
241

A
Annie_wang 已提交
242
Closes a KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
243

A
annie_wangli 已提交
244
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
245

A
annie_wangli 已提交
246
**Parameters**
A
annie_wangli 已提交
247

A
annie_wangli 已提交
248 249

| Name  | Type             | Mandatory| Description        |
A
annie_wangli 已提交
250
| ------- | -----------------   | ---- | --------------------------- |
A
annie_wangli 已提交
251
| appId    | string              | Yes  | Bundle name of the app that invokes the KV store.        |
A
Annie_wang 已提交
252 253
| storeId  | string  | Yes  | Unique identifier of the KV store to close. The length cannot exceed [MAX_STORE_ID_LENGTH](#constants).|
| kvStore  | [KVStore](#kvstore) | Yes  | KV store to close.    |
A
Annie_wang 已提交
254
| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
Z
zengyawen 已提交
255

A
annie_wangli 已提交
256
**Example**
Z
zengyawen 已提交
257

A
Annie_wang 已提交
258
```js
A
annie_wangli 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
let kvStore;
let kvManager;
const options = {
    createIfMissing : true,
    encrypt : false,
    backup : false,
    autoSync : true,
    kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
    schema : '',
    securityLevel : distributedData.SecurityLevel.S2,
 }
 try {
    kvManager.getKVStore('storeId', options, async function (err, store) {
    console.log('getKVStore success');
    kvStore = store;
A
Annie_wang 已提交
274
    kvManager.closeKVStore('appId', 'storeId', kvStore, function (err, data) {
A
annie_wangli 已提交
275 276 277 278 279 280 281 282
        console.log('closeKVStore success');
    });
    });
} catch (e) {
    console.log('closeKVStore e ' + e);
}
```

Z
zengyawen 已提交
283

A
annie_wangli 已提交
284
### closeKVStore<sup>8+</sup> ###
Z
zengyawen 已提交
285

A
annie_wangli 已提交
286
closeKVStore(appId: string, storeId: string, kvStore: KVStore): Promise&lt;void&gt;
Z
zengyawen 已提交
287

A
Annie_wang 已提交
288
Closes a KV store. This API uses a promise to return the result.
Z
zengyawen 已提交
289

A
annie_wangli 已提交
290
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
Z
zengyawen 已提交
291

A
annie_wangli 已提交
292 293 294
**Parameters**

| Name | Type| Mandatory | Description       |
A
annie_wangli 已提交
295
| -----  | ------  | ---- | ----------------------------- |
A
annie_wangli 已提交
296
| appId  | string  | Yes  | Bundle name of the app that invokes the KV store.           |
A
Annie_wang 已提交
297
| storeId | string | Yes  | Unique identifier of the KV store to close. The length cannot exceed [MAX_STORE_ID_LENGTH](#constants).|
A
annie_wangli 已提交
298
| kvStore | [KVStore](#kvstore)  | Yes  | KV store to close.       |
Z
zengyawen 已提交
299

A
annie_wangli 已提交
300
**Return value**
Z
zengyawen 已提交
301

A
annie_wangli 已提交
302
| Type         | Description           |
A
annie_wangli 已提交
303
| ------------- | -------------- |
A
Annie_wang 已提交
304
| Promise\<void> | Promise that returns no value.|
Z
zengyawen 已提交
305

A
annie_wangli 已提交
306
**Example**
Z
zengyawen 已提交
307

A
Annie_wang 已提交
308
```js
A
annie_wangli 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
let kvManager;
let kvStore;
const options = {
    createIfMissing : true,
    encrypt : false,
    backup : false,
    autoSync : true,
    kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
    schema : '',
    securityLevel : distributedData.SecurityLevel.S2,
}
 try {
    kvManager.getKVStore('storeId', options).then(async (store) => {
    console.log('getKVStore success');
    kvStore = store;
A
Annie_wang 已提交
324
    kvManager.closeKVStore('appId', 'storeId', kvStore).then(() => {
A
annie_wangli 已提交
325 326 327 328 329 330 331 332 333 334 335
        console.log('closeKVStore success');
    }).catch((err) => {
        console.log('closeKVStore err ' + JSON.stringify(err));
    });
    }).catch((err) => {
        console.log('CloseKVStore getKVStore err ' + JSON.stringify(err));
    });
 } catch (e) {
    console.log('closeKVStore e ' + e);
}  
```
A
annie_wangli 已提交
336 337 338 339


### deleteKVStore<sup>8+</sup> ###

A
annie_wangli 已提交
340
deleteKVStore(appId: string, storeId: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
341

A
Annie_wang 已提交
342
Deletes a KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
343

A
annie_wangli 已提交
344
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
345

A
annie_wangli 已提交
346 347 348
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
349
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
350
| appId  | string  | Yes  | Bundle name of the app that invokes the KV store.    |
A
Annie_wang 已提交
351
| storeId | string | Yes  | Unique identifier of the KV store to delete. The length cannot exceed [MAX_STORE_ID_LENGTH](#constants).|
A
Annie_wang 已提交
352
| callback | AsyncCallback&lt;void&gt;  | Yes  | Callback used to return the result.|
Z
zengyawen 已提交
353

A
annie_wangli 已提交
354
**Example**
Z
zengyawen 已提交
355

A
Annie_wang 已提交
356
```js
A
annie_wangli 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
let kvManager;
let kvStore;
const options = {
    createIfMissing : true,
    encrypt : false,
    backup : false,
    autoSync : true,
    kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
    schema : '',
    securityLevel : distributedData.SecurityLevel.S2,
}
try {
    kvManager.getKVStore('store', options, async function (err, store) {
        console.log('getKVStore success');
        kvStore = store;
A
Annie_wang 已提交
372
        kvManager.deleteKVStore('appId', 'storeId', function (err, data) {
A
annie_wangli 已提交
373
            console.log('deleteKVStore success');
Z
zengyawen 已提交
374
        });
A
annie_wangli 已提交
375 376 377 378 379
    });
} catch (e) {
    console.log('DeleteKVStore e ' + e);
}
```
Z
zengyawen 已提交
380

A
annie_wangli 已提交
381
### deleteKVStore<sup>8+</sup> ###
Z
zengyawen 已提交
382

A
annie_wangli 已提交
383
deleteKVStore(appId: string, storeId: string): Promise&lt;void&gt;
Z
zengyawen 已提交
384

A
Annie_wang 已提交
385
Deletes a KV store. This API uses a promise to return the result.
Z
zengyawen 已提交
386

A
annie_wangli 已提交
387
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
388

A
annie_wangli 已提交
389 390 391
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
392
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
393
| appId  | string  | Yes  | Bundle name of the app that invokes the KV store.    |
A
Annie_wang 已提交
394
| storeId | string | Yes  | Unique identifier of the KV store to delete. The length cannot exceed [MAX_STORE_ID_LENGTH](#constants).|
Z
zengyawen 已提交
395 396


A
annie_wangli 已提交
397
**Return value**
Z
zengyawen 已提交
398

A
annie_wangli 已提交
399
| Type         | Description           |
A
annie_wangli 已提交
400
| ------------- | -------------- |
A
Annie_wang 已提交
401
| Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
402

A
annie_wangli 已提交
403
**Example**
Z
zengyawen 已提交
404

A
Annie_wang 已提交
405
```js
A
annie_wangli 已提交
406 407 408 409 410 411 412 413 414 415 416 417
let kvManager;
let kvStore;
const options = {
    createIfMissing : true,
    encrypt : false,
    backup : false,
    autoSync : true,
    kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
    schema : '',
    securityLevel : distributedData.SecurityLevel.S2,
}
try {
A
Annie_wang 已提交
418
    kvManager.getKVStore('storeId', options).then(async (store) => {
A
annie_wangli 已提交
419 420
        console.log('getKVStore success');
        kvStore = store;
A
Annie_wang 已提交
421
        kvManager.deleteKVStore('appId', 'storeId').then(() => {
A
annie_wangli 已提交
422
            console.log('deleteKVStore success');
Z
zengyawen 已提交
423
        }).catch((err) => {
A
annie_wangli 已提交
424
            console.log('deleteKVStore err ' + JSON.stringify(err));
Z
zengyawen 已提交
425
        });
A
annie_wangli 已提交
426 427 428 429 430 431 432
    }).catch((err) => {
        console.log('getKVStore err ' + JSON.stringify(err));
    });
} catch (e) {
    console.log('deleteKVStore e ' + e);
}
```
Z
zengyawen 已提交
433 434


A
annie_wangli 已提交
435
### getAllKVStoreId<sup>8+</sup> ###
Z
zengyawen 已提交
436

A
annie_wangli 已提交
437
getAllKVStoreId(appId: string, callback: AsyncCallback&lt;string[]&gt;): void
Z
zengyawen 已提交
438

A
Annie_wang 已提交
439
Obtains the IDs of all KV stores that are created by [getKVStore()](#getkvstore) and have not been deleted by [deleteKVStore()](#deletekvstore8). This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
440

A
annie_wangli 已提交
441
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
Z
zengyawen 已提交
442

A
annie_wangli 已提交
443 444 445
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
446
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
447
| appId  | string  | Yes   | Bundle name of the app that invokes the KV store.    |
A
Annie_wang 已提交
448
| callback | AsyncCallback&lt;string[]&gt; | Yes  |Callback used to return the KV store IDs obtained. |
Z
zengyawen 已提交
449

A
annie_wangli 已提交
450
**Example**
Z
zengyawen 已提交
451

A
Annie_wang 已提交
452
```js
A
annie_wangli 已提交
453 454 455 456 457 458 459 460 461 462
let kvManager;
try {
    kvManager.getAllKVStoreId('appId', function (err, data) {
        console.log('GetAllKVStoreId success');
        console.log('GetAllKVStoreId size = ' + data.length);
    });
} catch (e) {
    console.log('GetAllKVStoreId e ' + e);
}
```
Z
zengyawen 已提交
463 464


A
annie_wangli 已提交
465
### getAllKVStoreId<sup>8+</sup> ###
Z
zengyawen 已提交
466

A
annie_wangli 已提交
467
getAllKVStoreId(appId: string): Promise&lt;string[]&gt;
Z
zengyawen 已提交
468

A
Annie_wang 已提交
469
Obtains the IDs of all KV stores that are created by [getKVStore()](#getkvstore) and have not been deleted by [deleteKVStore()](#deletekvstore8). This API uses a promise to return the result.
Z
zengyawen 已提交
470

A
annie_wangli 已提交
471
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
Z
zengyawen 已提交
472

A
annie_wangli 已提交
473 474 475
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
476
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
477
| appId  | string  | Yes   | Bundle name of the app that invokes the KV store.    |
A
annie_wangli 已提交
478 479


A
annie_wangli 已提交
480
**Return value**
A
annie_wangli 已提交
481

A
annie_wangli 已提交
482
| Type         | Description           |
A
annie_wangli 已提交
483 484
| ------------- | -------------- |
| Promise&lt;string[]&gt;| Promise used to return the KV store IDs obtained.|
Z
zengyawen 已提交
485

A
annie_wangli 已提交
486
**Example**
Z
zengyawen 已提交
487

A
Annie_wang 已提交
488
```js
A
annie_wangli 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501
let kvManager;
try {
    console.log('GetAllKVStoreId');
    kvManager.getAllKVStoreId('apppId').then((data) => {
        console.log('getAllKVStoreId success');
        console.log('size = ' + data.length);
    }).catch((err) => {
        console.log('getAllKVStoreId err ' + JSON.stringify(err));
    });
} catch(e) {
    console.log('getAllKVStoreId e ' + e);
}
```
Z
zengyawen 已提交
502 503


A
Annie_wang 已提交
504
### on('distributedDataServiceDie')<sup>8+</sup> ###
Z
zengyawen 已提交
505

A
annie_wangli 已提交
506
on(event: 'distributedDataServiceDie', deathCallback: Callback&lt;void&gt;): void
Z
zengyawen 已提交
507

A
Annie_wang 已提交
508
Subscribes to service status changes.
Z
zengyawen 已提交
509

A
annie_wangli 已提交
510
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
Z
zengyawen 已提交
511

A
annie_wangli 已提交
512 513 514
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
515
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
516 517
| event  | string | Yes   | Event to subscribe to. The value is **distributedDataServiceDie**, which indicates service status changes.|
| deathCallback  | Callback&lt;void&gt;  | Yes   | Callback invoked to return service status changes.|
A
annie_wangli 已提交
518

A
annie_wangli 已提交
519
**Example**
Z
zengyawen 已提交
520

A
Annie_wang 已提交
521
```js
A
annie_wangli 已提交
522 523 524 525 526 527
let kvManager;
try {
    
    console.log('KVManagerOn');
    const deathCallback = function () {
        console.log('death callback call');
A
annie_wangli 已提交
528
    }
A
annie_wangli 已提交
529 530 531 532 533
    kvManager.on('distributedDataServiceDie', deathCallback);
} catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
Z
zengyawen 已提交
534 535


A
Annie_wang 已提交
536
### off('distributedDataServiceDie')<sup>8+</sup> ###
Z
zengyawen 已提交
537

A
annie_wangli 已提交
538
off(event: 'distributedDataServiceDie', deathCallback?: Callback&lt;void&gt;): void
Z
zengyawen 已提交
539

A
Annie_wang 已提交
540
Unsubscribes from the service status changes.
Z
zengyawen 已提交
541

A
annie_wangli 已提交
542
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
Z
zengyawen 已提交
543

A
annie_wangli 已提交
544 545 546
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
547
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
548 549
| event  | string | Yes   | Event to unsubscribe from. The value is **distributedDataServiceDie**, which indicates service status changes.|
| deathCallback  | Callback&lt;void&gt;  | No   | Callback used to return service status changes.|
A
annie_wangli 已提交
550

Z
zengyawen 已提交
551

A
annie_wangli 已提交
552
**Example**
Z
zengyawen 已提交
553

A
Annie_wang 已提交
554
```js
A
annie_wangli 已提交
555 556 557 558 559
let kvManager;
try {
    console.log('KVManagerOff');
    const deathCallback = function () {
        console.log('death callback call');
A
annie_wangli 已提交
560
    }
A
annie_wangli 已提交
561 562 563 564
    kvManager.off('distributedDataServiceDie', deathCallback);
} catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
A
annie_wangli 已提交
565
    
A
annie_wangli 已提交
566
```
Z
zengyawen 已提交
567

A
annie_wangli 已提交
568 569 570
## Options

Provides KV store configuration.
Z
zengyawen 已提交
571

A
annie_wangli 已提交
572 573

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
574
| -----  | ------  | ----  | ----------------------- |
575 576 577 578 579 580 581
| createIfMissing  | boolean | No| Whether to create a KV store if no database file exists. By default, a KV store is created. <br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core   |
| encrypt  | boolean | No|Whether to encrypt database files. By default, database files are not encrypted. <br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core   |
| backup  | boolean | No|Whether to back up database files. By default, database files are backed up. <br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core    |
| autoSync  | boolean | No|Whether database files are automatically synchronized. By default, database files are not automatically synchronized.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core<br>**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC    |
| kvStoreType | [KVStoreType](#kvstoretype) | No|Type of the KV store to create. By default, a device KV store is created. The device KV store stores data for multiple devices that collaborate with each other.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core|
| securityLevel | [SecurityLevel](#securitylevel) | No|Security level of the KV store. By default, the security level is not set.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| schema<sup>8+</sup> | [Schema](#schema8) | No| Schema used to define the values stored in a KV store.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore|
Z
zengyawen 已提交
582 583


A
annie_wangli 已提交
584
## KVStoreType
Z
zengyawen 已提交
585

A
Annie_wang 已提交
586
Enumerates the KV store types.
Z
zengyawen 已提交
587

A
annie_wangli 已提交
588

A
Annie_wang 已提交
589
| Name | Value| Description                   |
A
annie_wangli 已提交
590
| ---   | ----  | ----------------------- |
A
Annie_wang 已提交
591 592 593
| DEVICE_COLLABORATION  | 0 | Device KV store.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore  |
| SINGLE_VERSION  | 1 | Single KV store.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core|
| MULTI_VERSION   | 2 | Multi-version KV store. This type is not supported currently.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore|
Z
zengyawen 已提交
594 595


A
annie_wangli 已提交
596
## SecurityLevel
Z
zengyawen 已提交
597

A
Annie_wang 已提交
598
Enumerates the KV store security levels.
A
annie_wangli 已提交
599

A
annie_wangli 已提交
600 601
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
Annie_wang 已提交
602
| Name | Value| Description                   |
A
annie_wangli 已提交
603
| ---   | ----  | ----------------------- |
A
annie_wangli 已提交
604
| NO_LEVEL  | 0 | No security level is set for the KV store.  |
A
Annie_wang 已提交
605 606 607 608 609
| S0  | 1 | The KV store security level is public.|
| S1  | 2 | The KV store security level is low. If data leakage occurs, minor impact will be caused on the database. For example, a KV store that contains system data such as wallpapers.|
| S2  | 3 | The KV store security level is medium. If data leakage occurs, moderate impact will be caused on the database. For example, a KV store that contains information created by users or call records, such as audio or video clips.|
| S3  | 5 | The KV store security level is high. If data leakage occurs, major impact will be caused on the database. For example, a KV store that contains information such as user fitness, health, and location data.|
| S4  | 6 | The KV store security level is critical. If data leakage occurs, severe impact will be caused on the database. For example, a KV store that contains information such as authentication credentials and financial data.|
A
annie_wangli 已提交
610 611 612 613 614 615


## Constants

Defines the KV store constants.

A
annie_wangli 已提交
616 617
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
Annie_wang 已提交
618
| Name | Value| Description                   |
A
annie_wangli 已提交
619
| ---   | ----  | ----------------------- |
A
Annie_wang 已提交
620 621 622 623
| MAX_KEY_LENGTH  | 1024 | Maximum length of a key in the KV store, in bytes. |
| MAX_VALUE_LENGTH  | 4194303 | Maximum length of a value in the KV store, in bytes. |
| MAX_KEY_LENGTH_DEVICE  | 896 | Maximum length of a device key, in bytes.|
| MAX_STORE_ID_LENGTH  | 128 | Maximum length of a KV store ID, in bytes. |
A
Annie_wang 已提交
624 625
| MAX_QUERY_LENGTH  | 512000 | Maximum query length, in bytes.|
| MAX_BATCH_SIZE  | 128 | Maximum number of batch operations.|
A
annie_wangli 已提交
626 627 628

## Schema<sup>8+</sup> ##

A
Annie_wang 已提交
629
Defines the schema of a KV store. You can create a **Schema** object and place it in [Options](#options) when creating or opening a KV store.
Z
zengyawen 已提交
630

A
annie_wangli 已提交
631
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
632

A
annie_wangli 已提交
633 634
| Name | Type| Description                   |
| ---   | ----  | ----------------------- |
A
Annie_wang 已提交
635
| root<sup>8+</sup>  | [FieldNode](#fieldnode8) | JSON root object.|
A
annie_wangli 已提交
636
| indexes<sup>8+</sup>  | Array\<string> | String array in JSON format. |
A
annie_wangli 已提交
637 638
| mode<sup>8+</sup>  | number | Schema mode. |
| skip<sup>8+</sup>  | number |  Size of a skip of the schema. |
A
annie_wangli 已提交
639

A
annie_wangli 已提交
640
### constructor<sup>8+</sup> ###
A
annie_wangli 已提交
641

A
annie_wangli 已提交
642
constructor()
A
annie_wangli 已提交
643

A
annie_wangli 已提交
644
A constructor used to create a **Schema** instance.
Z
zengyawen 已提交
645

A
annie_wangli 已提交
646
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
Z
zengyawen 已提交
647

A
annie_wangli 已提交
648
## FieldNode<sup>8+</sup> ##
Z
zengyawen 已提交
649

A
annie_wangli 已提交
650
Represents a **Schema** instance, which provides the methods for defining the values stored in a KV store.
Z
zengyawen 已提交
651

A
annie_wangli 已提交
652 653 654 655 656 657
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore

| Name | Type| Description                   |
| ---   | ----  | ----------------------- |
| nullable<sup>8+</sup>  | boolean | Whether the database field can be null.  |
| default<sup>8+</sup>  | string | Default value of a **FieldNode**.|
A
Annie_wang 已提交
658
| type<sup>8+</sup>  | number | Value of the data type corresponding to the specified node.|
A
annie_wangli 已提交
659 660 661 662

### constructor<sup>8+</sup> ###

constructor(name: string)
Z
zengyawen 已提交
663

A
annie_wangli 已提交
664 665 666 667 668 669
A constructor used to create a **FieldNode** instance with a string field.

**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore

**Parameters**

A
annie_wangli 已提交
670 671 672
| Name| Type| Mandatory| Description           |
| ------ | -------- | ---- | --------------- |
| name   | string   | Yes  | Value of **FieldNode**.|
Z
zengyawen 已提交
673

A
annie_wangli 已提交
674 675
### appendChild<sup>8+</sup> ###

A
annie_wangli 已提交
676
appendChild(child: FieldNode): boolean
A
annie_wangli 已提交
677

A
annie_wangli 已提交
678
Appends a child node to this **FieldNode**.
Z
zengyawen 已提交
679

A
annie_wangli 已提交
680
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
Z
zengyawen 已提交
681

A
annie_wangli 已提交
682 683 684
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
685
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
686
| child  | [FieldNode](#fieldnode8) | Yes   | Child node to append.  |
A
annie_wangli 已提交
687

A
annie_wangli 已提交
688
**Return value**
Z
zengyawen 已提交
689

A
annie_wangli 已提交
690
| Type         | Description           |
A
annie_wangli 已提交
691 692
| ------------- | -------------- |
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
693

A
annie_wangli 已提交
694
**Example**
Z
zengyawen 已提交
695

A
Annie_wang 已提交
696
```js
A
annie_wangli 已提交
697 698 699 700 701 702 703 704 705
import ddm from '@ohos.data.distributedData';
try {
    let node = new ddm.FieldNode("root");
    let child1 = new ddm.FieldNode("child1");
    let child2 = new ddm.FieldNode("child2");
    let child3 = new ddm.FieldNode("child3");
    node.appendChild(child1);
    node.appendChild(child2);
    node.appendChild(child3);
A
Annie_wang 已提交
706
    console.log("appendNode " + JSON.stringify(node));
A
annie_wangli 已提交
707 708 709 710 711 712 713 714
    child1 = null;
    child2 = null;
    child3 = null;
    node = null;
} catch (e) {
    console.log("AppendChild " + e);
}
```
A
annie_wangli 已提交
715 716 717 718


## KvStoreResultSet<sup>8+</sup> ##

A
Annie_wang 已提交
719 720 721
Provides methods to obtain the KV store result sets, and query and move the data read position.

Before calling any method in **KvStoreResultSet**, you must use [getKVStore](#getkvstore) to obtain a **KVStore** object.
A
annie_wangli 已提交
722

A
annie_wangli 已提交
723

A
annie_wangli 已提交
724 725
### getCount<sup>8+</sup> ###

A
annie_wangli 已提交
726
getCount(): number
A
annie_wangli 已提交
727

A
Annie_wang 已提交
728
Obtains the total number of rows in the result set.
A
annie_wangli 已提交
729

A
annie_wangli 已提交
730
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
731

A
annie_wangli 已提交
732 733 734
**Return value**

| Type  | Description              |
A
annie_wangli 已提交
735
| ------ | --------------    |
A
Annie_wang 已提交
736
| number |Total number of rows obtained.         |
A
annie_wangli 已提交
737

A
annie_wangli 已提交
738
**Example**
A
annie_wangli 已提交
739

A
Annie_wang 已提交
740
```js
A
annie_wangli 已提交
741 742 743 744
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
745
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
746 747
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
748
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
749 750
    });
    const count = resultSet.getCount();
A
Annie_wang 已提交
751
    console.log("getCount succeed:" + count);
A
annie_wangli 已提交
752
} catch (e) {
A
Annie_wang 已提交
753
    console.log("getCount failed: " + e);
A
annie_wangli 已提交
754 755
}
```
Z
zengyawen 已提交
756

A
annie_wangli 已提交
757
### getPosition<sup>8+</sup> ###
Z
zengyawen 已提交
758

A
annie_wangli 已提交
759
getPosition(): number
Z
zengyawen 已提交
760

A
annie_wangli 已提交
761
Obtains the current data read position (position from which data is read) in the result set.
Z
zengyawen 已提交
762

A
annie_wangli 已提交
763
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
Z
zengyawen 已提交
764

A
annie_wangli 已提交
765 766 767
**Return value**

| Type  | Description              |
A
annie_wangli 已提交
768
| ------ | --------------    |
A
annie_wangli 已提交
769
| number |Current data read position obtained.  |
Z
zengyawen 已提交
770

A
annie_wangli 已提交
771
**Example**
Z
zengyawen 已提交
772

A
Annie_wang 已提交
773
```js
A
annie_wangli 已提交
774 775 776 777
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
778
        console.log('getResultSet succeeded.');
A
annie_wangli 已提交
779 780
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
781
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
782 783
    });
    const position = resultSet.getPosition();
A
Annie_wang 已提交
784
    console.log("getPosition succeed:" + position);
A
annie_wangli 已提交
785
} catch (e) {
A
Annie_wang 已提交
786
    console.log("getPosition failed: " + e);
A
annie_wangli 已提交
787 788
}
```
Z
zengyawen 已提交
789 790


A
annie_wangli 已提交
791
### moveToFirst<sup>8+</sup> ###
Z
zengyawen 已提交
792

A
annie_wangli 已提交
793
moveToFirst(): boolean
Z
zengyawen 已提交
794

A
Annie_wang 已提交
795
Moves the data read position to the first row. If the result set is empty, **false** will be returned.
A
annie_wangli 已提交
796

A
annie_wangli 已提交
797
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
798

A
annie_wangli 已提交
799 800 801
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
802
| ------  | --------------    |
A
annie_wangli 已提交
803
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
804

A
annie_wangli 已提交
805
**Example**
A
annie_wangli 已提交
806

A
Annie_wang 已提交
807
```js
A
annie_wangli 已提交
808 809 810 811
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
812
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
813 814
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
815
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
816
    });
A
Annie_wang 已提交
817 818
    const moved1 = resultSet.moveToFirst();
    console.log("moveToFirst succeed: " + moved1);
A
annie_wangli 已提交
819
} catch (e) {
A
Annie_wang 已提交
820
    console.log("moveToFirst failed " + e);
A
annie_wangli 已提交
821 822
}
```
A
annie_wangli 已提交
823 824 825 826


### moveToLast<sup>8+</sup> ###

A
annie_wangli 已提交
827
moveToLast(): boolean
A
annie_wangli 已提交
828

A
Annie_wang 已提交
829
Moves the data read position to the last row. If the result set is empty, **false** will be returned.
A
annie_wangli 已提交
830

A
annie_wangli 已提交
831
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
832

A
annie_wangli 已提交
833 834 835
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
836
| ------  | --------------    |
A
annie_wangli 已提交
837
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
838

A
annie_wangli 已提交
839
**Example**
A
annie_wangli 已提交
840

A
Annie_wang 已提交
841
```js
A
annie_wangli 已提交
842 843 844 845
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
846
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
847 848
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
849
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
850
    });
A
Annie_wang 已提交
851 852
    const moved2 = resultSet.moveToLast();
    console.log("moveToLast succeed:" + moved2);
A
annie_wangli 已提交
853
} catch (e) {
A
Annie_wang 已提交
854
    console.log("moveToLast failed: " + e);
A
annie_wangli 已提交
855 856
}
```
A
annie_wangli 已提交
857 858 859 860


### moveToNext<sup>8+</sup> ###

A
annie_wangli 已提交
861
moveToNext(): boolean
A
annie_wangli 已提交
862

A
Annie_wang 已提交
863
Moves the data read position to the next row. If the result set is empty, **false** will be returned.
A
annie_wangli 已提交
864

A
annie_wangli 已提交
865
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
866

A
annie_wangli 已提交
867 868 869
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
870
| ------  | --------------    |
A
annie_wangli 已提交
871
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
872

A
annie_wangli 已提交
873
**Example**
A
annie_wangli 已提交
874

A
Annie_wang 已提交
875
```js
A
annie_wangli 已提交
876 877 878 879
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
880
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
881 882
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
883
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
884
    });
A
Annie_wang 已提交
885 886
    const moved3 = resultSet.moveToNext();
    console.log("moveToNext succeed: " + moved3);
A
annie_wangli 已提交
887
} catch (e) {
A
Annie_wang 已提交
888
    console.log("moveToNext failed: " + e);
A
annie_wangli 已提交
889 890
}
```
A
annie_wangli 已提交
891 892 893 894


### moveToPrevious<sup>8+</sup> ###

A
annie_wangli 已提交
895
moveToPrevious(): boolean
A
annie_wangli 已提交
896

A
Annie_wang 已提交
897
Moves the data read position to the previous row. If the result set is empty, **false** will be returned.
A
annie_wangli 已提交
898

A
annie_wangli 已提交
899
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
900

A
annie_wangli 已提交
901 902 903
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
904
| ------  | --------------    |
A
annie_wangli 已提交
905
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
906

A
annie_wangli 已提交
907
**Example**
A
annie_wangli 已提交
908

A
Annie_wang 已提交
909
```js
A
annie_wangli 已提交
910 911 912 913
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
914
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
915 916
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
917
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
918
    });
A
Annie_wang 已提交
919 920
    const moved4 = resultSet.moveToPrevious();
    console.log("moveToPrevious succeed:" + moved4);
A
annie_wangli 已提交
921
} catch (e) {
A
Annie_wang 已提交
922
    console.log("moveToPrevious failed: " + e);
A
annie_wangli 已提交
923 924
}
```
A
annie_wangli 已提交
925 926 927 928


### move<sup>8+</sup> ###

A
annie_wangli 已提交
929
move(offset: number): boolean
A
annie_wangli 已提交
930 931 932

Moves the data read position with the specified offset from the current position.

A
annie_wangli 已提交
933
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
934

A
annie_wangli 已提交
935 936 937
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
938
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
939
| 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.  |
A
annie_wangli 已提交
940

A
annie_wangli 已提交
941
**Return value**
A
annie_wangli 已提交
942

A
annie_wangli 已提交
943
| Type   | Description              |
A
annie_wangli 已提交
944
| ------  | --------------    |
A
annie_wangli 已提交
945
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
946

A
annie_wangli 已提交
947
**Example**
A
annie_wangli 已提交
948

A
Annie_wang 已提交
949
```js
A
annie_wangli 已提交
950 951 952 953
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
954
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
955 956
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
957
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
958
    });
A
Annie_wang 已提交
959 960
    const moved5 = resultSet.move();
    console.log("move succeed:" + moved5);
A
annie_wangli 已提交
961
} catch (e) {
A
Annie_wang 已提交
962
    console.log("move failed: " + e);
A
annie_wangli 已提交
963 964
}
```
A
annie_wangli 已提交
965 966 967 968


### moveToPosition<sup>8+</sup> ###

A
annie_wangli 已提交
969
moveToPosition(position: number): boolean
A
annie_wangli 已提交
970 971 972

Moves the data read position from 0 to an absolute position.

A
annie_wangli 已提交
973
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
974

A
annie_wangli 已提交
975 976 977
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
978
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
979
| position  | number  | Yes   |Absolute position to move to.         |
A
annie_wangli 已提交
980

A
annie_wangli 已提交
981
**Return value**
A
annie_wangli 已提交
982

A
annie_wangli 已提交
983
| Type   | Description              |
A
annie_wangli 已提交
984
| ------  | --------------    |
A
annie_wangli 已提交
985
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
986

A
annie_wangli 已提交
987
**Example**
A
annie_wangli 已提交
988

A
Annie_wang 已提交
989
```js
A
annie_wangli 已提交
990 991 992 993
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
994
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
995 996
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
997
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
998
    });
A
Annie_wang 已提交
999 1000
    const moved6 = resultSet.moveToPosition();
    console.log("moveToPosition succeed: " + moved6);
A
annie_wangli 已提交
1001
} catch (e) {
A
Annie_wang 已提交
1002
    console.log("moveToPosition failed: " + e);
A
annie_wangli 已提交
1003 1004
}
```
A
annie_wangli 已提交
1005 1006 1007 1008


### isFirst<sup>8+</sup> ###

A
annie_wangli 已提交
1009
isFirst(): boolean
A
annie_wangli 已提交
1010 1011 1012

Checks whether the data read position is the first row.

A
annie_wangli 已提交
1013
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1014

A
annie_wangli 已提交
1015 1016 1017
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
1018
| ------  | --------------    |
A
Annie_wang 已提交
1019
| boolean |Returns **true** if the first row is being read; returns **false** otherwise.  |
A
annie_wangli 已提交
1020

A
annie_wangli 已提交
1021
**Example**
A
annie_wangli 已提交
1022

A
Annie_wang 已提交
1023
```js
A
annie_wangli 已提交
1024 1025 1026 1027
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
1028
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
1029 1030
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
1031
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
1032
    });
A
Annie_wang 已提交
1033 1034
    const isfirst = resultSet.isFirst();
    console.log("Check isFirst succeed:" + isfirst);
A
annie_wangli 已提交
1035
} catch (e) {
A
Annie_wang 已提交
1036
    console.log("Check isFirst failed: " + e);
A
annie_wangli 已提交
1037 1038
}
```
A
annie_wangli 已提交
1039 1040 1041 1042


### isLast<sup>8+</sup> ###

A
annie_wangli 已提交
1043
isLast(): boolean
A
annie_wangli 已提交
1044 1045 1046

Checks whether the data read position is the last row.

A
annie_wangli 已提交
1047
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1048

A
annie_wangli 已提交
1049 1050 1051
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
1052
| ------  | --------------    |
A
Annie_wang 已提交
1053
| boolean |Returns **true** if the last row is being read; returns **false** otherwise.  |
A
annie_wangli 已提交
1054

A
annie_wangli 已提交
1055
**Example**
A
annie_wangli 已提交
1056

A
Annie_wang 已提交
1057
```js
A
annie_wangli 已提交
1058 1059 1060 1061
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
1062
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
1063 1064
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
1065
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
1066
    });
A
Annie_wang 已提交
1067 1068
    const islast = resultSet.isLast();
    console.log("Check isLast succeed: " + islast);
A
annie_wangli 已提交
1069
} catch (e) {
A
Annie_wang 已提交
1070
    console.log("Check isLast failed: " + e);
A
annie_wangli 已提交
1071 1072
}
```
A
annie_wangli 已提交
1073 1074 1075

### isBeforeFirst<sup>8+</sup> ###

A
annie_wangli 已提交
1076
isBeforeFirst(): boolean
A
annie_wangli 已提交
1077 1078 1079

Checks whether the data read position is before the first row.

A
annie_wangli 已提交
1080
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1081

A
annie_wangli 已提交
1082 1083 1084
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
1085
| ------  | --------------    |
A
Annie_wang 已提交
1086
| boolean |Returns **true** if the data read position is before the first row; returns **false** otherwise. |
A
annie_wangli 已提交
1087

A
annie_wangli 已提交
1088
**Example**
A
annie_wangli 已提交
1089

A
Annie_wang 已提交
1090
```js
A
annie_wangli 已提交
1091 1092 1093 1094
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
1095
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
1096 1097
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
1098
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
1099
    });
A
Annie_wang 已提交
1100 1101
    const isbeforefirst = resultSet.isBeforeFirst();
    console.log("Check isBeforeFirst succeed: " + isbeforefirst);
A
annie_wangli 已提交
1102
} catch (e) {
A
Annie_wang 已提交
1103
    console.log("Check isBeforeFirst failed: " + e);
A
annie_wangli 已提交
1104 1105
}
```
A
annie_wangli 已提交
1106 1107 1108 1109


### isAfterLast<sup>8+</sup> ###

A
annie_wangli 已提交
1110
isAfterLast(): boolean
A
annie_wangli 已提交
1111 1112 1113

Checks whether the data read position is after the last row.

A
annie_wangli 已提交
1114
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1115

A
annie_wangli 已提交
1116 1117 1118
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
1119
| ------  | --------------    |
A
annie_wangli 已提交
1120
| boolean |Returns **true** if the data read position is after the last row; returns **false** otherwise. |
A
annie_wangli 已提交
1121

A
annie_wangli 已提交
1122
**Example**
A
annie_wangli 已提交
1123

A
Annie_wang 已提交
1124
```js
A
annie_wangli 已提交
1125 1126 1127 1128
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
1129
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
1130 1131
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
1132
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
1133
    });
A
Annie_wang 已提交
1134 1135
    const isafterlast = resultSet.isAfterLast();
    console.log("Check isAfterLast succeed:" + isafterlast);
A
annie_wangli 已提交
1136
} catch (e) {
A
Annie_wang 已提交
1137
    console.log("Check isAfterLast failed: " + e);
A
annie_wangli 已提交
1138 1139
}
```
A
annie_wangli 已提交
1140 1141 1142 1143


### getEntry<sup>8+</sup> ###

A
annie_wangli 已提交
1144
getEntry(): Entry
A
annie_wangli 已提交
1145

A
Annie_wang 已提交
1146
Obtains the KV pair from the current position.
A
annie_wangli 已提交
1147

A
annie_wangli 已提交
1148
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1149

A
annie_wangli 已提交
1150 1151 1152
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1153
| ------  | -------   |
A
Annie_wang 已提交
1154
| [Entry](#entry) |KV pair obtained.|
A
annie_wangli 已提交
1155

A
annie_wangli 已提交
1156
**Example**
A
annie_wangli 已提交
1157

A
Annie_wang 已提交
1158
```js
A
annie_wangli 已提交
1159 1160 1161 1162
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
1163
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
1164 1165
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
1166
        console.log('getResultSet failed: ' + err);
A
annie_wangli 已提交
1167 1168
    });
    const entry  = resultSet.getEntry();
A
Annie_wang 已提交
1169
    console.log("getEntry succeed:" + JSON.stringify(entry));
A
annie_wangli 已提交
1170
} catch (e) {
A
Annie_wang 已提交
1171
    console.log("getEntry failed: " + e);
A
annie_wangli 已提交
1172 1173
}
```
A
annie_wangli 已提交
1174 1175 1176 1177 1178 1179


## Query<sup>8+</sup> ##

Provides methods to create a **Query** object, which defines different data query criteria.

A
annie_wangli 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

### constructor<sup>8+</sup> ###

constructor() 

A constructor used to create a **Schema** instance.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core


A
annie_wangli 已提交
1191 1192
### reset<sup>8+</sup> ###

A
annie_wangli 已提交
1193
reset(): Query
A
annie_wangli 已提交
1194

A
Annie_wang 已提交
1195
Resets the **Query** object.
A
annie_wangli 已提交
1196

A
annie_wangli 已提交
1197
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1198

A
annie_wangli 已提交
1199 1200 1201 1202

**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1203
| ------  | -------   |
Z
zengyawen 已提交
1204
| [Query](#query8) |**Query** object reset.|
A
annie_wangli 已提交
1205

A
annie_wangli 已提交
1206
**Example**
A
annie_wangli 已提交
1207

A
Annie_wang 已提交
1208
```js
A
annie_wangli 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
try {
    let query = new distributedData.Query();
    query.equalTo("key", "value");
    console.log("query is " + query.getSqlLike());
    query.reset();
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
    console.log("simply calls should be ok :" + e);
}
```
A
annie_wangli 已提交
1220 1221 1222 1223


### equalTo<sup>8+</sup> ###

A
Annie_wang 已提交
1224
equalTo(field: string, value: number|string|boolean): Query
A
annie_wangli 已提交
1225

A
Annie_wang 已提交
1226
Creates a **Query** object to match the specified field whose value is equal to the given value.
A
annie_wangli 已提交
1227

A
annie_wangli 已提交
1228
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1229

A
annie_wangli 已提交
1230 1231 1232
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1233
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1234
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
Annie_wang 已提交
1235
| value  | number\|string\|boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1236

A
annie_wangli 已提交
1237
**Return value**
A
annie_wangli 已提交
1238

A
annie_wangli 已提交
1239
| Type   | Description      |
A
annie_wangli 已提交
1240
| ------  | -------   |
A
Annie_wang 已提交
1241
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1242

A
annie_wangli 已提交
1243
**Example**
A
annie_wangli 已提交
1244

A
Annie_wang 已提交
1245
```js
A
annie_wangli 已提交
1246 1247 1248 1249 1250 1251
try {
    let query = new distributedData.Query();
    query.equalTo("field", "value");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1252
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1253 1254
}
```
A
annie_wangli 已提交
1255 1256 1257 1258


### notEqualTo<sup>8+</sup> ###

A
annie_wangli 已提交
1259
notEqualTo(field: string, value: number|string|boolean): Query
A
annie_wangli 已提交
1260

A
annie_wangli 已提交
1261
Creates a **Query** object to match the specified field whose value is not equal to the specified value.
A
annie_wangli 已提交
1262

A
annie_wangli 已提交
1263
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1264

A
annie_wangli 已提交
1265 1266 1267
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1268
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1269
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
Annie_wang 已提交
1270
| value  | number\|string\|boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1271

A
annie_wangli 已提交
1272
**Return value**
A
annie_wangli 已提交
1273

A
annie_wangli 已提交
1274
| Type   | Description      |
A
annie_wangli 已提交
1275
| ------  | -------   |
A
Annie_wang 已提交
1276
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1277

A
annie_wangli 已提交
1278
**Example**
A
annie_wangli 已提交
1279

A
Annie_wang 已提交
1280
```js
A
annie_wangli 已提交
1281 1282 1283 1284 1285 1286
try {
    let query = new distributedData.Query();
    query.notEqualTo("field", "value");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1287
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1288 1289
}
```
A
annie_wangli 已提交
1290 1291 1292 1293


### greaterThan<sup>8+</sup> ###

A
annie_wangli 已提交
1294
greaterThan(field: string, value: number|string|boolean): Query
A
annie_wangli 已提交
1295

A
annie_wangli 已提交
1296
Creates a **Query** object to match the specified field whose value is greater than the specified value.
A
annie_wangli 已提交
1297

A
annie_wangli 已提交
1298
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1299

A
annie_wangli 已提交
1300 1301 1302
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1303
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1304
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1305
| value  | number\|string\|boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1306

A
annie_wangli 已提交
1307
**Return value**
A
annie_wangli 已提交
1308

A
annie_wangli 已提交
1309
| Type   | Description      |
A
annie_wangli 已提交
1310
| ------  | -------   |
A
Annie_wang 已提交
1311
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1312

A
annie_wangli 已提交
1313
**Example**
A
annie_wangli 已提交
1314

A
Annie_wang 已提交
1315
```js
A
annie_wangli 已提交
1316 1317 1318 1319 1320 1321
try {
    let query = new distributedData.Query();
    query.greaterThan("field", "value");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1322
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1323 1324
}
```
A
annie_wangli 已提交
1325 1326 1327 1328


### lessThan<sup>8+</sup> ###

A
annie_wangli 已提交
1329
lessThan(field: string, value: number|string): Query
A
annie_wangli 已提交
1330

A
annie_wangli 已提交
1331
Creates a **Query** object to match the specified field whose value is less than the specified value.
A
annie_wangli 已提交
1332

A
annie_wangli 已提交
1333
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1334

A
annie_wangli 已提交
1335 1336 1337
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1338
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1339
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1340
| value  | number\|string\|boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1341

A
annie_wangli 已提交
1342
**Return value**
A
annie_wangli 已提交
1343

A
annie_wangli 已提交
1344
| Type   | Description      |
A
annie_wangli 已提交
1345
| ------  | -------   |
A
Annie_wang 已提交
1346
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1347

A
annie_wangli 已提交
1348
**Example**
A
annie_wangli 已提交
1349

A
Annie_wang 已提交
1350
```js
A
annie_wangli 已提交
1351 1352 1353 1354 1355 1356
try {
    let query = new distributedData.Query();
    query.lessThan("field", "value");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1357
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1358 1359
}
```
A
annie_wangli 已提交
1360 1361 1362 1363


### greaterThanOrEqualTo<sup>8+</sup> ###

A
annie_wangli 已提交
1364
greaterThanOrEqualTo(field: string, value: number|string): Query
A
annie_wangli 已提交
1365

A
annie_wangli 已提交
1366
Creates a **Query** object to match the specified field whose value is greater than or equal to the specified value.
A
annie_wangli 已提交
1367

A
annie_wangli 已提交
1368
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1369

A
annie_wangli 已提交
1370 1371 1372
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1373
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1374
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1375
| value  | number\|string\|boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1376

A
annie_wangli 已提交
1377
**Return value**
A
annie_wangli 已提交
1378

A
annie_wangli 已提交
1379
| Type   | Description      |
A
annie_wangli 已提交
1380
| ------  | -------   |
A
Annie_wang 已提交
1381
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1382

A
annie_wangli 已提交
1383
**Example**
A
annie_wangli 已提交
1384

A
Annie_wang 已提交
1385
```js
A
annie_wangli 已提交
1386 1387 1388 1389 1390 1391
try {
    let query = new distributedData.Query();
    query.greaterThanOrEqualTo("field", "value");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1392
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1393 1394
}
```
A
annie_wangli 已提交
1395 1396 1397 1398


### lessThanOrEqualTo<sup>8+</sup> ###

A
annie_wangli 已提交
1399
lessThanOrEqualTo(field: string, value: number|string): Query
A
annie_wangli 已提交
1400

A
annie_wangli 已提交
1401
Creates a **Query** object to match the specified field whose value is less than or equal to the specified value.
A
annie_wangli 已提交
1402

A
annie_wangli 已提交
1403
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1404

A
annie_wangli 已提交
1405 1406 1407
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1408
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1409
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1410
| value  | number\|string\|boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1411

A
annie_wangli 已提交
1412
**Return value**
A
annie_wangli 已提交
1413

A
annie_wangli 已提交
1414
| Type   | Description      |
A
annie_wangli 已提交
1415
| ------  | -------   |
A
Annie_wang 已提交
1416
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1417

A
annie_wangli 已提交
1418
**Example**
A
annie_wangli 已提交
1419

A
Annie_wang 已提交
1420
```js
A
annie_wangli 已提交
1421 1422 1423 1424 1425 1426
try {
    let query = new distributedData.Query();
    query.lessThanOrEqualTo("field", "value");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1427
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1428 1429
}
```
A
annie_wangli 已提交
1430 1431 1432 1433


### isNull<sup>8+</sup> ###

A
annie_wangli 已提交
1434
isNull(field: string): Query
A
annie_wangli 已提交
1435 1436 1437

Creates a **Query** object to match the specified field whose value is **null**.

A
annie_wangli 已提交
1438 1439 1440 1441 1442
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1443
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1444
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1445

A
annie_wangli 已提交
1446
**Return value**
A
annie_wangli 已提交
1447

A
annie_wangli 已提交
1448
| Type   | Description      |
A
annie_wangli 已提交
1449
| ------  | -------   |
A
Annie_wang 已提交
1450
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1451

A
annie_wangli 已提交
1452
**Example**
A
annie_wangli 已提交
1453

A
Annie_wang 已提交
1454
```js
A
annie_wangli 已提交
1455 1456 1457 1458 1459 1460
try {
    let query = new distributedData.Query();
    query.isNull("field");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1461
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1462 1463
}
```
A
annie_wangli 已提交
1464 1465 1466 1467


### inNumber<sup>8+</sup> ###

A
annie_wangli 已提交
1468
inNumber(field: string, valueList: number[]): Query
A
annie_wangli 已提交
1469 1470 1471 1472

Creates a **Query** object to match the specified field whose value is within the specified list of numbers.


A
annie_wangli 已提交
1473 1474 1475 1476 1477
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1478
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1479
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1480
| valueList  | number[]  | Yes   | List of numbers.|
A
annie_wangli 已提交
1481

A
annie_wangli 已提交
1482
**Return value**
A
annie_wangli 已提交
1483

A
annie_wangli 已提交
1484
| Type   | Description      |
A
annie_wangli 已提交
1485
| ------  | -------   |
A
Annie_wang 已提交
1486
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1487

A
annie_wangli 已提交
1488
**Example**
A
annie_wangli 已提交
1489

A
Annie_wang 已提交
1490
```js
A
annie_wangli 已提交
1491 1492 1493 1494 1495 1496
try {
    let query = new distributedData.Query();
    query.inNumber("field", [0, 1]);
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1497
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1498 1499
}
```
A
annie_wangli 已提交
1500 1501 1502 1503


### inString<sup>8+</sup> ###

A
annie_wangli 已提交
1504
inString(field: string, valueList: string[]): Query
A
annie_wangli 已提交
1505 1506 1507

Creates a **Query** object to match the specified field whose value is within the specified list of strings.

A
annie_wangli 已提交
1508
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1509

A
annie_wangli 已提交
1510 1511 1512
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1513
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1514
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1515
| valueList  | string[]  | Yes   | List of strings.|
A
annie_wangli 已提交
1516

A
annie_wangli 已提交
1517
**Return value**
A
annie_wangli 已提交
1518

A
annie_wangli 已提交
1519
| Type   | Description      |
A
annie_wangli 已提交
1520
| ------  | -------   |
A
Annie_wang 已提交
1521
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1522

A
annie_wangli 已提交
1523
**Example**
A
annie_wangli 已提交
1524

A
Annie_wang 已提交
1525
```js
A
annie_wangli 已提交
1526 1527 1528 1529 1530 1531
try {
    let query = new distributedData.Query();
    query.inString("field", ['test1', 'test2']);
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1532
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1533 1534
}
```
A
annie_wangli 已提交
1535 1536 1537 1538


### notInNumber<sup>8+</sup> ###

A
annie_wangli 已提交
1539
notInNumber(field: string, valueList: number[]): Query
A
annie_wangli 已提交
1540 1541 1542

Creates a **Query** object to match the specified field whose value is not within the specified list of numbers.

A
annie_wangli 已提交
1543
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1544

A
annie_wangli 已提交
1545 1546 1547
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1548
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1549
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1550
| valueList  | number[]  | Yes   | List of numbers.|
A
annie_wangli 已提交
1551

A
annie_wangli 已提交
1552
**Return value**
A
annie_wangli 已提交
1553

A
annie_wangli 已提交
1554
| Type   | Description      |
A
annie_wangli 已提交
1555
| ------  | -------   |
A
Annie_wang 已提交
1556
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1557

A
annie_wangli 已提交
1558
**Example**
A
annie_wangli 已提交
1559

A
Annie_wang 已提交
1560
```js
A
annie_wangli 已提交
1561 1562 1563 1564 1565 1566
try {
    let query = new distributedData.Query();
    query.notInNumber("field", [0, 1]);
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1567
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1568 1569
}
```
A
annie_wangli 已提交
1570 1571 1572 1573


### notInString<sup>8+</sup> ###

A
annie_wangli 已提交
1574
notInString(field: string, valueList: string[]): Query
A
annie_wangli 已提交
1575 1576 1577

Creates a **Query** object to match the specified field whose value is not within the specified list of strings.

A
annie_wangli 已提交
1578
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1579

A
annie_wangli 已提交
1580 1581 1582
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1583
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1584
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1585
| valueList  | string[]  | Yes   | List of strings.|
A
annie_wangli 已提交
1586

A
annie_wangli 已提交
1587
**Return value**
A
annie_wangli 已提交
1588

A
annie_wangli 已提交
1589
| Type   | Description      |
A
annie_wangli 已提交
1590
| ------  | -------   |
A
Annie_wang 已提交
1591
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1592

A
annie_wangli 已提交
1593
**Example**
A
annie_wangli 已提交
1594

A
Annie_wang 已提交
1595
```js
A
annie_wangli 已提交
1596 1597 1598 1599 1600 1601
try {
    let query = new distributedData.Query();
    query.notInString("field", ['test1', 'test2']);
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1602
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1603 1604
}
```
A
annie_wangli 已提交
1605 1606 1607 1608


### like<sup>8+</sup> ###

A
annie_wangli 已提交
1609
like(field: string, value: string): Query
A
annie_wangli 已提交
1610

A
annie_wangli 已提交
1611
Creates a **Query** object to match the specified field whose value is similar to the specified string.
A
annie_wangli 已提交
1612

A
annie_wangli 已提交
1613
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1614

A
annie_wangli 已提交
1615 1616 1617
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1618
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1619
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
Annie_wang 已提交
1620
| value  | string  | Yes   | String specified.|
A
annie_wangli 已提交
1621

A
annie_wangli 已提交
1622
**Return value**
A
annie_wangli 已提交
1623

A
annie_wangli 已提交
1624
| Type   | Description      |
A
annie_wangli 已提交
1625
| ------  | -------   |
A
Annie_wang 已提交
1626
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1627

A
annie_wangli 已提交
1628
**Example**
A
annie_wangli 已提交
1629

A
Annie_wang 已提交
1630
```js
A
annie_wangli 已提交
1631 1632 1633 1634 1635 1636
try {
    let query = new distributedData.Query();
    query.like("field", "value");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1637
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1638 1639
}
```
A
annie_wangli 已提交
1640 1641 1642 1643


### unlike<sup>8+</sup> ###

A
annie_wangli 已提交
1644
unlike(field: string, value: string): Query
A
annie_wangli 已提交
1645

A
annie_wangli 已提交
1646
Creates a **Query** object to match the specified field whose value is not similar to the specified string.
A
annie_wangli 已提交
1647

A
annie_wangli 已提交
1648
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1649

A
annie_wangli 已提交
1650 1651 1652
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1653
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1654 1655
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
| value  | string  | Yes   | String specified.|
A
annie_wangli 已提交
1656

A
annie_wangli 已提交
1657
**Return value**
A
annie_wangli 已提交
1658

A
annie_wangli 已提交
1659
| Type   | Description      |
A
annie_wangli 已提交
1660
| ------  | -------   |
A
Annie_wang 已提交
1661
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1662

A
annie_wangli 已提交
1663
**Example**
A
annie_wangli 已提交
1664

A
Annie_wang 已提交
1665
```js
A
annie_wangli 已提交
1666 1667 1668 1669 1670 1671
try {
    let query = new distributedData.Query();
    query.unlike("field", "value");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1672
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1673 1674
}
```
A
annie_wangli 已提交
1675 1676 1677 1678


### and<sup>8+</sup> ###

A
annie_wangli 已提交
1679
and(): Query
A
annie_wangli 已提交
1680 1681 1682

Creates a **Query** object with the AND condition.

A
annie_wangli 已提交
1683
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1684

A
annie_wangli 已提交
1685 1686 1687
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1688
| ------  | -------   |
A
annie_wangli 已提交
1689
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1690

A
annie_wangli 已提交
1691
**Example**
A
annie_wangli 已提交
1692

A
Annie_wang 已提交
1693
```js
A
annie_wangli 已提交
1694 1695 1696 1697 1698 1699 1700 1701
try {
    let query = new distributedData.Query();
    query.notEqualTo("field", "value1");
    query.and();
    query.notEqualTo("field", "value2");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1702
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1703 1704
}
```
A
annie_wangli 已提交
1705 1706 1707 1708


### or<sup>8+</sup> ###

A
annie_wangli 已提交
1709
or(): Query
A
annie_wangli 已提交
1710 1711 1712

Creates a **Query** object with the OR condition.

A
annie_wangli 已提交
1713
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1714

A
annie_wangli 已提交
1715 1716 1717
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1718
| ------  | -------   |
A
annie_wangli 已提交
1719
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1720

A
annie_wangli 已提交
1721
**Example**
A
annie_wangli 已提交
1722

A
Annie_wang 已提交
1723
```js
A
annie_wangli 已提交
1724 1725 1726 1727 1728 1729 1730 1731
try {
    let query = new distributedData.Query();
    query.notEqualTo("field", "value1");
    query.or();
    query.notEqualTo("field", "value2");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1732
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1733 1734
}
```
A
annie_wangli 已提交
1735 1736 1737 1738


### orderByAsc<sup>8+</sup> ###

A
annie_wangli 已提交
1739
orderByAsc(field: string): Query
A
annie_wangli 已提交
1740 1741 1742

Creates a **Query** object to sort the query results in ascending order.

A
annie_wangli 已提交
1743
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1744

A
annie_wangli 已提交
1745 1746 1747
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1748
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1749
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1750

A
annie_wangli 已提交
1751
**Return value**
A
annie_wangli 已提交
1752

A
annie_wangli 已提交
1753
| Type   | Description      |
A
annie_wangli 已提交
1754
| ------  | -------   |
A
Annie_wang 已提交
1755
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1756

A
annie_wangli 已提交
1757
**Example**
A
annie_wangli 已提交
1758

A
Annie_wang 已提交
1759
```js
A
annie_wangli 已提交
1760 1761 1762 1763 1764 1765 1766
try {
    let query = new distributedData.Query();
    query.notEqualTo("field", "value");
    query.orderByAsc("field");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1767
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1768 1769
}
```
A
annie_wangli 已提交
1770 1771 1772 1773


### orderByDesc<sup>8+</sup> ###

A
annie_wangli 已提交
1774
orderByDesc(field: string): Query
A
annie_wangli 已提交
1775 1776 1777

Creates a **Query** object to sort the query results in descending order.

A
annie_wangli 已提交
1778
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1779

A
annie_wangli 已提交
1780 1781 1782
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1783
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1784
| fieId  | string  | Yes   |Field to match. It cannot contain '^'. |
A
annie_wangli 已提交
1785

A
annie_wangli 已提交
1786
**Return value**
A
annie_wangli 已提交
1787

A
annie_wangli 已提交
1788
| Type   | Description      |
A
annie_wangli 已提交
1789
| ------  | -------   |
A
Annie_wang 已提交
1790
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1791

A
annie_wangli 已提交
1792
**Example**
A
annie_wangli 已提交
1793

A
Annie_wang 已提交
1794
```js
A
annie_wangli 已提交
1795 1796 1797 1798 1799 1800 1801
try {
    let query = new distributedData.Query();
    query.notEqualTo("field", "value");
    query.orderByDesc("field");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1802
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1803 1804
}
```
A
annie_wangli 已提交
1805 1806 1807 1808


### limit<sup>8+</sup> ###

A
annie_wangli 已提交
1809
limit(total: number, offset: number): Query
A
annie_wangli 已提交
1810 1811 1812

Creates a **Query** object to specify the number of results and where to start.

A
annie_wangli 已提交
1813
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1814

A
annie_wangli 已提交
1815 1816 1817
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1818
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1819 1820
| total  | number  | Yes   |Number of results to query. |
| offset | number  | Yes   |Start position for query. |
A
annie_wangli 已提交
1821

A
annie_wangli 已提交
1822
**Return value**
A
annie_wangli 已提交
1823

A
annie_wangli 已提交
1824
| Type   | Description      |
A
annie_wangli 已提交
1825
| ------  | -------   |
A
Annie_wang 已提交
1826
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1827

A
annie_wangli 已提交
1828
**Example**
A
annie_wangli 已提交
1829

A
Annie_wang 已提交
1830
```js
G
ge-yafang 已提交
1831 1832
let total = 10;
let offset = 1;
A
annie_wangli 已提交
1833 1834 1835
try {
    let query = new distributedData.Query();
    query.notEqualTo("field", "value");
G
ge-yafang 已提交
1836
    query.limit(total, offset);
A
annie_wangli 已提交
1837 1838 1839
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1840
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1841 1842
}
```
A
annie_wangli 已提交
1843 1844 1845 1846


### isNotNull<sup>8+</sup> ###

A
annie_wangli 已提交
1847
isNotNull(field: string): Query
A
annie_wangli 已提交
1848

A
Annie_wang 已提交
1849
Creates a **Query** object to match the specified field whose value is not **null**.
A
annie_wangli 已提交
1850

A
annie_wangli 已提交
1851
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1852

A
annie_wangli 已提交
1853 1854 1855
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1856
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
1857
| fieId  | string  | Yes   |Field to match. It cannot contain '^'.     |
A
annie_wangli 已提交
1858

A
annie_wangli 已提交
1859
**Return value**
A
annie_wangli 已提交
1860

A
annie_wangli 已提交
1861
| Type   | Description      |
A
annie_wangli 已提交
1862
| ------  | -------   |
A
Annie_wang 已提交
1863
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1864

A
annie_wangli 已提交
1865
**Example**
A
annie_wangli 已提交
1866

A
Annie_wang 已提交
1867
```js
A
annie_wangli 已提交
1868 1869 1870 1871 1872 1873
try {
    let query = new distributedData.Query();
    query.isNotNull("field");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1874
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1875 1876
}
```
A
annie_wangli 已提交
1877 1878 1879 1880


### beginGroup<sup>8+</sup> ###

A
annie_wangli 已提交
1881
beginGroup(): Query
A
annie_wangli 已提交
1882

A
annie_wangli 已提交
1883
Creates a **Query** object for a query condition group with a left parenthesis.
A
annie_wangli 已提交
1884

A
annie_wangli 已提交
1885
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1886

A
annie_wangli 已提交
1887 1888 1889
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1890
| ------  | -------   |
A
Annie_wang 已提交
1891
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1892

A
annie_wangli 已提交
1893
**Example**
A
annie_wangli 已提交
1894

A
Annie_wang 已提交
1895
```js
A
annie_wangli 已提交
1896 1897 1898 1899 1900 1901 1902 1903
try {
    let query = new distributedData.Query();
    query.beginGroup();
    query.isNotNull("field");
    query.endGroup();
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1904
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1905 1906
}
```
A
annie_wangli 已提交
1907 1908 1909 1910


### endGroup<sup>8+</sup> ###

A
annie_wangli 已提交
1911
endGroup(): Query
A
annie_wangli 已提交
1912

A
annie_wangli 已提交
1913
Creates a **Query** object for a query condition group with a right parenthesis.
A
annie_wangli 已提交
1914

A
annie_wangli 已提交
1915
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1916

A
annie_wangli 已提交
1917 1918 1919
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1920
| ------  | -------   |
A
Annie_wang 已提交
1921
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1922

A
annie_wangli 已提交
1923
**Example**
A
annie_wangli 已提交
1924

A
Annie_wang 已提交
1925
```js
A
annie_wangli 已提交
1926 1927 1928 1929 1930 1931 1932 1933
try {
    let query = new distributedData.Query();
    query.beginGroup();
    query.isNotNull("field");
    query.endGroup();
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1934
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1935 1936
}
```
A
annie_wangli 已提交
1937 1938 1939 1940


### prefixKey<sup>8+</sup> ###

A
annie_wangli 已提交
1941
prefixKey(prefix: string): Query
A
annie_wangli 已提交
1942 1943 1944

Creates a **Query** object with a specified key prefix.

A
annie_wangli 已提交
1945
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1946

A
annie_wangli 已提交
1947 1948 1949
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1950
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1951
| prefix | string  | Yes   |Key prefix.    |
A
annie_wangli 已提交
1952

A
annie_wangli 已提交
1953
**Return value**
A
annie_wangli 已提交
1954

A
annie_wangli 已提交
1955
| Type   | Description      |
A
annie_wangli 已提交
1956
| ------  | -------   |
A
Annie_wang 已提交
1957
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1958

A
annie_wangli 已提交
1959
**Example**
A
annie_wangli 已提交
1960

A
Annie_wang 已提交
1961
```js
A
annie_wangli 已提交
1962 1963 1964 1965 1966 1967 1968
try {
    let query = new distributedData.Query();
    query.prefixKey("$.name");
    query.prefixKey("0");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
1969
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1970 1971
}
```
A
annie_wangli 已提交
1972 1973 1974 1975


### setSuggestIndex<sup>8+</sup> ###

A
annie_wangli 已提交
1976
setSuggestIndex(index: string): Query
A
annie_wangli 已提交
1977 1978 1979

Creates a **Query** object with an index preferentially used for query.

A
annie_wangli 已提交
1980
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1981

A
annie_wangli 已提交
1982 1983 1984
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1985
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1986
| index  | string  | Yes   |Index preferentially used for query.  |
A
annie_wangli 已提交
1987

A
annie_wangli 已提交
1988
**Return value**
A
annie_wangli 已提交
1989

A
annie_wangli 已提交
1990
| Type   | Description      |
A
annie_wangli 已提交
1991
| ------  | -------   |
A
Annie_wang 已提交
1992
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1993

A
annie_wangli 已提交
1994
**Example**
A
annie_wangli 已提交
1995

A
Annie_wang 已提交
1996
```js
A
annie_wangli 已提交
1997 1998 1999 2000 2001 2002 2003
try {
    let query = new distributedData.Query();
    query.setSuggestIndex("$.name");
    query.setSuggestIndex("0");
    console.log("query is " + query.getSqlLike());
    query = null;
} catch (e) {
N
Neil Chen 已提交
2004
   console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
2005 2006
}
```
A
annie_wangli 已提交
2007 2008 2009 2010


### deviceId<sup>8+</sup> ###

A
annie_wangli 已提交
2011
deviceId(deviceId:string):Query
A
annie_wangli 已提交
2012 2013 2014

Creates a **Query** object with the device ID as the key prefix.

A
annie_wangli 已提交
2015
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2016

A
annie_wangli 已提交
2017 2018 2019
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2020
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2021
| deviceId | string  | Yes   |Device ID.  |
A
annie_wangli 已提交
2022 2023


A
annie_wangli 已提交
2024
**Return value**
A
annie_wangli 已提交
2025

A
annie_wangli 已提交
2026
| Type   | Description      |
A
annie_wangli 已提交
2027
| ------  | -------   |
A
Annie_wang 已提交
2028
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
2029

A
annie_wangli 已提交
2030
**Example**
A
annie_wangli 已提交
2031

A
Annie_wang 已提交
2032
```js
A
annie_wangli 已提交
2033 2034 2035 2036 2037 2038 2039 2040
try {
    let query = new distributedData.Query();
    query.deviceId("deviceId");
    console.log("query is " + query.getSqlLike());
} catch (e) {
    console.log("should be ok on Method Chaining : " + e);
}
```
A
annie_wangli 已提交
2041 2042 2043 2044


### getSqlLike<sup>8+</sup> ###

A
annie_wangli 已提交
2045
getSqlLike():string
A
annie_wangli 已提交
2046

A
Annie_wang 已提交
2047
Obtains the query statement of the **Query** object.
A
annie_wangli 已提交
2048

A
annie_wangli 已提交
2049
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2050

A
annie_wangli 已提交
2051 2052 2053
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
2054
| ------  | -------   |
A
Annie_wang 已提交
2055
| string |Returns the query statement obtained.|
A
annie_wangli 已提交
2056

A
annie_wangli 已提交
2057
**Example**
A
annie_wangli 已提交
2058

A
Annie_wang 已提交
2059
```js
A
annie_wangli 已提交
2060 2061 2062 2063 2064
try {
    let query = new distributedData.Query();
    let sql1 = query.getSqlLike();
    console.log("GetSqlLike sql=" + sql1);
} catch (e) {
N
Neil Chen 已提交
2065
    console.log("duplicated calls should be ok : " + e);
A
annie_wangli 已提交
2066 2067
}
```
A
annie_wangli 已提交
2068 2069 2070 2071


## KVStore

A
Annie_wang 已提交
2072 2073 2074
Provides methods to manage data in a KV store, for example, adding or deleting data and subscribing to data changes or completion of data synchronization.

Before calling any method in **KVStore**, you must use [getKVStore](#getkvstore) to obtain a **KVStore** object.
A
annie_wangli 已提交
2075

A
annie_wangli 已提交
2076 2077 2078 2079
### put

put(key: string, value: Uint8Array | string | number | boolean, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
2080
Adds a KV pair of the specified type to this KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2081

A
annie_wangli 已提交
2082
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2083

A
annie_wangli 已提交
2084
**Parameters**
A
annie_wangli 已提交
2085

A
annie_wangli 已提交
2086 2087 2088 2089
| Name | Type| Mandatory | Description                   |
| -----  | ------  | ----  | ----------------------- |
| key    | string  | Yes   |Key of the KV pair to add. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants).  |
| 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).  |
A
Annie_wang 已提交
2090
| callback | AsyncCallback&lt;void&gt; | Yes   |Callback used to return the result.  |
A
annie_wangli 已提交
2091

A
annie_wangli 已提交
2092
**Example**
A
annie_wangli 已提交
2093

A
Annie_wang 已提交
2094
```js
A
annie_wangli 已提交
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string';
const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err,data) {
        if (err != undefined) {
            console.log("put err: " + JSON.stringify(err));
            return;
        }
        console.log("put success");
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
2110 2111 2112 2113 2114 2115


### put

put(key: string, value: Uint8Array | string | number | boolean): Promise&lt;void&gt;

A
Annie_wang 已提交
2116
Adds a KV pair of the specified type to this KV store. This API uses a promise to return the result.
A
annie_wangli 已提交
2117

A
annie_wangli 已提交
2118
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2119

A
annie_wangli 已提交
2120 2121 2122
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2123
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2124 2125
| key    | string  | Yes   |Key of the KV pair to add. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants).  |
| 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).  |
A
annie_wangli 已提交
2126

A
annie_wangli 已提交
2127
**Return value**
A
annie_wangli 已提交
2128

A
annie_wangli 已提交
2129
| Type   | Description      |
A
annie_wangli 已提交
2130
| ------  | -------   |
A
Annie_wang 已提交
2131
| Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
2132

A
annie_wangli 已提交
2133
**Example**
A
annie_wangli 已提交
2134

A
Annie_wang 已提交
2135
```js
A
annie_wangli 已提交
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string';
const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then((data) => {
        console.log("put success: " + JSON.stringify(data));
    }).catch((err) => {
        console.log("put err: " + JSON.stringify(err));
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
2149 2150 2151 2152 2153

### delete

delete(key: string, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
2154
Deletes a KV pair from this KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2155

A
annie_wangli 已提交
2156
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2157

A
annie_wangli 已提交
2158 2159 2160
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2161
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2162
| key    | string  | Yes   |Key of the KV pair to delete. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants).  |
A
Annie_wang 已提交
2163
| callback  | AsyncCallback&lt;void&gt;  | Yes   |Callback used to return the result.  |
A
annie_wangli 已提交
2164

A
annie_wangli 已提交
2165
**Example**
A
annie_wangli 已提交
2166

A
Annie_wang 已提交
2167
```js
A
annie_wangli 已提交
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string';
const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err,data) {
        if (err != undefined) {
            console.log("put err: " + JSON.stringify(err));
            return;
        }
        console.log("put success");
        kvStore.delete(KEY_TEST_STRING_ELEMENT, function (err,data) {
A
annie_wangli 已提交
2179
            if (err != undefined) {
A
annie_wangli 已提交
2180
                console.log("delete err: " + JSON.stringify(err));
A
annie_wangli 已提交
2181 2182
                return;
            }
A
annie_wangli 已提交
2183
            console.log("delete success");
A
annie_wangli 已提交
2184
        });
A
annie_wangli 已提交
2185 2186 2187 2188 2189
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
2190 2191 2192 2193 2194

### delete

delete(key: string): Promise&lt;void&gt;

A
Annie_wang 已提交
2195
Deletes a KV pair from this KV store. This API uses a promise to return the result.
A
annie_wangli 已提交
2196

A
annie_wangli 已提交
2197
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2198

A
annie_wangli 已提交
2199 2200 2201
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2202
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2203
| key    | string  | Yes   |Key of the KV pair to delete. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants).  |
A
annie_wangli 已提交
2204

A
annie_wangli 已提交
2205
**Return value**
A
annie_wangli 已提交
2206

A
annie_wangli 已提交
2207
| Type   | Description      |
A
annie_wangli 已提交
2208
| ------  | -------   |
A
Annie_wang 已提交
2209
| Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
2210

A
annie_wangli 已提交
2211
**Example**
A
annie_wangli 已提交
2212

A
Annie_wang 已提交
2213
```js
A
annie_wangli 已提交
2214 2215 2216 2217 2218 2219 2220 2221
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string';
const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then((data) => {
        console.log("put success: " + JSON.stringify(data));
        kvStore.delete(KEY_TEST_STRING_ELEMENT).then((data) => {
            console.log("delete success");
A
annie_wangli 已提交
2222
        }).catch((err) => {
A
annie_wangli 已提交
2223
            console.log("delete err: " + JSON.stringify(err));
A
annie_wangli 已提交
2224
        });
A
annie_wangli 已提交
2225 2226 2227 2228 2229 2230 2231
    }).catch((err) => {
        console.log("put err: " + JSON.stringify(err));
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
2232

A
Annie_wang 已提交
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
### delete<sup>9+</sup>

delete(predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback&lt;void&gt;): void

Deletes KV pairs that meet the specified predicates. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------  | ----  | ----------------------- |
| predicates    | Predicates  | Yes   |Conditions for deleting data. If this parameter is **null**, define the processing logic.|
| callback  | AsyncCallback&lt;void&gt;  | Yes   |Callback used to return the result.  |

**Example**

```js
import dataSharePredicates from './@ohos.data.dataSharePredicates';
let kvStore;
try {
	let predicates = new dataSharePredicates.DataSharePredicates();
	kvStore.delete(predicates, function (err, data) {
		if (err == undefined) {
			console.log('delete success');
		} else {
			console.log('delete fail' + err);
		}
    });  
} catch (e) {
	console.log('An unexpected error occurred. Error:' + e);
}
```

### delete<sup>9+</sup>

delete(predicates: dataSharePredicates.DataSharePredicates): Promise&lt;void&gt;

Deletes KV pairs that meet the specified predicates. This API uses a promise to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------  | ----  | ----------------------- |
| predicates    | Predicates  | Yes   |Conditions for deleting data. If this parameter is **null**, define the processing logic.|


**Return value**

| Type   | Description      |
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

**Example**

```js
import dataSharePredicates from './@ohos.data.dataSharePredicates';
let kvStore;
try {
	let predicates = new dataSharePredicates.DataSharePredicates();
	let arr = ["name"];
	predicates.inKeys(arr);
	kvStore.put("name", "bob").then((data) => {
		console.log('put success' + JSON.stringify(data));
		kvStore.delete(predicates).then((data) => {
			console.log('delete success');
		}).catch((err) => {
			console.log('delete fail' + JSON.stringify(err));
		});
	}) .catch((err) => {
		console.log(' put fail' + err);
	});
}catch (e) {
	console.log("An unexpected error occurred. Error:" + e);
}

```
A
annie_wangli 已提交
2312

A
Annie_wang 已提交
2313
### on('dataChange')
A
annie_wangli 已提交
2314 2315 2316

on(event: 'dataChange', type: SubscribeType, observer: Callback&lt;ChangeNotification&gt;): void

A
Annie_wang 已提交
2317
Subscribes to data change notifications of the specified type.
A
annie_wangli 已提交
2318

A
annie_wangli 已提交
2319
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2320

A
annie_wangli 已提交
2321 2322 2323
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2324
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
2325
| event  |string  | Yes   |Event to subscribe to. The value is **dataChange**, which indicates data changes.      |
A
annie_wangli 已提交
2326
| type  |[SubscribeType](#subscribetype) | Yes   |Type of data changes.    |
A
Annie_wang 已提交
2327
| observer |Callback&lt;[ChangeNotification](#changenotification)&gt; | Yes   |Callback used to return the data changes.|
A
annie_wangli 已提交
2328

A
annie_wangli 已提交
2329
**Example**
A
annie_wangli 已提交
2330

A
Annie_wang 已提交
2331
```js
A
annie_wangli 已提交
2332 2333 2334 2335 2336
let kvStore;
kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL, function (data) {
    console.log("dataChange callback call data: " + JSON.stringify(data));
});
```
A
annie_wangli 已提交
2337 2338


A
Annie_wang 已提交
2339
### on('syncComplete')
A
annie_wangli 已提交
2340

A
annie_wangli 已提交
2341
on(event: 'syncComplete', syncCallback: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
2342

A
Annie_wang 已提交
2343
Subscribes to data synchronization complete events.
A
annie_wangli 已提交
2344

A
annie_wangli 已提交
2345
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2346

A
annie_wangli 已提交
2347 2348 2349
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2350
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
2351 2352
| event  |string | Yes   |Event to subscribe to. The value is **syncComplete**, which indicates completion of a data synchronization.      |
| syncCallback  |Callback&lt;Array&lt;[string, number]&gt;&gt; | Yes   |Callback used to return the data synchronization result.    |
A
annie_wangli 已提交
2353

A
annie_wangli 已提交
2354
**Example**
A
annie_wangli 已提交
2355

A
Annie_wang 已提交
2356
```js
A
annie_wangli 已提交
2357 2358
let kvStore;
kvStore.on('syncComplete', function (data) {
A
Annie_wang 已提交
2359
    console.log("callback call data: " + data);
A
annie_wangli 已提交
2360 2361
});
```
A
annie_wangli 已提交
2362

A
Annie_wang 已提交
2363
### off('dataChange')<sup>8+</sup>
A
annie_wangli 已提交
2364

A
annie_wangli 已提交
2365
off(event:'dataChange', observer?: Callback&lt;ChangeNotification&gt;): void
A
annie_wangli 已提交
2366

A
Annie_wang 已提交
2367
Unsubscribes from data change notifications.
A
annie_wangli 已提交
2368

A
annie_wangli 已提交
2369
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2370

A
annie_wangli 已提交
2371 2372 2373
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2374
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
2375 2376
| event  |string  | Yes   |Event to unsubscribe from. The value is **dataChange**, which indicates data changes.      |
| observer |Callback&lt;[ChangeNotification](#changenotification)&gt; |No   |Callback used to return the data changes.|
A
annie_wangli 已提交
2377

A
annie_wangli 已提交
2378
**Example**
A
annie_wangli 已提交
2379

A
Annie_wang 已提交
2380
```js
A
annie_wangli 已提交
2381 2382
let kvStore;
kvStore.on('dataChange', function (data) {
A
Annie_wang 已提交
2383
    console.log("callback call data: " + data);
A
annie_wangli 已提交
2384 2385
});
kvStore.off('dataChange', function (data) {
A
Annie_wang 已提交
2386
    console.log("callback call data: " + data);
A
annie_wangli 已提交
2387 2388
});
```
A
annie_wangli 已提交
2389

A
Annie_wang 已提交
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
### off('syncComplete')<sup>9+</sup>

off(event: 'syncComplete', syncCallback?: Callback&lt;Array&lt;[string, number]&gt;&gt;): void

Unsubscribes from data change events. This API uses a synchronous callback to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------  | ----  | ----------------------- |
| event  |string  | Yes   |Event to unsubscribe from. The value is **syncComplete**, which indicates completion of a data synchronization.      |
| syncCallback  |Callback&lt;Array&lt;[string, number]&gt;&gt;   | No   |Callback used to return the synchronization result.   |

**Example**

```js
let kvStore;
try {
    const func = function (data) {
        console.log('syncComplete ' + data)
    };
    kvStore.on('syncComplete', func);
    kvStore.off('syncComplete', func);
}catch(e) {
    console.log('syncComplete e ' + e);
}
```

A
annie_wangli 已提交
2420 2421 2422

### putBatch<sup>8+</sup>

A
annie_wangli 已提交
2423
putBatch(entries: Entry[], callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2424

A
Annie_wang 已提交
2425
Inserts KV pairs to this KV store in batches. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2426

A
annie_wangli 已提交
2427
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2428

A
annie_wangli 已提交
2429 2430 2431
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2432
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2433
| entries  |[Entry](#entry)[] | Yes   |KV pairs to insert in batches. |
A
Annie_wang 已提交
2434
| callback |Asyncallback&lt;void&gt; |Yes    |Callback used to return the result.|
A
annie_wangli 已提交
2435

A
annie_wangli 已提交
2436
**Example**
A
annie_wangli 已提交
2437

A
Annie_wang 已提交
2438
```js
A
annie_wangli 已提交
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
2449 2450
            }
        }
A
annie_wangli 已提交
2451
        entries.push(entry);
A
annie_wangli 已提交
2452
    }
A
annie_wangli 已提交
2453 2454 2455
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
2456
        kvStore.getEntries('batch_test_string_key', function (err,entries) {
A
annie_wangli 已提交
2457
            console.log('getEntries success');
2458 2459
            console.log('entries.length: ' + entries.length);
            console.log('entries[0]: ' + JSON.stringify(entries[0]));
A
annie_wangli 已提交
2460 2461 2462
        });
    });
}catch(e) {
A
Annie_wang 已提交
2463
    console.log('PutBatch e ' + JSON.stringify(e));
A
annie_wangli 已提交
2464 2465
}
```
A
annie_wangli 已提交
2466 2467 2468 2469


### putBatch<sup>8+</sup>

A
annie_wangli 已提交
2470
putBatch(entries: Entry[]): Promise&lt;void&gt;
A
annie_wangli 已提交
2471

A
Annie_wang 已提交
2472
Inserts KV pairs to this KV store in batches. This API uses a promise to return the result.
A
annie_wangli 已提交
2473

A
annie_wangli 已提交
2474
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2475

A
annie_wangli 已提交
2476 2477 2478
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2479
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2480
| entries  |[Entry](#entry)[] | Yes   |KV pairs to insert in batches. |
A
annie_wangli 已提交
2481

A
annie_wangli 已提交
2482
**Return value**
A
annie_wangli 已提交
2483

A
annie_wangli 已提交
2484
| Type   | Description      |
A
annie_wangli 已提交
2485
| ------  | -------   |
A
Annie_wang 已提交
2486
| Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
2487

A
annie_wangli 已提交
2488
**Example**
A
annie_wangli 已提交
2489

A
Annie_wang 已提交
2490
```js
A
annie_wangli 已提交
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
2501 2502
            }
        }
A
annie_wangli 已提交
2503 2504 2505 2506 2507
        entries.push(entry);
    }
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
2508
        kvStore.getEntries('batch_test_string_key').then((entries) => {
A
annie_wangli 已提交
2509 2510
            console.log('getEntries success');
            console.log('PutBatch ' + JSON.stringify(entries));
A
annie_wangli 已提交
2511
        }).catch((err) => {
A
annie_wangli 已提交
2512
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
2513
        });
A
annie_wangli 已提交
2514 2515 2516 2517
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
A
Annie_wang 已提交
2518 2519 2520 2521 2522 2523 2524
    console.log('PutBatch e ' + JSON.stringify(e));
}
```

### putBatch<sup>9+</sup>

putBatch(value: Array&lt;ValuesBucket&gt;, callback: AsyncCallback&lt;void&gt;): void
G
ge-yafang 已提交
2525

A
Annie_wang 已提交
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
Writes values to this KV store. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------  | ----  | ----------------------- |
| value   |Array[&lt;ValuesBucket&gt;]()[] | Yes   |Values to write.  |
| callback |Asyncallback&lt;void&gt; |Yes    |Callback used to return the result.|

**Example**

```js
let kvStore;
try {
    let v8Arr = [];
    let arr = new Uint8Array([4,5,6,7]);
    let vb1 = {key : "name_1", value : 32}
    let vb2 = {key : "name_2", value : arr};
    let vb3 = {key : "name_3", value : "lisi"};

    v8Arr.push(vb1);
    v8Arr.push(vb2);
    v8Arr.push(vb3);
    kvStore.putBatch(v8Arr, async function (err,data) {
                console.log('putBatch success');
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('putBatch e ' + JSON.stringify(e));
A
annie_wangli 已提交
2558 2559
}
```
A
annie_wangli 已提交
2560

A
Annie_wang 已提交
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
### putBatch<sup>9+</sup>

putBatch(value: Array&lt;ValuesBucket&gt;): Promise&lt;void&gt;

Writes values of the **valuesbucket** type to this KV store. This API uses a promise to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------  | ----  | ----------------------- |
| value  |Array&lt;[ValuesBucket&gt;](#)[] | Yes   |Values to write.  |

**Return value**

| Type   | Description      |
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

**Example**

```js
let kvStore;
try {
    let v8Arr = [];
    let arr = new Uint8Array([4,5,6,7]);
    let vb1 = {key : "name_1", value : 32}
    let vb2 = {key : "name_2", value : arr};
    let vb3 = {key : "name_3", value : "lisi"};

    v8Arr.push(vb1);
    v8Arr.push(vb2);
    v8Arr.push(vb3);
    kvStore.putBatch(v8Arr).then(async (err) => {
        console.log('putBatch success');
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('PutBatch e ' + JSON.stringify(e));
}

```
A
annie_wangli 已提交
2605 2606 2607

### deleteBatch<sup>8+</sup>

A
annie_wangli 已提交
2608
deleteBatch(keys: string[], callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2609

A
Annie_wang 已提交
2610
Deletes KV pairs from this KV store in batches. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2611

A
annie_wangli 已提交
2612
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2613

A
annie_wangli 已提交
2614 2615 2616
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2617
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2618
| keys  |string[] | Yes   |KV pairs to delete in batches. |
A
Annie_wang 已提交
2619
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback used to return the result. |
A
annie_wangli 已提交
2620

A
annie_wangli 已提交
2621
**Example**
A
annie_wangli 已提交
2622

A
Annie_wang 已提交
2623
```js
A
annie_wangli 已提交
2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
let kvStore;
try {
    let entries = [];
    let keys = [];
    for (var i = 0; i < 5; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
2635 2636
            }
        }
A
annie_wangli 已提交
2637 2638
        entries.push(entry);
        keys.push(key + i);
A
annie_wangli 已提交
2639
    }
A
annie_wangli 已提交
2640 2641 2642
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
A
Annie_wang 已提交
2643
        kvStore.deleteBatch(keys, async function (err,data) {
A
annie_wangli 已提交
2644 2645 2646 2647 2648 2649 2650
            console.log('deleteBatch success');
        });
    });
}catch(e) {
    console.log('DeleteBatch e ' + e);
}
```
A
annie_wangli 已提交
2651 2652 2653 2654


### deleteBatch<sup>8+</sup> ###

A
annie_wangli 已提交
2655
deleteBatch(keys: string[]): Promise&lt;void&gt;
A
annie_wangli 已提交
2656

A
Annie_wang 已提交
2657
Deletes KV pairs from this KV store in batches. This API uses a promise to return the result.
A
annie_wangli 已提交
2658

A
annie_wangli 已提交
2659
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2660

A
annie_wangli 已提交
2661 2662 2663
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2664
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2665
| keys   |string[] | Yes   |KV pairs to delete in batches. |
A
annie_wangli 已提交
2666

A
annie_wangli 已提交
2667
**Return value**
A
annie_wangli 已提交
2668

A
annie_wangli 已提交
2669
| Type   | Description      |
A
annie_wangli 已提交
2670
| ------  | -------   |
A
Annie_wang 已提交
2671
| Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
2672

A
annie_wangli 已提交
2673
**Example**
A
annie_wangli 已提交
2674

A
Annie_wang 已提交
2675
```js
A
annie_wangli 已提交
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
let kvStore;
try {
    let entries = [];
    let keys = [];
    for (var i = 0; i < 5; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
2687 2688
            }
        }
A
annie_wangli 已提交
2689 2690 2691 2692 2693 2694
        entries.push(entry);
        keys.push(key + i);
    }
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
A
Annie_wang 已提交
2695
        kvStore.deleteBatch(keys).then((err) => {
A
annie_wangli 已提交
2696
            console.log('deleteBatch success');
A
annie_wangli 已提交
2697
        }).catch((err) => {
A
annie_wangli 已提交
2698
            console.log('deleteBatch fail ' + JSON.stringify(err));
A
annie_wangli 已提交
2699
        });
A
annie_wangli 已提交
2700 2701 2702 2703 2704 2705 2706
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('DeleteBatch e ' + e);
}
```
A
annie_wangli 已提交
2707 2708 2709 2710


### startTransaction<sup>8+</sup> ###

A
annie_wangli 已提交
2711
startTransaction(callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2712

A
Annie_wang 已提交
2713
Starts the transaction in this KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2714

A
annie_wangli 已提交
2715
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2716

A
annie_wangli 已提交
2717 2718 2719
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2720
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
2721
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback used to return the result. |
A
annie_wangli 已提交
2722

A
annie_wangli 已提交
2723
**Example**
A
annie_wangli 已提交
2724

A
Annie_wang 已提交
2725
```js
A
annie_wangli 已提交
2726 2727 2728 2729 2730 2731 2732 2733 2734
let kvStore;
function putBatchString(len, prefix) {
    let entries = [];
    for (var i = 0; i < len; i++) {
        var entry = {
            key : prefix + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
2735 2736
            }
        }
A
annie_wangli 已提交
2737
        entries.push(entry);
A
annie_wangli 已提交
2738
    }
A
annie_wangli 已提交
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
    return entries;
}
try {
    var count = 0;
    kvStore.on('dataChange', 0, function (data) {
        console.log('startTransaction 0' + data)
        count++;
    });
    kvStore.startTransaction(async function (err,data) {
        console.log('startTransaction success');
        let entries = putBatchString(10, 'batch_test_string_key');
        console.log('entries: ' + JSON.stringify(entries));
A
Annie_wang 已提交
2751
        kvStore.putBatch(entries, async function (err,data) {
A
annie_wangli 已提交
2752
            console.log('putBatch success');
A
annie_wangli 已提交
2753
        });
A
annie_wangli 已提交
2754 2755 2756 2757 2758
    });
}catch(e) {
    console.log('startTransaction e ' + e);
}
```
A
annie_wangli 已提交
2759 2760 2761 2762


### startTransaction<sup>8+</sup> ###

A
annie_wangli 已提交
2763
startTransaction(): Promise&lt;void&gt;
A
annie_wangli 已提交
2764

A
Annie_wang 已提交
2765
Starts the transaction in this KV store. This API uses a promise to return the result.
A
annie_wangli 已提交
2766

A
annie_wangli 已提交
2767
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2768

A
annie_wangli 已提交
2769 2770 2771
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
2772
| ------  | -------   |
A
Annie_wang 已提交
2773
| Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
2774

A
annie_wangli 已提交
2775
**Example**
A
annie_wangli 已提交
2776

A
Annie_wang 已提交
2777
```js
A
annie_wangli 已提交
2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
let kvStore;
try {
    var count = 0;
    kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_ALL, function (data) {
        console.log('startTransaction ' + JSON.stringify(data));
        count++;
    });
    kvStore.startTransaction().then(async (err) => {
        console.log('startTransaction success');
    }).catch((err) => {
        console.log('startTransaction fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('startTransaction e ' + e);
}
```
A
annie_wangli 已提交
2794 2795 2796 2797


### commit<sup>8+</sup> ###

A
annie_wangli 已提交
2798
commit(callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2799

A
Annie_wang 已提交
2800
Submits the transaction in this KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2801

A
annie_wangli 已提交
2802
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2803

A
annie_wangli 已提交
2804 2805 2806
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2807
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
2808
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback used to return the result. |
A
annie_wangli 已提交
2809

A
annie_wangli 已提交
2810
**Example**
A
annie_wangli 已提交
2811

A
Annie_wang 已提交
2812
```js
A
annie_wangli 已提交
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
let kvStore;
try {
    kvStore.commit(function (err,data) {
        if (err == undefined) {
            console.log('commit success');
        } else {
            console.log('commit fail');
        }
    });
}catch(e) {
    console.log('Commit e ' + e);
}
```
A
annie_wangli 已提交
2826 2827 2828 2829


### commit<sup>8+</sup> ###

A
annie_wangli 已提交
2830
commit(): Promise&lt;void&gt;
A
annie_wangli 已提交
2831

A
Annie_wang 已提交
2832
Submits the transaction in this KV store. This API uses a promise to return the result.
A
annie_wangli 已提交
2833

A
annie_wangli 已提交
2834
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2835

A
annie_wangli 已提交
2836 2837 2838
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
2839
| ------  | -------   |
A
Annie_wang 已提交
2840
| Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
2841

A
annie_wangli 已提交
2842
**Example**
A
annie_wangli 已提交
2843

A
Annie_wang 已提交
2844
```js
A
annie_wangli 已提交
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
let kvStore;
try {
    kvStore.commit().then(async (err) => {
        console.log('commit success');
    }).catch((err) => {
        console.log('commit fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('Commit e ' + e);
}
```
A
annie_wangli 已提交
2856 2857 2858 2859


### rollback<sup>8+</sup> ###

A
annie_wangli 已提交
2860
rollback(callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2861

A
Annie_wang 已提交
2862
Rolls back the transaction in this KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2863

A
annie_wangli 已提交
2864
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2865

A
annie_wangli 已提交
2866 2867 2868
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2869
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
2870
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback used to return the result. |
A
annie_wangli 已提交
2871

A
annie_wangli 已提交
2872
**Example**
A
annie_wangli 已提交
2873

A
Annie_wang 已提交
2874
```js
A
annie_wangli 已提交
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
let kvStore;
try {
    kvStore.rollback(function (err,data) {
        if (err == undefined) {
            console.log('commit success');
        } else {
            console.log('commit fail');
        }
    });
}catch(e) {
    console.log('Rollback e ' + e);
}
```
A
annie_wangli 已提交
2888 2889 2890 2891


### rollback<sup>8+</sup> ###

A
annie_wangli 已提交
2892
rollback(): Promise&lt;void&gt;
A
annie_wangli 已提交
2893

A
Annie_wang 已提交
2894
Rolls back the transaction in this KV store. This API uses a promise to return the result.
A
annie_wangli 已提交
2895

A
annie_wangli 已提交
2896
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2897

A
annie_wangli 已提交
2898 2899 2900
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
2901
| ------  | -------   |
A
Annie_wang 已提交
2902
| Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
2903

A
annie_wangli 已提交
2904
**Example**
A
annie_wangli 已提交
2905

A
Annie_wang 已提交
2906
```js
A
annie_wangli 已提交
2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917
let kvStore;
try {
    kvStore.rollback().then(async (err) => {
        console.log('rollback success');
    }).catch((err) => {
        console.log('rollback fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('Rollback e ' + e);
}
```
A
annie_wangli 已提交
2918 2919 2920 2921


### enableSync<sup>8+</sup> ###

A
annie_wangli 已提交
2922
enableSync(enabled: boolean, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2923

A
Annie_wang 已提交
2924
Sets data synchronization, which can be enabled or disabled. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2925

A
annie_wangli 已提交
2926
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2927

A
annie_wangli 已提交
2928 2929 2930
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2931
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2932
| enabled  |boolean | Yes   |Whether to enable data synchronization. The value **true** means to enable data synchronization, and **false** means the opposite. |
A
Annie_wang 已提交
2933
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback used to return the result. |
A
annie_wangli 已提交
2934

A
annie_wangli 已提交
2935
**Example**
A
annie_wangli 已提交
2936

A
Annie_wang 已提交
2937
```js
A
annie_wangli 已提交
2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
let kvStore;
try {
    kvStore.enableSync(true, function (err,data) {
        if (err == undefined) {
            console.log('enableSync success');
        } else {
            console.log('enableSync fail');
        }
    });
}catch(e) {
    console.log('EnableSync e ' + e);
}
```
A
annie_wangli 已提交
2951 2952 2953 2954


### enableSync<sup>8+</sup> ###

A
annie_wangli 已提交
2955
enableSync(enabled: boolean): Promise&lt;void&gt;
A
annie_wangli 已提交
2956

A
Annie_wang 已提交
2957
Sets data synchronization, which can be enabled or disabled. This API uses a promise to return the result.
A
annie_wangli 已提交
2958

A
annie_wangli 已提交
2959
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2960

A
annie_wangli 已提交
2961 2962 2963
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2964
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2965
| enabled  |boolean | Yes   |Whether to enable data synchronization. The value **true** means to enable data synchronization, and **false** means the opposite. |
A
annie_wangli 已提交
2966

A
annie_wangli 已提交
2967
**Return value**
A
annie_wangli 已提交
2968

A
annie_wangli 已提交
2969
| Type   | Description      |
A
annie_wangli 已提交
2970
| ------  | -------   |
A
Annie_wang 已提交
2971
| Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
2972

A
annie_wangli 已提交
2973
**Example**
A
annie_wangli 已提交
2974

A
Annie_wang 已提交
2975
```js
A
annie_wangli 已提交
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
let kvStore;
try {
    kvStore.enableSync(true).then((err) => {
        console.log('enableSync success');
    }).catch((err) => {
        console.log('enableSync fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('EnableSync e ' + e);
}
```
A
annie_wangli 已提交
2987 2988 2989 2990


### setSyncRange<sup>8+</sup> ###

A
annie_wangli 已提交
2991
setSyncRange(localLabels: string[], remoteSupportLabels: string[], callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2992

A
Annie_wang 已提交
2993
Sets the data synchronization range. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2994

A
annie_wangli 已提交
2995
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2996

A
annie_wangli 已提交
2997 2998 2999
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3000
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3001 3002
| localLabels  |string[] | Yes   |Synchronization labels set for the local device. |
| remoteSupportLabels  |string[] | Yes   |Synchronization labels set for remote devices. |
A
Annie_wang 已提交
3003
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback used to return the result. |
A
annie_wangli 已提交
3004

A
annie_wangli 已提交
3005
**Example**
A
annie_wangli 已提交
3006

A
Annie_wang 已提交
3007
```js
A
annie_wangli 已提交
3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018
let kvStore;
try {
    const localLabels = ['A', 'B'];
    const remoteSupportLabels = ['C', 'D'];
    kvStore.setSyncRange(localLabels, remoteSupportLabels, function (err,data) {
        console.log('SetSyncRange put success');
    });
}catch(e) {
    console.log('SetSyncRange e ' + e);
}
```
A
annie_wangli 已提交
3019 3020 3021 3022


### setSyncRange<sup>8+</sup> ###

A
annie_wangli 已提交
3023
setSyncRange(localLabels: string[], remoteSupportLabels: string[]): Promise&lt;void&gt;
A
annie_wangli 已提交
3024

A
Annie_wang 已提交
3025
Sets the data synchronization range. This API uses a promise to return the result.
A
annie_wangli 已提交
3026

A
annie_wangli 已提交
3027
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3028

A
annie_wangli 已提交
3029 3030 3031
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3032
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3033 3034
| localLabels  |string[] | Yes   |Synchronization labels set for the local device. |
| remoteSupportLabels  |string[] | Yes   |Synchronization labels set for remote devices. |
A
annie_wangli 已提交
3035 3036


A
annie_wangli 已提交
3037
**Return value**
A
annie_wangli 已提交
3038

A
annie_wangli 已提交
3039
| Type   | Description      |
A
annie_wangli 已提交
3040
| ------  | -------   |
A
Annie_wang 已提交
3041
| Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
3042

A
annie_wangli 已提交
3043
**Example**
A
annie_wangli 已提交
3044

A
Annie_wang 已提交
3045
```js
A
annie_wangli 已提交
3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058
let kvStore;
try {
    const localLabels = ['A', 'B'];
    const remoteSupportLabels = ['C', 'D'];
    kvStore.setSyncRange(localLabels, remoteSupportLabels).then((err) => {
        console.log('setSyncRange success');
    }).catch((err) => {
        console.log('delete fail ' + err);
    });
}catch(e) {
    console.log('SetSyncRange e ' + e);
}
```
A
annie_wangli 已提交
3059 3060 3061 3062


## SubscribeType

A
Annie_wang 已提交
3063
Enumerates the subscription types.
A
annie_wangli 已提交
3064

A
annie_wangli 已提交
3065 3066
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
Annie_wang 已提交
3067
| Name | Value  | Description                   |
A
annie_wangli 已提交
3068
| -----  | ------   | ----------------------- |
A
annie_wangli 已提交
3069 3070 3071
| SUBSCRIBE_TYPE_LOCAL  |0 |Local data changes. |
| SUBSCRIBE_TYPE_REMOTE |1 |Remote data changes. |
| SUBSCRIBE_TYPE_ALL  |2 |Local and remote data changes. |
A
annie_wangli 已提交
3072 3073 3074 3075 3076

## ChangeNotification

Defines the content of data change notifications, including inserted data, updated data, deleted data, and device ID.

A
annie_wangli 已提交
3077 3078 3079
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Type  |Readable  |Writable  | Description                   |
A
annie_wangli 已提交
3080
| ----- | -------   | -----| ------|------------------------ |
A
annie_wangli 已提交
3081 3082 3083 3084
| insertEntries | [Entry](#entry)[]   | Yes |  Yes|Data inserted.  |
| updateEntries | [Entry](#entry)[]   | Yes |  Yes|Data updated.  |
| deleteEntries | [Entry](#entry)[]   | Yes |  Yes|Data deleted.  |
| deviceId | string   | Yes |  Yes|UUID of the device. |
A
annie_wangli 已提交
3085 3086 3087

## Entry

A
annie_wangli 已提交
3088
Defines the KV pairs stored in the KV store.
A
annie_wangli 已提交
3089

A
annie_wangli 已提交
3090 3091 3092
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Type  |Readable  |Writable  | Description                   |
A
annie_wangli 已提交
3093
| ----- | -------   | -----| ------|------------------------ |
A
annie_wangli 已提交
3094 3095
| key | string   | Yes |  Yes|Key of the KV pair stored in the KV store.  |
| value | [Value](#value) | Yes |  Yes|Value of the KV pair stored in the KV store.  |
A
annie_wangli 已提交
3096 3097 3098 3099


## Value

A
Annie_wang 已提交
3100
Defines the **value** object in a KV store.
A
annie_wangli 已提交
3101

A
annie_wangli 已提交
3102 3103 3104
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Type  |Readable  |Writable  | Description                   |
A
annie_wangli 已提交
3105
| ----- | -------   | -----| ------|------------------------ |
A
annie_wangli 已提交
3106
| type | [ValueType](#value)   | Yes |  Yes|Type of the value.  |
A
Annie_wang 已提交
3107
| value | Uint8Array \| string \| number \| boolean| Yes |  Yes|Value.  |
A
annie_wangli 已提交
3108 3109 3110

## ValueType

A
Annie_wang 已提交
3111
Enumerates the data types.
A
annie_wangli 已提交
3112

A
annie_wangli 已提交
3113 3114
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
Annie_wang 已提交
3115
| Name | Value  | Description                   |
A
annie_wangli 已提交
3116
| -----  | ------   | ----------------------- |
A
annie_wangli 已提交
3117 3118 3119 3120 3121 3122
| STRING  |0 |String. |
| INTEGER |1 |Integer. |
| FLOAT   |2 |Float (single-precision floating point). |
| BYTE_ARRAY   |3 |Byte array. |
| BOOLEAN   |4 |Boolean. |
| DOUBLE   |5 |Double (double-precision floating point). |
A
annie_wangli 已提交
3123

A
Annie_wang 已提交
3124

A
annie_wangli 已提交
3125 3126
## SingleKVStore

A
Annie_wang 已提交
3127 3128 3129 3130 3131
Provides methods to query and synchronize data in a single KV store. This class inherits from [KVStore](#kvstore).

Data is not distinguished by device in a single KV store. The data written to different devices using the same key will be overwritten. For example, a single KV store can be used to synchronize a user's calendar and contact data between different devices.

Before calling any method in **SingleKVStore**, you must use [getKVStore](#getkvstore) to obtain a **SingleKVStore** instance.
A
annie_wangli 已提交
3132 3133 3134 3135 3136

### get

get(key: string, callback: AsyncCallback&lt;Uint8Array | string | boolean | number&gt;): void

A
Annie_wang 已提交
3137
Obtains the value of the specified key. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3138

A
annie_wangli 已提交
3139
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3140

A
annie_wangli 已提交
3141 3142 3143
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3144
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3145
| key    |string   | Yes   |Key of the value to obtain. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants). |
A
Annie_wang 已提交
3146
| callback  |AsyncCallback&lt;Uint8Array \| string \| boolean \| number&gt;) | Yes   |Callback invoked to return the value obtained. |
A
annie_wangli 已提交
3147

A
annie_wangli 已提交
3148
**Example**
A
annie_wangli 已提交
3149

A
Annie_wang 已提交
3150
```js
A
annie_wangli 已提交
3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string';
const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err,data) {
        if (err != undefined) {
            console.log("put err: " + JSON.stringify(err));
            return;
        }
        console.log("put success");
        kvStore.get(KEY_TEST_STRING_ELEMENT, function (err,data) {
            console.log("get success data: " + data);
A
annie_wangli 已提交
3163
        });
A
annie_wangli 已提交
3164 3165 3166 3167 3168
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
3169 3170 3171 3172


### get

A
annie_wangli 已提交
3173
get(key: string): Promise&lt;Uint8Array | string | boolean | number&gt;
A
annie_wangli 已提交
3174

A
Annie_wang 已提交
3175
Obtains the value of the specified key. This API uses a promise to return the result.
A
annie_wangli 已提交
3176

A
annie_wangli 已提交
3177
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3178

A
annie_wangli 已提交
3179 3180 3181
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3182
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3183
| key    |string   | Yes   |Key of the value to obtain. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants). |
A
annie_wangli 已提交
3184 3185


A
annie_wangli 已提交
3186
**Return value**
A
annie_wangli 已提交
3187

A
annie_wangli 已提交
3188
| Type   | Description      |
A
annie_wangli 已提交
3189
| ------  | -------   |
A
annie_wangli 已提交
3190
|Promise&lt;Uint8Array \| string \| boolean \| number&gt; |Promise used to return the value obtained.|
A
annie_wangli 已提交
3191

A
annie_wangli 已提交
3192
**Example**
A
annie_wangli 已提交
3193

A
Annie_wang 已提交
3194
```js
A
annie_wangli 已提交
3195 3196 3197 3198 3199 3200 3201 3202
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string';
const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then((data) => {
        console.log("put success: " + JSON.stringify(data));
        kvStore.get(KEY_TEST_STRING_ELEMENT).then((data) => {
            console.log("get success data: " + data);
A
annie_wangli 已提交
3203
        }).catch((err) => {
A
annie_wangli 已提交
3204
            console.log("get err: " + JSON.stringify(err));
A
annie_wangli 已提交
3205
        });
A
annie_wangli 已提交
3206 3207 3208 3209 3210 3211 3212
    }).catch((err) => {
        console.log("put err: " + JSON.stringify(err));
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
3213 3214 3215

### getEntries<sup>8+</sup> ###

A
annie_wangli 已提交
3216
getEntries(keyPrefix: string, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
3217

A
Annie_wang 已提交
3218
Obtains all KV pairs that match the specified key prefix. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3219

A
annie_wangli 已提交
3220
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3221

A
annie_wangli 已提交
3222 3223 3224
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3225
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3226
| keyPrefix    |string   | Yes   |Key prefix to match. |
A
Annie_wang 已提交
3227
| callback    |AsyncCallback&lt;[Entry](#entry)[]&gt;   | Yes   |Callback invoked to return the KV pairs obtained. |
A
annie_wangli 已提交
3228

A
annie_wangli 已提交
3229
**Example**
A
annie_wangli 已提交
3230

A
Annie_wang 已提交
3231
```js
A
annie_wangli 已提交
3232 3233 3234 3235 3236 3237 3238 3239 3240 3241
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_number_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.INTEGER,
                value : 222
A
annie_wangli 已提交
3242 3243
            }
        }
A
annie_wangli 已提交
3244
        entries.push(entry);
A
annie_wangli 已提交
3245
    }
A
annie_wangli 已提交
3246 3247
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
3248
        kvStore.getEntries('batch_test_number_key', function (err,entries) {
A
annie_wangli 已提交
3249
            console.log('getEntries success');
3250 3251
            console.log('entries.length: ' + entries.length);
            console.log('entries[0]: ' + JSON.stringify(entries[0]));
A
annie_wangli 已提交
3252 3253 3254 3255 3256 3257
        });
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
3258 3259 3260 3261


### getEntries<sup>8+</sup> ###

A
annie_wangli 已提交
3262
getEntries(keyPrefix: string): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
3263

A
Annie_wang 已提交
3264
Obtains all KV pairs that match the specified key prefix. This API uses a promise to return the result.
A
annie_wangli 已提交
3265

A
annie_wangli 已提交
3266
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3267

A
annie_wangli 已提交
3268 3269 3270
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3271
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3272
| keyPrefix    |string   | Yes   |Key prefix to match. |
A
annie_wangli 已提交
3273

A
annie_wangli 已提交
3274
**Return value**
A
annie_wangli 已提交
3275

A
annie_wangli 已提交
3276
| Type   | Description      |
A
annie_wangli 已提交
3277
| ------  | -------   |
A
annie_wangli 已提交
3278
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
3279

A
annie_wangli 已提交
3280
**Example**
A
annie_wangli 已提交
3281

A
Annie_wang 已提交
3282
```js
A
annie_wangli 已提交
3283 3284 3285 3286 3287 3288 3289 3290 3291 3292
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
3293 3294
            }
        }
A
annie_wangli 已提交
3295 3296 3297 3298 3299
        entries.push(entry);
    }
    console.log('entries: ' + entries);
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
3300
        kvStore.getEntries('batch_test_string_key').then((entries) => {
A
annie_wangli 已提交
3301
            console.log('getEntries success');
3302 3303 3304 3305
            console.log('entries.length: ' + entries.length);
            console.log('entries[0]: ' + JSON.stringify(entries[0]));
            console.log('entries[0].value: ' + JSON.stringify(entries[0].value));
            console.log('entries[0].value.value: ' + entries[0].value.value);
A
annie_wangli 已提交
3306
        }).catch((err) => {
A
annie_wangli 已提交
3307
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
3308
        });
A
annie_wangli 已提交
3309 3310 3311 3312 3313 3314 3315
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
3316 3317 3318 3319


### getEntries<sup>8+</sup> ###

A
annie_wangli 已提交
3320
getEntries(query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
3321

A
Annie_wang 已提交
3322
Obtains the KV pairs that match the specified **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3323

A
annie_wangli 已提交
3324
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3325

A
annie_wangli 已提交
3326 3327 3328
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3329
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
3330
| query  |[Query](#query8)   | Yes   |Key prefix to match. |
A
annie_wangli 已提交
3331
| callback  |AsyncCallback&lt;[Entry](#entry)[]&gt;   | Yes   |Callback used to return the KV pairs obtained. |
A
annie_wangli 已提交
3332

A
annie_wangli 已提交
3333
**Example**
A
annie_wangli 已提交
3334

A
Annie_wang 已提交
3335
```js
A
annie_wangli 已提交
3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346
let kvStore;
try {
    var arr = new Uint8Array([21,31]);
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_bool_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.BYTE_ARRAY,
                value : arr
A
annie_wangli 已提交
3347 3348
            }
        }
A
annie_wangli 已提交
3349
        entries.push(entry);
A
annie_wangli 已提交
3350
    }
A
annie_wangli 已提交
3351 3352 3353 3354 3355
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
3356
        kvStore.getEntries(query, function (err,entries) {
A
annie_wangli 已提交
3357
            console.log('getEntries success');
3358 3359
            console.log('entries.length: ' + entries.length);
            console.log('entries[0]: ' + JSON.stringify(entries[0]));
A
annie_wangli 已提交
3360 3361 3362 3363 3364 3365 3366
        });
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
3367 3368 3369 3370


### getEntries<sup>8+</sup> ###

A
annie_wangli 已提交
3371
getEntries(query: Query): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
3372

A
Annie_wang 已提交
3373
Obtains the KV pairs that match the specified **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
3374

A
annie_wangli 已提交
3375
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3376

A
annie_wangli 已提交
3377 3378 3379
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3380
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3381
| query  |[Query](#query8)   | Yes   |**Query** object to match. |
A
annie_wangli 已提交
3382

A
annie_wangli 已提交
3383
**Return value**
A
annie_wangli 已提交
3384

A
annie_wangli 已提交
3385
| Type   | Description      |
A
annie_wangli 已提交
3386
| ------  | -------   |
A
annie_wangli 已提交
3387
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
3388

A
annie_wangli 已提交
3389
**Example**
A
annie_wangli 已提交
3390

A
Annie_wang 已提交
3391
```js
A
Annie_wang 已提交
3392
let kvStore;
A
annie_wangli 已提交
3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
try {
    var arr = new Uint8Array([21,31]);
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_bool_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.BYTE_ARRAY,
                value : arr
A
annie_wangli 已提交
3403 3404
            }
        }
A
annie_wangli 已提交
3405 3406 3407 3408 3409 3410 3411
        entries.push(entry);
    }
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
3412
        kvStore.getEntries(query).then((entries) => {
A
annie_wangli 已提交
3413
            console.log('getEntries success');
A
annie_wangli 已提交
3414
        }).catch((err) => {
A
annie_wangli 已提交
3415
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
3416
        });
A
annie_wangli 已提交
3417 3418 3419 3420 3421 3422 3423 3424
    }).catch((err) => {
        console.log('GetEntries putBatch fail ' + JSON.stringify(err))
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
3425 3426


A
Annie_wang 已提交
3427
### getResultSet<sup>8+</sup><a name="singlekvstore_getresultset"></a> ###
A
annie_wangli 已提交
3428

A
annie_wangli 已提交
3429
getResultSet(keyPrefix: string, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
3430

A
Annie_wang 已提交
3431
Obtains the result set with the specified prefix. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3432

A
annie_wangli 已提交
3433
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3434

A
annie_wangli 已提交
3435 3436 3437
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3438
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3439
| keyPrefix  |string   | Yes   |Key prefix to match.|
A
Annie_wang 已提交
3440
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt;   | Yes   |Callback invoked to return the result set obtained.|
A
annie_wangli 已提交
3441

A
annie_wangli 已提交
3442
**Example**
A
annie_wangli 已提交
3443

A
Annie_wang 已提交
3444
```js
A
annie_wangli 已提交
3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455
let kvStore;
try {
    let resultSet;
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
3456 3457
            }
        }
A
annie_wangli 已提交
3458
        entries.push(entry);
A
annie_wangli 已提交
3459
    }
A
annie_wangli 已提交
3460 3461
    kvStore.putBatch(entries, async function (err, data) {
        console.log('GetResultSet putBatch success');
A
Annie_wang 已提交
3462
        kvStore.getResultSet('batch_test_string_key', async function (err, result) {
A
Annie_wang 已提交
3463
            console.log('GetResultSet getResultSet succeed.');
A
annie_wangli 已提交
3464 3465 3466 3467 3468 3469 3470 3471 3472 3473
            resultSet = result;
            kvStore.closeResultSet(resultSet, function (err, data) {
                console.log('GetResultSet closeResultSet success');
            })
        });
    });
}catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
3474 3475 3476 3477


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3478
getResultSet(keyPrefix: string): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
3479

A
Annie_wang 已提交
3480
Obtains the result set with the specified prefix. This API uses a promise to return the result.
A
annie_wangli 已提交
3481

A
annie_wangli 已提交
3482
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3483

A
annie_wangli 已提交
3484 3485 3486
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3487
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3488
| keyPrefix  |string   | Yes   |Key prefix to match.|
A
annie_wangli 已提交
3489

A
annie_wangli 已提交
3490
**Return value**
A
annie_wangli 已提交
3491

A
annie_wangli 已提交
3492
| Type   | Description      |
A
annie_wangli 已提交
3493
| ------  | -------   |
Z
zengyawen 已提交
3494
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; |Promise used to return the result set obtained.|
A
annie_wangli 已提交
3495

A
annie_wangli 已提交
3496
**Example**
A
annie_wangli 已提交
3497

A
Annie_wang 已提交
3498
```js
A
annie_wangli 已提交
3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509
let kvStore;
try {
    let resultSet;
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
3510 3511
            }
        }
A
annie_wangli 已提交
3512
        entries.push(entry);
A
annie_wangli 已提交
3513
    }
A
annie_wangli 已提交
3514 3515 3516 3517 3518 3519
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
    }).catch((err) => {
        console.log('PutBatch putBatch fail ' + JSON.stringify(err));
    });
    kvStore.getResultSet('batch_test_string_key').then((result) => {
A
Annie_wang 已提交
3520
        console.log('GetResult getResultSet succeed.');
A
annie_wangli 已提交
3521 3522
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
3523
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
    });
    kvStore.closeResultSet(resultSet).then((err) => {
        console.log('GetResult closeResultSet success');
    }).catch((err) => {
        console.log('closeResultSet fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('GetResult e ' + e);
}
```
A
annie_wangli 已提交
3534 3535 3536 3537


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3538
getResultSet(query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
3539

A
Annie_wang 已提交
3540
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3541

A
annie_wangli 已提交
3542
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3543

A
annie_wangli 已提交
3544 3545 3546
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3547
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3548
| query  |Query    | Yes   |**Query** object to match.            |
A
Annie_wang 已提交
3549
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt;   | Yes   |Callback invoked to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
3550

A
annie_wangli 已提交
3551
**Example**
A
annie_wangli 已提交
3552

A
Annie_wang 已提交
3553
```js
A
annie_wangli 已提交
3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564
let kvStore;
try {
    let resultSet;
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
3565 3566
            }
        }
A
annie_wangli 已提交
3567
        entries.push(entry);
A
annie_wangli 已提交
3568
    }
A
annie_wangli 已提交
3569 3570 3571 3572
    kvStore.putBatch(entries, async function (err, data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
A
Annie_wang 已提交
3573
        kvStore.getResultSet(query, async function (err, result) {
A
Annie_wang 已提交
3574
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
3575 3576 3577 3578 3579 3580 3581
            resultSet = result;
        });
    });
} catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
3582 3583 3584 3585


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3586
getResultSet(query: Query): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
3587

A
Annie_wang 已提交
3588
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
3589

A
annie_wangli 已提交
3590
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3591

A
annie_wangli 已提交
3592 3593 3594
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3595
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3596
| query  |[Query](#query8)    | Yes   |**Query** object to match.            |
A
annie_wangli 已提交
3597

A
annie_wangli 已提交
3598
**Return value**
A
annie_wangli 已提交
3599

A
annie_wangli 已提交
3600
| Type   | Description      |
A
annie_wangli 已提交
3601
| ------  | -------   |
Z
zengyawen 已提交
3602
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
3603

A
annie_wangli 已提交
3604
**Example**
A
annie_wangli 已提交
3605

A
Annie_wang 已提交
3606
```js
A
annie_wangli 已提交
3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617
let kvStore;
try {
    let resultSet;
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
3618 3619
            }
        }
A
annie_wangli 已提交
3620
        entries.push(entry);
A
annie_wangli 已提交
3621
    }
A
annie_wangli 已提交
3622 3623 3624 3625 3626 3627 3628 3629
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
    const query = new distributedData.Query();
    query.prefixKey("batch_test");
    kvStore.getResultSet(query).then((result) => {
A
Annie_wang 已提交
3630
        console.log(' getResultSet succeed.');
A
annie_wangli 已提交
3631 3632
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
3633
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
3634 3635 3636 3637 3638
    });
}catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
3639

A
Annie_wang 已提交
3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714
### getResultSet<sup>9+</sup> ###

getResultSet(predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void

Obtains a **KvStoreResultSet** object that matches the specified **Predicates** object. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------   | ----  | ----------------------- |
| predicates  | Predicates    | Yes   |**Predicates** object to match. If this parameter is **null**, define the processing logic.            |
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultsetsup8sup)&gt;   | Yes   |Callback invoked to return the **KvStoreResultSet** object obtained.|

**Example**

```js
import dataSharePredicates from './@ohos.data.dataSharePredicates';
let kvStore;
try {
    let resultSet;
    let predicates = new dataSharePredicates.DataSharePredicates();
    predicates.prefixKey("batch_test_string_key");
    kvStore.getResultSet(predicates, async function (err, result) {
    console.log(' GetResultSet success');
    resultSet = result;
    kvStore.closeResultSet(resultSet, function (err, data) {
        console.log(' closeResultSet success');
        })
    });
}catch(e) {
    console.log('An unexpected error occurred. Error:' + e);
}
```
### getResultSet<sup>9+</sup> ###

getResultSet(predicates: dataSharePredicates.DataSharePredicates): Promise&lt;KvStoreResultSet&gt;

Obtains a **KvStoreResultSet** object that matches the specified **Predicates** object. This API uses a promise to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------   | ----  | ----------------------- |
| predicates  |[Predicates](#)  | Yes   |**Predicates** object to match. If this parameter is **null**, define the processing logic.           |

**Return value**

| Type   | Description      |
| ------  | -------   |
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; |Promise used to return the **KvStoreResultSet** object obtained.|

**Example**

```js
import dataSharePredicates from './@ohos.data.dataSharePredicates';
let kvStore;
try {
	let resultSet;
    let predicates =  new dataSharePredicates.DataSharePredicates();
    predicates.prefixKey("batch_test_string_key");
    kvStore.getResultSet(predicates) .then((result) => {
        console.log(' GetResultSet success');
        resultSet = result;
        kvStore.closeResultSet(resultSet, fun ction (err, data) {
            console.log(' closeResultSet success');
        })
    });
}catch(e) {
	console.log('An unexpected error occurred. Error:' + e);
}
```
A
annie_wangli 已提交
3715 3716
### closeResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3717
closeResultSet(resultSet: KvStoreResultSet, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
3718

A
Annie_wang 已提交
3719
Closes the **KvStoreResultSet** object returned by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3720

A
annie_wangli 已提交
3721
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3722

A
annie_wangli 已提交
3723 3724 3725
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3726
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3727
| resultSet  |[KvStoreResultSet](#kvstoreresultset8)   | Yes   |**KvStoreResultSet** object to close.            |
A
Annie_wang 已提交
3728
| callback  |AsyncCallback&lt;void&gt;   | Yes   |Callback used to return the result.            |
A
annie_wangli 已提交
3729

A
annie_wangli 已提交
3730
**Example**
A
annie_wangli 已提交
3731

A
Annie_wang 已提交
3732
```js
A
annie_wangli 已提交
3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746
let kvStore;
try {
    let resultSet = null;
    kvStore.closeResultSet(resultSet, function (err, data) {
        if (err == undefined) {
            console.log('closeResultSet success');
        } else {
            console.log('closeResultSet fail');
        }
    });
}catch(e) {
    console.log('CloseResultSet e ' + e);
}
```
A
annie_wangli 已提交
3747 3748 3749 3750


### closeResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3751
closeResultSet(resultSet: KvStoreResultSet): Promise&lt;void&gt;
A
annie_wangli 已提交
3752

A
Annie_wang 已提交
3753
Closes the **KvStoreResultSet** object returned by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses a promise to return the result.
A
annie_wangli 已提交
3754

A
annie_wangli 已提交
3755
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3756

A
annie_wangli 已提交
3757 3758 3759
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3760
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3761
| resultSet  |[KvStoreResultSet](#kvstoreresultset8)   | Yes   |**KvStoreResultSet** object to close.            |
A
annie_wangli 已提交
3762

A
annie_wangli 已提交
3763
**Return value**
A
annie_wangli 已提交
3764

A
annie_wangli 已提交
3765
| Type   | Description      |
A
annie_wangli 已提交
3766
| ------  | -------   |
A
Annie_wang 已提交
3767
|Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
3768

A
annie_wangli 已提交
3769
**Example**
A
annie_wangli 已提交
3770

A
Annie_wang 已提交
3771
```js
A
annie_wangli 已提交
3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783
let kvStore;
try {
    let resultSet = null;
    kvStore.closeResultSet(resultSet).then(() => {
        console.log('closeResultSet success');
    }).catch((err) => {
        console.log('closeResultSet fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('CloseResultSet e ' + e);
}
```
A
annie_wangli 已提交
3784 3785 3786 3787


### getResultSize<sup>8+</sup> ###

A
annie_wangli 已提交
3788 3789
getResultSize(query: Query, callback: AsyncCallback&lt;number&gt;): void

A
Annie_wang 已提交
3790
Obtains the number of results that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3791

A
annie_wangli 已提交
3792
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3793

A
annie_wangli 已提交
3794
**Parameters**
A
annie_wangli 已提交
3795

A
annie_wangli 已提交
3796
| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3797
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3798
| query  |[Query](#query8)   | Yes   |**Query** object to match.        |
A
Annie_wang 已提交
3799
| callback  |AsyncCallback&lt;number&gt;   | Yes   |Callback invoked to return the number of results obtained.        |
A
annie_wangli 已提交
3800

A
annie_wangli 已提交
3801
**Example**
A
annie_wangli 已提交
3802

A
Annie_wang 已提交
3803
```js
A
annie_wangli 已提交
3804 3805 3806 3807 3808 3809 3810 3811 3812 3813
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
3814 3815
            }
        }
A
annie_wangli 已提交
3816
        entries.push(entry);
A
annie_wangli 已提交
3817
    }
A
annie_wangli 已提交
3818 3819 3820 3821
    kvStore.putBatch(entries, async function (err, data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
A
Annie_wang 已提交
3822
        kvStore.getResultSize(query, async function (err, resultSize) {
A
Annie_wang 已提交
3823
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
3824 3825 3826 3827 3828 3829
        });
    });
} catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
3830 3831 3832 3833


### getResultSize<sup>8+</sup> ###

A
annie_wangli 已提交
3834 3835
getResultSize(query: Query): Promise&lt;number&gt;

A
Annie_wang 已提交
3836
Obtains the number of results that matches the specified **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
3837

A
annie_wangli 已提交
3838
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3839

A
annie_wangli 已提交
3840
**Parameters**
A
annie_wangli 已提交
3841

A
annie_wangli 已提交
3842
| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3843
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3844
| query  |[Query](#query8)   | Yes   |**Query** object to match.        |
A
annie_wangli 已提交
3845

A
annie_wangli 已提交
3846
**Return value**
A
annie_wangli 已提交
3847

A
annie_wangli 已提交
3848
| Type   | Description      |
A
annie_wangli 已提交
3849
| ------  | -------   |
A
annie_wangli 已提交
3850
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
3851

A
annie_wangli 已提交
3852
**Example**
A
annie_wangli 已提交
3853

A
Annie_wang 已提交
3854
```js
A
annie_wangli 已提交
3855 3856 3857 3858 3859 3860 3861 3862 3863 3864
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
3865 3866
            }
        }
A
annie_wangli 已提交
3867
        entries.push(entry);
A
annie_wangli 已提交
3868
    }
A
annie_wangli 已提交
3869 3870 3871 3872 3873 3874 3875 3876
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
    const query = new distributedData.Query();
    query.prefixKey("batch_test");
    kvStore.getResultSize(query).then((resultSize) => {
A
Annie_wang 已提交
3877
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
3878
    }).catch((err) => {
A
Annie_wang 已提交
3879
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
3880 3881 3882 3883 3884
    });
}catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
3885 3886 3887 3888


### removeDeviceData<sup>8+</sup> ###

A
annie_wangli 已提交
3889
removeDeviceData(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
3890

A
Annie_wang 已提交
3891
Deletes data of a device. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3892

A
annie_wangli 已提交
3893
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3894

A
annie_wangli 已提交
3895 3896 3897
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3898
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3899 3900
| deviceId  |string   | Yes   |ID of the target device.      |
| callback  |AsyncCallback&lt;void&gt;   | Yes   |Callback used to return the result.     |
A
annie_wangli 已提交
3901

A
annie_wangli 已提交
3902
**Example**
A
annie_wangli 已提交
3903

A
Annie_wang 已提交
3904
```js
A
annie_wangli 已提交
3905 3906 3907 3908 3909 3910 3911
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string_2';
const VALUE_TEST_STRING_ELEMENT = 'value-string-002';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, async function (err,data) {
        console.log('put success');
        const deviceid = 'no_exist_device_id';
A
Annie_wang 已提交
3912
        kvStore.removeDeviceData(deviceid, async function (err,data) {
A
annie_wangli 已提交
3913 3914 3915 3916
            if (err == undefined) {
                console.log('removeDeviceData success');
            } else {
                console.log('removeDeviceData fail');
A
Annie_wang 已提交
3917
                kvStore.get(KEY_TEST_STRING_ELEMENT, async function (err,data) {
A
annie_wangli 已提交
3918 3919 3920
                    console.log('RemoveDeviceData get success');
                });
            }
A
annie_wangli 已提交
3921
        });
A
annie_wangli 已提交
3922 3923 3924 3925 3926
    });
}catch(e) {
    console.log('RemoveDeviceData e ' + e);
}
```
A
annie_wangli 已提交
3927 3928 3929 3930


### removeDeviceData<sup>8+</sup> ###

A
annie_wangli 已提交
3931
removeDeviceData(deviceId: string): Promise&lt;void&gt;
A
annie_wangli 已提交
3932

A
Annie_wang 已提交
3933
Deletes data of a device. This API uses a promise to return the result.
A
annie_wangli 已提交
3934

A
annie_wangli 已提交
3935
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3936

A
annie_wangli 已提交
3937 3938 3939
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3940
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3941
| deviceId  |string   | Yes   |ID of the target device.      |
A
annie_wangli 已提交
3942

A
annie_wangli 已提交
3943
**Return value**
A
annie_wangli 已提交
3944

A
annie_wangli 已提交
3945
| Type   | Description      |
A
annie_wangli 已提交
3946
| ------  | -------   |
A
Annie_wang 已提交
3947
|Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
3948

A
annie_wangli 已提交
3949
**Example**
A
annie_wangli 已提交
3950

A
Annie_wang 已提交
3951
```js
A
annie_wangli 已提交
3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string_2';
const VALUE_TEST_STRING_ELEMENT = 'value-string-001';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then((err) => {
        console.log('removeDeviceData put success');
    }).catch((err) => {
        console.log('put fail ' + JSON.stringify(err));
    });
    const deviceid = 'no_exist_device_id';
    kvStore.removeDeviceData(deviceid).then((err) => {
        console.log('removeDeviceData success');
    }).catch((err) => {
        console.log('removeDeviceData fail ' + JSON.stringify(err));
    });
    kvStore.get(KEY_TEST_STRING_ELEMENT).then((data) => {
        console.log('get success data:' + data);
    }).catch((err) => {
        console.log('RemoveDeviceData get fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('RemoveDeviceData e ' + e);
}
```
A
annie_wangli 已提交
3976 3977


A
Annie_wang 已提交
3978
### on('syncComplete')<sup>8+</sup> ###
A
annie_wangli 已提交
3979

A
annie_wangli 已提交
3980
on(event: 'syncComplete', syncCallback: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
3981

A
Annie_wang 已提交
3982
Subscribes to the data synchronization complete events.
A
annie_wangli 已提交
3983

A
annie_wangli 已提交
3984
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3985

A
annie_wangli 已提交
3986 3987 3988
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3989
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
3990 3991
| event  |string   | Yes   |Event to subscribe to. The value is **syncComplete**, which indicates completion of a data synchronization.   |
| syncCallback  |Callback&lt;Array&lt;[string, number]&gt;&gt;   | Yes   |Callback invoked to return the synchronization result.   |
A
annie_wangli 已提交
3992

A
annie_wangli 已提交
3993
**Example**
A
annie_wangli 已提交
3994

A
Annie_wang 已提交
3995
```js
A
annie_wangli 已提交
3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011
let kvStore;
const KEY_TEST_FLOAT_ELEMENT = 'key_test_float';
const VALUE_TEST_FLOAT_ELEMENT = 321.12;
try {
    kvStore.on('syncComplete', function (data) {
        console.log('syncComplete ' + data)
    });
    kvStore.put(KEY_TEST_FLOAT_ELEMENT, VALUE_TEST_FLOAT_ELEMENT).then((data) => {
        console.log('syncComplete put success');
    }).catch((error) => {
        console.log('syncComplete put fail ' + error);
    });
}catch(e) {
    console.log('syncComplete put e ' + e);
}
```
A
annie_wangli 已提交
4012 4013


A
Annie_wang 已提交
4014
### off('syncComplete')<sup>8+</sup> ###
A
annie_wangli 已提交
4015

A
annie_wangli 已提交
4016
off(event: 'syncComplete', syncCallback?: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
4017

A
Annie_wang 已提交
4018
Unsubscribes from the data synchronization complete events.
A
annie_wangli 已提交
4019

A
annie_wangli 已提交
4020
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4021

A
annie_wangli 已提交
4022 4023 4024
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4025
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
4026
| event  |string   | Yes   |Event to unsubscribe from. The value is **syncComplete**, which indicates completion of a data synchronization.   |
A
annie_wangli 已提交
4027
| syncCallback  |Callback&lt;Array&lt;[string, number]&gt;&gt;   | No   |Callback used to return the synchronization result.   |
A
annie_wangli 已提交
4028

A
annie_wangli 已提交
4029
**Example**
A
annie_wangli 已提交
4030

A
Annie_wang 已提交
4031
```js
A
annie_wangli 已提交
4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042
let kvStore;
try {
    const func = function (data) {
        console.log('syncComplete ' + data)
    };
    kvStore.on('syncComplete', func);
    kvStore.off('syncComplete', func);
}catch(e) {
    console.log('syncComplete e ' + e);
}
```
A
annie_wangli 已提交
4043

A
Annie_wang 已提交
4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096
### on('dataChange')<sup>9+</sup> ###

on(event: 'dataChange', type: SubscribeType, listener: Callback&lt;ChangeNotification&gt;): void

Subscribes to data changes of the specified type. This API uses a synchronous callback to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------  | ----  | ----------------------- |
| event  |string  | Yes   |Event to subscribe to. The value is **dataChange**, which indicates data changes.      |
| type  |[SubscribeType](#subscribetype) | Yes   |Subscription type.    |
| listener |Callback&lt;[ChangeNotification](#changenotification)&gt; | Yes   |Callback used to return the result.|

**Example**

```js
let kvStore;
kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL, function (data) {
    console.log("dataChange callback call data: " + JSON.stringify(data));
});

```

### off('dataChange')<sup>9+</sup> ###

off(event:'dataChange', listener?: Callback&lt;ChangeNotification&gt;): void

Unsubscribes from the data change events. This API uses a synchronous callback to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------  | ----  | ----------------------- |
| event  |string  | Yes   |Event to unsubscribe from. The value is **dataChange**, which indicates data changes.      |
| listener |Callback&lt;[ChangeNotification](#changenotification)&gt; |No   |Callback used to return the result.|

**Example**

```js
let kvStore;
kvStore.on('dataChange', function (data) {
    console.log("callback call data: " + data);
});
kvStore.off('dataChange', function (data) {
    console.log("callback call data: " + data);
});
```
### sync<sup>7+</sup>
A
annie_wangli 已提交
4097 4098 4099 4100


sync(deviceIdList: string[], mode: SyncMode, allowedDelayMs?: number): void

A
Annie_wang 已提交
4101
Synchronizes the KV store manually. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
A
Annie_wang 已提交
4102

A
annie_wangli 已提交
4103
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
A
annie_wangli 已提交
4104

A
annie_wangli 已提交
4105
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4106

A
annie_wangli 已提交
4107 4108 4109
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4110
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
4111 4112
| deviceIdList  |string[]  | Yes   |List of IDs of the devices in the same networking environment to be synchronized.   |
| mode  |[SyncMode](#syncmode)   | Yes  |Synchronization mode.   |
A
annie_wangli 已提交
4113
| allowedDelayMs  |number   | No  |Allowed synchronization delay time, in ms.  |
A
annie_wangli 已提交
4114

A
annie_wangli 已提交
4115
**Example**
A
annie_wangli 已提交
4116

A
Annie_wang 已提交
4117
```js
A
annie_wangli 已提交
4118 4119 4120
let kvStore;
kvStore.sync('deviceIds', distributedData.SyncMode.PULL_ONLY, 1000);
```
A
annie_wangli 已提交
4121

A
Annie_wang 已提交
4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163
### sync<sup>9+</sup>
sync(deviceIds: string[], query: Query, mode: SyncMode, delayMs?: number): void

Synchronizes the KV store manually. This API uses a synchronous mode. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).

**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------   | ----  | ----------------------- |
| deviceIds  |string[]  | Yes   |List of IDs of the devices in the same networking environment to be synchronized.   |
| mode            |[SyncMode](#syncmode)  | Yes   |Synchronization mode. |
| query  |[Query](#querysup8sup)   | Yes  |**Query** object to match. |
| delayMs  |number   | No  |Allowed synchronization delay time, in ms.  |

**Example**

```js
let kvstore;
const KEY_TEST_SYNC_ELEMENT = 'key_test_sync';
const VALUE_TEST_SYNC_ELEMENT = 'value-string-001';
try {
    kvStore.on('syncComplete', function (data) {
        console.log('Sync dataChange');
    });
    kvStore.put(KEY_TEST_SYNC_ELEMENT + 'testSync101', VALUE_TEST_SYNC_ELEMENT, function (err,data) {
        console.log('Sync put success');
        const devices = ['deviceList'];
        const mode = distributedData.SyncMode.PULL_ONLY;
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
        query.deviceId('localDeviceId');
        kvStore.sync(devices, query, PULL_ONLY , 1000);
    });
}catch(e) {
    console.log('Sync e' + e);
}
```

A
annie_wangli 已提交
4164 4165
### setSyncParam<sup>8+</sup> ###

A
annie_wangli 已提交
4166
setSyncParam(defaultAllowedDelayMs: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
4167

A
Annie_wang 已提交
4168
Sets the default delay allowed for KV store synchronization. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4169

A
annie_wangli 已提交
4170
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4171

A
annie_wangli 已提交
4172 4173 4174
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4175
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4176 4177
| defaultAllowedDelayMs  |number  | Yes   |Default delay allowed for database synchronization, in ms.   |
| callback  |AsyncCallback&lt;void&gt;  | Yes  |Callback used to return the result.  |
A
annie_wangli 已提交
4178

A
annie_wangli 已提交
4179
**Example**
A
annie_wangli 已提交
4180

A
Annie_wang 已提交
4181
```js
A
annie_wangli 已提交
4182 4183 4184 4185 4186 4187 4188 4189 4190 4191
let kvStore;
try {
    const defaultAllowedDelayMs = 500;
    kvStore.setSyncParam(defaultAllowedDelayMs, function (err,data) {
        console.log('SetSyncParam put success');
    });
}catch(e) {
    console.log('testSingleKvStoreSetSyncParam e ' + e);
}
```
A
annie_wangli 已提交
4192 4193 4194 4195


### setSyncParam<sup>8+</sup> ###

A
annie_wangli 已提交
4196
setSyncParam(defaultAllowedDelayMs: number): Promise&lt;void&gt;
A
annie_wangli 已提交
4197

A
Annie_wang 已提交
4198
Sets the default delay allowed for KV store synchronization. This API uses a promise to return the result.
A
annie_wangli 已提交
4199

A
annie_wangli 已提交
4200
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4201

A
annie_wangli 已提交
4202 4203 4204
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4205
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4206
| defaultAllowedDelayMs  |number  | Yes   |Default delay allowed for database synchronization, in ms.   |
A
annie_wangli 已提交
4207 4208


A
annie_wangli 已提交
4209
**Return value**
A
annie_wangli 已提交
4210

A
annie_wangli 已提交
4211
| Type   | Description      |
A
annie_wangli 已提交
4212
| ------  | -------   |
A
Annie_wang 已提交
4213
|Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
4214

A
annie_wangli 已提交
4215
**Example**
A
annie_wangli 已提交
4216

A
Annie_wang 已提交
4217
```js
A
annie_wangli 已提交
4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229
let kvStore;
try {
    const defaultAllowedDelayMs = 500;
    kvStore.setSyncParam(defaultAllowedDelayMs).then((err) => {
        console.log('SetSyncParam put success');
    }).catch((err) => {
        console.log('SetSyncParam put fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('SetSyncParam e ' + e);
}
```
A
annie_wangli 已提交
4230 4231 4232 4233


### getSecurityLevel<sup>8+</sup> ###

A
annie_wangli 已提交
4234
getSecurityLevel(callback: AsyncCallback&lt;SecurityLevel&gt;): void
A
annie_wangli 已提交
4235

A
Annie_wang 已提交
4236
Obtains the security level of this KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4237

A
annie_wangli 已提交
4238
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4239

A
annie_wangli 已提交
4240 4241 4242
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4243
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
4244
| callback  |AsyncCallback&lt;[SecurityLevel](#securitylevel)&gt;  | Yes   |Callback invoked to return the security level obtained.   |
A
annie_wangli 已提交
4245

A
annie_wangli 已提交
4246
**Example**
A
annie_wangli 已提交
4247

A
Annie_wang 已提交
4248
```js
A
annie_wangli 已提交
4249 4250 4251 4252 4253 4254 4255 4256 4257
let kvStore;
try {
    kvStore.getSecurityLevel(function (err,data) {
        console.log('getSecurityLevel success');
    });
}catch(e) {
    console.log('GetSecurityLeve e ' + e);
}
```
A
annie_wangli 已提交
4258 4259 4260 4261


### getSecurityLevel<sup>8+</sup> ###

A
annie_wangli 已提交
4262
getSecurityLevel(): Promise&lt;SecurityLevel&gt;
A
annie_wangli 已提交
4263

A
Annie_wang 已提交
4264
Obtains the security level of this KV store. This API uses a promise to return the result.
A
annie_wangli 已提交
4265

A
annie_wangli 已提交
4266
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4267

A
annie_wangli 已提交
4268 4269 4270
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
4271
| ------  | -------   |
A
Annie_wang 已提交
4272
|Promise&lt;[SecurityLevel](#securitylevel)&gt; |Promise used to return the security level obtained.|
A
annie_wangli 已提交
4273

A
annie_wangli 已提交
4274
**Example**
A
annie_wangli 已提交
4275

A
Annie_wang 已提交
4276
```js
A
annie_wangli 已提交
4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287
let kvStore;
try {
    kvStore.getSecurityLevel().then((data) => {
        console.log(' getSecurityLevel success');
    }).catch((err) => {
        console.log('getSecurityLevel fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('GetSecurityLeve e ' + e);
}
```
A
annie_wangli 已提交
4288 4289 4290 4291


## DeviceKVStore<sup>8+</sup> ##

A
Annie_wang 已提交
4292 4293 4294 4295 4296 4297 4298
Provides methods to query and synchronize data in a device KV store. This class inherits from [KVStore](#kvstore).

Data 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.

For 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.

Before calling any method in **DeviceKVStore**, you must use [getKVStore](#getkvstore) to obtain a **DeviceKVStore** object.
A
annie_wangli 已提交
4299 4300 4301

### get<sup>8+</sup> ###

A
annie_wangli 已提交
4302
get(deviceId: string, key: string, callback: AsyncCallback&lt;boolean|string|number|Uint8Array&gt;): void
A
annie_wangli 已提交
4303

A
Annie_wang 已提交
4304
Obtains a string value that matches the specified device ID and key. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4305

A
Annie_wang 已提交
4306
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4307

A
annie_wangli 已提交
4308 4309 4310
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4311
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4312 4313
| deviceId  |string  | Yes   |ID of the target device.   |
| key       |string  | Yes   |Key to match.   |
A
annie_wangli 已提交
4314
| callback  |AsyncCallback&lt;boolean\|string\|number\|Uint8Array&gt;  | Yes   |Callback used to return the value obtained.   |
A
annie_wangli 已提交
4315

A
annie_wangli 已提交
4316
**Example**
A
annie_wangli 已提交
4317

A
Annie_wang 已提交
4318
```js
A
annie_wangli 已提交
4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string_2';
const VALUE_TEST_STRING_ELEMENT = 'value-string-002';
try{
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, async function (err,data) {
        console.log('put success');
        kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT, function (err,data) {
            console.log('get success');
        });
    })
}catch(e) {
    console.log('get e' + e);
}
```
A
annie_wangli 已提交
4333 4334 4335 4336


### get<sup>8+</sup> ###

A
annie_wangli 已提交
4337
get(deviceId: string, key: string): Promise&lt;boolean|string|number|Uint8Array&gt;
A
annie_wangli 已提交
4338

A
Annie_wang 已提交
4339
Obtains a string value that matches the specified device ID and key. This API uses a promise to return the result.
A
annie_wangli 已提交
4340

A
annie_wangli 已提交
4341
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4342

A
annie_wangli 已提交
4343 4344 4345
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4346
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4347 4348
| deviceId  |string  | Yes   |ID of the target device.   |
| key       |string  | Yes   |Key to match.   |
A
annie_wangli 已提交
4349

A
annie_wangli 已提交
4350
**Return value**
A
annie_wangli 已提交
4351

A
annie_wangli 已提交
4352
| Type   | Description      |
A
annie_wangli 已提交
4353
| ------  | -------   |
A
Annie_wang 已提交
4354
|Promise&lt;boolean\|string\|number\|Uint8Array&gt; |Promise used to return the string value obtained.|
A
annie_wangli 已提交
4355

A
annie_wangli 已提交
4356
**Example**
A
annie_wangli 已提交
4357

A
Annie_wang 已提交
4358
```js
A
annie_wangli 已提交
4359 4360 4361 4362 4363 4364 4365 4366 4367 4368
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string_2';
const VALUE_TEST_STRING_ELEMENT = 'value-string-002';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(async (data) => {
        console.log(' put success');
        kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT).then((data) => {
            console.log('get success');
        }).catch((err) => {
            console.log('get fail ' + JSON.stringify(err));
A
annie_wangli 已提交
4369
        });
A
annie_wangli 已提交
4370 4371 4372 4373 4374 4375 4376
    }).catch((error) => {
        console.log('put error' + error);
    });
} catch (e) {
    console.log('Get e ' + e);
}
```
A
annie_wangli 已提交
4377 4378 4379 4380


### getEntries<sup>8+</sup> ###

A
annie_wangli 已提交
4381
getEntries(deviceId: string, keyPrefix: string, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
4382

A
Annie_wang 已提交
4383
Obtains all KV pairs that match the specified device ID and key prefix. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4384

A
annie_wangli 已提交
4385
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4386

A
annie_wangli 已提交
4387 4388 4389
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4390
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4391 4392 4393
| deviceId  |string  | Yes   |ID of the target device.   |
| keyPrefix |string  | Yes   |Key prefix to match.   |
| callback  |AsyncCallback&lt;[Entry](#entry)[]&gt;  | Yes |Callback used to return the KV pairs obtained.   |
A
annie_wangli 已提交
4394

A
annie_wangli 已提交
4395
**Example**
A
annie_wangli 已提交
4396

A
Annie_wang 已提交
4397
```js
A
annie_wangli 已提交
4398 4399 4400 4401 4402 4403 4404 4405 4406 4407
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
4408 4409
            }
        }
A
annie_wangli 已提交
4410
        entries.push(entry);
A
annie_wangli 已提交
4411
    }
A
annie_wangli 已提交
4412 4413 4414
    console.log('entries: ' + entries);
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
4415
        kvStore.getEntries('localDeviceId', 'batch_test_string_key', function (err,entries) {
A
annie_wangli 已提交
4416
            console.log('getEntries success');
4417 4418
            console.log('entries.length: ' + entries.length);
            console.log('entries[0]: ' + JSON.stringify(entries[0]));
A
annie_wangli 已提交
4419 4420 4421 4422 4423 4424
        });
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
4425 4426 4427 4428


### getEntries<sup>8+</sup> ###

A
annie_wangli 已提交
4429
getEntries(deviceId: string, keyPrefix: string): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
4430

A
Annie_wang 已提交
4431
Obtains all KV pairs that match the specified device ID and key prefix. This API uses a promise to return the result.
A
annie_wangli 已提交
4432

A
annie_wangli 已提交
4433
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4434

A
annie_wangli 已提交
4435 4436 4437
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4438
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4439 4440
| deviceId  |string  | Yes   |ID of the target device.   |
| keyPrefix |string  | Yes   |Key prefix to match.   |
A
annie_wangli 已提交
4441

A
annie_wangli 已提交
4442
**Return value**
A
annie_wangli 已提交
4443

A
annie_wangli 已提交
4444
| Type   | Description      |
A
annie_wangli 已提交
4445
| ------  | -------   |
A
annie_wangli 已提交
4446
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
4447

A
annie_wangli 已提交
4448
**Example**
A
annie_wangli 已提交
4449

A
Annie_wang 已提交
4450
```js
A
annie_wangli 已提交
4451 4452 4453 4454 4455 4456 4457 4458 4459 4460
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
4461 4462
            }
        }
A
annie_wangli 已提交
4463 4464 4465 4466 4467
        entries.push(entry);
    }
    console.log('entries: ' + entries);
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
4468
        kvStore.getEntries('localDeviceId', 'batch_test_string_key').then((entries) => {
A
annie_wangli 已提交
4469
            console.log('getEntries success');
4470 4471 4472 4473
            console.log('entries.length: ' + entries.length);
            console.log('entries[0]: ' + JSON.stringify(entries[0]));
            console.log('entries[0].value: ' + JSON.stringify(entries[0].value));
            console.log('entries[0].value.value: ' + entries[0].value.value);
A
annie_wangli 已提交
4474
        }).catch((err) => {
A
annie_wangli 已提交
4475
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
4476
        });
A
annie_wangli 已提交
4477 4478 4479 4480 4481 4482 4483
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
4484 4485 4486 4487


### getEntries<sup>8+</sup> ###

A
annie_wangli 已提交
4488
getEntries(query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
4489

A
Annie_wang 已提交
4490
Obtains the KV pairs that match the specified **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4491

A
annie_wangli 已提交
4492
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4493

A
annie_wangli 已提交
4494 4495 4496
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4497
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4498 4499
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt;  | Yes   |Callback used to return the KV pairs obtained.   |
A
annie_wangli 已提交
4500

A
annie_wangli 已提交
4501
**Example**
A
annie_wangli 已提交
4502

A
Annie_wang 已提交
4503
```js
A
annie_wangli 已提交
4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514
let kvStore;
try {
    var arr = new Uint8Array([21,31]);
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_bool_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.BYTE_ARRAY,
                value : arr
A
annie_wangli 已提交
4515 4516
            }
        }
A
annie_wangli 已提交
4517
        entries.push(entry);
A
annie_wangli 已提交
4518
    }
A
annie_wangli 已提交
4519 4520 4521 4522 4523 4524
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
        query.deviceId('localDeviceId');
4525
        kvStore.getEntries(query, function (err,entries) {
A
annie_wangli 已提交
4526
            console.log('getEntries success');
4527 4528
            console.log('entries.length: ' + entries.length);
            console.log('entries[0]: ' + JSON.stringify(entries[0]));
A
annie_wangli 已提交
4529 4530 4531 4532 4533 4534 4535
        });
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
4536 4537 4538 4539


### getEntries<sup>8+</sup> ###

A
annie_wangli 已提交
4540
getEntries(query: Query): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
4541

A
Annie_wang 已提交
4542
Obtains the KV pairs that match the specified **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
4543

A
annie_wangli 已提交
4544
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4545

A
annie_wangli 已提交
4546 4547 4548
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4549
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4550
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4551

A
annie_wangli 已提交
4552
**Return value**
A
annie_wangli 已提交
4553

A
annie_wangli 已提交
4554
| Type   | Description      |
A
annie_wangli 已提交
4555
| ------  | -------   |
A
annie_wangli 已提交
4556
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
4557

A
annie_wangli 已提交
4558
**Example**
A
annie_wangli 已提交
4559

A
Annie_wang 已提交
4560
```js
A
annie_wangli 已提交
4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571
let kvStore;
try {
    var arr = new Uint8Array([21,31]);
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_bool_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.BYTE_ARRAY,
                value : arr
A
annie_wangli 已提交
4572 4573
            }
        }
A
annie_wangli 已提交
4574 4575 4576 4577 4578 4579 4580
        entries.push(entry);
    }
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
4581
        kvStore.getEntries(query).then((entries) => {
A
annie_wangli 已提交
4582
            console.log('getEntries success');
A
annie_wangli 已提交
4583
        }).catch((err) => {
A
annie_wangli 已提交
4584
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
4585
        });
A
annie_wangli 已提交
4586 4587 4588 4589 4590 4591 4592 4593
    }).catch((err) => {
        console.log('GetEntries putBatch fail ' + JSON.stringify(err))
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
4594 4595 4596 4597


### getEntries<sup>8+</sup> ###

A
annie_wangli 已提交
4598
getEntries(deviceId: string, query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
4599

A
Annie_wang 已提交
4600
Obtains the KV pairs that match the specified device ID and **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4601

A
annie_wangli 已提交
4602
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4603

A
annie_wangli 已提交
4604 4605 4606
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4607
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4608 4609
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
Annie_wang 已提交
4610
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt;  | Yes   |Callback invoked to return the KV pairs obtained.   |
A
annie_wangli 已提交
4611

A
annie_wangli 已提交
4612
**Example**
A
annie_wangli 已提交
4613

A
Annie_wang 已提交
4614
```js
A
annie_wangli 已提交
4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625
let kvStore;
try {
    var arr = new Uint8Array([21,31]);
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_bool_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.BYTE_ARRAY,
                value : arr
A
annie_wangli 已提交
4626 4627
            }
        }
A
annie_wangli 已提交
4628
        entries.push(entry);
A
annie_wangli 已提交
4629
    }
A
annie_wangli 已提交
4630 4631 4632 4633 4634 4635
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
        var query = new distributedData.Query();
        query.deviceId('localDeviceId');
        query.prefixKey("batch_test");
4636
        kvStore.getEntries('localDeviceId', query, function (err,entries) {
A
annie_wangli 已提交
4637
            console.log('getEntries success');
4638 4639
            console.log('entries.length: ' + entries.length);
            console.log('entries[0]: ' + JSON.stringify(entries[0]));
A
annie_wangli 已提交
4640 4641 4642 4643 4644 4645 4646
        })
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
4647 4648 4649 4650


### getEntries<sup>8+</sup> ###

A
annie_wangli 已提交
4651
getEntries(deviceId: string, query: Query): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
4652

A
Annie_wang 已提交
4653
Obtains the KV pairs that match the specified device ID and **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
4654

A
annie_wangli 已提交
4655
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4656

A
annie_wangli 已提交
4657 4658 4659
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4660
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4661 4662
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4663

A
annie_wangli 已提交
4664
**Return value**
A
annie_wangli 已提交
4665

A
annie_wangli 已提交
4666
| Type   | Description      |
A
annie_wangli 已提交
4667
| ------  | -------   |
A
annie_wangli 已提交
4668
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
4669

A
annie_wangli 已提交
4670
**Example**
A
annie_wangli 已提交
4671

A
Annie_wang 已提交
4672
```js
A
annie_wangli 已提交
4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683
let kvStore;
try {
    var arr = new Uint8Array([21,31]);
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_bool_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.BYTE_ARRAY,
                value : arr
A
annie_wangli 已提交
4684 4685
            }
        }
A
annie_wangli 已提交
4686 4687 4688 4689 4690 4691 4692 4693
        entries.push(entry);
    }
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
        var query = new distributedData.Query();
        query.deviceId('localDeviceId');
        query.prefixKey("batch_test");
4694
        kvStore.getEntries('localDeviceId', query).then((entries) => {
A
annie_wangli 已提交
4695
            console.log('getEntries success');
A
annie_wangli 已提交
4696
        }).catch((err) => {
A
annie_wangli 已提交
4697
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
4698
        });
A
annie_wangli 已提交
4699 4700 4701 4702 4703 4704 4705 4706
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
4707 4708


A
Annie_wang 已提交
4709
### getResultSet<sup>8+</sup><a name="devicekvstore_getresultset"></a> ###
A
annie_wangli 已提交
4710

A
annie_wangli 已提交
4711
getResultSet(deviceId: string, keyPrefix: string, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
4712

A
Annie_wang 已提交
4713
Obtains a **KvStoreResultSet** object that matches the specified device ID and key prefix. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4714

A
annie_wangli 已提交
4715
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4716

A
annie_wangli 已提交
4717 4718 4719
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4720
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4721 4722
| deviceId  |string  | Yes   |ID of the target device.   |
| keyPrefix |string  | Yes   |Key prefix to match.   |
A
Annie_wang 已提交
4723
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt;  | Yes |Callback invoked to return the **KvStoreResultSet** object obtained.   |
A
annie_wangli 已提交
4724

A
annie_wangli 已提交
4725
**Example**
A
annie_wangli 已提交
4726

A
Annie_wang 已提交
4727
```js
A
annie_wangli 已提交
4728 4729 4730 4731
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('localDeviceId', 'batch_test_string_key', async function (err, result) {
A
Annie_wang 已提交
4732
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
4733
        resultSet = result;
A
Annie_wang 已提交
4734
        kvStore.closeResultSet(resultSet, function (err, data) {
A
annie_wangli 已提交
4735 4736 4737 4738 4739 4740 4741
            console.log('closeResultSet success');
        })
    });
}catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
4742 4743 4744 4745


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
4746
getResultSet(deviceId: string, keyPrefix: string): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
4747

A
Annie_wang 已提交
4748
Obtains a **KvStoreResultSet** object that matches the specified device ID and key prefix. This API uses a promise to return the result.
A
annie_wangli 已提交
4749

A
annie_wangli 已提交
4750
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4751

A
annie_wangli 已提交
4752 4753 4754
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4755
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4756 4757
| deviceId  |string  | Yes   |ID of the target device.   |
| keyPrefix |string  | Yes   |Key prefix to match.   |
A
annie_wangli 已提交
4758

A
annie_wangli 已提交
4759
**Return value**
A
annie_wangli 已提交
4760

A
annie_wangli 已提交
4761
| Type   | Description      |
A
annie_wangli 已提交
4762
| ------  | -------   |
Z
zengyawen 已提交
4763
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4764

A
annie_wangli 已提交
4765
**Example**
A
annie_wangli 已提交
4766

A
Annie_wang 已提交
4767
```js
A
annie_wangli 已提交
4768 4769 4770 4771
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('localDeviceId', 'batch_test_string_key').then((result) => {
A
Annie_wang 已提交
4772
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
4773 4774
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
4775
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
4776 4777 4778 4779 4780 4781 4782 4783 4784 4785
    });
    kvStore.closeResultSet(resultSet).then((err) => {
        console.log('closeResultSet success');
    }).catch((err) => {
        console.log('closeResultSet fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
4786 4787 4788 4789


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
4790
getResultSet(query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
4791

A
Annie_wang 已提交
4792
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4793

A
annie_wangli 已提交
4794
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4795

A
annie_wangli 已提交
4796 4797 4798
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4799
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4800
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
Annie_wang 已提交
4801
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt;  | Yes |Callback invoked to return the **KvStoreResultSet** object obtained.   |
A
annie_wangli 已提交
4802

A
annie_wangli 已提交
4803
**Example**
A
annie_wangli 已提交
4804

A
Annie_wang 已提交
4805
```js
A
annie_wangli 已提交
4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816
let kvStore;
try {
    let resultSet;
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
4817 4818
            }
        }
A
annie_wangli 已提交
4819
        entries.push(entry);
A
annie_wangli 已提交
4820
    }
A
annie_wangli 已提交
4821 4822 4823 4824 4825
    kvStore.putBatch(entries, async function (err, data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
        query.deviceId('localDeviceId');
A
Annie_wang 已提交
4826
        kvStore.getResultSet(query, async function (err, result) {
A
Annie_wang 已提交
4827
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
4828
            resultSet = result;
A
Annie_wang 已提交
4829
            kvStore.closeResultSet(resultSet, function (err, data) {
A
annie_wangli 已提交
4830 4831 4832 4833 4834 4835 4836 4837
                console.log('closeResultSet success');
            })
        });
    });
} catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
4838 4839 4840 4841


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
4842
getResultSet(query: Query): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
4843

A
Annie_wang 已提交
4844
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
4845

A
annie_wangli 已提交
4846
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4847

A
annie_wangli 已提交
4848 4849 4850
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4851
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4852
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4853

A
annie_wangli 已提交
4854
**Return value**
A
annie_wangli 已提交
4855

A
annie_wangli 已提交
4856
| Type   | Description      |
A
annie_wangli 已提交
4857
| ------  | -------   |
Z
zengyawen 已提交
4858
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4859

A
annie_wangli 已提交
4860
**Example**
A
annie_wangli 已提交
4861

A
Annie_wang 已提交
4862
```js
A
annie_wangli 已提交
4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873
let kvStore;
try {
    let resultSet;
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
4874 4875
            }
        }
A
annie_wangli 已提交
4876
        entries.push(entry);
A
annie_wangli 已提交
4877
    }
A
annie_wangli 已提交
4878 4879 4880 4881 4882 4883 4884 4885 4886 4887
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
    }).catch((err) => {
        console.log('putBatch fail ' + err);
    });
    const query = new distributedData.Query();
    query.deviceId('localDeviceId');
    query.prefixKey("batch_test");
    console.log("GetResultSet " + query.getSqlLike());
    kvStore.getResultSet(query).then((result) => {
A
Annie_wang 已提交
4888
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
4889 4890
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
4891
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
4892 4893 4894 4895 4896 4897 4898 4899 4900 4901
    });
    kvStore.closeResultSet(resultSet).then((err) => {
        console.log('closeResultSet success');
    }).catch((err) => {
        console.log('closeResultSet fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
4902 4903 4904 4905


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
4906
getResultSet(deviceId: string, query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
4907

A
Annie_wang 已提交
4908
Obtains a **KvStoreResultSet** object that matches the specified device ID and **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4909

A
annie_wangli 已提交
4910
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4911

A
annie_wangli 已提交
4912 4913 4914
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4915
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4916 4917
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
Annie_wang 已提交
4918
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt;  | Yes |Callback invoked to return the **KvStoreResultSet** object obtained.   |
A
annie_wangli 已提交
4919

A
annie_wangli 已提交
4920
**Example**
A
annie_wangli 已提交
4921

A
Annie_wang 已提交
4922
```js
A
annie_wangli 已提交
4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933
let kvStore;
try {
    let resultSet;
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
4934 4935
            }
        }
A
annie_wangli 已提交
4936
        entries.push(entry);
A
annie_wangli 已提交
4937
    }
A
annie_wangli 已提交
4938 4939 4940 4941
    kvStore.putBatch(entries, async function (err, data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
A
Annie_wang 已提交
4942
        kvStore.getResultSet('localDeviceId', query, async function (err, result) {
A
Annie_wang 已提交
4943
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
4944
            resultSet = result;
A
Annie_wang 已提交
4945
            kvStore.closeResultSet(resultSet, function (err, data) {
A
annie_wangli 已提交
4946 4947 4948 4949 4950 4951 4952 4953
                console.log('closeResultSet success');
            })
        });
    });
} catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
4954 4955 4956 4957


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
4958
getResultSet(deviceId: string, query: Query): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
4959

A
Annie_wang 已提交
4960
Obtains a **KvStoreResultSet** object that matches the specified device ID and **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
4961

A
annie_wangli 已提交
4962
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4963

A
annie_wangli 已提交
4964 4965 4966
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4967
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4968 4969
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4970

A
annie_wangli 已提交
4971
**Return value**
A
annie_wangli 已提交
4972

A
annie_wangli 已提交
4973
| Type   | Description      |
A
annie_wangli 已提交
4974
| ------  | -------   |
Z
zengyawen 已提交
4975
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4976

A
annie_wangli 已提交
4977
**Example**
A
annie_wangli 已提交
4978

A
Annie_wang 已提交
4979
```js
A
annie_wangli 已提交
4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990
let kvStore;
try {
    let resultSet;
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
4991 4992
            }
        }
A
annie_wangli 已提交
4993
        entries.push(entry);
A
annie_wangli 已提交
4994
    }
A
annie_wangli 已提交
4995 4996 4997 4998 4999 5000 5001 5002
    kvStore.putBatch(entries).then(async (err) => {
        console.log('GetResultSet putBatch success');
    }).catch((err) => {
        console.log('PutBatch putBatch fail ' + JSON.stringify(err));
    });
    const query = new distributedData.Query();
    query.prefixKey("batch_test");
    kvStore.getResultSet('localDeviceId', query).then((result) => {
A
Annie_wang 已提交
5003
        console.log('GetResultSet getResultSet succeed.');
A
annie_wangli 已提交
5004 5005
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
5006
        console.log('GetResultSet getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019
    });
    query.deviceId('localDeviceId');
    console.log("GetResultSet " + query.getSqlLike());
    kvStore.closeResultSet(resultSet).then((err) => {
        console.log('GetResultSet closeResultSet success');
    }).catch((err) => {
        console.log('GetResultSet closeResultSet fail ' + JSON.stringify(err));
    });

}catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
5020 5021 5022 5023


### closeResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
5024
closeResultSet(resultSet: KvStoreResultSet, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
5025

A
Annie_wang 已提交
5026
Closes the **KvStoreResultSet** object returned by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
5027

A
annie_wangli 已提交
5028
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5029

A
annie_wangli 已提交
5030 5031 5032
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5033
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5034 5035
| resultSet  |[KvStoreResultSet](#getresultset8)  | Yes   |**KvStoreResultSet** object to close.  |
| callback   |AsyncCallback&lt;void&gt;                 | Yes   |Callback used to return the result.   |
A
annie_wangli 已提交
5036

A
annie_wangli 已提交
5037
**Example**
A
annie_wangli 已提交
5038

A
Annie_wang 已提交
5039
```js
A
annie_wangli 已提交
5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054
let kvStore;
try {
    console.log('CloseResultSet success');
    let resultSet = null;
    kvStore.closeResultSet(resultSet, function (err, data) {
        if (err == undefined) {
            console.log('closeResultSet success');
        } else {
            console.log('closeResultSet fail');
        }
    });
}catch(e) {
    console.log('CloseResultSet e ' + e);
}
```
A
annie_wangli 已提交
5055 5056 5057 5058


### closeResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
5059
closeResultSet(resultSet: KvStoreResultSet): Promise&lt;void&gt;
A
annie_wangli 已提交
5060

A
Annie_wang 已提交
5061
Closes the **KvStoreResultSet** object returned by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses a promise to return the result.
A
annie_wangli 已提交
5062

A
annie_wangli 已提交
5063
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5064

A
annie_wangli 已提交
5065 5066 5067
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5068
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5069
| resultSet  |[KvStoreResultSet](#getresultset8)  | Yes   |**KvStoreResultSet** object to close.  |
A
annie_wangli 已提交
5070

A
annie_wangli 已提交
5071
**Return value**
A
annie_wangli 已提交
5072

A
annie_wangli 已提交
5073
| Type   | Description      |
A
annie_wangli 已提交
5074
| ------  | -------   |
A
Annie_wang 已提交
5075
|Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
5076

A
annie_wangli 已提交
5077
**Example**
A
annie_wangli 已提交
5078

A
Annie_wang 已提交
5079
```js
A
annie_wangli 已提交
5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092
let kvStore;
try {
    console.log('CloseResultSet success');
    let resultSet = null;
    kvStore.closeResultSet(resultSet).then(() => {
        console.log('closeResultSet success');
    }).catch((err) => {
        console.log('closeResultSet fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('CloseResultSet e ' + e);
}
```
A
annie_wangli 已提交
5093 5094 5095 5096


### getResultSize<sup>8+</sup> ###

A
annie_wangli 已提交
5097
getResultSize(query: Query, callback: AsyncCallback&lt;number&gt;): void
A
annie_wangli 已提交
5098

A
Annie_wang 已提交
5099
Obtains the number of results that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
5100

A
annie_wangli 已提交
5101
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5102

A
annie_wangli 已提交
5103 5104 5105
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5106
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5107
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
A
Annie_wang 已提交
5108
| callback  |AsyncCallback&lt;number&gt;  | Yes   |Callback invoked to return the number of results obtained.   |
A
annie_wangli 已提交
5109

A
annie_wangli 已提交
5110
**Example**
A
annie_wangli 已提交
5111

A
Annie_wang 已提交
5112
```js
A
annie_wangli 已提交
5113 5114 5115 5116 5117 5118 5119 5120 5121 5122
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
5123 5124
            }
        }
A
annie_wangli 已提交
5125
        entries.push(entry);
A
annie_wangli 已提交
5126
    }
A
annie_wangli 已提交
5127 5128 5129 5130 5131
    kvStore.putBatch(entries, async function (err, data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
        query.deviceId('localDeviceId');
A
Annie_wang 已提交
5132
        kvStore.getResultSize(query, async function (err, resultSize) {
A
Annie_wang 已提交
5133
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
5134 5135 5136 5137 5138 5139
        });
    });
} catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
5140 5141 5142 5143


### getResultSize<sup>8+</sup> ###

A
annie_wangli 已提交
5144
getResultSize(query: Query): Promise&lt;number&gt;
A
annie_wangli 已提交
5145

A
Annie_wang 已提交
5146
Obtains the number of results that matches the specified **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
5147

A
annie_wangli 已提交
5148
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5149

A
annie_wangli 已提交
5150 5151 5152
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5153
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5154
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
5155

A
annie_wangli 已提交
5156
**Return value**
A
annie_wangli 已提交
5157

A
annie_wangli 已提交
5158
| Type   | Description      |
A
annie_wangli 已提交
5159
| ------  | -------   |
A
annie_wangli 已提交
5160
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
5161

A
annie_wangli 已提交
5162
**Example**
A
annie_wangli 已提交
5163

A
Annie_wang 已提交
5164
```js
A
annie_wangli 已提交
5165 5166 5167 5168 5169 5170 5171 5172 5173 5174
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
5175 5176
            }
        }
A
annie_wangli 已提交
5177
        entries.push(entry);
A
annie_wangli 已提交
5178
    }
A
annie_wangli 已提交
5179 5180 5181 5182 5183 5184 5185 5186 5187
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
    const query = new distributedData.Query();
    query.prefixKey("batch_test");
    query.deviceId('localDeviceId');
    kvStore.getResultSize(query).then((resultSize) => {
A
Annie_wang 已提交
5188
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
5189
    }).catch((err) => {
A
Annie_wang 已提交
5190
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
5191 5192 5193 5194 5195
    });
}catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
5196 5197 5198 5199 5200 5201


### getResultSize<sup>8+</sup> ###

getResultSize(deviceId: string, query: Query, callback: AsyncCallback&lt;number&gt;): void;

A
Annie_wang 已提交
5202
Obtains the number of results that matches the specified device ID and **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
5203

A
annie_wangli 已提交
5204
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5205

A
annie_wangli 已提交
5206 5207 5208
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5209
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5210 5211
| deviceId  |string                       | Yes   |ID of the target device.   |
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
A
Annie_wang 已提交
5212
| callback  |AsyncCallback&lt;number&gt;  | Yes   |Callback invoked to return the number of results obtained.   |
A
annie_wangli 已提交
5213

A
annie_wangli 已提交
5214
**Example**
A
annie_wangli 已提交
5215

A
Annie_wang 已提交
5216
```js
A
annie_wangli 已提交
5217 5218 5219 5220 5221 5222 5223 5224 5225 5226
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
5227 5228
            }
        }
A
annie_wangli 已提交
5229
        entries.push(entry);
A
annie_wangli 已提交
5230
    }
A
annie_wangli 已提交
5231 5232 5233 5234
    kvStore.putBatch(entries, async function (err, data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
A
Annie_wang 已提交
5235
        kvStore.getResultSize('localDeviceId', query, async function (err, resultSize) {
A
Annie_wang 已提交
5236
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
5237 5238 5239 5240 5241 5242
        });
    });
} catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
5243 5244 5245 5246


### getResultSize<sup>8+</sup> ###

A
annie_wangli 已提交
5247
getResultSize(deviceId: string, query: Query): Promise&lt;number&gt;
A
annie_wangli 已提交
5248

A
Annie_wang 已提交
5249
Obtains the number of results that matches the specified device ID and **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
5250

A
annie_wangli 已提交
5251
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5252

A
annie_wangli 已提交
5253 5254 5255
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5256
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5257 5258
| deviceId  |string                       | Yes   |ID of the target device.   |
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
5259

A
annie_wangli 已提交
5260
**Return value**
A
annie_wangli 已提交
5261

A
annie_wangli 已提交
5262
| Type   | Description      |
A
annie_wangli 已提交
5263
| ------  | -------   |
A
annie_wangli 已提交
5264
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
5265

A
annie_wangli 已提交
5266
**Example**
A
annie_wangli 已提交
5267

A
Annie_wang 已提交
5268
```js
A
annie_wangli 已提交
5269 5270 5271 5272 5273 5274 5275 5276 5277 5278
let kvStore;
try {
    let entries = [];
    for (var i = 0; i < 10; i++) {
        var key = 'batch_test_string_key';
        var entry = {
            key : key + i,
            value : {
                type : distributedData.ValueType.STRING,
                value : 'batch_test_string_value'
A
annie_wangli 已提交
5279 5280
            }
        }
A
annie_wangli 已提交
5281
        entries.push(entry);
A
annie_wangli 已提交
5282
    }
A
annie_wangli 已提交
5283 5284 5285 5286 5287 5288 5289 5290
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
    var query = new distributedData.Query();
    query.prefixKey("batch_test");
    kvStore.getResultSize('localDeviceId', query).then((resultSize) => {
A
Annie_wang 已提交
5291
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
5292
    }).catch((err) => {
A
Annie_wang 已提交
5293
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
5294 5295 5296 5297 5298
    });
}catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
5299 5300 5301 5302


### removeDeviceData<sup>8+</sup> ###

A
annie_wangli 已提交
5303
removeDeviceData(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
5304

A
Annie_wang 已提交
5305
Deletes data of the specified device from this KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
5306

A
annie_wangli 已提交
5307
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5308

A
annie_wangli 已提交
5309 5310 5311
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5312
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5313 5314
| deviceId  |string                       | Yes   |ID of the target device. |
| callback  |AsyncCallback&lt;void&gt;    | Yes   |Callback used to return the result.   |
A
annie_wangli 已提交
5315

A
annie_wangli 已提交
5316
**Example**
A
annie_wangli 已提交
5317

A
Annie_wang 已提交
5318
```js
A
annie_wangli 已提交
5319 5320 5321 5322 5323 5324 5325
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string';
const VALUE_TEST_STRING_ELEMENT = 'value-string-001';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, async function (err,data) {
        console.log('RemoveDeviceData  put success');
        const deviceid = 'no_exist_device_id';
A
Annie_wang 已提交
5326
        kvStore.removeDeviceData(deviceid, async function (err,data) {
A
annie_wangli 已提交
5327 5328 5329 5330
            if (err == undefined) {
                console.log('removeDeviceData success');
            } else {
                console.log('removeDeviceData fail');
A
Annie_wang 已提交
5331
                kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT, async function (err,data) {
A
annie_wangli 已提交
5332 5333 5334
                    console.log('RemoveDeviceData get success');
                });
            }
A
annie_wangli 已提交
5335
        });
A
annie_wangli 已提交
5336 5337 5338 5339 5340
    });
}catch(e) {
    console.log('RemoveDeviceData e ' + e);
}
```
A
annie_wangli 已提交
5341 5342 5343 5344


### removeDeviceData<sup>8+</sup> ###

A
annie_wangli 已提交
5345
removeDeviceData(deviceId: string): Promise&lt;void&gt;
A
annie_wangli 已提交
5346

A
Annie_wang 已提交
5347
Deletes data of the specified device from this KV store. This API uses a promise to return the result.
A
annie_wangli 已提交
5348

A
annie_wangli 已提交
5349
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5350

A
annie_wangli 已提交
5351 5352 5353
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5354
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5355
| deviceId  |string  | Yes   |ID of the target device. |
A
annie_wangli 已提交
5356

A
annie_wangli 已提交
5357
**Return value**
A
annie_wangli 已提交
5358

A
annie_wangli 已提交
5359
| Type   | Description      |
A
annie_wangli 已提交
5360
| ------  | -------   |
A
Annie_wang 已提交
5361
|Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
5362

A
annie_wangli 已提交
5363
**Example**
A
annie_wangli 已提交
5364

A
Annie_wang 已提交
5365
```js
A
annie_wangli 已提交
5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389
let kvStore;
const KEY_TEST_STRING_ELEMENT = 'key_test_string';
const VALUE_TEST_STRING_ELEMENT = 'value-string-001';
try {
    kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then((err) => {
        console.log('RemoveDeviceData put success');
    }).catch((err) => {
        console.log('RemoveDeviceData put fail ' + JSON.stringify(err));
    });
    const deviceid = 'no_exist_device_id';
    kvStore.removeDeviceData(deviceid).then((err) => {
        console.log('removeDeviceData success');
    }).catch((err) => {
        console.log('removeDeviceData fail ' + JSON.stringify(err));
    });
    kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT).then((data) => {
        console.log('RemoveDeviceData get success data:' + data);
    }).catch((err) => {
        console.log('RemoveDeviceData get fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('RemoveDeviceData e ' + e);
}
```
A
annie_wangli 已提交
5390 5391 5392 5393


### sync<sup>8+</sup> ###

A
annie_wangli 已提交
5394
sync(deviceIdList: string[], mode: SyncMode, allowedDelayMs?: number): void
A
annie_wangli 已提交
5395

A
Annie_wang 已提交
5396
Synchronizes the KV store manually. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
A
Annie_wang 已提交
5397

A
annie_wangli 已提交
5398
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
A
annie_wangli 已提交
5399

A
annie_wangli 已提交
5400
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
5401

A
annie_wangli 已提交
5402 5403 5404
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5405
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5406
| deviceIdList    |string[]               | Yes   |IDs of the devices to be synchronized.|
A
Annie_wang 已提交
5407
| mode            |[SyncMode](#syncmode)  | Yes   |Synchronization mode. |
A
annie_wangli 已提交
5408
| allowedDelayMs  |number                 | No   |Allowed synchronization delay time, in ms. |
A
annie_wangli 已提交
5409

A
annie_wangli 已提交
5410
**Example**
A
annie_wangli 已提交
5411

A
Annie_wang 已提交
5412
```js
A
annie_wangli 已提交
5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429
let kvStore;
const KEY_TEST_SYNC_ELEMENT = 'key_test_sync';
const VALUE_TEST_SYNC_ELEMENT = 'value-string-001';
try {
    kvStore.on('syncComplete', function (data) {
        console.log('Sync dataChange');
    });
    kvStore.put(KEY_TEST_SYNC_ELEMENT + 'testSync101', VALUE_TEST_SYNC_ELEMENT, function (err,data) {
        console.log('Sync put success');
        const devices = ['deviceList'];
        const mode = distributedData.SyncMode.PULL_ONLY;
        kvStore.sync(devices, mode);
    });
}catch(e) {
    console.log('Sync e' + e);
}
```
A
annie_wangli 已提交
5430

A
Annie_wang 已提交
5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472
### sync<sup>9+</sup> ###

sync(deviceIds: string[], query: Query, mode: SyncMode, delayMs?: number): void

Synchronizes the KV store manually. This API uses a synchronous mode. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).

**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------   | ----  | ----------------------- |
| deviceIds    |string[]               | Yes   |IDs of the devices to be synchronized.|
| query            |[Query](#query8)  | Yes   | **Query** object to match.|
| delayMs  |number                 | No   |Allowed synchronization delay time, in ms. |

**Example**

```js
let kvstore;
const KEY_TEST_SYNC_ELEMENT = 'key_test_sync';
const VALUE_TEST_SYNC_ELEMENT = 'value-string-001';
try {
    kvStore.on('syncComplete', function (data) {
        console.log('Sync dataChange');
    });
    kvStore.put(KEY_TEST_SYNC_ELEMENT + 'testSync101', VALUE_TEST_SYNC_ELEMENT, function (err,data) {
        console.log('Sync put success');
        const devices = ['deviceList'];
        const mode = distributedData.SyncMode.PULL_ONLY;
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
        query.deviceId('localDeviceId');
        kvStore.sync(devices, query, 1000);
    });
}catch(e) {
    console.log('Sync e' + e);
}
```

A
Annie_wang 已提交
5473
### on('syncComplete')<sup>8+</sup> ###
A
annie_wangli 已提交
5474

A
annie_wangli 已提交
5475
on(event: 'syncComplete', syncCallback: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
5476

A
Annie_wang 已提交
5477
Subscribes to the data synchronization complete events.
A
annie_wangli 已提交
5478

A
annie_wangli 已提交
5479
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
5480

A
annie_wangli 已提交
5481 5482 5483
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5484
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
5485
| event    |string      | Yes   |Event to subscribe to. The value is **syncComplete**, which indicates the data synchronization complete event.|
A
Annie_wang 已提交
5486
| syncCallback            |Callback<Array&lt;[string, number]&gt;> | Yes   |Callback used to return the synchronization result. |
A
annie_wangli 已提交
5487

A
annie_wangli 已提交
5488
**Example**
A
annie_wangli 已提交
5489

A
Annie_wang 已提交
5490
```js
A
Annie_wang 已提交
5491
let kvStore;
A
annie_wangli 已提交
5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506
const KEY_TEST_FLOAT_ELEMENT = 'key_test_float';
const VALUE_TEST_FLOAT_ELEMENT = 321.12;
try {
    kvStore.on('syncComplete', function (data) {
        console.log('syncComplete ' + data)
    });
    kvStore.put(KEY_TEST_FLOAT_ELEMENT, VALUE_TEST_FLOAT_ELEMENT).then((data) => {
        console.log('syncComplete put success');
    }).catch((error) => {
        console.log('syncComplete put fail ' + error);
    });
}catch(e) {
    console.log('syncComplete put e ' + e);
}
```
A
annie_wangli 已提交
5507 5508


A
Annie_wang 已提交
5509
### off('syncComplete')<sup>8+</sup> ###
A
annie_wangli 已提交
5510

A
annie_wangli 已提交
5511
off(event: 'syncComplete', syncCallback?: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
5512

A
Annie_wang 已提交
5513
Unsubscribes from the synchronization complete events. This API uses a synchronous callback to return the result.
A
annie_wangli 已提交
5514

A
annie_wangli 已提交
5515
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
5516

A
annie_wangli 已提交
5517 5518 5519
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5520
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
5521
| event         |string                           | Yes   |Event to unsubscribe from. The value is **syncComplete**, which indicates the data synchronization complete event.|
A
annie_wangli 已提交
5522
| syncCallback  |Callback<Array&lt;[string, number]&gt;&gt; | No   |Callback used to return the synchronization result. |
A
annie_wangli 已提交
5523

A
annie_wangli 已提交
5524
**Example**
A
annie_wangli 已提交
5525

A
Annie_wang 已提交
5526
```js
A
annie_wangli 已提交
5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537
let kvStore;
try {
    const func = function (data) {
        console.log('syncComplete ' + data)
    };
    kvStore.on('syncComplete', func);
    kvStore.off('syncComplete', func);
}catch(e) {
    console.log('syncComplete e ' + e);
}
```
A
annie_wangli 已提交
5538

A
Annie_wang 已提交
5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590
### on('dataChange')<sup>9+</sup> ###

on(event: 'dataChange', type: SubscribeType, listener: Callback&lt;ChangeNotification&gt;): void

Subscribes to data changes of the specified type. This API uses a synchronous callback to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------  | ----  | ----------------------- |
| event  |string  | Yes   |Event to subscribe to. The value is **dataChange**, which indicates data changes.      |
| type  |[SubscribeType](#subscribetype) | Yes   |Subscription type.    |
| listener |Callback&lt;[ChangeNotification](#changenotification)&gt; | Yes   |Callback used to return the result.|

**Example**

```js
let kvStore;
kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL, function (data) {
    console.log("dataChange callback call data: " + JSON.stringify(data));
});
```


### off('dataChange')<sup>9+</sup> ###

off(event:'dataChange', listener?: Callback&lt;ChangeNotification&gt;): void

Unsubscribes from the data change events. This API uses a synchronous callback to return the result.

**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
| -----  | ------  | ----  | ----------------------- |
| event  |string  | Yes   |Event to unsubscribe from. The value is **dataChange**, which indicates data changes.      |
| listener |Callback&lt;[ChangeNotification](#changenotification)&gt; |No   |Callback used to return the result.|

**Example**

```js
let kvStore;
kvStore.on('dataChange', function (data) {
    console.log("callback call data: " + data);
});
kvStore.off('dataChange', function (data) {
    console.log("callback call data: " + data);
});
```
A
annie_wangli 已提交
5591 5592 5593

## SyncMode

A
Annie_wang 已提交
5594
Enumerates the synchronization modes.
Z
zengyawen 已提交
5595

A
annie_wangli 已提交
5596 5597
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
Annie_wang 已提交
5598
| Name      | Value    | Description                   |
A
annie_wangli 已提交
5599 5600 5601 5602
| -----      | ------    | ----------------------- |
| PULL_ONLY  |0          |Pull data from the peer end to the local end only.|
| PUSH_ONLY  |1          |Push data from the local end to the peer end only.|
| PUSH_PULL  |2          |Push data from the local end to the peer end and then pull data from the peer end to the local end.|