1e41f4b71Sopenharmony_ci # @ohos.data.distributedKVStore (Distributed KV Store) (System API)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **distributedKVStore** module implements collaboration between databases for different devices that forms a Super Device. You can use the APIs provided by this module to save application data to a distributed key-value (KV) store and perform operations, such as adding, deleting, modifying, querying, and synchronizing data in distributed KV stores.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciThe **distributedKVStore** module provides the following functionalities:
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci- [KVManager](js-apis-distributedKVStore.md#kvmanager): provides a **KVManager** instance to obtain KV store information.
8e41f4b71Sopenharmony_ci- [KVStoreResultSet](js-apis-distributedKVStore.md#kvstoreresultset): provides APIs for accessing the results obtained from a KV store.
9e41f4b71Sopenharmony_ci- [Query](js-apis-distributedKVStore.md#query): provides APIs for setting predicates for data query.
10e41f4b71Sopenharmony_ci- [SingleKVStore](#singlekvstore): provides APIs for querying and synchronizing data in single KV stores. The single KV stores manage data without distinguishing devices.
11e41f4b71Sopenharmony_ci- [DeviceKVStore](#devicekvstore): provides APIs for querying and synchronizing data in device KV stores. This class inherits from [SingleKVStore](#singlekvstore). The device KV stores manage data by device.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci> **NOTE**
14e41f4b71Sopenharmony_ci>
15e41f4b71Sopenharmony_ci> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
16e41f4b71Sopenharmony_ci>
17e41f4b71Sopenharmony_ci> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.data.distributedKVStore](js-apis-distributedKVStore.md).
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci## Modules to Import
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci```ts
22e41f4b71Sopenharmony_ciimport { distributedKVStore } from '@kit.ArkData';
23e41f4b71Sopenharmony_ci```
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci## SingleKVStore
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ciImplements data management in a single KV store, such as adding data, deleting data, and subscribing to data changes or data sync completion.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ciBefore calling **SingleKVStore** APIs, you need to use [getKVStore](js-apis-distributedKVStore.md#getkvstore) to create a **SingleKVStore** instance.
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci### putBatch
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ciputBatch(value: Array<ValuesBucket>, callback: AsyncCallback<void>): void
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ciWrites batch data to this single KV store. This API uses an asynchronous callback to return the result.
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci**System API**: This is a system API.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci**Parameters**
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci| Name  | Type                                                    | Mandatory| Description              |
46e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------ |
47e41f4b71Sopenharmony_ci| value    | Array<[ValuesBucket](js-apis-data-valuesBucket.md#valuesbucket)> | Yes  | Data to write.|
48e41f4b71Sopenharmony_ci| callback | AsyncCallback<void>                                     | Yes  | Callback used to return the result.        |
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci**Error codes**
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md).
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci| ID| **Error Message**                            |
55e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- |
56e41f4b71Sopenharmony_ci| 401          | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.|
57e41f4b71Sopenharmony_ci| 202          | Permission verification failed, application which is not a system application uses system API.|
58e41f4b71Sopenharmony_ci| 15100003     | Database corrupted.                      |
59e41f4b71Sopenharmony_ci| 15100005     | Database or result set already closed.   |
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md).
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci| ID| **Error Message**                                |
64e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- |
65e41f4b71Sopenharmony_ci| 14800047     | The WAL file size exceeds the default limit. |
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci**Example**
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci```ts
70e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_citry {
73e41f4b71Sopenharmony_ci  let v8Arr: distributedKVStore.Entry[] = [];
74e41f4b71Sopenharmony_ci  let arr = new Uint8Array([4, 5, 6, 7]);
75e41f4b71Sopenharmony_ci  let vb1: distributedKVStore.Entry = { key: "name_1", value: {type: distributedKVStore.ValueType.INTEGER, value: 32} }
76e41f4b71Sopenharmony_ci  let vb2: distributedKVStore.Entry = { key: "name_2", value: {type: distributedKVStore.ValueType.BYTE_ARRAY, value: arr} };
77e41f4b71Sopenharmony_ci  let vb3: distributedKVStore.Entry = { key: "name_3", value: {type: distributedKVStore.ValueType.STRING, value: "lisi"} };
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci  v8Arr.push(vb1);
80e41f4b71Sopenharmony_ci  v8Arr.push(vb2);
81e41f4b71Sopenharmony_ci  v8Arr.push(vb3);
82e41f4b71Sopenharmony_ci  kvStore.putBatch(v8Arr, async (err: BusinessError) => {
83e41f4b71Sopenharmony_ci    if (err != undefined) {
84e41f4b71Sopenharmony_ci      console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`);
85e41f4b71Sopenharmony_ci      return;
86e41f4b71Sopenharmony_ci    }
87e41f4b71Sopenharmony_ci    console.info('Succeeded in putting batch');
88e41f4b71Sopenharmony_ci  })
89e41f4b71Sopenharmony_ci} catch (e) {
90e41f4b71Sopenharmony_ci  let error = e as BusinessError;
91e41f4b71Sopenharmony_ci  console.error(`Failed to put batch.code is ${error.code},message is ${error.message}`);
92e41f4b71Sopenharmony_ci}
93e41f4b71Sopenharmony_ci```
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci### putBatch
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ciputBatch(value: Array<ValuesBucket>): Promise<void>
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ciWrites batch data to this single KV store. This API uses a promise to return the result.
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**System API**: This is a system API.
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci**Parameters**
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci| Name| Type                                                    | Mandatory| Description              |
110e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | ------------------ |
111e41f4b71Sopenharmony_ci| value  | Array<[ValuesBucket](js-apis-data-valuesBucket.md#valuesbucket)> | Yes  | Data to write.|
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci**Return value**
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci| Type               | Description                     |
116e41f4b71Sopenharmony_ci| ------------------- | ------------------------- |
117e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.|
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci**Error codes**
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md).
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci| ID| **Error Message**                            |
124e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- |
125e41f4b71Sopenharmony_ci| 401          | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.|
126e41f4b71Sopenharmony_ci| 202          | Permission verification failed, application which is not a system application uses system API.|
127e41f4b71Sopenharmony_ci| 15100003     | Database corrupted.                      |
128e41f4b71Sopenharmony_ci| 15100005     | Database or result set already closed.   |
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md).
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci| ID| **Error Message**                                |
133e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- |
134e41f4b71Sopenharmony_ci| 14800047     | The WAL file size exceeds the default limit. |
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci**Example**
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci```ts
139e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_citry {
142e41f4b71Sopenharmony_ci  let v8Arr: distributedKVStore.Entry[] = [];
143e41f4b71Sopenharmony_ci  let arr = new Uint8Array([4, 5, 6, 7]);
144e41f4b71Sopenharmony_ci  let vb1: distributedKVStore.Entry = { key: "name_1", value: {type: distributedKVStore.ValueType.INTEGER, value: 32} }
145e41f4b71Sopenharmony_ci  let vb2: distributedKVStore.Entry = { key: "name_2", value: {type: distributedKVStore.ValueType.BYTE_ARRAY, value: arr} };
146e41f4b71Sopenharmony_ci  let vb3: distributedKVStore.Entry = { key: "name_3", value: {type: distributedKVStore.ValueType.STRING, value: "lisi"} };
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci  v8Arr.push(vb1);
149e41f4b71Sopenharmony_ci  v8Arr.push(vb2);
150e41f4b71Sopenharmony_ci  v8Arr.push(vb3);
151e41f4b71Sopenharmony_ci  kvStore.putBatch(v8Arr).then(async () => {
152e41f4b71Sopenharmony_ci    console.info(`Succeeded in putting patch`);
153e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
154e41f4b71Sopenharmony_ci    console.error(`putBatch fail.code is ${err.code},message is ${err.message}`);
155e41f4b71Sopenharmony_ci  });
156e41f4b71Sopenharmony_ci} catch (e) {
157e41f4b71Sopenharmony_ci  let error = e as BusinessError;
158e41f4b71Sopenharmony_ci  console.error(`putBatch fail.code is ${error.code},message is ${error.message}`);
159e41f4b71Sopenharmony_ci}
160e41f4b71Sopenharmony_ci```
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci### delete
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_cidelete(predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<void>)
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ciDeletes KV pairs from this KV store. This API uses an asynchronous callback to return the result.
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci**System API**: This is a system API.
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci**Parameters**
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci| Name    | Type                                                    | Mandatory| Description                                           |
177e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------- |
178e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes  | **DataSharePredicates** object that specifies the KV pairs to delete. If this parameter is **null**, define the processing logic.|
179e41f4b71Sopenharmony_ci| callback   | AsyncCallback<void>                                    | Yes  | Callback used to return the result.                                     |
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci**Error codes**
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md).
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci| ID| **Error Message**                          |
186e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- |
187e41f4b71Sopenharmony_ci| 401          | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.|
188e41f4b71Sopenharmony_ci| 202          | Permission verification failed, application which is not a system application uses system API.|
189e41f4b71Sopenharmony_ci| 15100003     | Database corrupted.                    |
190e41f4b71Sopenharmony_ci| 15100005    | Database or result set already closed. |
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md).
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci| ID| **Error Message**                                |
195e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- |
196e41f4b71Sopenharmony_ci| 14800047     | The WAL file size exceeds the default limit. |
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci**Example**
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci```ts
201e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
202e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_citry {
205e41f4b71Sopenharmony_ci  let predicates = new dataSharePredicates.DataSharePredicates();
206e41f4b71Sopenharmony_ci  let arr = ["name"];
207e41f4b71Sopenharmony_ci  predicates.inKeys(arr);
208e41f4b71Sopenharmony_ci  kvStore.put("name", "bob", (err:BusinessError) => {
209e41f4b71Sopenharmony_ci    if (err != undefined) {
210e41f4b71Sopenharmony_ci      console.error(`Failed to put.code is ${err.code},message is ${err.message}`);
211e41f4b71Sopenharmony_ci      return;
212e41f4b71Sopenharmony_ci    }
213e41f4b71Sopenharmony_ci    console.info("Succeeded in putting");
214e41f4b71Sopenharmony_ci    if (kvStore != null) {
215e41f4b71Sopenharmony_ci      kvStore.delete(predicates, (err:BusinessError) => {
216e41f4b71Sopenharmony_ci        if (err == undefined) {
217e41f4b71Sopenharmony_ci          console.info('Succeeded in deleting');
218e41f4b71Sopenharmony_ci        } else {
219e41f4b71Sopenharmony_ci          console.error(`Failed to delete.code is ${err.code},message is ${err.message}`);
220e41f4b71Sopenharmony_ci        }
221e41f4b71Sopenharmony_ci      });
222e41f4b71Sopenharmony_ci    }
223e41f4b71Sopenharmony_ci  });
224e41f4b71Sopenharmony_ci} catch (e) {
225e41f4b71Sopenharmony_ci  let error = e as BusinessError;
226e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`);
227e41f4b71Sopenharmony_ci}
228e41f4b71Sopenharmony_ci```
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci### delete
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_cidelete(predicates: dataSharePredicates.DataSharePredicates): Promise<void>
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ciDeletes KV pairs from this KV store. This API uses a promise to return the result.
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci**System API**: This is a system API.
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci**Parameters**
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ci| Name    | Type                                                    | Mandatory| Description                                           |
245e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------- |
246e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes  | **DataSharePredicates** object that specifies the KV pairs to delete. If this parameter is **null**, define the processing logic.|
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci**Return value**
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci| Type               | Description                     |
251e41f4b71Sopenharmony_ci| ------------------- | ------------------------- |
252e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.|
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci**Error codes**
255e41f4b71Sopenharmony_ci
256e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md).
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ci| ID| **Error Message**                            |
259e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------- |
260e41f4b71Sopenharmony_ci| 401          | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.|
261e41f4b71Sopenharmony_ci| 202          | Permission verification failed, application which is not a system application uses system API.|
262e41f4b71Sopenharmony_ci| 15100003     | Database corrupted.                      |
263e41f4b71Sopenharmony_ci| 15100005     | Database or result set already closed.   |
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ciFor details about the error codes, see [RDB Error Codes](errorcode-data-rdb.md).
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci| ID| **Error Message**                                |
268e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------- |
269e41f4b71Sopenharmony_ci| 14800047     | The WAL file size exceeds the default limit. |
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci**Example**
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci```ts
274e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
275e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
276e41f4b71Sopenharmony_ci
277e41f4b71Sopenharmony_citry {
278e41f4b71Sopenharmony_ci  let predicates = new dataSharePredicates.DataSharePredicates();
279e41f4b71Sopenharmony_ci  let arr = ["name"];
280e41f4b71Sopenharmony_ci  predicates.inKeys(arr);
281e41f4b71Sopenharmony_ci  kvStore.put("name", "bob").then(() => {
282e41f4b71Sopenharmony_ci    console.info(`Succeeded in putting data`);
283e41f4b71Sopenharmony_ci    if (kvStore != null) {
284e41f4b71Sopenharmony_ci      kvStore.delete(predicates).then(() => {
285e41f4b71Sopenharmony_ci        console.info('Succeeded in deleting');
286e41f4b71Sopenharmony_ci      }).catch((err: BusinessError) => {
287e41f4b71Sopenharmony_ci        console.error(`Failed to delete.code is ${err.code},message is ${err.message}`);
288e41f4b71Sopenharmony_ci      });
289e41f4b71Sopenharmony_ci    }
290e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
291e41f4b71Sopenharmony_ci    console.error(`Failed to put.code is ${err.code},message is ${err.message}`);
292e41f4b71Sopenharmony_ci  });
293e41f4b71Sopenharmony_ci} catch (e) {
294e41f4b71Sopenharmony_ci  let error = e as BusinessError;
295e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`);
296e41f4b71Sopenharmony_ci}
297e41f4b71Sopenharmony_ci```
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci### getResultSet
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_cigetResultSet(predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<KVStoreResultSet>): void
302e41f4b71Sopenharmony_ci
303e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified conditions. This API uses an asynchronous callback to return the result.
304e41f4b71Sopenharmony_ci
305e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
306e41f4b71Sopenharmony_ci
307e41f4b71Sopenharmony_ci**System API**: This is a system API.
308e41f4b71Sopenharmony_ci
309e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
310e41f4b71Sopenharmony_ci
311e41f4b71Sopenharmony_ci**Parameters**
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ci| Name    | Type                                                    | Mandatory| Description                                                        |
314e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
315e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes  | **DataSharePredicates** object that specifies the **KVStoreResultSet** object to obtain. If this parameter is **null**, define the processing logic.             |
316e41f4b71Sopenharmony_ci| callback   | AsyncCallback<[KVStoreResultSet](js-apis-distributedKVStore.md#kvstoreresultset)>   | Yes  | Callback used to return the **KVStoreResultSet** object obtained.|
317e41f4b71Sopenharmony_ci
318e41f4b71Sopenharmony_ci**Error codes**
319e41f4b71Sopenharmony_ci
320e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md).
321e41f4b71Sopenharmony_ci
322e41f4b71Sopenharmony_ci| ID| **Error Message**                          |
323e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- |
324e41f4b71Sopenharmony_ci| 401          | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.|
325e41f4b71Sopenharmony_ci| 202          | Permission verification failed, application which is not a system application uses system API.|
326e41f4b71Sopenharmony_ci| 15100001     | Over max  limits.                      |
327e41f4b71Sopenharmony_ci| 15100003     | Database corrupted.                    |
328e41f4b71Sopenharmony_ci| 15100005     | Database or result set already closed. |
329e41f4b71Sopenharmony_ci
330e41f4b71Sopenharmony_ci**Example**
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ci```ts
333e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
334e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
335e41f4b71Sopenharmony_ci
336e41f4b71Sopenharmony_citry {
337e41f4b71Sopenharmony_ci  let resultSet: distributedKVStore.KVStoreResultSet;
338e41f4b71Sopenharmony_ci  let predicates = new dataSharePredicates.DataSharePredicates();
339e41f4b71Sopenharmony_ci  predicates.prefixKey("batch_test_string_key");
340e41f4b71Sopenharmony_ci  kvStore.getResultSet(predicates, async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => {
341e41f4b71Sopenharmony_ci    if (err != undefined) {
342e41f4b71Sopenharmony_ci      console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`);
343e41f4b71Sopenharmony_ci      return;
344e41f4b71Sopenharmony_ci    }
345e41f4b71Sopenharmony_ci    console.info('Succeeded in getting result set');
346e41f4b71Sopenharmony_ci    resultSet = result;
347e41f4b71Sopenharmony_ci    if (kvStore != null) {
348e41f4b71Sopenharmony_ci      kvStore.closeResultSet(resultSet, (err: BusinessError) => {
349e41f4b71Sopenharmony_ci        if (err != undefined) {
350e41f4b71Sopenharmony_ci          console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`);
351e41f4b71Sopenharmony_ci          return;
352e41f4b71Sopenharmony_ci        }
353e41f4b71Sopenharmony_ci        console.info('Succeeded in closing result set');
354e41f4b71Sopenharmony_ci      });
355e41f4b71Sopenharmony_ci    }
356e41f4b71Sopenharmony_ci  });
357e41f4b71Sopenharmony_ci} catch (e) {
358e41f4b71Sopenharmony_ci  let error = e as BusinessError;
359e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`);
360e41f4b71Sopenharmony_ci}
361e41f4b71Sopenharmony_ci```
362e41f4b71Sopenharmony_ci
363e41f4b71Sopenharmony_ci### getResultSet
364e41f4b71Sopenharmony_ci
365e41f4b71Sopenharmony_cigetResultSet(predicates: dataSharePredicates.DataSharePredicates): Promise<KVStoreResultSet>
366e41f4b71Sopenharmony_ci
367e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified conditions. This API uses a promise to return the result.
368e41f4b71Sopenharmony_ci
369e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
370e41f4b71Sopenharmony_ci
371e41f4b71Sopenharmony_ci**System API**: This is a system API.
372e41f4b71Sopenharmony_ci
373e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_ci**Parameters**
376e41f4b71Sopenharmony_ci
377e41f4b71Sopenharmony_ci| Name    | Type                                                    | Mandatory| Description                                           |
378e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------- |
379e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes  | **DataSharePredicates** object that specifies the KV pairs to delete. If this parameter is **null**, define the processing logic.|
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ci**Return value**
382e41f4b71Sopenharmony_ci
383e41f4b71Sopenharmony_ci| Type                                                | Description                     |
384e41f4b71Sopenharmony_ci| ---------------------------------------------------- | ------------------------- |
385e41f4b71Sopenharmony_ci| Promise<[KVStoreResultSet](js-apis-distributedKVStore.md#kvstoreresultset)> | Promise used to return the **KVStoreResultSet** object obtained.|
386e41f4b71Sopenharmony_ci
387e41f4b71Sopenharmony_ci**Error codes**
388e41f4b71Sopenharmony_ci
389e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md).
390e41f4b71Sopenharmony_ci
391e41f4b71Sopenharmony_ci| ID| **Error Message**                          |
392e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- |
393e41f4b71Sopenharmony_ci| 401          | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.|
394e41f4b71Sopenharmony_ci| 202          | Permission verification failed, application which is not a system application uses system API.|
395e41f4b71Sopenharmony_ci| 15100001     | Over max  limits.                      |
396e41f4b71Sopenharmony_ci| 15100003     | Database corrupted.                    |
397e41f4b71Sopenharmony_ci| 15100005     | Database or result set already closed. |
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci**Example**
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ci```ts
402e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
403e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
404e41f4b71Sopenharmony_ci
405e41f4b71Sopenharmony_citry {
406e41f4b71Sopenharmony_ci  let resultSet: distributedKVStore.KVStoreResultSet;
407e41f4b71Sopenharmony_ci  let predicates = new dataSharePredicates.DataSharePredicates();
408e41f4b71Sopenharmony_ci  predicates.prefixKey("batch_test_string_key");
409e41f4b71Sopenharmony_ci  kvStore.getResultSet(predicates).then((result: distributedKVStore.KVStoreResultSet) => {
410e41f4b71Sopenharmony_ci    console.info('Succeeded in getting result set');
411e41f4b71Sopenharmony_ci    resultSet = result;
412e41f4b71Sopenharmony_ci    if (kvStore != null) {
413e41f4b71Sopenharmony_ci      kvStore.closeResultSet(resultSet).then(() => {
414e41f4b71Sopenharmony_ci        console.info('Succeeded in closing result set');
415e41f4b71Sopenharmony_ci      }).catch((err: BusinessError) => {
416e41f4b71Sopenharmony_ci        console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`);
417e41f4b71Sopenharmony_ci      });
418e41f4b71Sopenharmony_ci    }
419e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
420e41f4b71Sopenharmony_ci    console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`);
421e41f4b71Sopenharmony_ci  });
422e41f4b71Sopenharmony_ci
423e41f4b71Sopenharmony_ci} catch (e) {
424e41f4b71Sopenharmony_ci  let error = e as BusinessError;
425e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`);
426e41f4b71Sopenharmony_ci}
427e41f4b71Sopenharmony_ci```
428e41f4b71Sopenharmony_ci
429e41f4b71Sopenharmony_ci## DeviceKVStore
430e41f4b71Sopenharmony_ci
431e41f4b71Sopenharmony_ciProvides APIs for querying and synchronizing data in a device KV store. This class inherits from **SingleKVStore**.
432e41f4b71Sopenharmony_ci
433e41f4b71Sopenharmony_ciData is distinguished by device in a device KV store. Each device can only write and modify its own data. Data of other devices is read-only and cannot be modified.
434e41f4b71Sopenharmony_ci
435e41f4b71Sopenharmony_ciFor example, a device KV store can be used to implement image sharing between devices. The images of other devices can be viewed, but not be modified or deleted.
436e41f4b71Sopenharmony_ci
437e41f4b71Sopenharmony_ciBefore calling **DeviceKVStore** APIs, you need to use [getKVStore](js-apis-distributedKVStore.md#getkvstore) to create a **DeviceKVStore** instance.
438e41f4b71Sopenharmony_ci
439e41f4b71Sopenharmony_ci### getResultSet
440e41f4b71Sopenharmony_ci
441e41f4b71Sopenharmony_cigetResultSet(predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<KVStoreResultSet>): void
442e41f4b71Sopenharmony_ci
443e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified conditions for this device. This API uses an asynchronous callback to return the result.
444e41f4b71Sopenharmony_ci
445e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
446e41f4b71Sopenharmony_ci
447e41f4b71Sopenharmony_ci**System API**: This is a system API.
448e41f4b71Sopenharmony_ci
449e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
450e41f4b71Sopenharmony_ci
451e41f4b71Sopenharmony_ci**Parameters**
452e41f4b71Sopenharmony_ci
453e41f4b71Sopenharmony_ci| Name    | Type                                                        | Mandatory| Description                                                        |
454e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
455e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes  | **DataSharePredicates** object that specifies the **KVStoreResultSet** object to obtain. If this parameter is **null**, define the processing logic.             |
456e41f4b71Sopenharmony_ci| callback   | AsyncCallback<[KVStoreResultSet](js-apis-distributedKVStore.md#kvstoreresultset)>   | Yes  | Callback used to return the **KVStoreResultSet** object obtained.|
457e41f4b71Sopenharmony_ci
458e41f4b71Sopenharmony_ci**Error codes**
459e41f4b71Sopenharmony_ci
460e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md).
461e41f4b71Sopenharmony_ci
462e41f4b71Sopenharmony_ci| ID| **Error Message**                          |
463e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- |
464e41f4b71Sopenharmony_ci| 401          | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.|
465e41f4b71Sopenharmony_ci| 202          | Permission verification failed, application which is not a system application uses system API.|
466e41f4b71Sopenharmony_ci| 15100001     | Over max  limits.                      |
467e41f4b71Sopenharmony_ci| 15100003     | Database corrupted.                    |
468e41f4b71Sopenharmony_ci| 15100005     | Database or result set already closed. |
469e41f4b71Sopenharmony_ci
470e41f4b71Sopenharmony_ci**Example**
471e41f4b71Sopenharmony_ci
472e41f4b71Sopenharmony_ci```ts
473e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
474e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
475e41f4b71Sopenharmony_ci
476e41f4b71Sopenharmony_citry {
477e41f4b71Sopenharmony_ci  let resultSet: distributedKVStore.KVStoreResultSet;
478e41f4b71Sopenharmony_ci  let predicates = new dataSharePredicates.DataSharePredicates();
479e41f4b71Sopenharmony_ci  predicates.prefixKey("batch_test_string_key");
480e41f4b71Sopenharmony_ci  kvStore.getResultSet(predicates, async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => {
481e41f4b71Sopenharmony_ci    if (err != undefined) {
482e41f4b71Sopenharmony_ci      console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`);
483e41f4b71Sopenharmony_ci      return;
484e41f4b71Sopenharmony_ci    }
485e41f4b71Sopenharmony_ci    console.info('Succeeded in getting result set');
486e41f4b71Sopenharmony_ci    resultSet = result;
487e41f4b71Sopenharmony_ci    if (kvStore != null) {
488e41f4b71Sopenharmony_ci      kvStore.closeResultSet(resultSet, (err: BusinessError) => {
489e41f4b71Sopenharmony_ci        if (err != undefined) {
490e41f4b71Sopenharmony_ci          console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`);
491e41f4b71Sopenharmony_ci          return;
492e41f4b71Sopenharmony_ci        }
493e41f4b71Sopenharmony_ci        console.info('Succeeded in closing result set');
494e41f4b71Sopenharmony_ci      })
495e41f4b71Sopenharmony_ci    }
496e41f4b71Sopenharmony_ci  });
497e41f4b71Sopenharmony_ci} catch (e) {
498e41f4b71Sopenharmony_ci  let error = e as BusinessError;
499e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`);
500e41f4b71Sopenharmony_ci}
501e41f4b71Sopenharmony_ci```
502e41f4b71Sopenharmony_ci
503e41f4b71Sopenharmony_ci### getResultSet
504e41f4b71Sopenharmony_ci
505e41f4b71Sopenharmony_cigetResultSet(predicates: dataSharePredicates.DataSharePredicates): Promise<KVStoreResultSet>
506e41f4b71Sopenharmony_ci
507e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified conditions for this device. This API uses a promise to return the result.
508e41f4b71Sopenharmony_ci
509e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
510e41f4b71Sopenharmony_ci
511e41f4b71Sopenharmony_ci**System API**: This is a system API.
512e41f4b71Sopenharmony_ci
513e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
514e41f4b71Sopenharmony_ci
515e41f4b71Sopenharmony_ci**Parameters**
516e41f4b71Sopenharmony_ci
517e41f4b71Sopenharmony_ci| Name    | Type                                                        | Mandatory| Description                                           |
518e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------- |
519e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes  | **DataSharePredicates** object that specifies the KV pairs to delete. If this parameter is **null**, define the processing logic.|
520e41f4b71Sopenharmony_ci
521e41f4b71Sopenharmony_ci**Return value**
522e41f4b71Sopenharmony_ci
523e41f4b71Sopenharmony_ci| Type                                                | Description                     |
524e41f4b71Sopenharmony_ci| ---------------------------------------------------- | ------------------------- |
525e41f4b71Sopenharmony_ci| Promise<[KVStoreResultSet](js-apis-distributedKVStore.md#kvstoreresultset)> | Promise used to return the **KVStoreResultSet** object obtained.|
526e41f4b71Sopenharmony_ci
527e41f4b71Sopenharmony_ci**Error codes**
528e41f4b71Sopenharmony_ci
529e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md).
530e41f4b71Sopenharmony_ci
531e41f4b71Sopenharmony_ci| ID| **Error Message**                          |
532e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- |
533e41f4b71Sopenharmony_ci| 401          | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.|
534e41f4b71Sopenharmony_ci| 202          | Permission verification failed, application which is not a system application uses system API.|
535e41f4b71Sopenharmony_ci| 15100001     | Over max  limits.                      |
536e41f4b71Sopenharmony_ci| 15100003     | Database corrupted.                    |
537e41f4b71Sopenharmony_ci| 15100005     | Database or result set already closed. |
538e41f4b71Sopenharmony_ci
539e41f4b71Sopenharmony_ci**Example**
540e41f4b71Sopenharmony_ci
541e41f4b71Sopenharmony_ci```ts
542e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
543e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
544e41f4b71Sopenharmony_ci
545e41f4b71Sopenharmony_citry {
546e41f4b71Sopenharmony_ci  let resultSet: distributedKVStore.KVStoreResultSet;
547e41f4b71Sopenharmony_ci  let predicates = new dataSharePredicates.DataSharePredicates();
548e41f4b71Sopenharmony_ci  predicates.prefixKey("batch_test_string_key");
549e41f4b71Sopenharmony_ci  kvStore.getResultSet(predicates).then((result: distributedKVStore.KVStoreResultSet) => {
550e41f4b71Sopenharmony_ci    console.info('Succeeded in getting result set');
551e41f4b71Sopenharmony_ci    resultSet = result;
552e41f4b71Sopenharmony_ci    if (kvStore != null) {
553e41f4b71Sopenharmony_ci      kvStore.closeResultSet(resultSet).then(() => {
554e41f4b71Sopenharmony_ci        console.info('Succeeded in closing result set');
555e41f4b71Sopenharmony_ci      }).catch((err: BusinessError) => {
556e41f4b71Sopenharmony_ci        console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`);
557e41f4b71Sopenharmony_ci      });
558e41f4b71Sopenharmony_ci    }
559e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
560e41f4b71Sopenharmony_ci    console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`);
561e41f4b71Sopenharmony_ci  });
562e41f4b71Sopenharmony_ci} catch (e) {
563e41f4b71Sopenharmony_ci  let error = e as BusinessError;
564e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`);
565e41f4b71Sopenharmony_ci}
566e41f4b71Sopenharmony_ci```
567e41f4b71Sopenharmony_ci
568e41f4b71Sopenharmony_ci### getResultSet
569e41f4b71Sopenharmony_ci
570e41f4b71Sopenharmony_cigetResultSet(deviceId: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<KVStoreResultSet>): void
571e41f4b71Sopenharmony_ci
572e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified conditions for a device. This API uses an asynchronous callback to return the result.
573e41f4b71Sopenharmony_ci> **NOTE**
574e41f4b71Sopenharmony_ci>
575e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync).
576e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see the example of [sync](js-apis-distributedKVStore.md#sync).
577e41f4b71Sopenharmony_ci
578e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
579e41f4b71Sopenharmony_ci
580e41f4b71Sopenharmony_ci**System API**: This is a system API.
581e41f4b71Sopenharmony_ci
582e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
583e41f4b71Sopenharmony_ci
584e41f4b71Sopenharmony_ci**Parameters**
585e41f4b71Sopenharmony_ci
586e41f4b71Sopenharmony_ci| Name    | Type                                                    | Mandatory| Description                                                        |
587e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
588e41f4b71Sopenharmony_ci| deviceId  | string                                                       | Yes  | ID of the target device.                                    |
589e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes  | **DataSharePredicates** object that specifies the **KVStoreResultSet** object to obtain. If this parameter is **null**, define the processing logic.             |
590e41f4b71Sopenharmony_ci| callback   | AsyncCallback<[KVStoreResultSet](js-apis-distributedKVStore.md#kvstoreresultset)>   | Yes  | Callback used to return the **KVStoreResultSet** object obtained.|
591e41f4b71Sopenharmony_ci
592e41f4b71Sopenharmony_ci**Error codes**
593e41f4b71Sopenharmony_ci
594e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md).
595e41f4b71Sopenharmony_ci
596e41f4b71Sopenharmony_ci| ID| **Error Message**                          |
597e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- |
598e41f4b71Sopenharmony_ci| 401          | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.|
599e41f4b71Sopenharmony_ci| 202          | Permission verification failed, application which is not a system application uses system API.|
600e41f4b71Sopenharmony_ci| 15100001     | Over max  limits.                      |
601e41f4b71Sopenharmony_ci| 15100003     | Database corrupted.                    |
602e41f4b71Sopenharmony_ci| 15100005     | Database or result set already closed. |
603e41f4b71Sopenharmony_ci
604e41f4b71Sopenharmony_ci**Example**
605e41f4b71Sopenharmony_ci
606e41f4b71Sopenharmony_ci```ts
607e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
608e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
609e41f4b71Sopenharmony_ci
610e41f4b71Sopenharmony_citry {
611e41f4b71Sopenharmony_ci  let resultSet: distributedKVStore.KVStoreResultSet;
612e41f4b71Sopenharmony_ci  let predicates = new dataSharePredicates.DataSharePredicates();
613e41f4b71Sopenharmony_ci  predicates.prefixKey("batch_test_string_key");
614e41f4b71Sopenharmony_ci  kvStore.getResultSet('localDeviceId', predicates, async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => {
615e41f4b71Sopenharmony_ci    if (err != undefined) {
616e41f4b71Sopenharmony_ci      console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`);
617e41f4b71Sopenharmony_ci      return;
618e41f4b71Sopenharmony_ci    }
619e41f4b71Sopenharmony_ci    console.info('Succeeded in getting result set');
620e41f4b71Sopenharmony_ci    resultSet = result;
621e41f4b71Sopenharmony_ci    if (kvStore != null) {
622e41f4b71Sopenharmony_ci      kvStore.closeResultSet(resultSet, (err: BusinessError) => {
623e41f4b71Sopenharmony_ci        if (err != undefined) {
624e41f4b71Sopenharmony_ci          console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`);
625e41f4b71Sopenharmony_ci          return;
626e41f4b71Sopenharmony_ci        }
627e41f4b71Sopenharmony_ci        console.info('Succeeded in closing result set');
628e41f4b71Sopenharmony_ci      })
629e41f4b71Sopenharmony_ci    }
630e41f4b71Sopenharmony_ci  });
631e41f4b71Sopenharmony_ci} catch (e) {
632e41f4b71Sopenharmony_ci  let error = e as BusinessError;
633e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`);
634e41f4b71Sopenharmony_ci}
635e41f4b71Sopenharmony_ci```
636e41f4b71Sopenharmony_ci
637e41f4b71Sopenharmony_ci### getResultSet
638e41f4b71Sopenharmony_ci
639e41f4b71Sopenharmony_cigetResultSet(deviceId: string, predicates: dataSharePredicates.DataSharePredicates): Promise<KVStoreResultSet>
640e41f4b71Sopenharmony_ci
641e41f4b71Sopenharmony_ciObtains a **KVStoreResultSet** object that matches the specified conditions for a device. This API uses a promise to return the result.
642e41f4b71Sopenharmony_ci> **NOTE**
643e41f4b71Sopenharmony_ci>
644e41f4b71Sopenharmony_ci> **deviceId** can be obtained by [deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync).
645e41f4b71Sopenharmony_ci> For details about how to obtain **deviceId**, see the example of [sync](js-apis-distributedKVStore.md#sync).
646e41f4b71Sopenharmony_ci
647e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
648e41f4b71Sopenharmony_ci
649e41f4b71Sopenharmony_ci**System API**: This is a system API.
650e41f4b71Sopenharmony_ci
651e41f4b71Sopenharmony_ci**System capability**: SystemCapability.DistributedDataManager.DataShare.Provider
652e41f4b71Sopenharmony_ci
653e41f4b71Sopenharmony_ci**Parameters**
654e41f4b71Sopenharmony_ci
655e41f4b71Sopenharmony_ci| Name    | Type                                                    | Mandatory| Description                                           |
656e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------- |
657e41f4b71Sopenharmony_ci| deviceId  | string                                                       | Yes  | ID of the target device.                                    |
658e41f4b71Sopenharmony_ci| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes  | **DataSharePredicates** object that specifies the KV pairs to delete. If this parameter is **null**, define the processing logic.|
659e41f4b71Sopenharmony_ci
660e41f4b71Sopenharmony_ci**Return value**
661e41f4b71Sopenharmony_ci
662e41f4b71Sopenharmony_ci| Type                                                | Description                     |
663e41f4b71Sopenharmony_ci| ---------------------------------------------------- | ------------------------- |
664e41f4b71Sopenharmony_ci| Promise<[KVStoreResultSet](js-apis-distributedKVStore.md#kvstoreresultset)> | Promise used to return the **KVStoreResultSet** object obtained.|
665e41f4b71Sopenharmony_ci
666e41f4b71Sopenharmony_ci**Error codes**
667e41f4b71Sopenharmony_ci
668e41f4b71Sopenharmony_ciFor details about the error codes, see [Distributed KV Store Error Codes](errorcode-distributedKVStore.md).
669e41f4b71Sopenharmony_ci
670e41f4b71Sopenharmony_ci| ID| **Error Message**                          |
671e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- |
672e41f4b71Sopenharmony_ci| 401          | Parameter error.Possible causes:1.Mandatory parameters are left unspecified; 2.Incorrect parameters types.|
673e41f4b71Sopenharmony_ci| 202          | Permission verification failed, application which is not a system application uses system API.|
674e41f4b71Sopenharmony_ci| 15100001     | Over max  limits.                      |
675e41f4b71Sopenharmony_ci| 15100003     | Database corrupted.                    |
676e41f4b71Sopenharmony_ci| 15100005     | Database or result set already closed. |
677e41f4b71Sopenharmony_ci
678e41f4b71Sopenharmony_ci**Example**
679e41f4b71Sopenharmony_ci
680e41f4b71Sopenharmony_ci```ts
681e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
682e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
683e41f4b71Sopenharmony_ci
684e41f4b71Sopenharmony_citry {
685e41f4b71Sopenharmony_ci  let resultSet: distributedKVStore.KVStoreResultSet;
686e41f4b71Sopenharmony_ci  let predicates = new dataSharePredicates.DataSharePredicates();
687e41f4b71Sopenharmony_ci  predicates.prefixKey("batch_test_string_key");
688e41f4b71Sopenharmony_ci  kvStore.getResultSet('localDeviceId', predicates).then((result: distributedKVStore.KVStoreResultSet) => {
689e41f4b71Sopenharmony_ci    console.info('Succeeded in getting result set');
690e41f4b71Sopenharmony_ci    resultSet = result;
691e41f4b71Sopenharmony_ci    if (kvStore != null) {
692e41f4b71Sopenharmony_ci      kvStore.closeResultSet(resultSet).then(() => {
693e41f4b71Sopenharmony_ci        console.info('Succeeded in closing result set');
694e41f4b71Sopenharmony_ci      }).catch((err: BusinessError) => {
695e41f4b71Sopenharmony_ci        console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`);
696e41f4b71Sopenharmony_ci      });
697e41f4b71Sopenharmony_ci    }
698e41f4b71Sopenharmony_ci  }).catch((err: BusinessError) => {
699e41f4b71Sopenharmony_ci    console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`);
700e41f4b71Sopenharmony_ci  });
701e41f4b71Sopenharmony_ci} catch (e) {
702e41f4b71Sopenharmony_ci  let error = e as BusinessError;
703e41f4b71Sopenharmony_ci  console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`);
704e41f4b71Sopenharmony_ci}
705e41f4b71Sopenharmony_ci```
706