js-apis-distributed-data.md 164.9 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 574
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
575
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
576 577 578
| createIfMissing  | boolean | No| Whether to create a KV store if no database file exists. By default, a KV store is created.    |
| encrypt  | boolean | No|Whether to encrypt database files. By default, database files are not encrypted.    |
| backup  | boolean | No|Whether to back up database files. By default, database files are backed up.     |
A
Annie_wang 已提交
579
| autoSync  | boolean | No|Whether database files are automatically synchronized. By default, database files are not automatically synchronized.<br>**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC    |
A
annie_wangli 已提交
580
| 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.|
A
annie_wangli 已提交
581
| securityLevel | [SecurityLevel](#securitylevel) | No|Security level of the KV store. By default, the security level is not set. |
A
annie_wangli 已提交
582
| schema<sup>8+</sup> | [Schema](#schema8) | No| Schema used to define the values stored in a KV store.|
Z
zengyawen 已提交
583 584


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

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

A
annie_wangli 已提交
589

A
Annie_wang 已提交
590
| Name | Value| Description                   |
A
annie_wangli 已提交
591
| ---   | ----  | ----------------------- |
A
Annie_wang 已提交
592 593 594
| 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 已提交
595 596


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

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

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

A
Annie_wang 已提交
603
| Name | Value| Description                   |
A
annie_wangli 已提交
604
| ---   | ----  | ----------------------- |
A
annie_wangli 已提交
605
| NO_LEVEL  | 0 | No security level is set for the KV store.  |
A
Annie_wang 已提交
606 607 608 609 610
| 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 已提交
611 612 613 614 615 616


## Constants

Defines the KV store constants.

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

A
Annie_wang 已提交
619
| Name | Value| Description                   |
A
annie_wangli 已提交
620
| ---   | ----  | ----------------------- |
A
Annie_wang 已提交
621 622 623 624
| 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 已提交
625 626
| MAX_QUERY_LENGTH  | 512000 | Maximum query length, in bytes.|
| MAX_BATCH_SIZE  | 128 | Maximum number of batch operations.|
A
annie_wangli 已提交
627 628 629

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

A
Annie_wang 已提交
630
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 已提交
631

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

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

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

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

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

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

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

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

A
annie_wangli 已提交
653 654 655 656 657 658
**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 已提交
659
| type<sup>8+</sup>  | number | Value of the data type corresponding to the specified node.|
A
annie_wangli 已提交
660 661 662 663

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

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

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

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

**Parameters**

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

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

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

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

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

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

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

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

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

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

A
Annie_wang 已提交
697
```js
A
annie_wangli 已提交
698 699 700 701 702 703 704 705 706
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 已提交
707
    console.log("appendNode " + JSON.stringify(node));
A
annie_wangli 已提交
708 709 710 711 712 713 714 715
    child1 = null;
    child2 = null;
    child3 = null;
    node = null;
} catch (e) {
    console.log("AppendChild " + e);
}
```
A
annie_wangli 已提交
716 717 718 719


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

A
Annie_wang 已提交
720 721 722
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 已提交
723

A
annie_wangli 已提交
724

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

Checks whether the data read position is the first row.

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

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

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

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

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


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

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

Checks whether the data read position is the last row.

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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


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

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

A
annie_wangli 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
**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 已提交
1192 1193
### reset<sup>8+</sup> ###

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

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

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

A
annie_wangli 已提交
1200 1201 1202 1203

**Return value**

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

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

A
Annie_wang 已提交
1209
```js
A
annie_wangli 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
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 已提交
1221 1222 1223 1224


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

**Parameters**

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

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

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

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

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


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

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

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


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

**Parameters**

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

A
Annie_wang 已提交
1694
```js
A
annie_wangli 已提交
1695 1696 1697 1698 1699 1700 1701 1702
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 已提交
1703
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1704 1705
}
```
A
annie_wangli 已提交
1706 1707 1708 1709


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

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

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

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

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

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

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

A
Annie_wang 已提交
1724
```js
A
annie_wangli 已提交
1725 1726 1727 1728 1729 1730 1731 1732
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 已提交
1733
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1734 1735
}
```
A
annie_wangli 已提交
1736 1737 1738 1739


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

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

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

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

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

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

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

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

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

A
Annie_wang 已提交
1760
```js
A
annie_wangli 已提交
1761 1762 1763 1764 1765 1766 1767
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 已提交
1768
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1769 1770
}
```
A
annie_wangli 已提交
1771 1772 1773 1774


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

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

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

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

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

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

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

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

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

A
Annie_wang 已提交
1795
```js
A
annie_wangli 已提交
1796 1797 1798 1799 1800 1801 1802
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 已提交
1803
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1804 1805
}
```
A
annie_wangli 已提交
1806 1807 1808 1809


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

A
Annie_wang 已提交
1894
```js
A
annie_wangli 已提交
1895 1896 1897 1898 1899 1900 1901 1902
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 已提交
1903
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1904 1905
}
```
A
annie_wangli 已提交
1906 1907 1908 1909


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

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

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

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

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

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

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

A
Annie_wang 已提交
1924
```js
A
annie_wangli 已提交
1925 1926 1927 1928 1929 1930 1931 1932
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 已提交
1933
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1934 1935
}
```
A
annie_wangli 已提交
1936 1937 1938 1939


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

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

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

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

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

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

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

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

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

A
Annie_wang 已提交
1960
```js
A
annie_wangli 已提交
1961 1962 1963 1964 1965 1966 1967
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 已提交
1968
    console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
1969 1970
}
```
A
annie_wangli 已提交
1971 1972 1973 1974


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

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

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

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

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

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

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

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

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

A
Annie_wang 已提交
1995
```js
A
annie_wangli 已提交
1996 1997 1998 1999 2000 2001 2002
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 已提交
2003
   console.log("duplicated calls should be ok :" + e);
A
annie_wangli 已提交
2004 2005
}
```
A
annie_wangli 已提交
2006 2007 2008 2009


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

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

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

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

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

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


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

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

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

A
Annie_wang 已提交
2031
```js
A
annie_wangli 已提交
2032 2033 2034 2035 2036 2037 2038 2039
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 已提交
2040 2041 2042 2043


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

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

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

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

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

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

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

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


## KVStore

A
Annie_wang 已提交
2071 2072 2073
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 已提交
2074

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

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

A
Annie_wang 已提交
2079
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 已提交
2080

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

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

A
annie_wangli 已提交
2085 2086 2087 2088
| 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 已提交
2089
| callback | AsyncCallback&lt;void&gt; | Yes   |Callback used to return the result.  |
A
annie_wangli 已提交
2090

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

A
Annie_wang 已提交
2093
```js
A
annie_wangli 已提交
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
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 已提交
2109 2110 2111 2112 2113 2114


### put

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

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

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

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

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2122
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2123 2124
| 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 已提交
2125

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

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

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

A
Annie_wang 已提交
2134
```js
A
annie_wangli 已提交
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
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 已提交
2148 2149 2150 2151 2152

### delete

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

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

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

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

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2160
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2161
| 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 已提交
2162
| callback  | AsyncCallback&lt;void&gt;  | Yes   |Callback used to return the result.  |
A
annie_wangli 已提交
2163

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

A
Annie_wang 已提交
2166
```js
A
annie_wangli 已提交
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
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 已提交
2178
            if (err != undefined) {
A
annie_wangli 已提交
2179
                console.log("delete err: " + JSON.stringify(err));
A
annie_wangli 已提交
2180 2181
                return;
            }
A
annie_wangli 已提交
2182
            console.log("delete success");
A
annie_wangli 已提交
2183
        });
A
annie_wangli 已提交
2184 2185 2186 2187 2188
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
2189 2190 2191 2192 2193

### delete

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

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

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

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

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2201
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2202
| 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 已提交
2203

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

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

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

A
Annie_wang 已提交
2212
```js
A
annie_wangli 已提交
2213 2214 2215 2216 2217 2218 2219 2220
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 已提交
2221
        }).catch((err) => {
A
annie_wangli 已提交
2222
            console.log("delete err: " + JSON.stringify(err));
A
annie_wangli 已提交
2223
        });
