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