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](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。<br>从API version 10开始,context的参数类型为[BaseContext](../apis-ability-kit/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以下错误码的详细介绍请参见[分布式键值数据库错误码](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以下错误码的详细介绍请参见[分布式键值数据库错误码](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以下错误码的详细介绍请参见[分布式键值数据库错误码](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以下错误码的详细介绍请参见[分布式键值数据库错误码](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用于创建Schema实例的构造函数。 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](../apis-distributedservice-kit/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以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2254 2255| **错误码ID** | **错误信息** | 2256| ------------ | ---------------------------------------- | 2257| 15100003 | Database corrupted. | 2258| 15100005 | Database or result set already closed. | 2259 2260以下错误码的详细介绍请参见[关系型数据库错误码](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以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2311 2312| **错误码ID** | **错误信息** | 2313| ------------ | ---------------------------------------- | 2314| 15100003 | Database corrupted. | 2315| 15100005 | Database or result set already closed. | 2316 2317以下错误码的详细介绍请参见[关系型数据库错误码](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以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2360 2361| **错误码ID** | **错误信息** | 2362| ------------ | ---------------------------------------- | 2363| 15100003 | Database corrupted. | 2364| 15100005 | Database or result set already closed. | 2365 2366以下错误码的详细介绍请参见[关系型数据库错误码](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以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2439 2440| **错误码ID** | **错误信息** | 2441| ------------ | ---------------------------------------- | 2442| 15100003 | Database corrupted. | 2443| 15100005 | Database or result set already closed. | 2444 2445以下错误码的详细介绍请参见[关系型数据库错误码](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### delete 2490 2491delete(key: string, callback: AsyncCallback<void>): void 2492 2493从数据库中删除指定键值的数据,使用callback异步回调。 2494 2495**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 2496 2497**参数:** 2498 2499| 参数名 | 类型 | 必填 | 说明 | 2500| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 2501| key | string | 是 | 要删除数据的key,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 2502| callback | AsyncCallback<void> | 是 | 回调函数。 | 2503 2504**错误码:** 2505 2506以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2507 2508| **错误码ID** | **错误信息** | 2509| ------------ | -------------------------------------- | 2510| 15100003 | Database corrupted. | 2511| 15100005 | Database or result set already closed. | 2512 2513以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2514 2515| **错误码ID** | **错误信息** | 2516| ------------ | -------------------------------------------- | 2517| 14800047 | The WAL file size exceeds the default limit. | 2518 2519**示例:** 2520 2521```ts 2522import { BusinessError } from '@ohos.base'; 2523 2524const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 2525const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 2526try { 2527 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err: BusinessError) => { 2528 if (err != undefined) { 2529 console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 2530 return; 2531 } 2532 console.info('Succeeded in putting'); 2533 if (kvStore != null) { 2534 kvStore.delete(KEY_TEST_STRING_ELEMENT, (err: BusinessError) => { 2535 if (err != undefined) { 2536 console.error(`Failed to delete.code is ${err.code},message is ${err.message}`); 2537 return; 2538 } 2539 console.info('Succeeded in deleting'); 2540 }); 2541 } 2542 }); 2543} catch (e) { 2544 let error = e as BusinessError; 2545 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 2546} 2547``` 2548 2549### delete 2550 2551delete(key: string): Promise<void> 2552 2553从数据库中删除指定键值的数据,使用Promise异步回调。 2554 2555**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 2556 2557**参数:** 2558 2559| 参数名 | 类型 | 必填 | 说明 | 2560| ------ | -------- | ---- | ------------------------------------------------------------ | 2561| key | string | 是 | 要删除数据的key,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 2562 2563**返回值:** 2564 2565| 类型 | 说明 | 2566| ------------------- | ------------------------- | 2567| Promise<void> | 无返回结果的Promise对象。 | 2568 2569**错误码:** 2570 2571以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2572 2573| **错误码ID** | **错误信息** | 2574| ------------ | ---------------------------------------- | 2575| 15100003 | Database corrupted. | 2576| 15100005 | Database or result set already closed. | 2577 2578以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2579 2580| **错误码ID** | **错误信息** | 2581| ------------ | -------------------------------------------- | 2582| 14800047 | The WAL file size exceeds the default limit. | 2583 2584**示例:** 2585 2586```ts 2587import { BusinessError } from '@ohos.base'; 2588 2589const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 2590const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 2591try { 2592 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(() => { 2593 console.info(`Succeeded in putting data`); 2594 if (kvStore != null) { 2595 kvStore.delete(KEY_TEST_STRING_ELEMENT).then(() => { 2596 console.info('Succeeded in deleting'); 2597 }).catch((err: BusinessError) => { 2598 console.error(`Failed to delete.code is ${err.code},message is ${err.message}`); 2599 }); 2600 } 2601 }).catch((err: BusinessError) => { 2602 console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 2603 }); 2604} catch (e) { 2605 let error = e as BusinessError; 2606 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 2607} 2608``` 2609 2610### deleteBatch 2611 2612deleteBatch(keys: string[], callback: AsyncCallback<void>): void 2613 2614批量删除SingleKVStore数据库中的键值对,使用callback异步回调。 2615 2616**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 2617 2618**参数:** 2619 2620| 参数名 | 类型 | 必填 | 说明 | 2621| -------- | ------------------------- | ---- | ------------------------ | 2622| keys | string[] | 是 | 表示要批量删除的键值对。 | 2623| callback | AsyncCallback<void> | 是 | 回调函数。 | 2624 2625**错误码:** 2626 2627以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2628 2629| **错误码ID** | **错误信息** | 2630| ------------ | ---------------------------------------- | 2631| 15100003 | Database corrupted. | 2632| 15100005 | Database or result set already closed. | 2633 2634以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2635 2636| **错误码ID** | **错误信息** | 2637| ------------ | -------------------------------------------- | 2638| 14800047 | The WAL file size exceeds the default limit. | 2639 2640**示例:** 2641 2642```ts 2643import { BusinessError } from '@ohos.base'; 2644 2645try { 2646 let entries: distributedKVStore.Entry[] = []; 2647 let keys: string[] = []; 2648 for (let i = 0; i < 5; i++) { 2649 let key = 'batch_test_string_key'; 2650 let entry: distributedKVStore.Entry = { 2651 key: key + i, 2652 value: { 2653 type: distributedKVStore.ValueType.STRING, 2654 value: 'batch_test_string_value' 2655 } 2656 } 2657 entries.push(entry); 2658 keys.push(key + i); 2659 } 2660 console.info(`entries: ${entries}`); 2661 kvStore.putBatch(entries, async (err: BusinessError) => { 2662 if (err != undefined) { 2663 console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 2664 return; 2665 } 2666 console.info('Succeeded in putting Batch'); 2667 if (kvStore != null) { 2668 kvStore.deleteBatch(keys, async (err: BusinessError) => { 2669 if (err != undefined) { 2670 console.error(`Failed to delete Batch.code is ${err.code},message is ${err.message}`); 2671 return; 2672 } 2673 console.info('Succeeded in deleting Batch'); 2674 }); 2675 } 2676 }); 2677} catch (e) { 2678 let error = e as BusinessError; 2679 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 2680} 2681``` 2682 2683### deleteBatch 2684 2685deleteBatch(keys: string[]): Promise<void> 2686 2687批量删除SingleKVStore数据库中的键值对,使用Promise异步回调。 2688 2689**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 2690 2691**参数:** 2692 2693| 参数名 | 类型 | 必填 | 说明 | 2694| ------ | -------- | ---- | ------------------------ | 2695| keys | string[] | 是 | 表示要批量删除的键值对。 | 2696 2697**返回值:** 2698 2699| 类型 | 说明 | 2700| ------------------- | ------------------------- | 2701| Promise<void> | 无返回结果的Promise对象。 | 2702 2703**错误码:** 2704 2705以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2706 2707| **错误码ID** | **错误信息** | 2708| ------------ | ---------------------------------------- | 2709| 15100003 | Database corrupted. | 2710| 15100005 | Database or result set already closed. | 2711 2712以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2713 2714| **错误码ID** | **错误信息** | 2715| ------------ | -------------------------------------------- | 2716| 14800047 | The WAL file size exceeds the default limit. | 2717 2718**示例:** 2719 2720```ts 2721import { BusinessError } from '@ohos.base'; 2722 2723try { 2724 let entries: distributedKVStore.Entry[] = []; 2725 let keys: string[] = []; 2726 for (let i = 0; i < 5; i++) { 2727 let key = 'batch_test_string_key'; 2728 let entry: distributedKVStore.Entry = { 2729 key: key + i, 2730 value: { 2731 type: distributedKVStore.ValueType.STRING, 2732 value: 'batch_test_string_value' 2733 } 2734 } 2735 entries.push(entry); 2736 keys.push(key + i); 2737 } 2738 console.info(`entries: ${entries}`); 2739 kvStore.putBatch(entries).then(async () => { 2740 console.info('Succeeded in putting Batch'); 2741 if (kvStore != null) { 2742 kvStore.deleteBatch(keys).then(() => { 2743 console.info('Succeeded in deleting Batch'); 2744 }).catch((err: BusinessError) => { 2745 console.error(`Failed to delete Batch.code is ${err.code},message is ${err.message}`); 2746 }); 2747 } 2748 }).catch((err: BusinessError) => { 2749 console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 2750 }); 2751} catch (e) { 2752 let error = e as BusinessError; 2753 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 2754} 2755``` 2756 2757### removeDeviceData 2758 2759removeDeviceData(deviceId: string, callback: AsyncCallback<void>): void 2760 2761删除指定设备的数据,使用callback异步回调。 2762> **说明:** 2763> 2764> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 2765> deviceId具体获取方式请参考[sync接口示例](#sync) 2766 2767**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 2768 2769**参数:** 2770 2771| 参数名 | 类型 | 必填 | 说明 | 2772| -------- | ------------------------- | ---- | ---------------------- | 2773| deviceId | string | 是 | 表示要删除设备的名称。 | 2774| callback | AsyncCallback<void> | 是 | 回调函数。 | 2775 2776**错误码:** 2777 2778以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2779 2780| **错误码ID** | **错误信息** | 2781| ------------ | -------------------------------------- | 2782| 15100005 | Database or result set already closed. | 2783 2784**示例:** 2785 2786```ts 2787import { BusinessError } from '@ohos.base'; 2788 2789const KEY_TEST_STRING_ELEMENT = 'key_test_string_2'; 2790const VALUE_TEST_STRING_ELEMENT = 'value-string-002'; 2791try { 2792 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, async (err: BusinessError) => { 2793 console.info('Succeeded in putting data'); 2794 const deviceid = 'no_exist_device_id'; 2795 if (kvStore != null) { 2796 kvStore.removeDeviceData(deviceid, async (err: BusinessError) => { 2797 if (err == undefined) { 2798 console.info('succeeded in removing device data'); 2799 } else { 2800 console.error(`Failed to remove device data.code is ${err.code},message is ${err.message} `); 2801 if (kvStore != null) { 2802 kvStore.get(KEY_TEST_STRING_ELEMENT, async (err: BusinessError, data: boolean | string | number | Uint8Array) => { 2803 console.info('Succeeded in getting data'); 2804 }); 2805 } 2806 } 2807 }); 2808 } 2809 }); 2810} catch (e) { 2811 let error = e as BusinessError; 2812 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`) 2813} 2814``` 2815 2816### removeDeviceData 2817 2818removeDeviceData(deviceId: string): Promise<void> 2819 2820删除指定设备的数据,使用Promise异步回调。 2821> **说明:** 2822> 2823> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 2824> deviceId具体获取方式请参考[sync接口示例](#sync) 2825 2826**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 2827 2828**参数:** 2829 2830| 参数名 | 类型 | 必填 | 说明 | 2831| -------- | -------- | ---- | ---------------------- | 2832| deviceId | string | 是 | 表示要删除设备的名称。 | 2833 2834**返回值:** 2835 2836| 类型 | 说明 | 2837| ------------------- | ------------------------- | 2838| Promise<void> | 无返回结果的Promise对象。 | 2839 2840**错误码:** 2841 2842以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2843 2844| **错误码ID** | **错误信息** | 2845| ------------ | -------------------------------------- | 2846| 15100005 | Database or result set already closed. | 2847 2848**示例:** 2849 2850```ts 2851import { BusinessError } from '@ohos.base'; 2852 2853const KEY_TEST_STRING_ELEMENT = 'key_test_string_2'; 2854const VALUE_TEST_STRING_ELEMENT = 'value-string-001'; 2855try { 2856 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(() => { 2857 console.info('Succeeded in putting data'); 2858 }).catch((err: BusinessError) => { 2859 console.error(`Failed to put data.code is ${err.code},message is ${err.message} `); 2860 }); 2861 const deviceid = 'no_exist_device_id'; 2862 kvStore.removeDeviceData(deviceid).then(() => { 2863 console.info('succeeded in removing device data'); 2864 }).catch((err: BusinessError) => { 2865 console.error(`Failed to remove device data.code is ${err.code},message is ${err.message} `); 2866 }); 2867 kvStore.get(KEY_TEST_STRING_ELEMENT).then((data: boolean | string | number | Uint8Array) => { 2868 console.info('Succeeded in getting data'); 2869 }).catch((err: BusinessError) => { 2870 console.error(`Failed to get data.code is ${err.code},message is ${err.message} `); 2871 }); 2872} catch (e) { 2873 let error = e as BusinessError; 2874 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`) 2875} 2876``` 2877 2878### get 2879 2880get(key: string, callback: AsyncCallback<boolean | string | number | Uint8Array>): void 2881 2882获取指定键的值,使用callback异步回调。 2883 2884**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 2885 2886**参数:** 2887 2888| 参数名 | 类型 | 必填 | 说明 | 2889| ----- | ------ | ---- | ----------------------- | 2890| key |string | 是 |要查询数据的key,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 2891| callback |AsyncCallback<boolean \| string \| number \| Uint8Array> | 是 |回调函数。返回获取查询的值。 | 2892 2893**错误码:** 2894 2895以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2896 2897| **错误码ID** | **错误信息** | 2898| ------------ | -------------------------------------- | 2899| 15100003 | Database corrupted. | 2900| 15100004 | Not found. | 2901| 15100005 | Database or result set already closed. | 2902 2903**示例:** 2904 2905```ts 2906import { BusinessError } from '@ohos.base'; 2907 2908 2909const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 2910const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 2911try { 2912 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err: BusinessError) => { 2913 if (err != undefined) { 2914 console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 2915 return; 2916 } 2917 console.info("Succeeded in putting"); 2918 if (kvStore != null) { 2919 kvStore.get(KEY_TEST_STRING_ELEMENT, (err: BusinessError, data: boolean | string | number | Uint8Array) => { 2920 if (err != undefined) { 2921 console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 2922 return; 2923 } 2924 console.info(`Succeeded in getting data.data=${data}`); 2925 }); 2926 } 2927 }); 2928} catch (e) { 2929 let error = e as BusinessError; 2930 console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 2931} 2932``` 2933 2934### get 2935 2936get(key: string): Promise<boolean | string | number | Uint8Array> 2937 2938获取指定键的值,使用Promise异步回调。 2939 2940**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 2941 2942**参数:** 2943 2944| 参数名 | 类型 | 必填 | 说明 | 2945| ------ | -------- | ---- | ------------------------------------------------------------ | 2946| key | string | 是 | 要查询数据的key,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 2947 2948**返回值:** 2949 2950| 类型 | 说明 | 2951| ------ | ------- | 2952|Promise<Uint8Array \| string \| boolean \| number> |Promise对象。返回获取查询的值。| 2953 2954**错误码:** 2955 2956以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 2957 2958| **错误码ID** | **错误信息** | 2959| ------------ | -------------------------------------- | 2960| 15100003 | Database corrupted. | 2961| 15100004 | Not found. | 2962| 15100005 | Database or result set already closed. | 2963 2964**示例:** 2965 2966```ts 2967import { BusinessError } from '@ohos.base'; 2968 2969 2970const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 2971const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 2972try { 2973 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(() => { 2974 console.info(`Succeeded in putting data`); 2975 if (kvStore != null) { 2976 kvStore.get(KEY_TEST_STRING_ELEMENT).then((data: boolean | string | number | Uint8Array) => { 2977 console.info(`Succeeded in getting data.data=${data}`); 2978 }).catch((err: BusinessError) => { 2979 console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 2980 }); 2981 } 2982 }).catch((err: BusinessError) => { 2983 console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 2984 }); 2985} catch (e) { 2986 let error = e as BusinessError; 2987 console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 2988} 2989``` 2990 2991### getEntries 2992 2993getEntries(keyPrefix: string, callback: AsyncCallback<Entry[]>): void 2994 2995获取匹配指定键前缀的所有键值对,使用callback异步回调。 2996 2997**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 2998 2999**参数:** 3000 3001| 参数名 | 类型 | 必填 | 说明 | 3002| --------- | -------------------------------------- | ---- | ---------------------------------------- | 3003| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 3004| callback | AsyncCallback<[Entry](#entry)[]> | 是 | 回调函数。返回匹配指定前缀的键值对列表。 | 3005 3006**错误码:** 3007 3008以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3009 3010| **错误码ID** | **错误信息** | 3011| ------------ | -------------------------------------- | 3012| 15100003 | Database corrupted. | 3013| 15100005 | Database or result set already closed. | 3014 3015**示例:** 3016 3017```ts 3018import { BusinessError } from '@ohos.base'; 3019 3020try { 3021 let entries: distributedKVStore.Entry[] = []; 3022 for (let i = 0; i < 10; i++) { 3023 let key = 'batch_test_string_key'; 3024 let entry: distributedKVStore.Entry = { 3025 key: key + i, 3026 value: { 3027 type: distributedKVStore.ValueType.STRING, 3028 value: 'batch_test_string_value' 3029 } 3030 } 3031 entries.push(entry); 3032 } 3033 console.info(`entries: ${entries}`); 3034 kvStore.putBatch(entries, async (err: BusinessError) => { 3035 if (err != undefined) { 3036 console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 3037 return; 3038 } 3039 console.info('Succeeded in putting Batch'); 3040 if (kvStore != null) { 3041 kvStore.getEntries('batch_test_string_key', (err: BusinessError, entries: distributedKVStore.Entry[]) => { 3042 if (err != undefined) { 3043 console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 3044 return; 3045 } 3046 console.info('Succeeded in getting Entries'); 3047 console.info(`entries.length: ${entries.length}`); 3048 console.info(`entries[0]: ${entries[0]}`); 3049 }); 3050 } 3051 }); 3052} catch (e) { 3053 let error = e as BusinessError; 3054 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message} `); 3055} 3056``` 3057 3058### getEntries 3059 3060getEntries(keyPrefix: string): Promise<Entry[]> 3061 3062获取匹配指定键前缀的所有键值对,使用Promise异步回调。 3063 3064**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3065 3066**参数:** 3067 3068| 参数名 | 类型 | 必填 | 说明 | 3069| --------- | -------- | ---- | -------------------- | 3070| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 3071 3072**返回值:** 3073 3074| 类型 | 说明 | 3075| -------------------------------- | ------------------------------------------- | 3076| Promise<[Entry](#entry)[]> | Promise对象。返回匹配指定前缀的键值对列表。 | 3077 3078**错误码:** 3079 3080以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3081 3082| **错误码ID** | **错误信息** | 3083| ------------ | -------------------------------------- | 3084| 15100003 | Database corrupted. | 3085| 15100005 | Database or result set already closed. | 3086 3087**示例:** 3088 3089```ts 3090import { BusinessError } from '@ohos.base'; 3091 3092 3093try { 3094 let entries: distributedKVStore.Entry[] = []; 3095 for (let i = 0; i < 10; i++) { 3096 let key = 'batch_test_string_key'; 3097 let entry: distributedKVStore.Entry = { 3098 key: key + i, 3099 value: { 3100 type: distributedKVStore.ValueType.STRING, 3101 value: 'batch_test_string_value' 3102 } 3103 } 3104 entries.push(entry); 3105 } 3106 console.info(`entries: ${entries}`); 3107 kvStore.putBatch(entries).then(async () => { 3108 console.info('Succeeded in putting Batch'); 3109 if (kvStore != null) { 3110 kvStore.getEntries('batch_test_string_key').then((entries: distributedKVStore.Entry[]) => { 3111 console.info('Succeeded in getting Entries'); 3112 console.info(`PutBatch ${entries}`); 3113 }).catch((err: BusinessError) => { 3114 console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 3115 }); 3116 } 3117 }).catch((err: BusinessError) => { 3118 console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 3119 }); 3120} catch (e) { 3121 let error = e as BusinessError; 3122 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message} `); 3123} 3124``` 3125 3126### getEntries 3127 3128getEntries(query: Query, callback: AsyncCallback<Entry[]>): void 3129 3130获取与指定Query对象匹配的键值对列表,使用callback异步回调。 3131 3132**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3133 3134**参数:** 3135 3136| 参数名 | 类型 | 必填 | 说明 | 3137| -------- | -------------------------------------- | ---- | ----------------------------------------------- | 3138| query | [Query](#query) | 是 | 表示要匹配的键前缀。 | 3139| callback | AsyncCallback<[Entry](#entry)[]> | 是 | 回调函数。返回与指定Query对象匹配的键值对列表。 | 3140 3141**错误码:** 3142 3143以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3144 3145| **错误码ID** | **错误信息** | 3146| ------------ | -------------------------------------- | 3147| 15100003 | Database corrupted. | 3148| 15100005 | Database or result set already closed. | 3149 3150**示例:** 3151 3152```ts 3153import { BusinessError } from '@ohos.base'; 3154 3155try { 3156 let arr = new Uint8Array([21, 31]); 3157 let entries: distributedKVStore.Entry[] = []; 3158 for (let i = 0; i < 10; i++) { 3159 let key = 'batch_test_bool_key'; 3160 let entry: distributedKVStore.Entry = { 3161 key: key + i, 3162 value: { 3163 type: distributedKVStore.ValueType.BYTE_ARRAY, 3164 value: arr 3165 } 3166 } 3167 entries.push(entry); 3168 } 3169 console.info(`entries: {entries}`); 3170 kvStore.putBatch(entries, async (err: BusinessError) => { 3171 console.info('Succeeded in putting Batch'); 3172 const query = new distributedKVStore.Query(); 3173 query.prefixKey("batch_test"); 3174 if (kvStore != null) { 3175 kvStore.getEntries(query, (err: BusinessError, entries: distributedKVStore.Entry[]) => { 3176 if (err != undefined) { 3177 console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 3178 return; 3179 } 3180 console.info('Succeeded in getting Entries'); 3181 console.info(`entries.length: ${entries.length}`); 3182 console.info(`entries[0]: ${entries[0]}`); 3183 }); 3184 } 3185 }); 3186} catch (e) { 3187 let error = e as BusinessError; 3188 console.error(`Failed to get Entries.code is ${error.code},message is ${error.message}`); 3189} 3190``` 3191 3192### getEntries 3193 3194getEntries(query: Query): Promise<Entry[]> 3195 3196获取与指定Query对象匹配的键值对列表,使用Promise异步回调。 3197 3198**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3199 3200**参数:** 3201 3202| 参数名 | 类型 | 必填 | 说明 | 3203| ------ | -------------- | ---- | -------------- | 3204| query | [Query](#query) | 是 | 表示查询对象。 | 3205 3206**返回值:** 3207 3208| 类型 | 说明 | 3209| -------------------------------- | -------------------------------------------------- | 3210| Promise<[Entry](#entry)[]> | Promise对象。返回与指定Query对象匹配的键值对列表。 | 3211 3212**错误码:** 3213 3214以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3215 3216| **错误码ID** | **错误信息** | 3217| ------------ | -------------------------------------- | 3218| 15100003 | Database corrupted. | 3219| 15100005 | Database or result set already closed. | 3220 3221**示例:** 3222 3223```ts 3224import { BusinessError } from '@ohos.base'; 3225 3226try { 3227 let arr = new Uint8Array([21, 31]); 3228 let entries: distributedKVStore.Entry[] = []; 3229 for (let i = 0; i < 10; i++) { 3230 let key = 'batch_test_bool_key'; 3231 let entry: distributedKVStore.Entry = { 3232 key: key + i, 3233 value: { 3234 type: distributedKVStore.ValueType.BYTE_ARRAY, 3235 value: arr 3236 } 3237 } 3238 entries.push(entry); 3239 } 3240 console.info(`entries: {entries}`); 3241 kvStore.putBatch(entries).then(async () => { 3242 console.info('Succeeded in putting Batch'); 3243 const query = new distributedKVStore.Query(); 3244 query.prefixKey("batch_test"); 3245 if (kvStore != null) { 3246 kvStore.getEntries(query).then((entries: distributedKVStore.Entry[]) => { 3247 console.info('Succeeded in getting Entries'); 3248 }).catch((err: BusinessError) => { 3249 console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 3250 }); 3251 } 3252 }).catch((err: BusinessError) => { 3253 console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`) 3254 }); 3255 console.info('Succeeded in getting Entries'); 3256} catch (e) { 3257 let error = e as BusinessError; 3258 console.error(`Failed to get Entries.code is ${error.code},message is ${error.message}`); 3259} 3260``` 3261 3262### getResultSet 3263 3264getResultSet(keyPrefix: string, callback: AsyncCallback<KVStoreResultSet>): void 3265 3266从SingleKVStore数据库中获取具有指定前缀的结果集,使用callback异步回调。 3267 3268**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3269 3270**参数:** 3271 3272| 参数名 | 类型 | 必填 | 说明 | 3273| --------- | ---------------------------------------------------------- | ---- | ------------------------------------ | 3274| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 3275| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | 是 | 回调函数。返回具有指定前缀的结果集。 | 3276 3277**错误码:** 3278 3279以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3280 3281| **错误码ID** | **错误信息** | 3282| ------------ | -------------------------------------- | 3283| 15100001 | Over max limits. | 3284| 15100003 | Database corrupted. | 3285| 15100005 | Database or result set already closed. | 3286 3287 3288**示例:** 3289 3290```ts 3291import { BusinessError } from '@ohos.base'; 3292 3293try { 3294 let resultSet: distributedKVStore.KVStoreResultSet; 3295 let entries: distributedKVStore.Entry[] = []; 3296 for (let i = 0; i < 10; i++) { 3297 let key = 'batch_test_string_key'; 3298 let entry: distributedKVStore.Entry = { 3299 key: key + i, 3300 value: { 3301 type: distributedKVStore.ValueType.STRING, 3302 value: 'batch_test_string_value' 3303 } 3304 } 3305 entries.push(entry); 3306 } 3307 kvStore.putBatch(entries, async (err: BusinessError) => { 3308 if (err != undefined) { 3309 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 3310 return; 3311 } 3312 console.info('Succeeded in putting batch'); 3313 if (kvStore != null) { 3314 kvStore.getResultSet('batch_test_string_key', async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 3315 if (err != undefined) { 3316 console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3317 return; 3318 } 3319 console.info('Succeeded in getting result set'); 3320 resultSet = result; 3321 if (kvStore != null) { 3322 kvStore.closeResultSet(resultSet, (err: BusinessError) => { 3323 if (err != undefined) { 3324 console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 3325 return; 3326 } 3327 console.info('Succeeded in closing result set'); 3328 }); 3329 } 3330 }); 3331 } 3332 }); 3333} catch (e) { 3334 let error = e as BusinessError; 3335 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 3336} 3337``` 3338 3339### getResultSet 3340 3341getResultSet(keyPrefix: string): Promise<KVStoreResultSet> 3342 3343从SingleKVStore数据库中获取具有指定前缀的结果集,使用Promise异步回调。 3344 3345**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3346 3347**参数:** 3348 3349| 参数名 | 类型 | 必填 | 说明 | 3350| --------- | -------- | ---- | -------------------- | 3351| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 3352 3353**返回值:** 3354 3355| 类型 | 说明 | 3356| ---------------------------------------------------- | --------------------------------------- | 3357| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise对象。返回具有指定前缀的结果集。 | 3358 3359**错误码:** 3360 3361以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3362 3363| **错误码ID** | **错误信息** | 3364| ------------ | -------------------------------------- | 3365| 15100001 | Over max limits. | 3366| 15100003 | Database corrupted. | 3367| 15100005 | Database or result set already closed. | 3368 3369**示例:** 3370 3371```ts 3372import { BusinessError } from '@ohos.base'; 3373 3374try { 3375 let resultSet: distributedKVStore.KVStoreResultSet; 3376 let entries: distributedKVStore.Entry[] = []; 3377 for (let i = 0; i < 10; i++) { 3378 let key = 'batch_test_string_key'; 3379 let entry: distributedKVStore.Entry = { 3380 key: key + i, 3381 value: { 3382 type: distributedKVStore.ValueType.STRING, 3383 value: 'batch_test_string_value' 3384 } 3385 } 3386 entries.push(entry); 3387 } 3388 kvStore.putBatch(entries).then(async () => { 3389 console.info('Succeeded in putting batch'); 3390 }).catch((err: BusinessError) => { 3391 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 3392 }); 3393 kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 3394 console.info('Succeeded in getting result set'); 3395 resultSet = result; 3396 if (kvStore != null) { 3397 kvStore.closeResultSet(resultSet).then(() => { 3398 console.info('Succeeded in closing result set'); 3399 }).catch((err: BusinessError) => { 3400 console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 3401 }); 3402 } 3403 }).catch((err: BusinessError) => { 3404 console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3405 }); 3406} catch (e) { 3407 let error = e as BusinessError; 3408 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 3409} 3410``` 3411 3412### getResultSet 3413 3414getResultSet(query: Query, callback: AsyncCallback<KVStoreResultSet>): void 3415 3416获取与指定Query对象匹配的KVStoreResultSet对象,使用callback异步回调。 3417 3418**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3419 3420**参数:** 3421 3422| 参数名 | 类型 | 必填 | 说明 | 3423| -------- | ---------------------------------------------------------- | ---- | --------------------------------------------------------- | 3424| query | Query | 是 | 表示查询对象。 | 3425| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | 是 | 回调函数,获取与指定Query对象匹配的KVStoreResultSet对象。 | 3426 3427**错误码:** 3428 3429以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3430 3431| **错误码ID** | **错误信息** | 3432| ------------ | -------------------------------------- | 3433| 15100001 | Over max limits. | 3434| 15100003 | Database corrupted. | 3435| 15100005 | Database or result set already closed. | 3436 3437**示例:** 3438 3439```ts 3440import { BusinessError } from '@ohos.base'; 3441 3442try { 3443 let resultSet: distributedKVStore.KVStoreResultSet; 3444 let entries: distributedKVStore.Entry[] = []; 3445 for (let i = 0; i < 10; i++) { 3446 let key = 'batch_test_string_key'; 3447 let entry: distributedKVStore.Entry = { 3448 key: key + i, 3449 value: { 3450 type: distributedKVStore.ValueType.STRING, 3451 value: 'batch_test_string_value' 3452 } 3453 } 3454 entries.push(entry); 3455 } 3456 kvStore.putBatch(entries, async (err: BusinessError) => { 3457 if (err != undefined) { 3458 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 3459 return; 3460 } 3461 console.info('Succeeded in putting batch'); 3462 const query = new distributedKVStore.Query(); 3463 query.prefixKey("batch_test"); 3464 if (kvStore != null) { 3465 kvStore.getResultSet(query, async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 3466 if (err != undefined) { 3467 console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3468 return; 3469 } 3470 console.info('Succeeded in getting result set'); 3471 }); 3472 } 3473 }); 3474} catch (e) { 3475 let error = e as BusinessError; 3476 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 3477} 3478``` 3479 3480### getResultSet 3481 3482getResultSet(query: Query): Promise<KVStoreResultSet> 3483 3484获取与指定Query对象匹配的KVStoreResultSet对象,使用Promise异步回调。 3485 3486**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3487 3488**参数:** 3489 3490| 参数名 | 类型 | 必填 | 说明 | 3491| ------ | -------------- | ---- | -------------- | 3492| query | [Query](#query) | 是 | 表示查询对象。 | 3493 3494**返回值:** 3495 3496| 类型 | 说明 | 3497| ---------------------------------------------------- | ------------------------------------------------------------ | 3498| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise对象。获取与指定Query对象匹配的KVStoreResultSet对象。 | 3499 3500**错误码:** 3501 3502以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3503 3504| **错误码ID** | **错误信息** | 3505| ------------ | -------------------------------------- | 3506| 15100001 | Over max limits. | 3507| 15100003 | Database corrupted. | 3508| 15100005 | Database or result set already closed. | 3509 3510**示例:** 3511 3512```ts 3513import { BusinessError } from '@ohos.base'; 3514 3515try { 3516 let resultSet: distributedKVStore.KVStoreResultSet; 3517 let entries: distributedKVStore.Entry[] = []; 3518 for (let i = 0; i < 10; i++) { 3519 let key = 'batch_test_string_key'; 3520 let entry: distributedKVStore.Entry = { 3521 key: key + i, 3522 value: { 3523 type: distributedKVStore.ValueType.STRING, 3524 value: 'batch_test_string_value' 3525 } 3526 } 3527 entries.push(entry); 3528 } 3529 kvStore.putBatch(entries).then(async () => { 3530 console.info('Succeeded in putting batch'); 3531 }).catch((err: BusinessError) => { 3532 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 3533 }); 3534 const query = new distributedKVStore.Query(); 3535 query.prefixKey("batch_test"); 3536 kvStore.getResultSet(query).then((result: distributedKVStore.KVStoreResultSet) => { 3537 console.info('Succeeded in getting result set'); 3538 resultSet = result; 3539 }).catch((err: BusinessError) => { 3540 console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3541 }); 3542} catch (e) { 3543 let error = e as BusinessError; 3544 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 3545} 3546``` 3547 3548### closeResultSet 3549 3550closeResultSet(resultSet: KVStoreResultSet, callback: AsyncCallback<void>): void 3551 3552关闭由[SingleKvStore.getResultSet](#getresultset-1)返回的KVStoreResultSet对象,使用callback异步回调。 3553 3554**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3555 3556**参数:** 3557 3558| 参数名 | 类型 | 必填 | 说明 | 3559| --------- | ------------------------------------- | ---- | ---------------------------------- | 3560| resultSet | [KVStoreResultSet](#kvstoreresultset) | 是 | 表示要关闭的KVStoreResultSet对象。 | 3561| callback | AsyncCallback<void> | 是 | 回调函数。 | 3562 3563**示例:** 3564 3565```ts 3566import { BusinessError } from '@ohos.base'; 3567 3568let resultSet: distributedKVStore.KVStoreResultSet; 3569try { 3570 kvStore.getResultSet('batch_test_string_key', async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 3571 if (err != undefined) { 3572 console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3573 return; 3574 } 3575 console.info('Succeeded in getting result set'); 3576 resultSet = result; 3577 if (kvStore != null) { 3578 kvStore.closeResultSet(resultSet, (err: BusinessError) => { 3579 if (err != undefined) { 3580 console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 3581 return; 3582 } 3583 console.info('Succeeded in closing result set'); 3584 }) 3585 } 3586 }); 3587} catch (e) { 3588 let error = e as BusinessError; 3589 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 3590} 3591 3592``` 3593 3594### closeResultSet 3595 3596closeResultSet(resultSet: KVStoreResultSet): Promise<void> 3597 3598关闭由[SingleKvStore.getResultSet](#getresultset-1)返回的KVStoreResultSet对象,使用Promise异步回调。 3599 3600**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3601 3602**参数:** 3603 3604| 参数名 | 类型 | 必填 | 说明 | 3605| --------- | ------------------------------------- | ---- | ---------------------------------- | 3606| resultSet | [KVStoreResultSet](#kvstoreresultset) | 是 | 表示要关闭的KVStoreResultSet对象。 | 3607 3608**返回值:** 3609 3610| 类型 | 说明 | 3611| ------------------- | ------------------------- | 3612| Promise<void> | 无返回结果的Promise对象。 | 3613 3614**示例:** 3615 3616```ts 3617import { BusinessError } from '@ohos.base'; 3618 3619let resultSet: distributedKVStore.KVStoreResultSet; 3620try { 3621 kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 3622 console.info('Succeeded in getting result set'); 3623 resultSet = result; 3624 if (kvStore != null) { 3625 kvStore.closeResultSet(resultSet).then(() => { 3626 console.info('Succeeded in closing result set'); 3627 }).catch((err: BusinessError) => { 3628 console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 3629 }); 3630 } 3631 }).catch((err: BusinessError) => { 3632 console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 3633 }); 3634 3635} catch (e) { 3636 let error = e as BusinessError; 3637 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 3638} 3639``` 3640 3641### getResultSize 3642 3643getResultSize(query: Query, callback: AsyncCallback<number>): void 3644 3645获取与指定Query对象匹配的结果数,使用callback异步回调。 3646 3647**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3648 3649**参数:** 3650 3651| 参数名 | 类型 | 必填 | 说明 | 3652| -------- | --------------------------- | ---- | ------------------------------------------- | 3653| query | [Query](#query) | 是 | 表示查询对象。 | 3654| callback | AsyncCallback<number> | 是 | 回调函数。返回与指定Query对象匹配的结果数。 | 3655 3656**错误码:** 3657 3658以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3659 3660| **错误码ID** | **错误信息** | 3661| ------------ | -------------------------------------- | 3662| 15100003 | Database corrupted. | 3663| 15100005 | Database or result set already closed. | 3664 3665**示例:** 3666 3667```ts 3668import { BusinessError } from '@ohos.base'; 3669 3670try { 3671 let entries: distributedKVStore.Entry[] = []; 3672 for (let i = 0; i < 10; i++) { 3673 let key = 'batch_test_string_key'; 3674 let entry: distributedKVStore.Entry = { 3675 key: key + i, 3676 value: { 3677 type: distributedKVStore.ValueType.STRING, 3678 value: 'batch_test_string_value' 3679 } 3680 } 3681 entries.push(entry); 3682 } 3683 kvStore.putBatch(entries, async (err: BusinessError) => { 3684 console.info('Succeeded in putting batch'); 3685 const query = new distributedKVStore.Query(); 3686 query.prefixKey("batch_test"); 3687 if (kvStore != null) { 3688 kvStore.getResultSize(query, async (err: BusinessError, resultSize: number) => { 3689 if (err != undefined) { 3690 console.error(`Failed to get result size.code is ${err.code},message is ${err.message}`); 3691 return; 3692 } 3693 console.info('Succeeded in getting result set size'); 3694 }); 3695 } 3696 }); 3697} catch (e) { 3698 let error = e as BusinessError; 3699 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 3700} 3701``` 3702 3703### getResultSize 3704 3705getResultSize(query: Query): Promise<number> 3706 3707获取与指定Query对象匹配的结果数,使用Promise异步回调。 3708 3709**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3710 3711**参数:** 3712 3713| 参数名 | 类型 | 必填 | 说明 | 3714| ------ | -------------- | ---- | -------------- | 3715| query | [Query](#query) | 是 | 表示查询对象。 | 3716 3717**返回值:** 3718 3719| 类型 | 说明 | 3720| --------------------- | ----------------------------------------------- | 3721| Promise<number> | Promise对象。获取与指定QuerV9对象匹配的结果数。 | 3722 3723**错误码:** 3724 3725以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3726 3727| **错误码ID** | **错误信息** | 3728| ------------ | -------------------------------------- | 3729| 15100003 | Database corrupted. | 3730| 15100005 | Database or result set already closed. | 3731 3732**示例:** 3733 3734```ts 3735import { BusinessError } from '@ohos.base'; 3736 3737try { 3738 let entries: distributedKVStore.Entry[] = []; 3739 for (let i = 0; i < 10; i++) { 3740 let key = 'batch_test_string_key'; 3741 let entry: distributedKVStore.Entry = { 3742 key: key + i, 3743 value: { 3744 type: distributedKVStore.ValueType.STRING, 3745 value: 'batch_test_string_value' 3746 } 3747 } 3748 entries.push(entry); 3749 } 3750 kvStore.putBatch(entries).then(async () => { 3751 console.info('Succeeded in putting batch'); 3752 }).catch((err: BusinessError) => { 3753 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 3754 }); 3755 const query = new distributedKVStore.Query(); 3756 query.prefixKey("batch_test"); 3757 kvStore.getResultSize(query).then((resultSize: number) => { 3758 console.info('Succeeded in getting result set size'); 3759 }).catch((err: BusinessError) => { 3760 console.error(`Failed to get result size.code is ${err.code},message is ${err.message}`); 3761 }); 3762} catch (e) { 3763 let error = e as BusinessError; 3764 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 3765} 3766``` 3767 3768### backup 3769 3770backup(file:string, callback: AsyncCallback<void>):void 3771 3772以指定名称备份数据库,使用callback异步回调。 3773 3774**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3775 3776**参数:** 3777 3778| 参数名 | 类型 | 必填 | 说明 | 3779| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 3780| file | string | 是 | 备份数据库的指定名称,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 3781| callback | AsyncCallback<void> | 是 | 回调函数。当以指定名称备份数据库成功,err为undefined,否则为错误对象。 | 3782 3783**错误码:** 3784 3785以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3786 3787| **错误码ID** | **错误信息** | 3788| ------------ | -------------------------------------- | 3789| 15100005 | Database or result set already closed. | 3790 3791**示例:** 3792 3793```ts 3794import { BusinessError } from '@ohos.base'; 3795 3796let file = "BK001"; 3797try { 3798 kvStore.backup(file, (err: BusinessError) => { 3799 if (err) { 3800 console.error(`Failed to backup.code is ${err.code},message is ${err.message} `); 3801 } else { 3802 console.info(`Succeeded in backupping data`); 3803 } 3804 }); 3805} catch (e) { 3806 let error = e as BusinessError; 3807 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 3808} 3809``` 3810 3811### backup 3812 3813backup(file:string): Promise<void> 3814 3815以指定名称备份数据库,使用Promise异步回调。 3816 3817**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3818 3819**参数:** 3820 3821| 参数名 | 类型 | 必填 | 说明 | 3822| ------ | -------- | ---- | ------------------------------------------------------------ | 3823| file | string | 是 | 备份数据库的指定名称,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 3824 3825**返回值:** 3826 3827| 类型 | 说明 | 3828| ------------------- | ------------------------- | 3829| Promise<void> | 无返回结果的Promise对象。 | 3830 3831**错误码:** 3832 3833以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3834 3835| **错误码ID** | **错误信息** | 3836| ------------ | -------------------------------------- | 3837| 15100005 | Database or result set already closed. | 3838 3839**示例:** 3840 3841```ts 3842import { BusinessError } from '@ohos.base'; 3843 3844let file = "BK001"; 3845try { 3846 kvStore.backup(file).then(() => { 3847 console.info(`Succeeded in backupping data`); 3848 }).catch((err: BusinessError) => { 3849 console.error(`Failed to backup.code is ${err.code},message is ${err.message}`); 3850 }); 3851} catch (e) { 3852 let error = e as BusinessError; 3853 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 3854} 3855``` 3856 3857### restore 3858 3859restore(file:string, callback: AsyncCallback<void>):void 3860 3861从指定的数据库文件恢复数据库,使用callback异步回调。 3862 3863**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3864 3865**参数:** 3866 3867| 参数名 | 类型 | 必填 | 说明 | 3868| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 3869| file | string | 是 | 指定的数据库文件名称,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 3870| callback | AsyncCallback<void> | 是 | 回调函数。当从指定的数据库文件恢复数据库成功,err为undefined,否则为错误对象。 | 3871 3872**错误码:** 3873 3874以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3875 3876| **错误码ID** | **错误信息** | 3877| ------------ | -------------------------------------- | 3878| 15100005 | Database or result set already closed. | 3879 3880**示例:** 3881 3882```ts 3883import { BusinessError } from '@ohos.base'; 3884 3885let file = "BK001"; 3886try { 3887 kvStore.restore(file, (err: BusinessError) => { 3888 if (err) { 3889 console.error(`Failed to restore.code is ${err.code},message is ${err.message}`); 3890 } else { 3891 console.info(`Succeeded in restoring data`); 3892 } 3893 }); 3894} catch (e) { 3895 let error = e as BusinessError; 3896 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 3897} 3898``` 3899 3900### restore 3901 3902restore(file:string): Promise<void> 3903 3904从指定的数据库文件恢复数据库,使用Promise异步回调。 3905 3906**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3907 3908**参数:** 3909 3910| 参数名 | 类型 | 必填 | 说明 | 3911| ------ | -------- | ---- | ------------------------------------------------------------ | 3912| file | string | 是 | 指定的数据库文件名称,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 3913 3914**返回值:** 3915 3916| 类型 | 说明 | 3917| ------------------- | ------------------------- | 3918| Promise<void> | 无返回结果的Promise对象。 | 3919 3920**错误码:** 3921 3922以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 3923 3924| **错误码ID** | **错误信息** | 3925| ------------ | -------------------------------------- | 3926| 15100005 | Database or result set already closed. | 3927 3928**示例:** 3929 3930```ts 3931import { BusinessError } from '@ohos.base'; 3932 3933let file = "BK001"; 3934try { 3935 kvStore.restore(file).then(() => { 3936 console.info(`Succeeded in restoring data`); 3937 }).catch((err: BusinessError) => { 3938 console.error(`Failed to restore.code is ${err.code},message is ${err.message}`); 3939 }); 3940} catch (e) { 3941 let error = e as BusinessError; 3942 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 3943} 3944``` 3945 3946### deleteBackup 3947 3948deleteBackup(files:Array<string>, callback: AsyncCallback<Array<[string, number]>>):void 3949 3950根据指定名称删除备份文件,使用callback异步回调。 3951 3952**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3953 3954**参数:** 3955 3956| 参数名 | 类型 | 必填 | 说明 | 3957| -------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ | 3958| files | Array<string> | 是 | 删除备份文件所指定的名称,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 3959| callback | AsyncCallback<Array<[string, number]>> | 是 | 回调函数,返回删除备份的文件名及其处理结果。 | 3960 3961**示例:** 3962 3963```ts 3964import { BusinessError } from '@ohos.base'; 3965 3966let files = ["BK001", "BK002"]; 3967try { 3968 kvStore.deleteBackup(files, (err: BusinessError, data: [string, number][]) => { 3969 if (err) { 3970 console.error(`Failed to delete Backup.code is ${err.code},message is ${err.message}`); 3971 } else { 3972 console.info(`Succeed in deleting Backup.data=${data}`); 3973 } 3974 }); 3975} catch (e) { 3976 let error = e as BusinessError; 3977 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 3978} 3979``` 3980 3981### deleteBackup 3982 3983deleteBackup(files:Array<string>): Promise<Array<[string, number]>> 3984 3985根据指定名称删除备份文件,使用Promise异步回调。 3986 3987**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 3988 3989**参数:** 3990 3991| 参数名 | 类型 | 必填 | 说明 | 3992| ------ | ------------------- | ---- | ------------------------------------------------------------ | 3993| files | Array<string> | 是 | 删除备份文件所指定的名称,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 3994 3995**返回值:** 3996 3997| 类型 | 说明 | 3998| -------------------------------------------- | ----------------------------------------------- | 3999| Promise<Array<[string, number]>> | Promise对象,返回删除备份的文件名及其处理结果。 | 4000 4001**示例:** 4002 4003```ts 4004import { BusinessError } from '@ohos.base'; 4005 4006let files = ["BK001", "BK002"]; 4007try { 4008 kvStore.deleteBackup(files).then((data: [string, number][]) => { 4009 console.info(`Succeed in deleting Backup.data=${data}`); 4010 }).catch((err: BusinessError) => { 4011 console.error(`Failed to delete Backup.code is ${err.code},message is ${err.message}`); 4012 }) 4013} catch (e) { 4014 let error = e as BusinessError; 4015 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4016} 4017``` 4018 4019### startTransaction 4020 4021startTransaction(callback: AsyncCallback<void>): void 4022 4023启动SingleKVStore数据库中的事务,使用callback异步回调。 4024 4025**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4026 4027**参数:** 4028 4029| 参数名 | 类型 | 必填 | 说明 | 4030| -------- | ------------------------- | ---- | ---------- | 4031| callback | AsyncCallback<void> | 是 | 回调函数。 | 4032 4033**错误码:** 4034 4035以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4036 4037| **错误码ID** | **错误信息** | 4038| ------------ | ---------------------------------------- | 4039| 15100005 | Database or result set already closed. | 4040 4041以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4042 4043| **错误码ID** | **错误信息** | 4044| ------------ | -------------------------------------------- | 4045| 14800047 | The WAL file size exceeds the default limit. | 4046 4047**示例:** 4048 4049```ts 4050import { BusinessError } from '@ohos.base'; 4051 4052function putBatchString(len: number, prefix: string) { 4053 let entries: distributedKVStore.Entry[] = []; 4054 for (let i = 0; i < len; i++) { 4055 let entry: distributedKVStore.Entry = { 4056 key: prefix + i, 4057 value: { 4058 type: distributedKVStore.ValueType.STRING, 4059 value: 'batch_test_string_value' 4060 } 4061 } 4062 entries.push(entry); 4063 } 4064 return entries; 4065} //自定义函数,放置在作用域最外侧,防止语法检查报错 4066 4067try { 4068 let count = 0; 4069 kvStore.on('dataChange', 0, (data: distributedKVStore.ChangeNotification) => { 4070 console.info(`startTransaction 0 ${data}`); 4071 count++; 4072 }); 4073 kvStore.startTransaction(async (err: BusinessError) => { 4074 if (err != undefined) { 4075 console.error(`Failed to start Transaction.code is ${err.code},message is ${err.message}`); 4076 return; 4077 } 4078 console.info('Succeeded in starting Transaction'); 4079 let entries = putBatchString(10, 'batch_test_string_key'); 4080 console.info(`entries: ${entries}`); 4081 if (kvStore != null) { 4082 kvStore.putBatch(entries, async (err: BusinessError) => { 4083 if (err != undefined) { 4084 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 4085 return; 4086 } 4087 console.info('Succeeded in putting Batch'); 4088 }); 4089 } 4090 }); 4091} catch (e) { 4092 let error = e as BusinessError; 4093 console.error(`Failed to start Transaction.code is ${error.code},message is ${error.message}`); 4094} 4095``` 4096 4097### startTransaction 4098 4099startTransaction(): Promise<void> 4100 4101启动SingleKVStore数据库中的事务,使用Promise异步回调。 4102 4103**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4104 4105**返回值:** 4106 4107| 类型 | 说明 | 4108| ------------------- | ------------------------- | 4109| Promise<void> | 无返回结果的Promise对象。 | 4110 4111**错误码:** 4112 4113以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4114 4115| **错误码ID** | **错误信息** | 4116| ------------ | ---------------------------------------- | 4117| 15100005 | Database or result set already closed. | 4118 4119以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4120 4121| **错误码ID** | **错误信息** | 4122| ------------ | -------------------------------------------- | 4123| 14800047 | The WAL file size exceeds the default limit. | 4124 4125**示例:** 4126 4127```ts 4128import { BusinessError } from '@ohos.base'; 4129 4130try { 4131 let count = 0; 4132 kvStore.on('dataChange', distributedKVStore.SubscribeType.SUBSCRIBE_TYPE_ALL, (data: distributedKVStore.ChangeNotification) => { 4133 console.info(`startTransaction 0 ${data}`); 4134 count++; 4135 }); 4136 kvStore.startTransaction().then(async () => { 4137 console.info('Succeeded in starting Transaction'); 4138 }).catch((err: BusinessError) => { 4139 console.error(`Failed to start Transaction.code is ${err.code},message is ${err.message}`); 4140 }); 4141} catch (e) { 4142 let error = e as BusinessError; 4143 console.error(`Failed to start Transaction.code is ${error.code},message is ${error.message}`); 4144} 4145``` 4146 4147### commit 4148 4149commit(callback: AsyncCallback<void>): void 4150 4151提交SingleKVStore数据库中的事务,使用callback异步回调。 4152 4153**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4154 4155**参数:** 4156 4157| 参数名 | 类型 | 必填 | 说明 | 4158| -------- | ------------------------- | ---- | ---------- | 4159| callback | AsyncCallback<void> | 是 | 回调函数。 | 4160 4161**错误码:** 4162 4163以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4164 4165| **错误码ID** | **错误信息** | 4166| ------------ | -------------------------------------- | 4167| 15100005 | Database or result set already closed. | 4168 4169**示例:** 4170 4171```ts 4172import { BusinessError } from '@ohos.base'; 4173 4174try { 4175 kvStore.commit((err: BusinessError) => { 4176 if (err == undefined) { 4177 console.info('Succeeded in committing'); 4178 } else { 4179 console.error(`Failed to commit.code is ${err.code},message is ${err.message}`); 4180 } 4181 }); 4182} catch (e) { 4183 let error = e as BusinessError; 4184 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4185} 4186``` 4187 4188### commit 4189 4190commit(): Promise<void> 4191 4192提交SingleKVStore数据库中的事务,使用Promise异步回调。 4193 4194**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4195 4196**返回值:** 4197 4198| 类型 | 说明 | 4199| ------------------- | ------------------------- | 4200| Promise<void> | 无返回结果的Promise对象。 | 4201 4202**错误码:** 4203 4204以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4205 4206| **错误码ID** | **错误信息** | 4207| ------------ | -------------------------------------- | 4208| 15100005 | Database or result set already closed. | 4209 4210**示例:** 4211 4212```ts 4213import { BusinessError } from '@ohos.base'; 4214 4215try { 4216 kvStore.commit().then(async () => { 4217 console.info('Succeeded in committing'); 4218 }).catch((err: BusinessError) => { 4219 console.error(`Failed to commit.code is ${err.code},message is ${err.message}`); 4220 }); 4221} catch (e) { 4222 let error = e as BusinessError; 4223 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4224} 4225``` 4226 4227### rollback 4228 4229rollback(callback: AsyncCallback<void>): void 4230 4231在SingleKVStore数据库中回滚事务,使用callback异步回调。 4232 4233**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4234 4235**参数:** 4236 4237| 参数名 | 类型 | 必填 | 说明 | 4238| -------- | ------------------------- | ---- | ---------- | 4239| callback | AsyncCallback<void> | 是 | 回调函数。 | 4240 4241**错误码:** 4242 4243以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4244 4245| **错误码ID** | **错误信息** | 4246| ------------ | -------------------------------------- | 4247| 15100005 | Database or result set already closed. | 4248 4249**示例:** 4250 4251```ts 4252import { BusinessError } from '@ohos.base'; 4253 4254try { 4255 kvStore.rollback((err: BusinessError) => { 4256 if (err == undefined) { 4257 console.info('Succeeded in rolling back'); 4258 } else { 4259 console.error(`Failed to rollback.code is ${err.code},message is ${err.message}`); 4260 } 4261 }); 4262} catch (e) { 4263 let error = e as BusinessError; 4264 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4265} 4266``` 4267 4268### rollback 4269 4270rollback(): Promise<void> 4271 4272在SingleKVStore数据库中回滚事务,使用Promise异步回调。 4273 4274**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4275 4276**返回值:** 4277 4278| 类型 | 说明 | 4279| ------------------- | ------------------------- | 4280| Promise<void> | 无返回结果的Promise对象。 | 4281 4282**错误码:** 4283 4284以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4285 4286| **错误码ID** | **错误信息** | 4287| ------------ | -------------------------------------- | 4288| 15100005 | Database or result set already closed. | 4289 4290**示例:** 4291 4292```ts 4293import { BusinessError } from '@ohos.base'; 4294 4295try { 4296 kvStore.rollback().then(async () => { 4297 console.info('Succeeded in rolling back'); 4298 }).catch((err: BusinessError) => { 4299 console.error(`Failed to rollback.code is ${err.code},message is ${err.message}`); 4300 }); 4301} catch (e) { 4302 let error = e as BusinessError; 4303 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4304} 4305``` 4306 4307### enableSync 4308 4309enableSync(enabled: boolean, callback: AsyncCallback<void>): void 4310 4311设定是否开启同步,使用callback异步回调。 4312 4313**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4314 4315**参数:** 4316 4317| 参数名 | 类型 | 必填 | 说明 | 4318| -------- | ------------------------- | ---- | --------------------------------------------------------- | 4319| enabled | boolean | 是 | 设定是否开启同步,true表示开启同步,false表示不启用同步。 | 4320| callback | AsyncCallback<void> | 是 | 回调函数。 | 4321 4322**示例:** 4323 4324```ts 4325import { BusinessError } from '@ohos.base'; 4326 4327try { 4328 kvStore.enableSync(true, (err: BusinessError) => { 4329 if (err == undefined) { 4330 console.info('Succeeded in enabling sync'); 4331 } else { 4332 console.error(`Failed to enable sync.code is ${err.code},message is ${err.message}`); 4333 } 4334 }); 4335} catch (e) { 4336 let error = e as BusinessError; 4337 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4338} 4339``` 4340 4341### enableSync 4342 4343enableSync(enabled: boolean): Promise<void> 4344 4345设定是否开启同步,使用Promise异步回调。 4346 4347**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4348 4349**参数:** 4350 4351| 参数名 | 类型 | 必填 | 说明 | 4352| ------- | -------- | ---- | --------------------------------------------------------- | 4353| enabled | boolean | 是 | 设定是否开启同步,true表示开启同步,false表示不启用同步。 | 4354 4355**返回值:** 4356 4357| 类型 | 说明 | 4358| ------------------- | ------------------------- | 4359| Promise<void> | 无返回结果的Promise对象。 | 4360 4361**示例:** 4362 4363```ts 4364import { BusinessError } from '@ohos.base'; 4365 4366try { 4367 kvStore.enableSync(true).then(() => { 4368 console.info('Succeeded in enabling sync'); 4369 }).catch((err: BusinessError) => { 4370 console.error(`Failed to enable sync.code is ${err.code},message is ${err.message}`); 4371 }); 4372} catch (e) { 4373 let error = e as BusinessError; 4374 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4375} 4376``` 4377 4378### setSyncRange 4379 4380setSyncRange(localLabels: string[], remoteSupportLabels: string[], callback: AsyncCallback<void>): void 4381 4382设置同步范围标签,使用callback异步回调。 4383 4384**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4385 4386**参数:** 4387 4388| 参数名 | 类型 | 必填 | 说明 | 4389| ------------------- | ------------------------- | ---- | -------------------------------- | 4390| localLabels | string[] | 是 | 表示本地设备的同步标签。 | 4391| remoteSupportLabels | string[] | 是 | 表示要同步数据的设备的同步标签。 | 4392| callback | AsyncCallback<void> | 是 | 回调函数。 | 4393 4394**示例:** 4395 4396```ts 4397import { BusinessError } from '@ohos.base'; 4398 4399try { 4400 const localLabels = ['A', 'B']; 4401 const remoteSupportLabels = ['C', 'D']; 4402 kvStore.setSyncRange(localLabels, remoteSupportLabels, (err: BusinessError) => { 4403 if (err != undefined) { 4404 console.error(`Failed to set syncRange.code is ${err.code},message is ${err.message}`); 4405 return; 4406 } 4407 console.info('Succeeded in setting syncRange'); 4408 }); 4409} catch (e) { 4410 let error = e as BusinessError; 4411 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4412} 4413``` 4414 4415### setSyncRange 4416 4417setSyncRange(localLabels: string[], remoteSupportLabels: string[]): Promise<void> 4418 4419设置同步范围标签,使用Promise异步回调。 4420 4421**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4422 4423**参数:** 4424 4425| 参数名 | 类型 | 必填 | 说明 | 4426| ------------------- | -------- | ---- | -------------------------------- | 4427| localLabels | string[] | 是 | 表示本地设备的同步标签。 | 4428| remoteSupportLabels | string[] | 是 | 表示要同步数据的设备的同步标签。 | 4429 4430**返回值:** 4431 4432| 类型 | 说明 | 4433| ------------------- | ------------------------- | 4434| Promise<void> | 无返回结果的Promise对象。 | 4435 4436**示例:** 4437 4438```ts 4439import { BusinessError } from '@ohos.base'; 4440 4441try { 4442 const localLabels = ['A', 'B']; 4443 const remoteSupportLabels = ['C', 'D']; 4444 kvStore.setSyncRange(localLabels, remoteSupportLabels).then(() => { 4445 console.info('Succeeded in setting syncRange'); 4446 }).catch((err: BusinessError) => { 4447 console.error(`Failed to set syncRange.code is ${err.code},message is ${err.message}`); 4448 }); 4449} catch (e) { 4450 let error = e as BusinessError; 4451 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4452} 4453``` 4454 4455### setSyncParam 4456 4457setSyncParam(defaultAllowedDelayMs: number, callback: AsyncCallback<void>): void 4458 4459设置数据库同步允许的默认延迟,使用callback异步回调。 4460 4461**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4462 4463**参数:** 4464 4465| 参数名 | 类型 | 必填 | 说明 | 4466| --------------------- | ------------------------- | ---- | -------------------------------------------- | 4467| defaultAllowedDelayMs | number | 是 | 表示数据库同步允许的默认延迟,以毫秒为单位。 | 4468| callback | AsyncCallback<void> | 是 | 回调函数。 | 4469 4470**示例:** 4471 4472```ts 4473import { BusinessError } from '@ohos.base'; 4474 4475try { 4476 const defaultAllowedDelayMs = 500; 4477 kvStore.setSyncParam(defaultAllowedDelayMs, (err: BusinessError) => { 4478 if (err != undefined) { 4479 console.error(`Failed to set syncParam.code is ${err.code},message is ${err.message}`); 4480 return; 4481 } 4482 console.info('Succeeded in setting syncParam'); 4483 }); 4484} catch (e) { 4485 let error = e as BusinessError; 4486 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4487} 4488``` 4489 4490### setSyncParam 4491 4492setSyncParam(defaultAllowedDelayMs: number): Promise<void> 4493 4494设置数据库同步允许的默认延迟,使用Promise异步回调。 4495 4496**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4497 4498**参数:** 4499 4500| 参数名 | 类型 | 必填 | 说明 | 4501| --------------------- | -------- | ---- | -------------------------------------------- | 4502| defaultAllowedDelayMs | number | 是 | 表示数据库同步允许的默认延迟,以毫秒为单位。 | 4503 4504**返回值:** 4505 4506| 类型 | 说明 | 4507| ------------------- | ------------------------- | 4508| Promise<void> | 无返回结果的Promise对象。 | 4509 4510**示例:** 4511 4512```ts 4513import { BusinessError } from '@ohos.base'; 4514 4515try { 4516 const defaultAllowedDelayMs = 500; 4517 kvStore.setSyncParam(defaultAllowedDelayMs).then(() => { 4518 console.info('Succeeded in setting syncParam'); 4519 }).catch((err: BusinessError) => { 4520 console.error(`Failed to set syncParam.code is ${err.code},message is ${err.message}`); 4521 }); 4522} catch (e) { 4523 let error = e as BusinessError; 4524 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4525} 4526``` 4527 4528### sync 4529 4530sync(deviceIds: string[], mode: SyncMode, delayMs?: number): void 4531 4532在手动同步方式下,触发数据库同步。关于键值型数据库的同步方式说明,请见[键值型数据库跨设备数据同步](../../database/data-sync-of-kv-store.md)。 4533> **说明:** 4534> 4535> 其中deviceIds为[DeviceBasicInfo](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#devicebasicinfo)中的networkId, 通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 4536 4537**需要权限**: ohos.permission.DISTRIBUTED_DATASYNC。 4538 4539**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4540 4541**参数:** 4542 4543| 参数名 | 类型 | 必填 | 说明 | 4544| --------- | --------------------- | ---- | ---------------------------------------------- | 4545| deviceIds | string[] | 是 | 同一组网环境下,需要同步的设备的networkId列表。 | 4546| mode | [SyncMode](#syncmode) | 是 | 同步模式。 | 4547| delayMs | number | 否 | 可选参数,允许延时时间,单位:ms(毫秒),默认为0。 | 4548 4549**错误码:** 4550 4551以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4552 4553| **错误码ID** | **错误信息** | 4554| ------------ | ------------------- | 4555| 15100003 | Database corrupted. | 4556| 15100004 | Not found. | 4557 4558**示例:** 4559 4560```ts 4561import deviceManager from '@ohos.distributedDeviceManager'; 4562import UIAbility from '@ohos.app.ability.UIAbility'; 4563import { BusinessError } from '@ohos.base'; 4564 4565let devManager: deviceManager.DeviceManager; 4566const KEY_TEST_SYNC_ELEMENT = 'key_test_sync'; 4567const VALUE_TEST_SYNC_ELEMENT = 'value-string-001'; 4568// create deviceManager 4569export default class EntryAbility extends UIAbility { 4570 onCreate() { 4571 let context = this.context; 4572 try { 4573 devManager = deviceManager.createDeviceManager(context.applicationInfo.name); 4574 let deviceIds: string[] = []; 4575 if (devManager != null) { 4576 let devices = devManager.getAvailableDeviceListSync(); 4577 for (let i = 0; i < devices.length; i++) { 4578 deviceIds[i] = devices[i].networkId as string; 4579 } 4580 } 4581 try { 4582 if (kvStore != null) { 4583 kvStore.on('syncComplete', (data: [string, number][]) => { 4584 console.info('Sync dataChange'); 4585 }); 4586 if (kvStore != null) { 4587 kvStore.put(KEY_TEST_SYNC_ELEMENT + 'testSync101', VALUE_TEST_SYNC_ELEMENT, (err: BusinessError) => { 4588 if (err != undefined) { 4589 console.error(`Failed to sync.code is ${err.code},message is ${err.message}`); 4590 return; 4591 } 4592 console.info('Succeeded in putting data'); 4593 const mode = distributedKVStore.SyncMode.PULL_ONLY; 4594 if (kvStore != null) { 4595 kvStore.sync(deviceIds, mode, 1000); 4596 } 4597 }); 4598 } 4599 } 4600 } catch (e) { 4601 let error = e as BusinessError; 4602 console.error(`Failed to sync.code is ${error.code},message is ${error.message}`); 4603 } 4604 4605 } catch (err) { 4606 let error = err as BusinessError; 4607 console.error("createDeviceManager errCode:" + error.code + ",errMessage:" + error.message); 4608 } 4609 } 4610} 4611``` 4612 4613### sync 4614 4615sync(deviceIds: string[], query: Query, mode: SyncMode, delayMs?: number): void 4616 4617在手动同步方式下,触发数据库同步,此方法为同步方法。关于键值型数据库的同步方式说明,请见[键值型数据库跨设备数据同步](../../database/data-sync-of-kv-store.md)。 4618> **说明:** 4619> 4620> 其中deviceIds为[DeviceBasicInfo](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#devicebasicinfo)中的networkId, 通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 4621 4622**需要权限**: ohos.permission.DISTRIBUTED_DATASYNC。 4623 4624**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4625 4626**参数:** 4627 4628| 参数名 | 类型 | 必填 | 说明 | 4629| --------- | --------------------- | ---- | ---------------------------------------------- | 4630| deviceIds | string[] | 是 | 同一组网环境下,需要同步的设备的networkId列表。 | 4631| mode | [SyncMode](#syncmode) | 是 | 同步模式。 | 4632| query | [Query](#query) | 是 | 表示数据库的查询谓词条件 | 4633| delayMs | number | 否 | 可选参数,允许延时时间,单位:ms(毫秒),默认为0。 | 4634 4635**错误码:** 4636 4637以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4638 4639| **错误码ID** | **错误信息** | 4640| ------------ | ------------------- | 4641| 15100003 | Database corrupted. | 4642| 15100004 | Not found. | 4643 4644**示例:** 4645 4646```ts 4647import deviceManager from '@ohos.distributedDeviceManager'; 4648import UIAbility from '@ohos.app.ability.UIAbility'; 4649import { BusinessError } from '@ohos.base'; 4650 4651let devManager: deviceManager.DeviceManager; 4652const KEY_TEST_SYNC_ELEMENT = 'key_test_sync'; 4653const VALUE_TEST_SYNC_ELEMENT = 'value-string-001'; 4654// create deviceManager 4655export default class EntryAbility extends UIAbility { 4656 onCreate() { 4657 let context = this.context; 4658 try { 4659 let devManager = deviceManager.createDeviceManager(context.applicationInfo.name); 4660 let deviceIds: string[] = []; 4661 if (devManager != null) { 4662 let devices = devManager.getAvailableDeviceListSync(); 4663 for (let i = 0; i < devices.length; i++) { 4664 deviceIds[i] = devices[i].networkId as string; 4665 } 4666 } 4667 try { 4668 if (kvStore != null) { 4669 kvStore.on('syncComplete', (data: [string, number][]) => { 4670 console.info('Sync dataChange'); 4671 }); 4672 if (kvStore != null) { 4673 kvStore.put(KEY_TEST_SYNC_ELEMENT + 'testSync101', VALUE_TEST_SYNC_ELEMENT, (err: BusinessError) => { 4674 if (err != undefined) { 4675 console.error(`Failed to sync.code is ${err.code},message is ${err.message}`); 4676 return; 4677 } 4678 console.info('Succeeded in putting data'); 4679 const mode = distributedKVStore.SyncMode.PULL_ONLY; 4680 const query = new distributedKVStore.Query(); 4681 query.prefixKey("batch_test"); 4682 query.deviceId(devManager.getLocalDeviceNetworkId()); 4683 if (kvStore != null) { 4684 kvStore.sync(deviceIds, query, mode, 1000); 4685 } 4686 }); 4687 } 4688 } 4689 } catch (e) { 4690 let error = e as BusinessError; 4691 console.error(`Failed to sync.code is ${error.code},message is ${error.message}`); 4692 } 4693 4694 } catch (err) { 4695 let error = err as BusinessError; 4696 console.error("createDeviceManager errCode:" + error.code + ",errMessage:" + error.message); 4697 } 4698 } 4699} 4700``` 4701 4702### on('dataChange') 4703 4704on(event: 'dataChange', type: SubscribeType, listener: Callback<ChangeNotification>): void 4705 4706订阅指定类型的数据变更通知。 4707 4708**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4709 4710**参数:** 4711 4712| 参数名 | 类型 | 必填 | 说明 | 4713| -------- | --------------------------------------------------------- | ---- | ---------------------------------------------------- | 4714| event | string | 是 | 订阅的事件名,固定为'dataChange',表示数据变更事件。 | 4715| type | [SubscribeType](#subscribetype) | 是 | 表示订阅的类型。 | 4716| listener | Callback<[ChangeNotification](#changenotification)> | 是 | 回调函数。 | 4717 4718**错误码:** 4719 4720以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4721 4722| **错误码ID** | **错误信息** | 4723| ------------ | -------------------------------------- | 4724| 15100001 | Over max limits. | 4725| 15100005 | Database or result set already closed. | 4726 4727**示例:** 4728 4729```ts 4730import { BusinessError } from '@ohos.base'; 4731 4732try { 4733 kvStore.on('dataChange', distributedKVStore.SubscribeType.SUBSCRIBE_TYPE_LOCAL, (data: distributedKVStore.ChangeNotification) => { 4734 console.info(`dataChange callback call data: ${data}`); 4735 }); 4736} catch (e) { 4737 let error = e as BusinessError; 4738 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4739} 4740``` 4741 4742### on('syncComplete') 4743 4744on(event: 'syncComplete', syncCallback: Callback<Array<[string, number]>>): void 4745 4746订阅同步完成事件回调通知。 4747 4748**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4749 4750**参数:** 4751 4752| 参数名 | 类型 | 必填 | 说明 | 4753| ------------ | --------------------------------------------- | ---- | ------------------------------------------------------ | 4754| event | string | 是 | 订阅的事件名,固定为'syncComplete',表示同步完成事件。 | 4755| syncCallback | Callback<Array<[string, number]>> | 是 | 回调函数。用于向调用方发送同步结果的回调。 | 4756 4757**示例:** 4758 4759```ts 4760import { BusinessError } from '@ohos.base'; 4761 4762 4763const KEY_TEST_FLOAT_ELEMENT = 'key_test_float'; 4764const VALUE_TEST_FLOAT_ELEMENT = 321.12; 4765try { 4766 kvStore.on('syncComplete', (data: [string, number][]) => { 4767 console.info(`syncComplete ${data}`); 4768 }); 4769 kvStore.put(KEY_TEST_FLOAT_ELEMENT, VALUE_TEST_FLOAT_ELEMENT).then(() => { 4770 console.info('succeeded in putting'); 4771 }).catch((err: BusinessError) => { 4772 console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 4773 }); 4774} catch (e) { 4775 let error = e as BusinessError; 4776 console.error(`Failed to subscribe syncComplete.code is ${error.code},message is ${error.message}`); 4777} 4778``` 4779 4780### off('dataChange') 4781 4782off(event:'dataChange', listener?: Callback<ChangeNotification>): void 4783 4784取消订阅数据变更通知。 4785 4786**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4787 4788**参数:** 4789 4790| 参数名 | 类型 | 必填 | 说明 | 4791| -------- | --------------------------------------------------------- | ---- | -------------------------------------------------------- | 4792| event | string | 是 | 取消订阅的事件名,固定为'dataChange',表示数据变更事件。 | 4793| listener | Callback<[ChangeNotification](#changenotification)> | 否 | 取消订阅的函数。如不设置callback,则取消所有已订阅的函数。 | 4794 4795**错误码:** 4796 4797以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4798 4799| **错误码ID** | **错误信息** | 4800| ------------ | -------------------------------------- | 4801| 15100005 | Database or result set already closed. | 4802 4803**示例:** 4804 4805```ts 4806import { BusinessError } from '@ohos.base'; 4807 4808class KvstoreModel { 4809 call(data: distributedKVStore.ChangeNotification) { 4810 console.info(`dataChange : ${data}`); 4811 } 4812 4813 subscribeDataChange() { 4814 try { 4815 if (kvStore != null) { 4816 kvStore.on('dataChange', distributedKVStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, this.call); 4817 } 4818 } catch (err) { 4819 let error = err as BusinessError; 4820 console.error(`Failed to subscribeDataChange.code is ${error.code},message is ${error.message}`); 4821 } 4822 } 4823 4824 unsubscribeDataChange() { 4825 try { 4826 if (kvStore != null) { 4827 kvStore.off('dataChange', this.call); 4828 } 4829 } catch (err) { 4830 let error = err as BusinessError; 4831 console.error(`Failed to unsubscribeDataChange.code is ${error.code},message is ${error.message}`); 4832 } 4833 } 4834} 4835``` 4836 4837### off('syncComplete') 4838 4839off(event: 'syncComplete', syncCallback?: Callback<Array<[string, number]>>): void 4840 4841取消订阅同步完成事件回调通知。 4842 4843**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4844 4845**参数:** 4846 4847| 参数名 | 类型 | 必填 | 说明 | 4848| ------------ | --------------------------------------------- | ---- | ---------------------------------------------------------- | 4849| event | string | 是 | 取消订阅的事件名,固定为'syncComplete',表示同步完成事件。 | 4850| syncCallback | Callback<Array<[string, number]>> | 否 | 取消订阅的函数。如不设置callback,则取消所有已订阅的函数。 | 4851 4852**示例:** 4853 4854```ts 4855import { BusinessError } from '@ohos.base'; 4856 4857class KvstoreModel { 4858 call(data: [string, number][]) { 4859 console.info(`syncComplete : ${data}`); 4860 } 4861 4862 subscribeDataChange() { 4863 try { 4864 if (kvStore != null) { 4865 kvStore.on('syncComplete', this.call); 4866 } 4867 } catch (err) { 4868 let error = err as BusinessError; 4869 console.error(`Failed to subscribeDataChange.code is ${error.code},message is ${error.message}`); 4870 } 4871 } 4872 4873 unsubscribeDataChange() { 4874 try { 4875 if (kvStore != null) { 4876 kvStore.off('syncComplete', this.call); 4877 } 4878 } catch (err) { 4879 let error = err as BusinessError; 4880 console.error(`Failed to unsubscribeDataChange.code is ${error.code},message is ${error.message}`); 4881 } 4882 } 4883} 4884``` 4885 4886### getSecurityLevel 4887 4888getSecurityLevel(callback: AsyncCallback<SecurityLevel>): void 4889 4890获取数据库的安全级别,使用callback异步回调。 4891 4892**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4893 4894**参数:** 4895 4896| 参数名 | 类型 | 必填 | 说明 | 4897| -------- | ---------------------------------------------------- | ---- | -------------------------------- | 4898| callback | AsyncCallback<[SecurityLevel](#securitylevel)> | 是 | 回调函数。返回数据库的安全级别。 | 4899 4900**错误码:** 4901 4902以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4903 4904| **错误码ID** | **错误信息** | 4905| ------------ | -------------------------------------- | 4906| 15100005 | Database or result set already closed. | 4907 4908**示例:** 4909 4910```ts 4911import { BusinessError } from '@ohos.base'; 4912 4913try { 4914 kvStore.getSecurityLevel((err: BusinessError, data: distributedKVStore.SecurityLevel) => { 4915 if (err != undefined) { 4916 console.error(`Failed to get SecurityLevel.code is ${err.code},message is ${err.message}`); 4917 return; 4918 } 4919 console.info('Succeeded in getting securityLevel'); 4920 }); 4921} catch (e) { 4922 let error = e as BusinessError; 4923 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4924} 4925``` 4926 4927### getSecurityLevel 4928 4929getSecurityLevel(): Promise<SecurityLevel> 4930 4931获取数据库的安全级别,使用Promise异步回调。 4932 4933**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4934 4935**返回值:** 4936 4937| 类型 | 说明 | 4938| ---------------------------------------------- | ----------------------------------- | 4939| Promise<[SecurityLevel](#securitylevel)> | Promise对象。返回数据库的安全级别。 | 4940 4941**错误码:** 4942 4943以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4944 4945| **错误码ID** | **错误信息** | 4946| ------------ | -------------------------------------- | 4947| 15100005 | Database or result set already closed. | 4948 4949**示例:** 4950 4951```ts 4952import { BusinessError } from '@ohos.base'; 4953 4954try { 4955 kvStore.getSecurityLevel().then((data: distributedKVStore.SecurityLevel) => { 4956 console.info('Succeeded in getting securityLevel'); 4957 }).catch((err: BusinessError) => { 4958 console.error(`Failed to get SecurityLevel.code is ${err.code},message is ${err.message}`); 4959 }); 4960} catch (e) { 4961 let error = e as BusinessError; 4962 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 4963} 4964``` 4965 4966## DeviceKVStore 4967 4968设备协同数据库,继承自SingleKVStore,提供查询数据和同步数据的方法。 4969 4970设备协同数据库,以设备维度对数据进行区分,每台设备仅能写入和修改本设备的数据,其它设备的数据对其是只读的,无法修改其它设备的数据。 4971 4972比如,可以使用设备协同数据库实现设备间的图片分享,可以查看其他设备的图片,但无法修改和删除其他设备的图片。 4973 4974在调用DeviceKVStore的方法前,需要先通过[getKVStore](#getkvstore)构建一个DeviceKVStore实例。 4975 4976### get 4977 4978get(key: string, callback: AsyncCallback<boolean | string | number | Uint8Array>): void 4979 4980获取本设备指定键的值,使用callback异步回调。 4981 4982**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 4983 4984**参数:** 4985 4986| 参数名 | 类型 | 必填 | 说明 | 4987| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4988| key | string | 是 | 要查询数据的key,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 4989| callback | AsyncCallback<boolean \| string \| number \| Uint8Array> | 是 | 回调函数。返回获取查询的值。 | 4990 4991**错误码:** 4992 4993以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 4994 4995| **错误码ID** | **错误信息** | 4996| ------------ | -------------------------------------- | 4997| 15100003 | Database corrupted. | 4998| 15100004 | Not found. | 4999| 15100005 | Database or result set already closed. | 5000 5001**示例:** 5002 5003```ts 5004import { BusinessError } from '@ohos.base'; 5005 5006const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 5007const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 5008try { 5009 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err: BusinessError) => { 5010 if (err != undefined) { 5011 console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 5012 return; 5013 } 5014 console.info("Succeeded in putting"); 5015 if (kvStore != null) { 5016 kvStore.get(KEY_TEST_STRING_ELEMENT, (err: BusinessError, data: boolean | string | number | Uint8Array) => { 5017 if (err != undefined) { 5018 console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 5019 return; 5020 } 5021 console.info(`Succeeded in getting data.data=${data}`); 5022 }); 5023 } 5024 }); 5025} catch (e) { 5026 let error = e as BusinessError; 5027 console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 5028} 5029``` 5030 5031### get 5032 5033get(key: string): Promise<boolean | string | number | Uint8Array> 5034 5035获取本设备指定键的值,使用Promise异步回调。 5036 5037**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 5038 5039**参数:** 5040 5041| 参数名 | 类型 | 必填 | 说明 | 5042| ------ | ------ | ---- | ------------------------------------------------------------ | 5043| key | string | 是 | 要查询数据的key,不能为空且长度不大于[MAX_KEY_LENGTH](#constants)。 | 5044 5045**返回值:** 5046 5047| 类型 | 说明 | 5048| -------------------------------------------------------- | ------------------------------- | 5049| Promise<Uint8Array \| string \| boolean \| number> | Promise对象。返回获取查询的值。 | 5050 5051**错误码:** 5052 5053以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5054 5055| **错误码ID** | **错误信息** | 5056| ------------ | -------------------------------------- | 5057| 15100003 | Database corrupted. | 5058| 15100004 | Not found. | 5059| 15100005 | Database or result set already closed. | 5060 5061**示例:** 5062 5063```ts 5064import { BusinessError } from '@ohos.base'; 5065 5066const KEY_TEST_STRING_ELEMENT = 'key_test_string'; 5067const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; 5068try { 5069 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(() => { 5070 console.info(`Succeeded in putting data`); 5071 if (kvStore != null) { 5072 kvStore.get(KEY_TEST_STRING_ELEMENT).then((data: boolean | string | number | Uint8Array) => { 5073 console.info(`Succeeded in getting data.data=${data}`); 5074 }).catch((err: BusinessError) => { 5075 console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 5076 }); 5077 } 5078 }).catch((err: BusinessError) => { 5079 console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 5080 }); 5081} catch (e) { 5082 let error = e as BusinessError; 5083 console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 5084} 5085``` 5086 5087### get 5088 5089get(deviceId: string, key: string, callback: AsyncCallback<boolean | string | number | Uint8Array>): void 5090 5091获取与指定设备ID和key匹配的string值,使用callback异步回调。 5092> **说明:** 5093> 5094> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 5095> deviceId具体获取方式请参考[sync接口示例](#sync)。 5096 5097**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5098 5099**参数:** 5100 5101| 参数名 | 类型 | 必填 | 说明 | 5102| ----- | ------ | ---- | ----------------------- | 5103| deviceId |string | 是 |标识要查询其数据的设备。 | 5104| key |string | 是 |表示要查询key值的键。 | 5105| callback |AsyncCallback<boolean\|string\|number\|Uint8Array> | 是 |回调函数,返回匹配给定条件的字符串值。 | 5106 5107**错误码:** 5108 5109以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5110 5111| **错误码ID** | **错误信息** | 5112| ------------ | -------------------------------------- | 5113| 15100003 | Database corrupted. | 5114| 15100004 | Not found. | 5115| 15100005 | Database or result set already closed. | 5116 5117**示例:** 5118 5119```ts 5120import { BusinessError } from '@ohos.base'; 5121 5122const KEY_TEST_STRING_ELEMENT = 'key_test_string_2'; 5123const VALUE_TEST_STRING_ELEMENT = 'value-string-002'; 5124try { 5125 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, async (err: BusinessError) => { 5126 if (err != undefined) { 5127 console.error(`Failed to put.code is ${err.code},message is ${err.message}`); 5128 return; 5129 } 5130 console.info('Succeeded in putting'); 5131 if (kvStore != null) { 5132 kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT, (err: BusinessError, data: boolean | string | number | Uint8Array) => { 5133 if (err != undefined) { 5134 console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 5135 return; 5136 } 5137 console.info('Succeeded in getting'); 5138 }); 5139 } 5140 }) 5141} catch (e) { 5142 let error = e as BusinessError; 5143 console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 5144} 5145``` 5146 5147### get 5148 5149get(deviceId: string, key: string): Promise<boolean | string | number | Uint8Array> 5150 5151获取与指定设备ID和key匹配的string值,使用Promise异步回调。 5152> **说明:** 5153> 5154> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 5155> deviceId具体获取方式请参考[sync接口示例](#sync)。 5156 5157**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5158 5159**参数:** 5160 5161| 参数名 | 类型 | 必填 | 说明 | 5162| -------- | -------- | ---- | ------------------------ | 5163| deviceId | string | 是 | 标识要查询其数据的设备。 | 5164| key | string | 是 | 表示要查询key值的键。 | 5165 5166**返回值:** 5167 5168| 类型 | 说明 | 5169| ------ | ------- | 5170|Promise<boolean\|string\|number\|Uint8Array> |Promise对象。返回匹配给定条件的字符串值。| 5171 5172**错误码:** 5173 5174以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5175 5176| **错误码ID** | **错误信息** | 5177| ------------ | -------------------------------------- | 5178| 15100003 | Database corrupted. | 5179| 15100004 | Not found. | 5180| 15100005 | Database or result set already closed. | 5181 5182**示例:** 5183 5184```ts 5185import { BusinessError } from '@ohos.base'; 5186 5187const KEY_TEST_STRING_ELEMENT = 'key_test_string_2'; 5188const VALUE_TEST_STRING_ELEMENT = 'value-string-002'; 5189try { 5190 kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(async () => { 5191 console.info('Succeeded in putting'); 5192 if (kvStore != null) { 5193 kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT).then((data: boolean | string | number | Uint8Array) => { 5194 console.info('Succeeded in getting'); 5195 }).catch((err: BusinessError) => { 5196 console.error(`Failed to get.code is ${err.code},message is ${err.message}`); 5197 }); 5198 } 5199 }).catch((error: BusinessError) => { 5200 console.error(`Failed to put.code is ${error.code},message is ${error.message}`); 5201 }); 5202} catch (e) { 5203 let error = e as BusinessError; 5204 console.error(`Failed to get.code is ${error.code},message is ${error.message}`); 5205} 5206``` 5207 5208### getEntries 5209 5210getEntries(keyPrefix: string, callback: AsyncCallback<Entry[]>): void 5211 5212获取匹配本设备指定键前缀的所有键值对,使用callback异步回调。 5213 5214**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 5215 5216**参数:** 5217 5218| 参数名 | 类型 | 必填 | 说明 | 5219| --------- | -------------------------------------- | ---- | ---------------------------------------- | 5220| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 5221| callback | AsyncCallback<[Entry](#entry)[]> | 是 | 回调函数。返回匹配指定前缀的键值对列表。 | 5222 5223**错误码:** 5224 5225以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5226 5227| **错误码ID** | **错误信息** | 5228| ------------ | -------------------------------------- | 5229| 15100003 | Database corrupted. | 5230| 15100005 | Database or result set already closed. | 5231 5232**示例:** 5233 5234```ts 5235import { BusinessError } from '@ohos.base'; 5236 5237try { 5238 let entries: distributedKVStore.Entry[] = []; 5239 for (let i = 0; i < 10; i++) { 5240 let key = 'batch_test_string_key'; 5241 let entry: distributedKVStore.Entry = { 5242 key: key + i, 5243 value: { 5244 type: distributedKVStore.ValueType.STRING, 5245 value: 'batch_test_string_value' 5246 } 5247 } 5248 entries.push(entry); 5249 } 5250 console.info(`entries: ${entries}`); 5251 kvStore.putBatch(entries, async (err: BusinessError) => { 5252 if (err != undefined) { 5253 console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 5254 return; 5255 } 5256 console.info('Succeeded in putting Batch'); 5257 if (kvStore != null) { 5258 kvStore.getEntries('batch_test_string_key', (err: BusinessError, entries: distributedKVStore.Entry[]) => { 5259 if (err != undefined) { 5260 console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 5261 return; 5262 } 5263 console.info('Succeeded in getting Entries'); 5264 console.info(`entries.length: ${entries.length}`); 5265 console.info(`entries[0]: ${entries[0]}`); 5266 }); 5267 } 5268 }); 5269} catch (e) { 5270 let error = e as BusinessError; 5271 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message} `); 5272} 5273``` 5274 5275### getEntries 5276 5277getEntries(keyPrefix: string): Promise<Entry[]> 5278 5279获取匹配本设备指定键前缀的所有键值对,使用Promise异步回调。 5280 5281**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 5282 5283**参数:** 5284 5285| 参数名 | 类型 | 必填 | 说明 | 5286| --------- | ------ | ---- | -------------------- | 5287| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 5288 5289**返回值:** 5290 5291| 类型 | 说明 | 5292| -------------------------------- | ------------------------------------------- | 5293| Promise<[Entry](#entry)[]> | Promise对象。返回匹配指定前缀的键值对列表。 | 5294 5295**错误码:** 5296 5297以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5298 5299| **错误码ID** | **错误信息** | 5300| ------------ | -------------------------------------- | 5301| 15100003 | Database corrupted. | 5302| 15100005 | Database or result set already closed. | 5303 5304**示例:** 5305 5306```ts 5307import { BusinessError } from '@ohos.base'; 5308 5309try { 5310 let entries: distributedKVStore.Entry[] = []; 5311 for (let i = 0; i < 10; i++) { 5312 let key = 'batch_test_string_key'; 5313 let entry: distributedKVStore.Entry = { 5314 key: key + i, 5315 value: { 5316 type: distributedKVStore.ValueType.STRING, 5317 value: 'batch_test_string_value' 5318 } 5319 } 5320 entries.push(entry); 5321 } 5322 console.info(`entries: ${entries}`); 5323 kvStore.putBatch(entries).then(async () => { 5324 console.info('Succeeded in putting Batch'); 5325 if (kvStore != null) { 5326 kvStore.getEntries('batch_test_string_key').then((entries: distributedKVStore.Entry[]) => { 5327 console.info('Succeeded in getting Entries'); 5328 console.info(`PutBatch ${entries}`); 5329 }).catch((err: BusinessError) => { 5330 console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 5331 }); 5332 } 5333 }).catch((err: BusinessError) => { 5334 console.error(`Failed to put Batch.code is ${err.code},message is ${err.message}`); 5335 }); 5336} catch (e) { 5337 let error = e as BusinessError; 5338 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message} `); 5339} 5340``` 5341 5342### getEntries 5343 5344getEntries(deviceId: string, keyPrefix: string, callback: AsyncCallback<Entry[]>): void 5345 5346获取与指定设备ID和key前缀匹配的所有键值对,使用callback异步回调。 5347> **说明:** 5348> 5349> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 5350> deviceId具体获取方式请参考[sync接口示例](#sync)。 5351 5352**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5353 5354**参数:** 5355 5356| 参数名 | 类型 | 必填 | 说明 | 5357| --------- | -------------------------------------- | ---- | ---------------------------------------------- | 5358| deviceId | string | 是 | 标识要查询其数据的设备。 | 5359| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 5360| callback | AsyncCallback<[Entry](#entry)[]> | 是 | 回调函数,返回满足给定条件的所有键值对的列表。 | 5361 5362**错误码:** 5363 5364以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5365 5366| **错误码ID** | **错误信息** | 5367| ------------ | -------------------------------------- | 5368| 15100003 | Database corrupted. | 5369| 15100005 | Database or result set already closed. | 5370 5371**示例:** 5372 5373```ts 5374import { BusinessError } from '@ohos.base'; 5375 5376try { 5377 let entries: distributedKVStore.Entry[] = []; 5378 for (let i = 0; i < 10; i++) { 5379 let key = 'batch_test_string_key'; 5380 let entry: distributedKVStore.Entry = { 5381 key: key + i, 5382 value: { 5383 type: distributedKVStore.ValueType.STRING, 5384 value: 'batch_test_string_value' 5385 } 5386 } 5387 entries.push(entry); 5388 } 5389 console.info(`entries : ${entries}`); 5390 kvStore.putBatch(entries, async (err: BusinessError) => { 5391 if (err != undefined) { 5392 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 5393 return; 5394 } 5395 console.info('Succeeded in putting batch'); 5396 if (kvStore != null) { 5397 kvStore.getEntries('localDeviceId', 'batch_test_string_key', (err: BusinessError, entries: distributedKVStore.Entry[]) => { 5398 if (err != undefined) { 5399 console.error(`Failed to get entries.code is ${err.code},message is ${err.message}`); 5400 return; 5401 } 5402 console.info('Succeeded in getting entries'); 5403 console.info(`entries.length: ${entries.length}`); 5404 console.info(`entries[0]: ${entries[0]}`); 5405 }); 5406 } 5407 }); 5408} catch (e) { 5409 let error = e as BusinessError; 5410 console.error(`Failed to put batch.code is ${error.code},message is ${error.message}`); 5411} 5412``` 5413 5414### getEntries 5415 5416getEntries(deviceId: string, keyPrefix: string): Promise<Entry[]> 5417 5418获取与指定设备ID和key前缀匹配的所有键值对,使用Promise异步回调。 5419> **说明:** 5420> 5421> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 5422> deviceId具体获取方式请参考[sync接口示例](#sync)。 5423 5424**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5425 5426**参数:** 5427 5428| 参数名 | 类型 | 必填 | 说明 | 5429| --------- | -------- | ---- | ------------------------ | 5430| deviceId | string | 是 | 标识要查询其数据的设备。 | 5431| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 5432 5433**返回值:** 5434 5435| 类型 | 说明 | 5436| -------------------------------- | ------------------------------------------------- | 5437| Promise<[Entry](#entry)[]> | Promise对象。返回匹配给定条件的所有键值对的列表。 | 5438 5439**错误码:** 5440 5441以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5442 5443| **错误码ID** | **错误信息** | 5444| ------------ | -------------------------------------- | 5445| 15100003 | Database corrupted. | 5446| 15100005 | Database or result set already closed. | 5447 5448**示例:** 5449 5450```ts 5451import { BusinessError } from '@ohos.base'; 5452 5453try { 5454 let entries: distributedKVStore.Entry[] = []; 5455 for (let i = 0; i < 10; i++) { 5456 let key = 'batch_test_string_key'; 5457 let entry: distributedKVStore.Entry = { 5458 key: key + i, 5459 value: { 5460 type: distributedKVStore.ValueType.STRING, 5461 value: 'batch_test_string_value' 5462 } 5463 } 5464 entries.push(entry); 5465 } 5466 console.info(`entries: ${entries}`); 5467 kvStore.putBatch(entries).then(async () => { 5468 console.info('Succeeded in putting batch'); 5469 if (kvStore != null) { 5470 kvStore.getEntries('localDeviceId', 'batch_test_string_key').then((entries: distributedKVStore.Entry[]) => { 5471 console.info('Succeeded in getting entries'); 5472 console.info(`entries.length: ${entries.length}`); 5473 console.info(`entries[0]: ${entries[0]}`); 5474 console.info(`entries[0].value: ${entries[0].value}`); 5475 console.info(`entries[0].value.value: ${entries[0].value.value}`); 5476 }).catch((err: BusinessError) => { 5477 console.error(`Failed to get entries.code is ${err.code},message is ${err.message}`); 5478 }); 5479 } 5480 }).catch((err: BusinessError) => { 5481 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 5482 }); 5483} catch (e) { 5484 let error = e as BusinessError; 5485 console.error(`Failed to put batch.code is ${error.code},message is ${error.message}`); 5486} 5487``` 5488 5489### getEntries 5490 5491getEntries(query: Query, callback: AsyncCallback<Entry[]>): void 5492 5493获取本设备与指定Query对象匹配的键值对列表,使用callback异步回调。 5494 5495**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 5496 5497**参数:** 5498 5499| 参数名 | 类型 | 必填 | 说明 | 5500| -------- | -------------------------------------- | ---- | ----------------------------------------------------- | 5501| query | [Query](#query) | 是 | 表示要匹配的键前缀。 | 5502| callback | AsyncCallback<[Entry](#entry)[]> | 是 | 回调函数。返回本设备与指定Query对象匹配的键值对列表。 | 5503 5504**错误码:** 5505 5506以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5507 5508| **错误码ID** | **错误信息** | 5509| ------------ | -------------------------------------- | 5510| 15100003 | Database corrupted. | 5511| 15100005 | Database or result set already closed. | 5512 5513**示例:** 5514 5515```ts 5516import { BusinessError } from '@ohos.base'; 5517 5518try { 5519 let arr = new Uint8Array([21, 31]); 5520 let entries: distributedKVStore.Entry[] = []; 5521 for (let i = 0; i < 10; i++) { 5522 let key = 'batch_test_bool_key'; 5523 let entry: distributedKVStore.Entry = { 5524 key: key + i, 5525 value: { 5526 type: distributedKVStore.ValueType.BYTE_ARRAY, 5527 value: arr 5528 } 5529 } 5530 entries.push(entry); 5531 } 5532 console.info(`entries: {entries}`); 5533 kvStore.putBatch(entries, async (err: BusinessError) => { 5534 console.info('Succeeded in putting Batch'); 5535 const query = new distributedKVStore.Query(); 5536 query.prefixKey("batch_test"); 5537 if (kvStore != null) { 5538 kvStore.getEntries(query, (err: BusinessError, entries: distributedKVStore.Entry[]) => { 5539 if (err != undefined) { 5540 console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 5541 return; 5542 } 5543 console.info('Succeeded in getting Entries'); 5544 console.info(`entries.length: ${entries.length}`); 5545 console.info(`entries[0]: ${entries[0]}`); 5546 }); 5547 } 5548 }); 5549} catch (e) { 5550 let error = e as BusinessError; 5551 console.error(`Failed to get Entries.code is ${error.code},message is ${error.message}`); 5552} 5553``` 5554 5555### getEntries 5556 5557getEntries(query: Query): Promise<Entry[]> 5558 5559获取本设备与指定Query对象匹配的键值对列表,使用Promise异步回调。 5560 5561**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 5562 5563**参数:** 5564 5565| 参数名 | 类型 | 必填 | 说明 | 5566| ------ | -------------- | ---- | -------------- | 5567| query | [Query](#query) | 是 | 表示查询对象。 | 5568 5569**返回值:** 5570 5571| 类型 | 说明 | 5572| -------------------------------- | -------------------------------------------------------- | 5573| Promise<[Entry](#entry)[]> | Promise对象。返回本设备与指定Query对象匹配的键值对列表。 | 5574 5575**错误码:** 5576 5577以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5578 5579| **错误码ID** | **错误信息** | 5580| ------------ | -------------------------------------- | 5581| 15100003 | Database corrupted. | 5582| 15100005 | Database or result set already closed. | 5583 5584**示例:** 5585 5586```ts 5587import { BusinessError } from '@ohos.base'; 5588 5589try { 5590 let arr = new Uint8Array([21, 31]); 5591 let entries: distributedKVStore.Entry[] = []; 5592 for (let i = 0; i < 10; i++) { 5593 let key = 'batch_test_bool_key'; 5594 let entry: distributedKVStore.Entry = { 5595 key: key + i, 5596 value: { 5597 type: distributedKVStore.ValueType.BYTE_ARRAY, 5598 value: arr 5599 } 5600 } 5601 entries.push(entry); 5602 } 5603 console.info(`entries: {entries}`); 5604 kvStore.putBatch(entries).then(async () => { 5605 console.info('Succeeded in putting Batch'); 5606 const query = new distributedKVStore.Query(); 5607 query.prefixKey("batch_test"); 5608 if (kvStore != null) { 5609 kvStore.getEntries(query).then((entries: distributedKVStore.Entry[]) => { 5610 console.info('Succeeded in getting Entries'); 5611 }).catch((err: BusinessError) => { 5612 console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`); 5613 }); 5614 } 5615 }).catch((err: BusinessError) => { 5616 console.error(`Failed to get Entries.code is ${err.code},message is ${err.message}`) 5617 }); 5618 console.info('Succeeded in getting Entries'); 5619} catch (e) { 5620 let error = e as BusinessError; 5621 console.error(`Failed to get Entries.code is ${error.code},message is ${error.message}`); 5622} 5623``` 5624 5625### getEntries 5626 5627getEntries(deviceId: string, query: Query, callback: AsyncCallback<Entry[]>): void 5628 5629获取与指定设备ID和Query对象匹配的键值对列表,使用callback异步回调。 5630> **说明:** 5631> 5632> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 5633> deviceId具体获取方式请参考[sync接口示例](#sync)。 5634 5635**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5636 5637**参数:** 5638 5639| 参数名 | 类型 | 必填 | 说明 | 5640| -------- | -------------------------------------- | ---- | ------------------------------------------------------- | 5641| deviceId | string | 是 | 键值对所属的设备ID。 | 5642| query | [Query](#query) | 是 | 表示查询对象。 | 5643| callback | AsyncCallback<[Entry](#entry)[]> | 是 | 回调函数。返回与指定设备ID和Query对象匹配的键值对列表。 | 5644 5645**错误码:** 5646 5647以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5648 5649| **错误码ID** | **错误信息** | 5650| ------------ | -------------------------------------- | 5651| 15100003 | Database corrupted. | 5652| 15100005 | Database or result set already closed. | 5653 5654**示例:** 5655 5656```ts 5657import { BusinessError } from '@ohos.base'; 5658 5659try { 5660 let arr = new Uint8Array([21, 31]); 5661 let entries: distributedKVStore.Entry[] = []; 5662 for (let i = 0; i < 10; i++) { 5663 let key = 'batch_test_bool_key'; 5664 let entry: distributedKVStore.Entry = { 5665 key: key + i, 5666 value: { 5667 type: distributedKVStore.ValueType.BYTE_ARRAY, 5668 value: arr 5669 } 5670 } 5671 entries.push(entry); 5672 } 5673 console.info(`entries: ${entries}`); 5674 kvStore.putBatch(entries, async (err: BusinessError) => { 5675 if (err != undefined) { 5676 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 5677 return; 5678 } 5679 console.info('Succeeded in putting batch'); 5680 let query = new distributedKVStore.Query(); 5681 query.deviceId('localDeviceId'); 5682 query.prefixKey("batch_test"); 5683 if (kvStore != null) { 5684 kvStore.getEntries('localDeviceId', query, (err: BusinessError, entries: distributedKVStore.Entry[]) => { 5685 if (err != undefined) { 5686 console.error(`Failed to get entries.code is ${err.code},message is ${err.message}`); 5687 return; 5688 } 5689 console.info('Succeeded in getting entries'); 5690 console.info(`entries.length: ${entries.length}`); 5691 console.info(`entries[0]: ${entries[0]}`); 5692 }) 5693 } 5694 }); 5695 console.info('Succeeded in getting entries'); 5696} catch (e) { 5697 let error = e as BusinessError; 5698 console.error(`Failed to get entries.code is ${error.code},message is ${error.message}`); 5699} 5700``` 5701 5702### getEntries 5703 5704getEntries(deviceId: string, query: Query): Promise<Entry[]> 5705 5706获取与指定设备ID和Query对象匹配的键值对列表,使用Promise异步回调。 5707> **说明:** 5708> 5709> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 5710> deviceId具体获取方式请参考[sync接口示例](#sync)。 5711 5712**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5713 5714**参数:** 5715 5716| 参数名 | 类型 | 必填 | 说明 | 5717| -------- | -------------- | ---- | -------------------- | 5718| deviceId | string | 是 | 键值对所属的设备ID。 | 5719| query | [Query](#query) | 是 | 表示查询对象。 | 5720 5721**返回值:** 5722 5723| 类型 | 说明 | 5724| -------------------------------- | ---------------------------------------------------------- | 5725| Promise<[Entry](#entry)[]> | Promise对象。返回与指定设备ID和Query对象匹配的键值对列表。 | 5726 5727**错误码:** 5728 5729以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5730 5731| **错误码ID** | **错误信息** | 5732| ------------ | -------------------------------------- | 5733| 15100003 | Database corrupted. | 5734| 15100005 | Database or result set already closed. | 5735 5736**示例:** 5737 5738```ts 5739import { BusinessError } from '@ohos.base'; 5740 5741try { 5742 let arr = new Uint8Array([21, 31]); 5743 let entries: distributedKVStore.Entry[] = []; 5744 for (let i = 0; i < 10; i++) { 5745 let key = 'batch_test_bool_key'; 5746 let entry: distributedKVStore.Entry = { 5747 key: key + i, 5748 value: { 5749 type: distributedKVStore.ValueType.BYTE_ARRAY, 5750 value: arr 5751 } 5752 } 5753 entries.push(entry); 5754 } 5755 console.info(`entries: ${entries}`); 5756 kvStore.putBatch(entries).then(async () => { 5757 console.info('Succeeded in putting batch'); 5758 let query = new distributedKVStore.Query(); 5759 query.deviceId('localDeviceId'); 5760 query.prefixKey("batch_test"); 5761 if (kvStore != null) { 5762 kvStore.getEntries('localDeviceId', query).then((entries: distributedKVStore.Entry[]) => { 5763 console.info('Succeeded in getting entries'); 5764 }).catch((err: BusinessError) => { 5765 console.error(`Failed to get entries.code is ${err.code},message is ${err.message}`); 5766 }); 5767 } 5768 }).catch((err: BusinessError) => { 5769 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 5770 }); 5771 console.info('Succeeded in getting entries'); 5772} catch (e) { 5773 let error = e as BusinessError; 5774 console.error(`Failed to get entries.code is ${error.code},message is ${error.message}`); 5775} 5776``` 5777 5778### getResultSet 5779 5780getResultSet(keyPrefix: string, callback: AsyncCallback<KVStoreResultSet>): void 5781 5782从DeviceKVStore数据库中获取本设备具有指定前缀的结果集,使用callback异步回调。 5783 5784**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 5785 5786**参数:** 5787 5788| 参数名 | 类型 | 必填 | 说明 | 5789| --------- | ---------------------------------------------------------- | ---- | ------------------------------------ | 5790| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 5791| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | 是 | 回调函数。返回具有指定前缀的结果集。 | 5792 5793**错误码:** 5794 5795以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5796 5797| **错误码ID** | **错误信息** | 5798| ------------ | -------------------------------------- | 5799| 15100001 | Over max limits. | 5800| 15100003 | Database corrupted. | 5801| 15100005 | Database or result set already closed. | 5802 5803**示例:** 5804 5805```ts 5806import { BusinessError } from '@ohos.base'; 5807 5808try { 5809 let resultSet: distributedKVStore.KVStoreResultSet; 5810 let entries: distributedKVStore.Entry[] = []; 5811 for (let i = 0; i < 10; i++) { 5812 let key = 'batch_test_string_key'; 5813 let entry: distributedKVStore.Entry = { 5814 key: key + i, 5815 value: { 5816 type: distributedKVStore.ValueType.STRING, 5817 value: 'batch_test_string_value' 5818 } 5819 } 5820 entries.push(entry); 5821 } 5822 kvStore.putBatch(entries, async (err: BusinessError) => { 5823 if (err != undefined) { 5824 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 5825 return; 5826 } 5827 console.info('Succeeded in putting batch'); 5828 if (kvStore != null) { 5829 kvStore.getResultSet('batch_test_string_key', async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 5830 if (err != undefined) { 5831 console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 5832 return; 5833 } 5834 console.info('Succeeded in getting result set'); 5835 resultSet = result; 5836 if (kvStore != null) { 5837 kvStore.closeResultSet(resultSet, (err: BusinessError) => { 5838 if (err != undefined) { 5839 console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 5840 return; 5841 } 5842 console.info('Succeeded in closing result set'); 5843 }) 5844 } 5845 }); 5846 } 5847 }); 5848} catch (e) { 5849 let error = e as BusinessError; 5850 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`); 5851} 5852``` 5853 5854### getResultSet 5855 5856getResultSet(keyPrefix: string): Promise<KVStoreResultSet> 5857 5858从DeviceKVStore数据库中获取本设备具有指定前缀的结果集,使用Promise异步回调。 5859 5860**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 5861 5862**参数:** 5863 5864| 参数名 | 类型 | 必填 | 说明 | 5865| --------- | ------ | ---- | -------------------- | 5866| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 5867 5868**返回值:** 5869 5870| 类型 | 说明 | 5871| ---------------------------------------------------- | --------------------------------------- | 5872| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise对象。返回具有指定前缀的结果集。 | 5873 5874**错误码:** 5875 5876以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5877 5878| **错误码ID** | **错误信息** | 5879| ------------ | -------------------------------------- | 5880| 15100001 | Over max limits. | 5881| 15100003 | Database corrupted. | 5882| 15100005 | Database or result set already closed. | 5883 5884**示例:** 5885 5886```ts 5887import { BusinessError } from '@ohos.base'; 5888 5889try { 5890 let resultSet: distributedKVStore.KVStoreResultSet; 5891 let entries: distributedKVStore.Entry[] = []; 5892 for (let i = 0; i < 10; i++) { 5893 let key = 'batch_test_string_key'; 5894 let entry: distributedKVStore.Entry = { 5895 key: key + i, 5896 value: { 5897 type: distributedKVStore.ValueType.STRING, 5898 value: 'batch_test_string_value' 5899 } 5900 } 5901 entries.push(entry); 5902 } 5903 kvStore.putBatch(entries).then(async () => { 5904 console.info('Succeeded in putting batch'); 5905 }).catch((err: BusinessError) => { 5906 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 5907 }); 5908 kvStore.getResultSet('batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 5909 console.info('Succeeded in getting result set'); 5910 resultSet = result; 5911 if (kvStore != null) { 5912 kvStore.closeResultSet(resultSet).then(() => { 5913 console.info('Succeeded in closing result set'); 5914 }).catch((err: BusinessError) => { 5915 console.error(`Failed to close resultset.code is ${err.code},message is ${err.message}`); 5916 }); 5917 } 5918 }).catch((err: BusinessError) => { 5919 console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 5920 }); 5921} catch (e) { 5922 let error = e as BusinessError; 5923 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 5924} 5925``` 5926 5927### getResultSet 5928 5929getResultSet(deviceId: string, keyPrefix: string, callback: AsyncCallback<KVStoreResultSet>): void 5930 5931获取与指定设备ID和key前缀匹配的KVStoreResultSet对象,使用callback异步回调。 5932> **说明:** 5933> 5934> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 5935> deviceId具体获取方式请参考[sync接口示例](#sync)。 5936 5937**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5938 5939**参数:** 5940 5941| 参数名 | 类型 | 必填 | 说明 | 5942| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5943| deviceId | string | 是 | 标识要查询其数据的设备。 | 5944| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 5945| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | 是 | 回调函数。返回与指定设备ID和key前缀匹配的KVStoreResultSet对象。 | 5946 5947**错误码:** 5948 5949以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 5950 5951| **错误码ID** | **错误信息** | 5952| ------------ | -------------------------------------- | 5953| 15100001 | Over max limits. | 5954| 15100003 | Database corrupted. | 5955| 15100005 | Database or result set already closed. | 5956 5957**示例:** 5958 5959```ts 5960import { BusinessError } from '@ohos.base'; 5961 5962try { 5963 let resultSet: distributedKVStore.KVStoreResultSet; 5964 kvStore.getResultSet('localDeviceId', 'batch_test_string_key', async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 5965 if (err != undefined) { 5966 console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 5967 return; 5968 } 5969 console.info('Succeeded in getting resultSet'); 5970 resultSet = result; 5971 if (kvStore != null) { 5972 kvStore.closeResultSet(resultSet, (err: BusinessError) => { 5973 if (err != undefined) { 5974 console.error(`Failed to close resultSet.code is ${err.code},message is ${err.message}`); 5975 return; 5976 } 5977 console.info('Succeeded in closing resultSet'); 5978 }) 5979 } 5980 }); 5981} catch (e) { 5982 let error = e as BusinessError; 5983 console.error(`Failed to get resultSet.code is ${error.code},message is ${error.message}`); 5984} 5985``` 5986 5987### getResultSet 5988 5989getResultSet(deviceId: string, keyPrefix: string): Promise<KVStoreResultSet> 5990 5991获取与指定设备ID和key前缀匹配的KVStoreResultSet对象,使用Promise异步回调。 5992> **说明:** 5993> 5994> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 5995> deviceId具体获取方式请参考[sync接口示例](#sync)。 5996 5997**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 5998 5999**参数:** 6000 6001| 参数名 | 类型 | 必填 | 说明 | 6002| --------- | -------- | ---- | ------------------------ | 6003| deviceId | string | 是 | 标识要查询其数据的设备。 | 6004| keyPrefix | string | 是 | 表示要匹配的键前缀。 | 6005 6006**返回值:** 6007 6008| 类型 | 说明 | 6009| ------------------------------------------------------ | ------------------------------------------------------------ | 6010| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise对象。返回与指定设备ID和key前缀匹配的KVStoreResultSet对象。 | 6011 6012**错误码:** 6013 6014以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 6015 6016| **错误码ID** | **错误信息** | 6017| ------------ | -------------------------------------- | 6018| 15100001 | Over max limits. | 6019| 15100003 | Database corrupted. | 6020| 15100005 | Database or result set already closed. | 6021 6022**示例:** 6023 6024```ts 6025import { BusinessError } from '@ohos.base'; 6026 6027try { 6028 let resultSet: distributedKVStore.KVStoreResultSet; 6029 kvStore.getResultSet('localDeviceId', 'batch_test_string_key').then((result: distributedKVStore.KVStoreResultSet) => { 6030 console.info('Succeeded in getting resultSet'); 6031 resultSet = result; 6032 if (kvStore != null) { 6033 kvStore.closeResultSet(resultSet).then(() => { 6034 console.info('Succeeded in closing resultSet'); 6035 }).catch((err: BusinessError) => { 6036 console.error(`Failed to close resultSet.code is ${err.code},message is ${err.message}`); 6037 }); 6038 } 6039 }).catch((err: BusinessError) => { 6040 console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 6041 }); 6042} catch (e) { 6043 let error = e as BusinessError; 6044 console.error(`Failed to get resultSet.code is ${error.code},message is ${error.message}`); 6045} 6046``` 6047 6048### getResultSet 6049 6050getResultSet(deviceId: string, query: Query, callback: AsyncCallback<KVStoreResultSet>): void 6051 6052获取与指定设备ID和Query对象匹配的KVStoreResultSet对象,使用callback异步回调。 6053> **说明:** 6054> 6055> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 6056> deviceId具体获取方式请参考[sync接口示例](#sync)。 6057 6058**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6059 6060**参数:** 6061 6062| 参数名 | 类型 | 必填 | 说明 | 6063| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6064| deviceId | string | 是 | KVStoreResultSet对象所属的设备ID。 | 6065| query | [Query](#query) | 是 | 表示查询对象。 | 6066| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | 是 | 回调函数。返回与指定设备ID和Query对象匹配的KVStoreResultSet对象。 | 6067 6068**错误码:** 6069 6070以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 6071 6072| **错误码ID** | **错误信息** | 6073| ------------ | -------------------------------------- | 6074| 15100001 | Over max limits. | 6075| 15100003 | Database corrupted. | 6076| 15100005 | Database or result set already closed. | 6077 6078**示例:** 6079 6080```ts 6081import { BusinessError } from '@ohos.base'; 6082 6083try { 6084 let resultSet: distributedKVStore.KVStoreResultSet; 6085 let entries: distributedKVStore.Entry[] = []; 6086 for (let i = 0; i < 10; i++) { 6087 let key = 'batch_test_string_key'; 6088 let entry: distributedKVStore.Entry = { 6089 key: key + i, 6090 value: { 6091 type: distributedKVStore.ValueType.STRING, 6092 value: 'batch_test_string_value' 6093 } 6094 } 6095 entries.push(entry); 6096 } 6097 kvStore.putBatch(entries, async (err: BusinessError) => { 6098 if (err != undefined) { 6099 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6100 return; 6101 } 6102 console.info('Succeeded in putting batch'); 6103 const query = new distributedKVStore.Query(); 6104 query.prefixKey("batch_test"); 6105 if (kvStore != null) { 6106 kvStore.getResultSet('localDeviceId', query, async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 6107 if (err != undefined) { 6108 console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 6109 return; 6110 } 6111 console.info('Succeeded in getting resultSet'); 6112 resultSet = result; 6113 if (kvStore != null) { 6114 kvStore.closeResultSet(resultSet, (err: BusinessError) => { 6115 if (err != undefined) { 6116 console.error(`Failed to close resultSet.code is ${err.code},message is ${err.message}`); 6117 return; 6118 } 6119 console.info('Succeeded in closing resultSet'); 6120 }) 6121 } 6122 }); 6123 } 6124 }); 6125} catch (e) { 6126 let error = e as BusinessError; 6127 console.error(`Failed to get resultSet.code is ${error.code},message is ${error.message}`); 6128} 6129``` 6130 6131### getResultSet 6132 6133getResultSet(deviceId: string, query: Query): Promise<KVStoreResultSet> 6134 6135获取与指定设备ID和Query对象匹配的KVStoreResultSet对象,使用Promise异步回调。 6136> **说明:** 6137> 6138> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 6139> deviceId具体获取方式请参考[sync接口示例](#sync)。 6140 6141**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6142 6143**参数:** 6144 6145| 参数名 | 类型 | 必填 | 说明 | 6146| -------- | -------------- | ---- | ---------------------------------- | 6147| deviceId | string | 是 | KVStoreResultSet对象所属的设备ID。 | 6148| query | [Query](#query) | 是 | 表示查询对象。 | 6149 6150**返回值:** 6151 6152| 类型 | 说明 | 6153| ------------------------------------------------------ | ------------------------------------------------------------ | 6154| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise对象。返回与指定设备ID和Query对象匹配的KVStoreResultSet对象。 | 6155 6156**错误码:** 6157 6158以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 6159 6160| **错误码ID** | **错误信息** | 6161| ------------ | -------------------------------------- | 6162| 15100001 | Over max limits. | 6163| 15100003 | Database corrupted. | 6164| 15100005 | Database or result set already closed. | 6165 6166**示例:** 6167 6168```ts 6169import { BusinessError } from '@ohos.base'; 6170 6171try { 6172 let resultSet: distributedKVStore.KVStoreResultSet; 6173 let entries: distributedKVStore.Entry[] = []; 6174 for (let i = 0; i < 10; i++) { 6175 let key = 'batch_test_string_key'; 6176 let entry: distributedKVStore.Entry = { 6177 key: key + i, 6178 value: { 6179 type: distributedKVStore.ValueType.STRING, 6180 value: 'batch_test_string_value' 6181 } 6182 } 6183 entries.push(entry); 6184 } 6185 kvStore.putBatch(entries).then(async () => { 6186 console.info('Succeeded in putting batch'); 6187 }).catch((err: BusinessError) => { 6188 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6189 }); 6190 const query = new distributedKVStore.Query(); 6191 query.prefixKey("batch_test"); 6192 if (kvStore != null) { 6193 kvStore.getResultSet('localDeviceId', query).then((result: distributedKVStore.KVStoreResultSet) => { 6194 console.info('Succeeded in getting resultSet'); 6195 resultSet = result; 6196 if (kvStore != null) { 6197 kvStore.closeResultSet(resultSet).then(() => { 6198 console.info('Succeeded in closing resultSet'); 6199 }).catch((err: BusinessError) => { 6200 console.error(`Failed to close resultSet.code is ${err.code},message is ${err.message}`); 6201 }); 6202 } 6203 }).catch((err: BusinessError) => { 6204 console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 6205 }); 6206 } 6207 query.deviceId('localDeviceId'); 6208 console.info("GetResultSet " + query.getSqlLike()); 6209 6210} catch (e) { 6211 let error = e as BusinessError; 6212 console.error(`Failed to get resultSet.code is ${error.code},message is ${error.message}`); 6213} 6214``` 6215 6216### getResultSet 6217 6218getResultSet(query: Query): Promise<KVStoreResultSet> 6219 6220获取与本设备指定Query对象匹配的KVStoreResultSet对象,使用Promise异步回调。 6221 6222**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 6223 6224**参数:** 6225 6226| 参数名 | 类型 | 必填 | 说明 | 6227| ------ | -------------- | ---- | -------------- | 6228| query | [Query](#query) | 是 | 表示查询对象。 | 6229 6230**返回值:** 6231 6232| 类型 | 说明 | 6233| ---------------------------------------------------- | ------------------------------------------------------------ | 6234| Promise<[KVStoreResultSet](#kvstoreresultset)> | Promise对象。获取与本设备指定Query对象匹配的KVStoreResultSet对象。 | 6235 6236**错误码:** 6237 6238以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 6239 6240| **错误码ID** | **错误信息** | 6241| ------------ | -------------------------------------- | 6242| 15100001 | Over max limits. | 6243| 15100003 | Database corrupted. | 6244| 15100005 | Database or result set already closed. | 6245 6246**示例:** 6247 6248```ts 6249import { BusinessError } from '@ohos.base'; 6250 6251try { 6252 let resultSet: distributedKVStore.KVStoreResultSet; 6253 let entries: distributedKVStore.Entry[] = []; 6254 for (let i = 0; i < 10; i++) { 6255 let key = 'batch_test_string_key'; 6256 let entry: distributedKVStore.Entry = { 6257 key: key + i, 6258 value: { 6259 type: distributedKVStore.ValueType.STRING, 6260 value: 'batch_test_string_value' 6261 } 6262 } 6263 entries.push(entry); 6264 } 6265 kvStore.putBatch(entries).then(async () => { 6266 console.info('Succeeded in putting batch'); 6267 }).catch((err: BusinessError) => { 6268 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6269 }); 6270 const query = new distributedKVStore.Query(); 6271 query.prefixKey("batch_test"); 6272 kvStore.getResultSet(query).then((result: distributedKVStore.KVStoreResultSet) => { 6273 console.info('Succeeded in getting result set'); 6274 resultSet = result; 6275 }).catch((err: BusinessError) => { 6276 console.error(`Failed to get resultset.code is ${err.code},message is ${err.message}`); 6277 }); 6278} catch (e) { 6279 let error = e as BusinessError; 6280 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 6281} 6282``` 6283 6284### getResultSet 6285 6286getResultSet(query: Query, callback:AsyncCallback<KVStoreResultSet>): void 6287 6288获取与本设备指定Query对象匹配的KVStoreResultSet对象,使用callback异步回调。 6289> **说明:** 6290> 6291> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 6292> deviceId具体获取方式请参考[sync接口示例](#sync)。 6293 6294**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 6295 6296**参数:** 6297 6298| 参数名 | 类型 | 必填 | 说明 | 6299| -------- | -------------- | ---- | ---------------------------------- | 6300| query | [Query](#query) | 是 | 表示查询对象。 | 6301| callback | AsyncCallback<[KVStoreResultSet](#kvstoreresultset)> | 是 | 回调函数,获取与指定Predicates对象匹配的KVStoreResultSet对象。 | 6302 6303 6304**错误码:** 6305 6306以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 6307 6308| **错误码ID** | **错误信息** | 6309| ------------ | -------------------------------------- | 6310| 15100001 | Over max limits. | 6311| 15100003 | Database corrupted. | 6312| 15100005 | Database or result set already closed. | 6313 6314**示例:** 6315 6316```ts 6317import { BusinessError } from '@ohos.base'; 6318 6319try { 6320 let resultSet: distributedKVStore.KVStoreResultSet; 6321 let entries: distributedKVStore.Entry[] = []; 6322 for (let i = 0; i < 10; i++) { 6323 let key = 'batch_test_string_key'; 6324 let entry: distributedKVStore.Entry = { 6325 key: key + i, 6326 value: { 6327 type: distributedKVStore.ValueType.STRING, 6328 value: 'batch_test_string_value' 6329 } 6330 } 6331 entries.push(entry); 6332 } 6333 kvStore.putBatch(entries, async (err: BusinessError) => { 6334 if (err != undefined) { 6335 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6336 return; 6337 } 6338 console.info('Succeeded in putting batch'); 6339 const query = new distributedKVStore.Query(); 6340 query.prefixKey("batch_test"); 6341 if (kvStore != null) { 6342 kvStore.getResultSet(query, async (err: BusinessError, result: distributedKVStore.KVStoreResultSet) => { 6343 if (err != undefined) { 6344 console.error(`Failed to get resultSet.code is ${err.code},message is ${err.message}`); 6345 return; 6346 } 6347 console.info('Succeeded in getting resultSet'); 6348 resultSet = result; 6349 if (kvStore != null) { 6350 kvStore.closeResultSet(resultSet, (err: BusinessError) => { 6351 if (err != undefined) { 6352 console.error(`Failed to close resultSet.code is ${err.code},message is ${err.message}`); 6353 return; 6354 } 6355 console.info('Succeeded in closing resultSet'); 6356 }) 6357 } 6358 }); 6359 } 6360 }); 6361} catch (e) { 6362 let error = e as BusinessError; 6363 console.error(`Failed to get resultSet.code is ${error.code},message is ${error.message}`); 6364} 6365``` 6366 6367### getResultSize 6368 6369getResultSize(query: Query, callback: AsyncCallback<number>): void 6370 6371获取与本设备指定Query对象匹配的结果数,使用callback异步回调。 6372 6373**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 6374 6375**参数:** 6376 6377| 参数名 | 类型 | 必填 | 说明 | 6378| -------- | --------------------------- | ---- | ------------------------------------------------- | 6379| query | [Query](#query) | 是 | 表示查询对象。 | 6380| callback | AsyncCallback<number> | 是 | 回调函数。返回与本设备指定Query对象匹配的结果数。 | 6381 6382**错误码:** 6383 6384以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 6385 6386| **错误码ID** | **错误信息** | 6387| ------------ | -------------------------------------- | 6388| 15100003 | Database corrupted. | 6389| 15100005 | Database or result set already closed. | 6390 6391**示例:** 6392 6393```ts 6394import { BusinessError } from '@ohos.base'; 6395 6396try { 6397 let entries: distributedKVStore.Entry[] = []; 6398 for (let i = 0; i < 10; i++) { 6399 let key = 'batch_test_string_key'; 6400 let entry: distributedKVStore.Entry = { 6401 key: key + i, 6402 value: { 6403 type: distributedKVStore.ValueType.STRING, 6404 value: 'batch_test_string_value' 6405 } 6406 } 6407 entries.push(entry); 6408 } 6409 kvStore.putBatch(entries, async (err: BusinessError) => { 6410 console.info('Succeeded in putting batch'); 6411 const query = new distributedKVStore.Query(); 6412 query.prefixKey("batch_test"); 6413 if (kvStore != null) { 6414 kvStore.getResultSize(query, async (err: BusinessError, resultSize: number) => { 6415 if (err != undefined) { 6416 console.error(`Failed to get result size.code is ${err.code},message is ${err.message}`); 6417 return; 6418 } 6419 console.info('Succeeded in getting result set size'); 6420 }); 6421 } 6422 }); 6423} catch (e) { 6424 let error = e as BusinessError; 6425 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 6426} 6427``` 6428 6429### getResultSize 6430 6431getResultSize(query: Query): Promise<number> 6432 6433获取与本设备指定Query对象匹配的结果数,使用Promise异步回调。 6434 6435**系统能力:** SystemCapability.DistributedDataManager.KVStore.Core 6436 6437**参数:** 6438 6439| 参数名 | 类型 | 必填 | 说明 | 6440| ------ | -------------- | ---- | -------------- | 6441| query | [Query](#query) | 是 | 表示查询对象。 | 6442 6443**返回值:** 6444 6445| 类型 | 说明 | 6446| --------------------- | ---------------------------------------------------- | 6447| Promise<number> | Promise对象。获取与本设备指定Query对象匹配的结果数。 | 6448 6449**错误码:** 6450 6451以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 6452 6453| **错误码ID** | **错误信息** | 6454| ------------ | -------------------------------------- | 6455| 15100003 | Database corrupted. | 6456| 15100005 | Database or result set already closed. | 6457 6458**示例:** 6459 6460```ts 6461import { BusinessError } from '@ohos.base'; 6462 6463try { 6464 let entries: distributedKVStore.Entry[] = []; 6465 for (let i = 0; i < 10; i++) { 6466 let key = 'batch_test_string_key'; 6467 let entry: distributedKVStore.Entry = { 6468 key: key + i, 6469 value: { 6470 type: distributedKVStore.ValueType.STRING, 6471 value: 'batch_test_string_value' 6472 } 6473 } 6474 entries.push(entry); 6475 } 6476 kvStore.putBatch(entries).then(async () => { 6477 console.info('Succeeded in putting batch'); 6478 }).catch((err: BusinessError) => { 6479 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6480 }); 6481 const query = new distributedKVStore.Query(); 6482 query.prefixKey("batch_test"); 6483 kvStore.getResultSize(query).then((resultSize: number) => { 6484 console.info('Succeeded in getting result set size'); 6485 }).catch((err: BusinessError) => { 6486 console.error(`Failed to get result size.code is ${err.code},message is ${err.message}`); 6487 }); 6488} catch (e) { 6489 let error = e as BusinessError; 6490 console.error(`An unexpected error occurred.code is ${error.code},message is ${error.code}`); 6491} 6492``` 6493 6494### getResultSize 6495 6496getResultSize(deviceId: string, query: Query, callback: AsyncCallback<number>): void; 6497 6498获取与指定设备ID和Query对象匹配的结果数,使用callback异步回调。 6499> **说明:** 6500> 6501> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 6502> deviceId具体获取方式请参考[sync接口示例](#sync)。 6503 6504**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6505 6506**参数:** 6507 6508| 参数名 | 类型 | 必填 | 说明 | 6509| -------- | --------------------------- | ---- | --------------------------------------------------- | 6510| deviceId | string | 是 | KVStoreResultSet对象所属的设备ID。 | 6511| query | [Query](#query) | 是 | 表示查询对象。 | 6512| callback | AsyncCallback<number> | 是 | 回调函数。返回与指定设备ID和Query对象匹配的结果数。 | 6513 6514**错误码:** 6515 6516以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 6517 6518| **错误码ID** | **错误信息** | 6519| ------------ | -------------------------------------- | 6520| 15100003 | Database corrupted. | 6521| 15100005 | Database or result set already closed. | 6522 6523**示例:** 6524 6525```ts 6526import { BusinessError } from '@ohos.base'; 6527 6528try { 6529 let entries: distributedKVStore.Entry[] = []; 6530 for (let i = 0; i < 10; i++) { 6531 let key = 'batch_test_string_key'; 6532 let entry: distributedKVStore.Entry = { 6533 key: key + i, 6534 value: { 6535 type: distributedKVStore.ValueType.STRING, 6536 value: 'batch_test_string_value' 6537 } 6538 } 6539 entries.push(entry); 6540 } 6541 kvStore.putBatch(entries, async (err: BusinessError) => { 6542 if (err != undefined) { 6543 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6544 return; 6545 } 6546 console.info('Succeeded in putting batch'); 6547 const query = new distributedKVStore.Query(); 6548 query.prefixKey("batch_test"); 6549 if (kvStore != null) { 6550 kvStore.getResultSize('localDeviceId', query, async (err: BusinessError, resultSize: number) => { 6551 if (err != undefined) { 6552 console.error(`Failed to get resultSize.code is ${err.code},message is ${err.message}`); 6553 return; 6554 } 6555 console.info('Succeeded in getting resultSize'); 6556 }); 6557 } 6558 }); 6559} catch (e) { 6560 let error = e as BusinessError; 6561 console.error(`Failed to get resultSize.code is ${error.code},message is ${error.message}`); 6562} 6563``` 6564 6565### getResultSize 6566 6567getResultSize(deviceId: string, query: Query): Promise<number> 6568 6569获取与指定设备ID和Query对象匹配的结果数,使用Promise异步回调。 6570> **说明:** 6571> 6572> 其中deviceId通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 6573> deviceId具体获取方式请参考[sync接口示例](#sync)。 6574 6575**系统能力:** SystemCapability.DistributedDataManager.KVStore.DistributedKVStore 6576 6577**参数:** 6578 6579| 参数名 | 类型 | 必填 | 说明 | 6580| -------- | -------------- | ---- | ---------------------------------- | 6581| deviceId | string | 是 | KVStoreResultSet对象所属的设备ID。 | 6582| query | [Query](#query) | 是 | 表示查询对象。 | 6583 6584**返回值:** 6585 6586| 类型 | 说明 | 6587| --------------------- | ------------------------------------------------------ | 6588| Promise<number> | Promise对象。返回与指定设备ID和Query对象匹配的结果数。 | 6589 6590**错误码:** 6591 6592以下错误码的详细介绍请参见[分布式键值数据库错误码](errorcode-distributedKVStore.md)。 6593 6594| **错误码ID** | **错误信息** | 6595| ------------ | -------------------------------------- | 6596| 15100003 | Database corrupted. | 6597| 15100005 | Database or result set already closed. | 6598 6599**示例:** 6600 6601```ts 6602import { BusinessError } from '@ohos.base'; 6603 6604try { 6605 let entries: distributedKVStore.Entry[] = []; 6606 for (let i = 0; i < 10; i++) { 6607 let key = 'batch_test_string_key'; 6608 let entry: distributedKVStore.Entry = { 6609 key: key + i, 6610 value: { 6611 type: distributedKVStore.ValueType.STRING, 6612 value: 'batch_test_string_value' 6613 } 6614 } 6615 entries.push(entry); 6616 } 6617 kvStore.putBatch(entries).then(async () => { 6618 console.info('Succeeded in putting batch'); 6619 }).catch((err: BusinessError) => { 6620 console.error(`Failed to put batch.code is ${err.code},message is ${err.message}`); 6621 }); 6622 let query = new distributedKVStore.Query(); 6623 query.prefixKey("batch_test"); 6624 kvStore.getResultSize('localDeviceId', query).then((resultSize: number) => { 6625 console.info('Succeeded in getting resultSize'); 6626 }).catch((err: BusinessError) => { 6627 console.error(`Failed to get resultSize.code is ${err.code},message is ${err.message}`); 6628 }); 6629} catch (e) { 6630 let error = e as BusinessError; 6631 console.error(`Failed to get resultSize.code is ${error.code},message is ${error.message}`); 6632} 6633``` 6634