A
annie_wangli 已提交
2224 2225 2226 2227 2228 2229 2230
    }).catch((err) => {
        console.log("put err: " + JSON.stringify(err));
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
2231

A
Annie_wang 已提交
2232 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
### 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 已提交
2311

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

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

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

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

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

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

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

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


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

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

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

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

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

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2349
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
2350 2351
| 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 已提交
2352

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

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

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

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

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

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

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

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2373
| -----  | ------  | ----  | ----------------------- |
A
Annie_wang 已提交
2374 2375
| 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 已提交
2376

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

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

A
Annie_wang 已提交
2389 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
### 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 已提交
2419 2420 2421

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

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

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

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

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

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

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

A
Annie_wang 已提交
2437
```js
A
annie_wangli 已提交
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
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 已提交
2448 2449
            }
        }
A
annie_wangli 已提交
2450
        entries.push(entry);
A
annie_wangli 已提交
2451
    }
A
annie_wangli 已提交
2452 2453 2454
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
A
Annie_wang 已提交
2455
        kvStore.getEntries('batch_test_string_key', function (err,entrys) {
A
annie_wangli 已提交
2456 2457 2458 2459 2460 2461
            console.log('getEntries success');
            console.log('entrys.length: ' + entrys.length);
            console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
        });
    });
}catch(e) {
A
Annie_wang 已提交
2462
    console.log('PutBatch e ' + JSON.stringify(e));
A
annie_wangli 已提交
2463 2464
}
```
A
annie_wangli 已提交
2465 2466 2467 2468


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

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

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

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

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

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

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

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

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

A
Annie_wang 已提交
2489
```js
A
annie_wangli 已提交
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499
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 已提交
2500 2501
            }
        }
A
annie_wangli 已提交
2502 2503 2504 2505 2506
        entries.push(entry);
    }
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
A
Annie_wang 已提交
2507
        kvStore.getEntries('batch_test_string_key').then((entrys) => {
A
annie_wangli 已提交
2508 2509
            console.log('getEntries success');
            console.log('PutBatch ' + JSON.stringify(entries));
A
annie_wangli 已提交
2510
        }).catch((err) => {
A
annie_wangli 已提交
2511
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
2512
        });
A
annie_wangli 已提交
2513 2514 2515 2516
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
A
Annie_wang 已提交
2517 2518 2519 2520 2521 2522 2523 2524 2525 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
    console.log('PutBatch e ' + JSON.stringify(e));
}
```

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

putBatch(value: Array&lt;ValuesBucket&gt;, callback: AsyncCallback&lt;void&gt;): void
  
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 已提交
2557 2558
}
```
A
annie_wangli 已提交
2559

A
Annie_wang 已提交
2560 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
### 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 已提交
2604 2605 2606

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

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

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

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

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

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

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

A
Annie_wang 已提交
2622
```js
A
annie_wangli 已提交
2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633
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 已提交
2634 2635
            }
        }
A
annie_wangli 已提交
2636 2637
        entries.push(entry);
        keys.push(key + i);
A
annie_wangli 已提交
2638
    }
A
annie_wangli 已提交
2639 2640 2641
    console.log('entries: ' + JSON.stringify(entries));
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
A
Annie_wang 已提交
2642
        kvStore.deleteBatch(keys, async function (err,data) {
A
annie_wangli 已提交
2643 2644 2645 2646 2647 2648 2649
            console.log('deleteBatch success');
        });
    });
}catch(e) {
    console.log('DeleteBatch e ' + e);
}
```
A
annie_wangli 已提交
2650 2651 2652 2653


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

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

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

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

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

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

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

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

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

A
Annie_wang 已提交
2674
```js
A
annie_wangli 已提交
2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
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 已提交
2686 2687
            }
        }
A
annie_wangli 已提交
2688 2689 2690 2691 2692 2693
        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 已提交
2694
        kvStore.deleteBatch(keys).then((err) => {
A
annie_wangli 已提交
2695
            console.log('deleteBatch success');
A
annie_wangli 已提交
2696
        }).catch((err) => {
A
annie_wangli 已提交
2697
            console.log('deleteBatch fail ' + JSON.stringify(err));
A
annie_wangli 已提交
2698
        });
A
annie_wangli 已提交
2699 2700 2701 2702 2703 2704 2705
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('DeleteBatch e ' + e);
}
```
A
annie_wangli 已提交
2706 2707 2708 2709


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

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

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

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

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

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

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

A
Annie_wang 已提交
2724
```js
A
annie_wangli 已提交
2725 2726 2727 2728 2729 2730 2731 2732 2733
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 已提交
2734 2735
            }
        }
A
annie_wangli 已提交
2736
        entries.push(entry);
A
annie_wangli 已提交
2737
    }
A
annie_wangli 已提交
2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
    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 已提交
2750
        kvStore.putBatch(entries, async function (err,data) {
A
annie_wangli 已提交
2751
            console.log('putBatch success');
A
annie_wangli 已提交
2752
        });
A
annie_wangli 已提交
2753 2754 2755 2756 2757
    });
}catch(e) {
    console.log('startTransaction e ' + e);
}
```
A
annie_wangli 已提交
2758 2759 2760 2761


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

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

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

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

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

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

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

A
Annie_wang 已提交
2776
```js
A
annie_wangli 已提交
2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
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 已提交
2793 2794 2795 2796


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

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

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

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

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

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

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

A
Annie_wang 已提交
2811
```js
A
annie_wangli 已提交
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824
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 已提交
2825 2826 2827 2828


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

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

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

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

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

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

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

A
Annie_wang 已提交
2843
```js
A
annie_wangli 已提交
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854
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 已提交
2855 2856 2857 2858


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

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

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

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

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

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

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

A
Annie_wang 已提交
2873
```js
A
annie_wangli 已提交
2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
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 已提交
2887 2888 2889 2890


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

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

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

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

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

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

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

A
Annie_wang 已提交
2905
```js
A
annie_wangli 已提交
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
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 已提交
2917 2918 2919 2920


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

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

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

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

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

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

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

A
Annie_wang 已提交
2936
```js
A
annie_wangli 已提交
2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
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 已提交
2950 2951 2952 2953


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

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

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

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

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

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

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

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

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

A
Annie_wang 已提交
2974
```js
A
annie_wangli 已提交
2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985
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 已提交
2986 2987 2988 2989


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

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

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

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

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

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

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

A
Annie_wang 已提交
3006
```js
A
annie_wangli 已提交
3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
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 已提交
3018 3019 3020 3021


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

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

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

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

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

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


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

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

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

A
Annie_wang 已提交
3044
```js
A
annie_wangli 已提交
3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057
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 已提交
3058 3059 3060 3061


## SubscribeType

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

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

A
Annie_wang 已提交
3066
| Name | Value  | Description                   |
A
annie_wangli 已提交
3067
| -----  | ------   | ----------------------- |
A
annie_wangli 已提交
3068 3069 3070
| 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 已提交
3071 3072 3073 3074 3075

## ChangeNotification

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

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

| Name | Type  |Readable  |Writable  | Description                   |
A
annie_wangli 已提交
3079
| ----- | -------   | -----| ------|------------------------ |
A
annie_wangli 已提交
3080 3081 3082 3083
| 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 已提交
3084 3085 3086

## Entry

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

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

| Name | Type  |Readable  |Writable  | Description                   |
A
annie_wangli 已提交
3092
| ----- | -------   | -----| ------|------------------------ |
A
annie_wangli 已提交
3093 3094
| 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 已提交
3095 3096 3097 3098


## Value

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

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

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

## ValueType

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

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

A
Annie_wang 已提交
3114
| Name | Value  | Description                   |
A
annie_wangli 已提交
3115
| -----  | ------   | ----------------------- |
A
annie_wangli 已提交
3116 3117 3118 3119 3120 3121
| 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 已提交
3122

A
Annie_wang 已提交
3123

A
annie_wangli 已提交
3124 3125
## SingleKVStore

A
Annie_wang 已提交
3126 3127 3128 3129 3130
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 已提交
3131 3132 3133 3134 3135

### get

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

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

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

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

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3143
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3144
| 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 已提交
3145
| callback  |AsyncCallback&lt;Uint8Array \| string \| boolean \| number&gt;) | Yes   |Callback invoked to return the value obtained. |
A
annie_wangli 已提交
3146

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

A
Annie_wang 已提交
3149
```js
A
annie_wangli 已提交
3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161
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 已提交
3162
        });
A
annie_wangli 已提交
3163 3164 3165 3166 3167
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
3168 3169 3170 3171


### get

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

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

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

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

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3181
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3182
| 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 已提交
3183 3184


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

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

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

A
Annie_wang 已提交
3193
```js
A
annie_wangli 已提交
3194 3195 3196 3197 3198 3199 3200 3201
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 已提交
3202
        }).catch((err) => {
A
annie_wangli 已提交
3203
            console.log("get err: " + JSON.stringify(err));
A
annie_wangli 已提交
3204
        });
A
annie_wangli 已提交
3205 3206 3207 3208 3209 3210 3211
    }).catch((err) => {
        console.log("put err: " + JSON.stringify(err));
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
3212 3213 3214

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

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

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

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

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

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

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

A
Annie_wang 已提交
3230
```js
A
annie_wangli 已提交
3231 3232 3233 3234 3235 3236 3237 3238 3239 3240
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 已提交
3241 3242
            }
        }
A
annie_wangli 已提交
3243
        entries.push(entry);
A
annie_wangli 已提交
3244
    }
A
annie_wangli 已提交
3245 3246
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
A
Annie_wang 已提交
3247
        kvStore.getEntries('batch_test_number_key', function (err,entrys) {
A
annie_wangli 已提交
3248 3249 3250 3251 3252 3253 3254 3255 3256
            console.log('getEntries success');
            console.log('entrys.length: ' + entrys.length);
            console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
        });
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
3257 3258 3259 3260


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

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

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

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

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

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

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

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

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

A
Annie_wang 已提交
3281
```js
A
annie_wangli 已提交
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291
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 已提交
3292 3293
            }
        }
A
annie_wangli 已提交
3294 3295 3296 3297 3298
        entries.push(entry);
    }
    console.log('entries: ' + entries);
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
A
Annie_wang 已提交
3299
        kvStore.getEntries('batch_test_string_key').then((entrys) => {
A
annie_wangli 已提交
3300 3301 3302 3303 3304
            console.log('getEntries success');
            console.log('entrys.length: ' + entrys.length);
            console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
            console.log('entrys[0].value: ' + JSON.stringify(entrys[0].value));
            console.log('entrys[0].value.value: ' + entrys[0].value.value);
A
annie_wangli 已提交
3305
        }).catch((err) => {
A
annie_wangli 已提交
3306
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
3307
        });
A
annie_wangli 已提交
3308 3309 3310 3311 3312 3313 3314
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
3315 3316 3317 3318


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

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

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

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

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

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

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

A
Annie_wang 已提交
3334
```js
A
annie_wangli 已提交
3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345
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 已提交
3346 3347
            }
        }
A
annie_wangli 已提交
3348
        entries.push(entry);
A
annie_wangli 已提交
3349
    }
A
annie_wangli 已提交
3350 3351 3352 3353 3354
    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");
A
Annie_wang 已提交
3355
        kvStore.getEntries(query, function (err,entrys) {
A
annie_wangli 已提交
3356 3357 3358 3359 3360 3361 3362 3363 3364 3365
            console.log('getEntries success');
            console.log('entrys.length: ' + entrys.length);
            console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
        });
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
3366 3367 3368 3369


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

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

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

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

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

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

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

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

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

A
Annie_wang 已提交
3390
```js
A
Annie_wang 已提交
3391
let kvStore;
A
annie_wangli 已提交
3392 3393 3394 3395 3396 3397 3398 3399 3400 3401
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 已提交
3402 3403
            }
        }
A
annie_wangli 已提交
3404 3405 3406 3407 3408 3409 3410
        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");
A
Annie_wang 已提交
3411
        kvStore.getEntries(query).then((entrys) => {
A
annie_wangli 已提交
3412
            console.log('getEntries success');
A
annie_wangli 已提交
3413
        }).catch((err) => {
A
annie_wangli 已提交
3414
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
3415
        });
A
annie_wangli 已提交
3416 3417 3418 3419 3420 3421 3422 3423
    }).catch((err) => {
        console.log('GetEntries putBatch fail ' + JSON.stringify(err))
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
3424 3425


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

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

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

A
annie_wangli 已提交
3432
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3433

A
annie_wangli 已提交
3434 3435 3436
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3437
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3438
| keyPrefix  |string   | Yes   |Key prefix to match.|
A
Annie_wang 已提交
3439
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt;   | Yes   |Callback invoked to return the result set obtained.|
A
annie_wangli 已提交
3440

A
annie_wangli 已提交
3441
**Example**
A
annie_wangli 已提交
3442

A
Annie_wang 已提交
3443
```js
A
annie_wangli 已提交
3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454
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 已提交
3455 3456
            }
        }
A
annie_wangli 已提交
3457
        entries.push(entry);
A
annie_wangli 已提交
3458
    }
A
annie_wangli 已提交
3459 3460
    kvStore.putBatch(entries, async function (err, data) {
        console.log('GetResultSet putBatch success');
A
Annie_wang 已提交
3461
        kvStore.getResultSet('batch_test_string_key', async function (err, result) {
A
Annie_wang 已提交
3462
            console.log('GetResultSet getResultSet succeed.');
A
annie_wangli 已提交
3463 3464 3465 3466 3467 3468 3469 3470 3471 3472
            resultSet = result;
            kvStore.closeResultSet(resultSet, function (err, data) {
                console.log('GetResultSet closeResultSet success');
            })
        });
    });
}catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
3473 3474 3475 3476


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3477
getResultSet(keyPrefix: string): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
3478

A
Annie_wang 已提交
3479
Obtains the result set with the specified prefix. This API uses a promise to return the result.
A
annie_wangli 已提交
3480

A
annie_wangli 已提交
3481
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3482

A
annie_wangli 已提交
3483 3484 3485
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3486
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3487
| keyPrefix  |string   | Yes   |Key prefix to match.|
A
annie_wangli 已提交
3488

A
annie_wangli 已提交
3489
**Return value**
A
annie_wangli 已提交
3490

A
annie_wangli 已提交
3491
| Type   | Description      |
A
annie_wangli 已提交
3492
| ------  | -------   |
Z
zengyawen 已提交
3493
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; |Promise used to return the result set obtained.|
A
annie_wangli 已提交
3494

A
annie_wangli 已提交
3495
**Example**
A
annie_wangli 已提交
3496

A
Annie_wang 已提交
3497
```js
A
annie_wangli 已提交
3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508
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 已提交
3509 3510
            }
        }
A
annie_wangli 已提交
3511
        entries.push(entry);
A
annie_wangli 已提交
3512
    }
A
annie_wangli 已提交
3513 3514 3515 3516 3517 3518
    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 已提交
3519
        console.log('GetResult getResultSet succeed.');
A
annie_wangli 已提交
3520 3521
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
3522
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
    });
    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 已提交
3533 3534 3535 3536


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3537
getResultSet(query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
3538

A
Annie_wang 已提交
3539
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3540

A
annie_wangli 已提交
3541
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3542

A
annie_wangli 已提交
3543 3544 3545
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3546
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3547
| query  |Query    | Yes   |**Query** object to match.            |
A
Annie_wang 已提交
3548
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt;   | Yes   |Callback invoked to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
3549

A
annie_wangli 已提交
3550
**Example**
A
annie_wangli 已提交
3551

A
Annie_wang 已提交
3552
```js
A
annie_wangli 已提交
3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563
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 已提交
3564 3565
            }
        }
A
annie_wangli 已提交
3566
        entries.push(entry);
A
annie_wangli 已提交
3567
    }
A
annie_wangli 已提交
3568 3569 3570 3571
    kvStore.putBatch(entries, async function (err, data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
A
Annie_wang 已提交
3572
        kvStore.getResultSet(query, async function (err, result) {
A
Annie_wang 已提交
3573
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
3574 3575 3576 3577 3578 3579 3580
            resultSet = result;
        });
    });
} catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
3581 3582 3583 3584


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3585
getResultSet(query: Query): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
3586

A
Annie_wang 已提交
3587
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
3588

A
annie_wangli 已提交
3589
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3590

A
annie_wangli 已提交
3591 3592 3593
**Parameters**

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

A
annie_wangli 已提交
3597
**Return value**
A
annie_wangli 已提交
3598

A
annie_wangli 已提交
3599
| Type   | Description      |
A
annie_wangli 已提交
3600
| ------  | -------   |
Z
zengyawen 已提交
3601
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
3602

A
annie_wangli 已提交
3603
**Example**
A
annie_wangli 已提交
3604

A
Annie_wang 已提交
3605
```js
A
annie_wangli 已提交
3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616
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 已提交
3617 3618
            }
        }
A
annie_wangli 已提交
3619
        entries.push(entry);
A
annie_wangli 已提交
3620
    }
A
annie_wangli 已提交
3621 3622 3623 3624 3625 3626 3627 3628
    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 已提交
3629
        console.log(' getResultSet succeed.');
A
annie_wangli 已提交
3630 3631
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
3632
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
3633 3634 3635 3636 3637
    });
}catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
3638

A
Annie_wang 已提交
3639 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
### 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 已提交
3714 3715
### closeResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3716
closeResultSet(resultSet: KvStoreResultSet, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
3717

A
Annie_wang 已提交
3718
Closes the **KvStoreResultSet** object returned by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3719

A
annie_wangli 已提交
3720
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3721

A
annie_wangli 已提交
3722 3723 3724
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3725
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3726
| resultSet  |[KvStoreResultSet](#kvstoreresultset8)   | Yes   |**KvStoreResultSet** object to close.            |
A
Annie_wang 已提交
3727
| callback  |AsyncCallback&lt;void&gt;   | Yes   |Callback used to return the result.            |
A
annie_wangli 已提交
3728

A
annie_wangli 已提交
3729
**Example**
A
annie_wangli 已提交
3730

A
Annie_wang 已提交
3731
```js
A
annie_wangli 已提交
3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745
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 已提交
3746 3747 3748 3749


### closeResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3750
closeResultSet(resultSet: KvStoreResultSet): Promise&lt;void&gt;
A
annie_wangli 已提交
3751

A
Annie_wang 已提交
3752
Closes the **KvStoreResultSet** object returned by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses a promise to return the result.
A
annie_wangli 已提交
3753

A
annie_wangli 已提交
3754
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3755

A
annie_wangli 已提交
3756 3757 3758
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3759
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3760
| resultSet  |[KvStoreResultSet](#kvstoreresultset8)   | Yes   |**KvStoreResultSet** object to close.            |
A
annie_wangli 已提交
3761

A
annie_wangli 已提交
3762
**Return value**
A
annie_wangli 已提交
3763

A
annie_wangli 已提交
3764
| Type   | Description      |
A
annie_wangli 已提交
3765
| ------  | -------   |
A
Annie_wang 已提交
3766
|Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
3767

A
annie_wangli 已提交
3768
**Example**
A
annie_wangli 已提交
3769

A
Annie_wang 已提交
3770
```js
A
annie_wangli 已提交
3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
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 已提交
3783 3784 3785 3786


### getResultSize<sup>8+</sup> ###

A
annie_wangli 已提交
3787 3788
getResultSize(query: Query, callback: AsyncCallback&lt;number&gt;): void

A
Annie_wang 已提交
3789
Obtains the number of results that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3790

A
annie_wangli 已提交
3791
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3792

A
annie_wangli 已提交
3793
**Parameters**
A
annie_wangli 已提交
3794

A
annie_wangli 已提交
3795
| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3796
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3797
| query  |[Query](#query8)   | Yes   |**Query** object to match.        |
A
Annie_wang 已提交
3798
| callback  |AsyncCallback&lt;number&gt;   | Yes   |Callback invoked to return the number of results obtained.        |
A
annie_wangli 已提交
3799

A
annie_wangli 已提交
3800
**Example**
A
annie_wangli 已提交
3801

A
Annie_wang 已提交
3802
```js
A
annie_wangli 已提交
3803 3804 3805 3806 3807 3808 3809 3810 3811 3812
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 已提交
3813 3814
            }
        }
A
annie_wangli 已提交
3815
        entries.push(entry);
A
annie_wangli 已提交
3816
    }
A
annie_wangli 已提交
3817 3818 3819 3820
    kvStore.putBatch(entries, async function (err, data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
A
Annie_wang 已提交
3821
        kvStore.getResultSize(query, async function (err, resultSize) {
A
Annie_wang 已提交
3822
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
3823 3824 3825 3826 3827 3828
        });
    });
} catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
3829 3830 3831 3832


### getResultSize<sup>8+</sup> ###

A
annie_wangli 已提交
3833 3834
getResultSize(query: Query): Promise&lt;number&gt;

A
Annie_wang 已提交
3835
Obtains the number of results that matches the specified **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
3836

A
annie_wangli 已提交
3837
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3838

A
annie_wangli 已提交
3839
**Parameters**
A
annie_wangli 已提交
3840

A
annie_wangli 已提交
3841
| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3842
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3843
| query  |[Query](#query8)   | Yes   |**Query** object to match.        |
A
annie_wangli 已提交
3844

A
annie_wangli 已提交
3845
**Return value**
A
annie_wangli 已提交
3846

A
annie_wangli 已提交
3847
| Type   | Description      |
A
annie_wangli 已提交
3848
| ------  | -------   |
A
annie_wangli 已提交
3849
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
3850

A
annie_wangli 已提交
3851
**Example**
A
annie_wangli 已提交
3852

A
Annie_wang 已提交
3853
```js
A
annie_wangli 已提交
3854 3855 3856 3857 3858 3859 3860 3861 3862 3863
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 已提交
3864 3865
            }
        }
A
annie_wangli 已提交
3866
        entries.push(entry);
A
annie_wangli 已提交
3867
    }
A
annie_wangli 已提交
3868 3869 3870 3871 3872 3873 3874 3875
    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 已提交
3876
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
3877
    }).catch((err) => {
A
Annie_wang 已提交
3878
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
3879 3880 3881 3882 3883
    });
}catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
3884 3885 3886 3887


### removeDeviceData<sup>8+</sup> ###

A
annie_wangli 已提交
3888
removeDeviceData(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
3889

A
Annie_wang 已提交
3890
Deletes data of a device. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3891

A
annie_wangli 已提交
3892
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3893

A
annie_wangli 已提交
3894 3895 3896
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3897
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3898 3899
| deviceId  |string   | Yes   |ID of the target device.      |
| callback  |AsyncCallback&lt;void&gt;   | Yes   |Callback used to return the result.     |
A
annie_wangli 已提交
3900

A
annie_wangli 已提交
3901
**Example**
A
annie_wangli 已提交
3902

A
Annie_wang 已提交
3903
```js
A
annie_wangli 已提交
3904 3905 3906 3907 3908 3909 3910
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 已提交
3911
        kvStore.removeDeviceData(deviceid, async function (err,data) {
A
annie_wangli 已提交
3912 3913 3914 3915
            if (err == undefined) {
                console.log('removeDeviceData success');
            } else {
                console.log('removeDeviceData fail');
A
Annie_wang 已提交
3916
                kvStore.get(KEY_TEST_STRING_ELEMENT, async function (err,data) {
A
annie_wangli 已提交
3917 3918 3919
                    console.log('RemoveDeviceData get success');
                });
            }
A
annie_wangli 已提交
3920
        });
A
annie_wangli 已提交
3921 3922 3923 3924 3925
    });
}catch(e) {
    console.log('RemoveDeviceData e ' + e);
}
```
A
annie_wangli 已提交
3926 3927 3928 3929


### removeDeviceData<sup>8+</sup> ###

A
annie_wangli 已提交
3930
removeDeviceData(deviceId: string): Promise&lt;void&gt;
A
annie_wangli 已提交
3931

A
Annie_wang 已提交
3932
Deletes data of a device. This API uses a promise to return the result.
A
annie_wangli 已提交
3933

A
annie_wangli 已提交
3934
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3935

A
annie_wangli 已提交
3936 3937 3938
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3939
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3940
| deviceId  |string   | Yes   |ID of the target device.      |
A
annie_wangli 已提交
3941

A
annie_wangli 已提交
3942
**Return value**
A
annie_wangli 已提交
3943

A
annie_wangli 已提交
3944
| Type   | Description      |
A
annie_wangli 已提交
3945
| ------  | -------   |
A
Annie_wang 已提交
3946
|Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
3947

A
annie_wangli 已提交
3948
**Example**
A
annie_wangli 已提交
3949

A
Annie_wang 已提交
3950
```js
A
annie_wangli 已提交
3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974
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 已提交
3975 3976


A
Annie_wang 已提交
3977
### on('syncComplete')<sup>8+</sup> ###
A
annie_wangli 已提交
3978

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

A
Annie_wang 已提交
3981
Subscribes to the data synchronization complete events.
A
annie_wangli 已提交
3982

A
annie_wangli 已提交
3983
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3984

A
annie_wangli 已提交
3985 3986 3987
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3988
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
3989 3990
| 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 已提交
3991

A
annie_wangli 已提交
3992
**Example**
A
annie_wangli 已提交
3993

A
Annie_wang 已提交
3994
```js
A
annie_wangli 已提交
3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010
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 已提交
4011 4012


A
Annie_wang 已提交
4013
### off('syncComplete')<sup>8+</sup> ###
A
annie_wangli 已提交
4014

A
annie_wangli 已提交
4015
off(event: 'syncComplete', syncCallback?: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
4016

A
Annie_wang 已提交
4017
Unsubscribes from the data synchronization complete events.
A
annie_wangli 已提交
4018

A
annie_wangli 已提交
4019
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4020

A
annie_wangli 已提交
4021 4022 4023
**Parameters**

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

A
annie_wangli 已提交
4028
**Example**
A
annie_wangli 已提交
4029

A
Annie_wang 已提交
4030
```js
A
annie_wangli 已提交
4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041
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 已提交
4042

A
Annie_wang 已提交
4043 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
### 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 已提交
4096 4097 4098 4099


sync(deviceIdList: string[], mode: SyncMode, allowedDelayMs?: number): void

A
Annie_wang 已提交
4100
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 已提交
4101

A
annie_wangli 已提交
4102
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
A
annie_wangli 已提交
4103

A
annie_wangli 已提交
4104
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4105

A
annie_wangli 已提交
4106 4107 4108
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4109
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
4110 4111
| 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 已提交
4112
| allowedDelayMs  |number   | No  |Allowed synchronization delay time, in ms.  |
A
annie_wangli 已提交
4113

A
annie_wangli 已提交
4114
**Example**
A
annie_wangli 已提交
4115

A
Annie_wang 已提交
4116
```js
A
annie_wangli 已提交
4117 4118 4119
let kvStore;
kvStore.sync('deviceIds', distributedData.SyncMode.PULL_ONLY, 1000);
```
A
annie_wangli 已提交
4120

A
Annie_wang 已提交
4121 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
### 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 已提交
4163 4164
### setSyncParam<sup>8+</sup> ###

A
annie_wangli 已提交
4165
setSyncParam(defaultAllowedDelayMs: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
4166

A
Annie_wang 已提交
4167
Sets the default delay allowed for KV store synchronization. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4168

A
annie_wangli 已提交
4169
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4170

A
annie_wangli 已提交
4171 4172 4173
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4174
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4175 4176
| 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 已提交
4177

A
annie_wangli 已提交
4178
**Example**
A
annie_wangli 已提交
4179

A
Annie_wang 已提交
4180
```js
A
annie_wangli 已提交
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190
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 已提交
4191 4192 4193 4194


### setSyncParam<sup>8+</sup> ###

A
annie_wangli 已提交
4195
setSyncParam(defaultAllowedDelayMs: number): Promise&lt;void&gt;
A
annie_wangli 已提交
4196

A
Annie_wang 已提交
4197
Sets the default delay allowed for KV store synchronization. This API uses a promise to return the result.
A
annie_wangli 已提交
4198

A
annie_wangli 已提交
4199
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4200

A
annie_wangli 已提交
4201 4202 4203
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4204
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4205
| defaultAllowedDelayMs  |number  | Yes   |Default delay allowed for database synchronization, in ms.   |
A
annie_wangli 已提交
4206 4207


A
annie_wangli 已提交
4208
**Return value**
A
annie_wangli 已提交
4209

A
annie_wangli 已提交
4210
| Type   | Description      |
A
annie_wangli 已提交
4211
| ------  | -------   |
A
Annie_wang 已提交
4212
|Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
4213

A
annie_wangli 已提交
4214
**Example**
A
annie_wangli 已提交
4215

A
Annie_wang 已提交
4216
```js
A
annie_wangli 已提交
4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228
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 已提交
4229 4230 4231 4232


### getSecurityLevel<sup>8+</sup> ###

A
annie_wangli 已提交
4233
getSecurityLevel(callback: AsyncCallback&lt;SecurityLevel&gt;): void
A
annie_wangli 已提交
4234

A
Annie_wang 已提交
4235
Obtains the security level of this KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4236

A
annie_wangli 已提交
4237
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4238

A
annie_wangli 已提交
4239 4240 4241
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4242
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
4243
| callback  |AsyncCallback&lt;[SecurityLevel](#securitylevel)&gt;  | Yes   |Callback invoked to return the security level obtained.   |
A
annie_wangli 已提交
4244

A
annie_wangli 已提交
4245
**Example**
A
annie_wangli 已提交
4246

A
Annie_wang 已提交
4247
```js
A
annie_wangli 已提交
4248 4249 4250 4251 4252 4253 4254 4255 4256
let kvStore;
try {
    kvStore.getSecurityLevel(function (err,data) {
        console.log('getSecurityLevel success');
    });
}catch(e) {
    console.log('GetSecurityLeve e ' + e);
}
```
A
annie_wangli 已提交
4257 4258 4259 4260


### getSecurityLevel<sup>8+</sup> ###

A
annie_wangli 已提交
4261
getSecurityLevel(): Promise&lt;SecurityLevel&gt;
A
annie_wangli 已提交
4262

A
Annie_wang 已提交
4263
Obtains the security level of this KV store. This API uses a promise to return the result.
A
annie_wangli 已提交
4264

A
annie_wangli 已提交
4265
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
4266

A
annie_wangli 已提交
4267 4268 4269
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
4270
| ------  | -------   |
A
Annie_wang 已提交
4271
|Promise&lt;[SecurityLevel](#securitylevel)&gt; |Promise used to return the security level obtained.|
A
annie_wangli 已提交
4272

A
annie_wangli 已提交
4273
**Example**
A
annie_wangli 已提交
4274

A
Annie_wang 已提交
4275
```js
A
annie_wangli 已提交
4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286
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 已提交
4287 4288 4289 4290


## DeviceKVStore<sup>8+</sup> ##

A
Annie_wang 已提交
4291 4292 4293 4294 4295 4296 4297
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 已提交
4298 4299 4300

### get<sup>8+</sup> ###

A
annie_wangli 已提交
4301
get(deviceId: string, key: string, callback: AsyncCallback&lt;boolean|string|number|Uint8Array&gt;): void
A
annie_wangli 已提交
4302

A
Annie_wang 已提交
4303
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 已提交
4304

A
Annie_wang 已提交
4305
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4306

A
annie_wangli 已提交
4307 4308 4309
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4310
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4311 4312
| deviceId  |string  | Yes   |ID of the target device.   |
| key       |string  | Yes   |Key to match.   |
A
annie_wangli 已提交
4313
| callback  |AsyncCallback&lt;boolean\|string\|number\|Uint8Array&gt;  | Yes   |Callback used to return the value obtained.   |
A
annie_wangli 已提交
4314

A
annie_wangli 已提交
4315
**Example**
A
annie_wangli 已提交
4316

A
Annie_wang 已提交
4317
```js
A
annie_wangli 已提交
4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331
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 已提交
4332 4333 4334 4335


### get<sup>8+</sup> ###

A
annie_wangli 已提交
4336
get(deviceId: string, key: string): Promise&lt;boolean|string|number|Uint8Array&gt;
A
annie_wangli 已提交
4337

A
Annie_wang 已提交
4338
Obtains a string value that matches the specified device ID and key. This API uses a promise to return the result.
A
annie_wangli 已提交
4339

A
annie_wangli 已提交
4340
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4341

A
annie_wangli 已提交
4342 4343 4344
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4345
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4346 4347
| deviceId  |string  | Yes   |ID of the target device.   |
| key       |string  | Yes   |Key to match.   |
A
annie_wangli 已提交
4348

A
annie_wangli 已提交
4349
**Return value**
A
annie_wangli 已提交
4350

A
annie_wangli 已提交
4351
| Type   | Description      |
A
annie_wangli 已提交
4352
| ------  | -------   |
A
Annie_wang 已提交
4353
|Promise&lt;boolean\|string\|number\|Uint8Array&gt; |Promise used to return the string value obtained.|
A
annie_wangli 已提交
4354

A
annie_wangli 已提交
4355
**Example**
A
annie_wangli 已提交
4356

A
Annie_wang 已提交
4357
```js
A
annie_wangli 已提交
4358 4359 4360 4361 4362 4363 4364 4365 4366 4367
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 已提交
4368
        });
A
annie_wangli 已提交
4369 4370 4371 4372 4373 4374 4375
    }).catch((error) => {
        console.log('put error' + error);
    });
} catch (e) {
    console.log('Get e ' + e);
}
```
A
annie_wangli 已提交
4376 4377 4378 4379


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

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

A
Annie_wang 已提交
4382
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 已提交
4383

A
annie_wangli 已提交
4384
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4385

A
annie_wangli 已提交
4386 4387 4388
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4389
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4390 4391 4392
| 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 已提交
4393

A
annie_wangli 已提交
4394
**Example**
A
annie_wangli 已提交
4395

A
Annie_wang 已提交
4396
```js
A
annie_wangli 已提交
4397 4398 4399 4400 4401 4402 4403 4404 4405 4406
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 已提交
4407 4408
            }
        }
A
annie_wangli 已提交
4409
        entries.push(entry);
A
annie_wangli 已提交
4410
    }
A
annie_wangli 已提交
4411 4412 4413
    console.log('entries: ' + entries);
    kvStore.putBatch(entries, async function (err,data) {
        console.log('putBatch success');
A
Annie_wang 已提交
4414
        kvStore.getEntries('localDeviceId', 'batch_test_string_key', function (err,entrys) {
A
annie_wangli 已提交
4415 4416 4417 4418 4419 4420 4421 4422 4423
            console.log('getEntries success');
            console.log('entrys.length: ' + entrys.length);
            console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
        });
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
4424 4425 4426 4427


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

A
annie_wangli 已提交
4428
getEntries(deviceId: string, keyPrefix: string): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
4429

A
Annie_wang 已提交
4430
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 已提交
4431

A
annie_wangli 已提交
4432
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4433

A
annie_wangli 已提交
4434 4435 4436
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4437
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4438 4439
| deviceId  |string  | Yes   |ID of the target device.   |
| keyPrefix |string  | Yes   |Key prefix to match.   |
A
annie_wangli 已提交
4440

A
annie_wangli 已提交
4441
**Return value**
A
annie_wangli 已提交
4442

A
annie_wangli 已提交
4443
| Type   | Description      |
A
annie_wangli 已提交
4444
| ------  | -------   |
A
annie_wangli 已提交
4445
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
4446

A
annie_wangli 已提交
4447
**Example**
A
annie_wangli 已提交
4448

A
Annie_wang 已提交
4449
```js
A
annie_wangli 已提交
4450 4451 4452 4453 4454 4455 4456 4457 4458 4459
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 已提交
4460 4461
            }
        }
A
annie_wangli 已提交
4462 4463 4464 4465 4466
        entries.push(entry);
    }
    console.log('entries: ' + entries);
    kvStore.putBatch(entries).then(async (err) => {
        console.log('putBatch success');
A
Annie_wang 已提交
4467
        kvStore.getEntries('localDeviceId', 'batch_test_string_key').then((entrys) => {
A
annie_wangli 已提交
4468 4469 4470 4471 4472
            console.log('getEntries success');
            console.log('entrys.length: ' + entrys.length);
            console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
            console.log('entrys[0].value: ' + JSON.stringify(entrys[0].value));
            console.log('entrys[0].value.value: ' + entrys[0].value.value);
A
annie_wangli 已提交
4473
        }).catch((err) => {
A
annie_wangli 已提交
4474
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
4475
        });
A
annie_wangli 已提交
4476 4477 4478 4479 4480 4481 4482
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
4483 4484 4485 4486


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

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

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

A
annie_wangli 已提交
4491
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4492

A
annie_wangli 已提交
4493 4494 4495
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4496
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4497 4498
| 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 已提交
4499

A
annie_wangli 已提交
4500
**Example**
A
annie_wangli 已提交
4501

A
Annie_wang 已提交
4502
```js
A
annie_wangli 已提交
4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513
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 已提交
4514 4515
            }
        }
A
annie_wangli 已提交
4516
        entries.push(entry);
A
annie_wangli 已提交
4517
    }
A
annie_wangli 已提交
4518 4519 4520 4521 4522 4523
    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');
A
Annie_wang 已提交
4524
        kvStore.getEntries(query, function (err,entrys) {
A
annie_wangli 已提交
4525 4526 4527 4528 4529 4530 4531 4532 4533 4534
            console.log('getEntries success');
            console.log('entrys.length: ' + entrys.length);
            console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
        });
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
4535 4536 4537 4538


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

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

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

A
annie_wangli 已提交
4543
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4544

A
annie_wangli 已提交
4545 4546 4547
**Parameters**

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

A
annie_wangli 已提交
4551
**Return value**
A
annie_wangli 已提交
4552

A
annie_wangli 已提交
4553
| Type   | Description      |
A
annie_wangli 已提交
4554
| ------  | -------   |
A
annie_wangli 已提交
4555
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
4556

A
annie_wangli 已提交
4557
**Example**
A
annie_wangli 已提交
4558

A
Annie_wang 已提交
4559
```js
A
annie_wangli 已提交
4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570
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 已提交
4571 4572
            }
        }
A
annie_wangli 已提交
4573 4574 4575 4576 4577 4578 4579
        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");
A
Annie_wang 已提交
4580
        kvStore.getEntries(query).then((entrys) => {
A
annie_wangli 已提交
4581
            console.log('getEntries success');
A
annie_wangli 已提交
4582
        }).catch((err) => {
A
annie_wangli 已提交
4583
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
4584
        });
A
annie_wangli 已提交
4585 4586 4587 4588 4589 4590 4591 4592
    }).catch((err) => {
        console.log('GetEntries putBatch fail ' + JSON.stringify(err))
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
4593 4594 4595 4596


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

A
annie_wangli 已提交
4597
getEntries(deviceId: string, query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
4598

A
Annie_wang 已提交
4599
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 已提交
4600

A
annie_wangli 已提交
4601
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4602

A
annie_wangli 已提交
4603 4604 4605
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4606
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4607 4608
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
Annie_wang 已提交
4609
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt;  | Yes   |Callback invoked to return the KV pairs obtained.   |
A
annie_wangli 已提交
4610

A
annie_wangli 已提交
4611
**Example**
A
annie_wangli 已提交
4612

A
Annie_wang 已提交
4613
```js
A
annie_wangli 已提交
4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624
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 已提交
4625 4626
            }
        }
A
annie_wangli 已提交
4627
        entries.push(entry);
A
annie_wangli 已提交
4628
    }
A
annie_wangli 已提交
4629 4630 4631 4632 4633 4634
    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");
A
Annie_wang 已提交
4635
        kvStore.getEntries('localDeviceId', query, function (err,entrys) {
A
annie_wangli 已提交
4636 4637 4638 4639 4640 4641 4642 4643 4644 4645
            console.log('getEntries success');
            console.log('entrys.length: ' + entrys.length);
            console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
        })
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
4646 4647 4648 4649


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

A
annie_wangli 已提交
4650
getEntries(deviceId: string, query: Query): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
4651

A
Annie_wang 已提交
4652
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 已提交
4653

A
annie_wangli 已提交
4654
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4655

A
annie_wangli 已提交
4656 4657 4658
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4659
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4660 4661
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4662

A
annie_wangli 已提交
4663
**Return value**
A
annie_wangli 已提交
4664

A
annie_wangli 已提交
4665
| Type   | Description      |
A
annie_wangli 已提交
4666
| ------  | -------   |
A
annie_wangli 已提交
4667
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
4668

A
annie_wangli 已提交
4669
**Example**
A
annie_wangli 已提交
4670

A
Annie_wang 已提交
4671
```js
A
annie_wangli 已提交
4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682
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 已提交
4683 4684
            }
        }
A
annie_wangli 已提交
4685 4686 4687 4688 4689 4690 4691 4692
        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");
A
Annie_wang 已提交
4693
        kvStore.getEntries('localDeviceId', query).then((entrys) => {
A
annie_wangli 已提交
4694
            console.log('getEntries success');
A
annie_wangli 已提交
4695
        }).catch((err) => {
A
annie_wangli 已提交
4696
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
4697
        });
A
annie_wangli 已提交
4698 4699 4700 4701 4702 4703 4704 4705
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
4706 4707


A
Annie_wang 已提交
4708
### getResultSet<sup>8+</sup><a name="devicekvstore_getresultset"></a> ###
A
annie_wangli 已提交
4709

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

A
Annie_wang 已提交
4712
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 已提交
4713

A
annie_wangli 已提交
4714
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4715

A
annie_wangli 已提交
4716 4717 4718
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4719
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4720 4721
| deviceId  |string  | Yes   |ID of the target device.   |
| keyPrefix |string  | Yes   |Key prefix to match.   |
A
Annie_wang 已提交
4722
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt;  | Yes |Callback invoked to return the **KvStoreResultSet** object obtained.   |
A
annie_wangli 已提交
4723

A
annie_wangli 已提交
4724
**Example**
A
annie_wangli 已提交
4725

A
Annie_wang 已提交
4726
```js
A
annie_wangli 已提交
4727 4728 4729 4730
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('localDeviceId', 'batch_test_string_key', async function (err, result) {
A
Annie_wang 已提交
4731
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
4732
        resultSet = result;
A
Annie_wang 已提交
4733
        kvStore.closeResultSet(resultSet, function (err, data) {
A
annie_wangli 已提交
4734 4735 4736 4737 4738 4739 4740
            console.log('closeResultSet success');
        })
    });
}catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
4741 4742 4743 4744


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
4745
getResultSet(deviceId: string, keyPrefix: string): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
4746

A
Annie_wang 已提交
4747
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 已提交
4748

A
annie_wangli 已提交
4749
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4750

A
annie_wangli 已提交
4751 4752 4753
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4754
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4755 4756
| deviceId  |string  | Yes   |ID of the target device.   |
| keyPrefix |string  | Yes   |Key prefix to match.   |
A
annie_wangli 已提交
4757

A
annie_wangli 已提交
4758
**Return value**
A
annie_wangli 已提交
4759

A
annie_wangli 已提交
4760
| Type   | Description      |
A
annie_wangli 已提交
4761
| ------  | -------   |
Z
zengyawen 已提交
4762
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4763

A
annie_wangli 已提交
4764
**Example**
A
annie_wangli 已提交
4765

A
Annie_wang 已提交
4766
```js
A
annie_wangli 已提交
4767 4768 4769 4770
let kvStore;
try {
    let resultSet;
    kvStore.getResultSet('localDeviceId', 'batch_test_string_key').then((result) => {
A
Annie_wang 已提交
4771
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
4772 4773
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
4774
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
4775 4776 4777 4778 4779 4780 4781 4782 4783 4784
    });
    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 已提交
4785 4786 4787 4788


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
4789
getResultSet(query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
4790

A
Annie_wang 已提交
4791
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4792

A
annie_wangli 已提交
4793
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4794

A
annie_wangli 已提交
4795 4796 4797
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4798
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4799
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
Annie_wang 已提交
4800
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt;  | Yes |Callback invoked to return the **KvStoreResultSet** object obtained.   |
A
annie_wangli 已提交
4801

A
annie_wangli 已提交
4802
**Example**
A
annie_wangli 已提交
4803

A
Annie_wang 已提交
4804
```js
A
annie_wangli 已提交
4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815
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 已提交
4816 4817
            }
        }
A
annie_wangli 已提交
4818
        entries.push(entry);
A
annie_wangli 已提交
4819
    }
A
annie_wangli 已提交
4820 4821 4822 4823 4824
    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 已提交
4825
        kvStore.getResultSet(query, async function (err, result) {
A
Annie_wang 已提交
4826
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
4827
            resultSet = result;
A
Annie_wang 已提交
4828
            kvStore.closeResultSet(resultSet, function (err, data) {
A
annie_wangli 已提交
4829 4830 4831 4832 4833 4834 4835 4836
                console.log('closeResultSet success');
            })
        });
    });
} catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
4837 4838 4839 4840


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
4841
getResultSet(query: Query): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
4842

A
Annie_wang 已提交
4843
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
4844

A
annie_wangli 已提交
4845
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4846

A
annie_wangli 已提交
4847 4848 4849
**Parameters**

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

A
annie_wangli 已提交
4853
**Return value**
A
annie_wangli 已提交
4854

A
annie_wangli 已提交
4855
| Type   | Description      |
A
annie_wangli 已提交
4856
| ------  | -------   |
Z
zengyawen 已提交
4857
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4858

A
annie_wangli 已提交
4859
**Example**
A
annie_wangli 已提交
4860

A
Annie_wang 已提交
4861
```js
A
annie_wangli 已提交
4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872
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 已提交
4873 4874
            }
        }
A
annie_wangli 已提交
4875
        entries.push(entry);
A
annie_wangli 已提交
4876
    }
A
annie_wangli 已提交
4877 4878 4879 4880 4881 4882 4883 4884 4885 4886
    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 已提交
4887
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
4888 4889
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
4890
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
4891 4892 4893 4894 4895 4896 4897 4898 4899 4900
    });
    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 已提交
4901 4902 4903 4904


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
4905
getResultSet(deviceId: string, query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
4906

A
Annie_wang 已提交
4907
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 已提交
4908

A
annie_wangli 已提交
4909
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4910

A
annie_wangli 已提交
4911 4912 4913
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4914
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4915 4916
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
Annie_wang 已提交
4917
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt;  | Yes |Callback invoked to return the **KvStoreResultSet** object obtained.   |
A
annie_wangli 已提交
4918

A
annie_wangli 已提交
4919
**Example**
A
annie_wangli 已提交
4920

A
Annie_wang 已提交
4921
```js
A
annie_wangli 已提交
4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932
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 已提交
4933 4934
            }
        }
A
annie_wangli 已提交
4935
        entries.push(entry);
A
annie_wangli 已提交
4936
    }
A
annie_wangli 已提交
4937 4938 4939 4940
    kvStore.putBatch(entries, async function (err, data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
A
Annie_wang 已提交
4941
        kvStore.getResultSet('localDeviceId', query, async function (err, result) {
A
Annie_wang 已提交
4942
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
4943
            resultSet = result;
A
Annie_wang 已提交
4944
            kvStore.closeResultSet(resultSet, function (err, data) {
A
annie_wangli 已提交
4945 4946 4947 4948 4949 4950 4951 4952
                console.log('closeResultSet success');
            })
        });
    });
} catch(e) {
    console.log('GetResultSet e ' + e);
}
```
A
annie_wangli 已提交
4953 4954 4955 4956


### getResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
4957
getResultSet(deviceId: string, query: Query): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
4958

A
Annie_wang 已提交
4959
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 已提交
4960

A
annie_wangli 已提交
4961
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4962

A
annie_wangli 已提交
4963 4964 4965
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4966
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4967 4968
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4969

A
annie_wangli 已提交
4970
**Return value**
A
annie_wangli 已提交
4971

A
annie_wangli 已提交
4972
| Type   | Description      |
A
annie_wangli 已提交
4973
| ------  | -------   |
Z
zengyawen 已提交
4974
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4975

A
annie_wangli 已提交
4976
**Example**
A
annie_wangli 已提交
4977

A
Annie_wang 已提交
4978
```js
A
annie_wangli 已提交
4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989
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 已提交
4990 4991
            }
        }
A
annie_wangli 已提交
4992
        entries.push(entry);
A
annie_wangli 已提交
4993
    }
A
annie_wangli 已提交
4994 4995 4996 4997 4998 4999 5000 5001
    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 已提交
5002
        console.log('GetResultSet getResultSet succeed.');
A
annie_wangli 已提交
5003 5004
        resultSet = result;
    }).catch((err) => {
A
Annie_wang 已提交
5005
        console.log('GetResultSet getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018
    });
    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 已提交
5019 5020 5021 5022


### closeResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
5023
closeResultSet(resultSet: KvStoreResultSet, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
5024

A
Annie_wang 已提交
5025
Closes the **KvStoreResultSet** object returned by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
5026

A
annie_wangli 已提交
5027
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5028

A
annie_wangli 已提交
5029 5030 5031
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5032
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5033 5034
| resultSet  |[KvStoreResultSet](#getresultset8)  | Yes   |**KvStoreResultSet** object to close.  |
| callback   |AsyncCallback&lt;void&gt;                 | Yes   |Callback used to return the result.   |
A
annie_wangli 已提交
5035

A
annie_wangli 已提交
5036
**Example**
A
annie_wangli 已提交
5037

A
Annie_wang 已提交
5038
```js
A
annie_wangli 已提交
5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053
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 已提交
5054 5055 5056 5057


### closeResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
5058
closeResultSet(resultSet: KvStoreResultSet): Promise&lt;void&gt;
A
annie_wangli 已提交
5059

A
Annie_wang 已提交
5060
Closes the **KvStoreResultSet** object returned by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses a promise to return the result.
A
annie_wangli 已提交
5061

A
annie_wangli 已提交
5062
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5063

A
annie_wangli 已提交
5064 5065 5066
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5067
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5068
| resultSet  |[KvStoreResultSet](#getresultset8)  | Yes   |**KvStoreResultSet** object to close.  |
A
annie_wangli 已提交
5069

A
annie_wangli 已提交
5070
**Return value**
A
annie_wangli 已提交
5071

A
annie_wangli 已提交
5072
| Type   | Description      |
A
annie_wangli 已提交
5073
| ------  | -------   |
A
Annie_wang 已提交
5074
|Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
5075

A
annie_wangli 已提交
5076
**Example**
A
annie_wangli 已提交
5077

A
Annie_wang 已提交
5078
```js
A
annie_wangli 已提交
5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091
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 已提交
5092 5093 5094 5095


### getResultSize<sup>8+</sup> ###

A
annie_wangli 已提交
5096
getResultSize(query: Query, callback: AsyncCallback&lt;number&gt;): void
A
annie_wangli 已提交
5097

A
Annie_wang 已提交
5098
Obtains the number of results that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
5099

A
annie_wangli 已提交
5100
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5101

A
annie_wangli 已提交
5102 5103 5104
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5105
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5106
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
A
Annie_wang 已提交
5107
| callback  |AsyncCallback&lt;number&gt;  | Yes   |Callback invoked to return the number of results obtained.   |
A
annie_wangli 已提交
5108

A
annie_wangli 已提交
5109
**Example**
A
annie_wangli 已提交
5110

A
Annie_wang 已提交
5111
```js
A
annie_wangli 已提交
5112 5113 5114 5115 5116 5117 5118 5119 5120 5121
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 已提交
5122 5123
            }
        }
A
annie_wangli 已提交
5124
        entries.push(entry);
A
annie_wangli 已提交
5125
    }
A
annie_wangli 已提交
5126 5127 5128 5129 5130
    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 已提交
5131
        kvStore.getResultSize(query, async function (err, resultSize) {
A
Annie_wang 已提交
5132
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
5133 5134 5135 5136 5137 5138
        });
    });
} catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
5139 5140 5141 5142


### getResultSize<sup>8+</sup> ###

A
annie_wangli 已提交
5143
getResultSize(query: Query): Promise&lt;number&gt;
A
annie_wangli 已提交
5144

A
Annie_wang 已提交
5145
Obtains the number of results that matches the specified **Query** object. This API uses a promise to return the result.
A
annie_wangli 已提交
5146

A
annie_wangli 已提交
5147
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5148

A
annie_wangli 已提交
5149 5150 5151
**Parameters**

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

A
annie_wangli 已提交
5155
**Return value**
A
annie_wangli 已提交
5156

A
annie_wangli 已提交
5157
| Type   | Description      |
A
annie_wangli 已提交
5158
| ------  | -------   |
A
annie_wangli 已提交
5159
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
5160

A
annie_wangli 已提交
5161
**Example**
A
annie_wangli 已提交
5162

A
Annie_wang 已提交
5163
```js
A
annie_wangli 已提交
5164 5165 5166 5167 5168 5169 5170 5171 5172 5173
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 已提交
5174 5175
            }
        }
A
annie_wangli 已提交
5176
        entries.push(entry);
A
annie_wangli 已提交
5177
    }
A
annie_wangli 已提交
5178 5179 5180 5181 5182 5183 5184 5185 5186
    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 已提交
5187
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
5188
    }).catch((err) => {
A
Annie_wang 已提交
5189
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
5190 5191 5192 5193 5194
    });
}catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
5195 5196 5197 5198 5199 5200


### getResultSize<sup>8+</sup> ###

getResultSize(deviceId: string, query: Query, callback: AsyncCallback&lt;number&gt;): void;

A
Annie_wang 已提交
5201
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 已提交
5202

A
annie_wangli 已提交
5203
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5204

A
annie_wangli 已提交
5205 5206 5207
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5208
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5209 5210
| deviceId  |string                       | Yes   |ID of the target device.   |
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
A
Annie_wang 已提交
5211
| callback  |AsyncCallback&lt;number&gt;  | Yes   |Callback invoked to return the number of results obtained.   |
A
annie_wangli 已提交
5212

A
annie_wangli 已提交
5213
**Example**
A
annie_wangli 已提交
5214

A
Annie_wang 已提交
5215
```js
A
annie_wangli 已提交
5216 5217 5218 5219 5220 5221 5222 5223 5224 5225
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 已提交
5226 5227
            }
        }
A
annie_wangli 已提交
5228
        entries.push(entry);
A
annie_wangli 已提交
5229
    }
A
annie_wangli 已提交
5230 5231 5232 5233
    kvStore.putBatch(entries, async function (err, data) {
        console.log('putBatch success');
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
A
Annie_wang 已提交
5234
        kvStore.getResultSize('localDeviceId', query, async function (err, resultSize) {
A
Annie_wang 已提交
5235
            console.log('getResultSet succeed.');
A
annie_wangli 已提交
5236 5237 5238 5239 5240 5241
        });
    });
} catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
5242 5243 5244 5245


### getResultSize<sup>8+</sup> ###

A
annie_wangli 已提交
5246
getResultSize(deviceId: string, query: Query): Promise&lt;number&gt;
A
annie_wangli 已提交
5247

A
Annie_wang 已提交
5248
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 已提交
5249

A
annie_wangli 已提交
5250
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5251

A
annie_wangli 已提交
5252 5253 5254
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5255
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5256 5257
| deviceId  |string                       | Yes   |ID of the target device.   |
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
5258

A
annie_wangli 已提交
5259
**Return value**
A
annie_wangli 已提交
5260

A
annie_wangli 已提交
5261
| Type   | Description      |
A
annie_wangli 已提交
5262
| ------  | -------   |
A
annie_wangli 已提交
5263
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
5264

A
annie_wangli 已提交
5265
**Example**
A
annie_wangli 已提交
5266

A
Annie_wang 已提交
5267
```js
A
annie_wangli 已提交
5268 5269 5270 5271 5272 5273 5274 5275 5276 5277
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 已提交
5278 5279
            }
        }
A
annie_wangli 已提交
5280
        entries.push(entry);
A
annie_wangli 已提交
5281
    }
A
annie_wangli 已提交
5282 5283 5284 5285 5286 5287 5288 5289
    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 已提交
5290
        console.log('getResultSet succeed.');
A
annie_wangli 已提交
5291
    }).catch((err) => {
A
Annie_wang 已提交
5292
        console.log('getResultSet failed: ' + JSON.stringify(err));
A
annie_wangli 已提交
5293 5294 5295 5296 5297
    });
}catch(e) {
    console.log('GetResultSize e ' + e);
}
```
A
annie_wangli 已提交
5298 5299 5300 5301


### removeDeviceData<sup>8+</sup> ###

A
annie_wangli 已提交
5302
removeDeviceData(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
5303

A
Annie_wang 已提交
5304
Deletes data of the specified device from this KV store. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
5305

A
annie_wangli 已提交
5306
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5307

A
annie_wangli 已提交
5308 5309 5310
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5311
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5312 5313
| deviceId  |string                       | Yes   |ID of the target device. |
| callback  |AsyncCallback&lt;void&gt;    | Yes   |Callback used to return the result.   |
A
annie_wangli 已提交
5314

A
annie_wangli 已提交
5315
**Example**
A
annie_wangli 已提交
5316

A
Annie_wang 已提交
5317
```js
A
annie_wangli 已提交
5318 5319 5320 5321 5322 5323 5324
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 已提交
5325
        kvStore.removeDeviceData(deviceid, async function (err,data) {
A
annie_wangli 已提交
5326 5327 5328 5329
            if (err == undefined) {
                console.log('removeDeviceData success');
            } else {
                console.log('removeDeviceData fail');
A
Annie_wang 已提交
5330
                kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT, async function (err,data) {
A
annie_wangli 已提交
5331 5332 5333
                    console.log('RemoveDeviceData get success');
                });
            }
A
annie_wangli 已提交
5334
        });
A
annie_wangli 已提交
5335 5336 5337 5338 5339
    });
}catch(e) {
    console.log('RemoveDeviceData e ' + e);
}
```
A
annie_wangli 已提交
5340 5341 5342 5343


### removeDeviceData<sup>8+</sup> ###

A
annie_wangli 已提交
5344
removeDeviceData(deviceId: string): Promise&lt;void&gt;
A
annie_wangli 已提交
5345

A
Annie_wang 已提交
5346
Deletes data of the specified device from this KV store. This API uses a promise to return the result.
A
annie_wangli 已提交
5347

A
annie_wangli 已提交
5348
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
5349

A
annie_wangli 已提交
5350 5351 5352
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5353
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5354
| deviceId  |string  | Yes   |ID of the target device. |
A
annie_wangli 已提交
5355

A
annie_wangli 已提交
5356
**Return value**
A
annie_wangli 已提交
5357

A
annie_wangli 已提交
5358
| Type   | Description      |
A
annie_wangli 已提交
5359
| ------  | -------   |
A
Annie_wang 已提交
5360
|Promise&lt;void&gt; |Promise that returns no value.|
A
annie_wangli 已提交
5361

A
annie_wangli 已提交
5362
**Example**
A
annie_wangli 已提交
5363

A
Annie_wang 已提交
5364
```js
A
annie_wangli 已提交
5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388
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 已提交
5389 5390 5391 5392


### sync<sup>8+</sup> ###

A
annie_wangli 已提交
5393
sync(deviceIdList: string[], mode: SyncMode, allowedDelayMs?: number): void
A
annie_wangli 已提交
5394

A
Annie_wang 已提交
5395
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 已提交
5396

A
annie_wangli 已提交
5397
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
A
annie_wangli 已提交
5398

A
annie_wangli 已提交
5399
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
5400

A
annie_wangli 已提交
5401 5402 5403
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5404
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5405
| deviceIdList    |string[]               | Yes   |IDs of the devices to be synchronized.|
A
Annie_wang 已提交
5406
| mode            |[SyncMode](#syncmode)  | Yes   |Synchronization mode. |
A
annie_wangli 已提交
5407
| allowedDelayMs  |number                 | No   |Allowed synchronization delay time, in ms. |
A
annie_wangli 已提交
5408

A
annie_wangli 已提交
5409
**Example**
A
annie_wangli 已提交
5410

A
Annie_wang 已提交
5411
```js
A
annie_wangli 已提交
5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428
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 已提交
5429

A
Annie_wang 已提交
5430 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
### 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 已提交
5472
### on('syncComplete')<sup>8+</sup> ###
A
annie_wangli 已提交
5473

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

A
Annie_wang 已提交
5476
Subscribes to the data synchronization complete events.
A
annie_wangli 已提交
5477

A
annie_wangli 已提交
5478
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
5479

A
annie_wangli 已提交
5480 5481 5482
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5483
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
5484
| event    |string      | Yes   |Event to subscribe to. The value is **syncComplete**, which indicates the data synchronization complete event.|
A
Annie_wang 已提交
5485
| syncCallback            |Callback<Array&lt;[string, number]&gt;> | Yes   |Callback used to return the synchronization result. |
A
annie_wangli 已提交
5486

A
annie_wangli 已提交
5487
**Example**
A
annie_wangli 已提交
5488

A
Annie_wang 已提交
5489
```js
A
Annie_wang 已提交
5490
let kvStore;
A
annie_wangli 已提交
5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505
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 已提交
5506 5507


A
Annie_wang 已提交
5508
### off('syncComplete')<sup>8+</sup> ###
A
annie_wangli 已提交
5509

A
annie_wangli 已提交
5510
off(event: 'syncComplete', syncCallback?: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
5511

A
Annie_wang 已提交
5512
Unsubscribes from the synchronization complete events. This API uses a synchronous callback to return the result.
A
annie_wangli 已提交
5513

A
annie_wangli 已提交
5514
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
5515

A
annie_wangli 已提交
5516 5517 5518
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5519
| -----  | ------   | ----  | ----------------------- |
A
Annie_wang 已提交
5520
| event         |string                           | Yes   |Event to unsubscribe from. The value is **syncComplete**, which indicates the data synchronization complete event.|
A
annie_wangli 已提交
5521
| syncCallback  |Callback<Array&lt;[string, number]&gt;&gt; | No   |Callback used to return the synchronization result. |
A
annie_wangli 已提交
5522

A
annie_wangli 已提交
5523
**Example**
A
annie_wangli 已提交
5524

A
Annie_wang 已提交
5525
```js
A
annie_wangli 已提交
5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536
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 已提交
5537

A
Annie_wang 已提交
5538 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
### 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 已提交
5590 5591 5592

## SyncMode

A
Annie_wang 已提交
5593
Enumerates the synchronization modes.
Z
zengyawen 已提交
5594

A
annie_wangli 已提交
5595 5596
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
Annie_wang 已提交
5597
| Name      | Value    | Description                   |
A
annie_wangli 已提交
5598 5599 5600 5601
| -----      | ------    | ----------------------- |
| 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.|