1# @ohos.data.relationalStore (关系型数据库) 2 3关系型数据库(Relational Database,RDB)是一种基于关系模型来管理数据的数据库。关系型数据库基于SQLite组件提供了一套完整的对本地数据库进行管理的机制,对外提供了一系列的增、删、改、查等接口,也可以直接运行用户输入的SQL语句来满足复杂的场景需要。支持通过[ResultSet.getSendableRow](#getsendablerow12)方法获取Sendable数据,进行跨线程传递。 4 5为保证插入并读取数据成功,建议一条数据不要超过2M。超出该大小,插入成功,读取失败。 6 7大数据量场景下查询数据可能会导致耗时长甚至应用卡死,如有相关操作可参考文档[批量数据写数据库场景](../../arkts-utils/batch-database-operations-guide.md),且有建议如下: 8- 单次查询数据量不超过5000条。 9- 在[TaskPool](../apis-arkts/js-apis-taskpool.md)中查询。 10- 拼接SQL语句尽量简洁。 11- 合理地分批次查询。 12 13该模块提供以下关系型数据库相关的常用功能: 14 15- [RdbPredicates](#rdbpredicates): 数据库中用来代表数据实体的性质、特征或者数据实体之间关系的词项,主要用来定义数据库的操作条件。 16- [RdbStore](#rdbstore):提供管理关系数据库(RDB)方法的接口。 17- [ResultSet](#resultset):提供用户调用关系型数据库查询接口之后返回的结果集合。 18 19> **说明:** 20> 21> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 22 23## 导入模块 24 25```ts 26import { relationalStore } from '@kit.ArkData'; 27``` 28 29## relationalStore.getRdbStore 30 31getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback<RdbStore>): void 32 33获得一个相关的RdbStore,操作关系型数据库,用户可以根据自己的需求配置RdbStore的参数,然后通过RdbStore调用相关接口可以执行相关的数据操作,使用callback异步回调。 34 35当用非加密方式打开一个已有的加密数据库时,会返回错误码14800011,表示数据库损坏。此时用加密方式可以正常打开该数据库。 36 37getRdbStore目前不支持多线程并发操作。 38 39**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 40 41**参数:** 42 43| 参数名 | 类型 | 必填 | 说明 | 44| -------- | ---------------------------------------------- | ---- | ------------------------------------------------------------ | 45| 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)。 | 46| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 47| callback | AsyncCallback<[RdbStore](#rdbstore)> | 是 | 指定callback回调函数,返回RdbStore对象。 | 48 49**错误码:** 50 51以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 52 53| **错误码ID** | **错误信息** | 54|-----------|---------| 55| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 56| 14800000 | Inner error. | 57| 14800010 | Invalid database path. | 58| 14800011 | Database corrupted. | 59| 14801001 | Only supported in stage mode. | 60| 14801002 | The data group id is not valid. | 61| 14800017 | Config changed. | 62| 14800021 | SQLite: Generic error. | 63| 14800022 | SQLite: Callback routine requested an abort. | 64| 14800023 | SQLite: Access permission denied. | 65| 14800027 | SQLite: Attempt to write a readonly database. | 66| 14800028 | SQLite: Some kind of disk I/O error occurred. | 67| 14800029 | SQLite: The database is full. | 68| 14800030 | SQLite: Unable to open the database file. | 69 70**示例:** 71 72FA模型示例: 73 74<!--code_no_check_fa--> 75```js 76import { featureAbility } from '@kit.AbilityKit'; 77import { BusinessError } from '@kit.BasicServicesKit'; 78 79let store: relationalStore.RdbStore | undefined = undefined; 80let context = featureAbility.getContext(); 81 82const STORE_CONFIG: relationalStore.StoreConfig = { 83 name: "RdbTest.db", 84 securityLevel: relationalStore.SecurityLevel.S3 85}; 86 87relationalStore.getRdbStore(context, STORE_CONFIG, (err: BusinessError, rdbStore: relationalStore.RdbStore) => { 88 store = rdbStore; 89 if (err) { 90 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 91 return; 92 } 93 console.info('Get RdbStore successfully.'); 94}) 95``` 96 97Stage模型示例: 98 99```ts 100import { UIAbility } from '@kit.AbilityKit'; 101import { window } from '@kit.ArkUI'; 102import { BusinessError } from '@kit.BasicServicesKit'; 103 104let store: relationalStore.RdbStore | undefined = undefined; 105 106class EntryAbility extends UIAbility { 107 onWindowStageCreate(windowStage: window.WindowStage) { 108 const STORE_CONFIG: relationalStore.StoreConfig = { 109 name: "RdbTest.db", 110 securityLevel: relationalStore.SecurityLevel.S3 111 }; 112 113 relationalStore.getRdbStore(this.context, STORE_CONFIG, (err: BusinessError, rdbStore: relationalStore.RdbStore) => { 114 store = rdbStore; 115 if (err) { 116 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 117 return; 118 } 119 console.info('Get RdbStore successfully.'); 120 }) 121 } 122} 123``` 124 125## relationalStore.getRdbStore 126 127getRdbStore(context: Context, config: StoreConfig): Promise<RdbStore> 128 129获得一个相关的RdbStore,操作关系型数据库,用户可以根据自己的需求配置RdbStore的参数,然后通过RdbStore调用相关接口可以执行相关的数据操作,使用Promise异步回调。 130 131当用非加密方式打开一个已有的加密数据库时,会返回错误码14800011,表示数据库损坏。此时用加密方式可以正常打开该数据库。 132 133getRdbStore目前不支持多线程并发操作。 134 135**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 136 137**参数:** 138 139| 参数名 | 类型 | 必填 | 说明 | 140| ------- | -------------------------------- | ---- | ------------------------------------------------------------ | 141| 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)。 | 142| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 143 144**返回值**: 145 146| 类型 | 说明 | 147| ----------------------------------------- | --------------------------------- | 148| Promise<[RdbStore](#rdbstore)> | Promise对象。返回RdbStore对象。 | 149 150**错误码:** 151 152以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 153 154| **错误码ID** | **错误信息** | 155|-----------| ------------------------------------------------------------ | 156| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 157| 14800000 | Inner error. | 158| 14800010 | Invalid database path. | 159| 14800011 | Database corrupted. | 160| 14801001 | Only supported in stage mode. | 161| 14801002 | The data group id is not valid. | 162| 14800017 | Config changed. | 163| 14800021 | SQLite: Generic error. | 164| 14800027 | SQLite: Attempt to write a readonly database. | 165| 14800028 | SQLite: Some kind of disk I/O error occurred. | 166| 14800029 | SQLite: The database is full. | 167| 14800030 | SQLite: Unable to open the database file. | 168 169**示例:** 170 171FA模型示例: 172 173<!--code_no_check_fa--> 174```js 175import { featureAbility } from '@kit.AbilityKit'; 176import { BusinessError } from '@kit.BasicServicesKit'; 177 178let store: relationalStore.RdbStore | undefined = undefined; 179let context = featureAbility.getContext(); 180 181const STORE_CONFIG: relationalStore.StoreConfig = { 182 name: "RdbTest.db", 183 securityLevel: relationalStore.SecurityLevel.S3 184}; 185 186relationalStore.getRdbStore(context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => { 187 store = rdbStore; 188 console.info('Get RdbStore successfully.') 189}).catch((err: BusinessError) => { 190 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 191}) 192``` 193 194Stage模型示例: 195 196```ts 197import { UIAbility } from '@kit.AbilityKit'; 198import { window } from '@kit.ArkUI'; 199import { BusinessError } from '@kit.BasicServicesKit'; 200 201let store: relationalStore.RdbStore | undefined = undefined; 202 203class EntryAbility extends UIAbility { 204 onWindowStageCreate(windowStage: window.WindowStage) { 205 const STORE_CONFIG: relationalStore.StoreConfig = { 206 name: "RdbTest.db", 207 securityLevel: relationalStore.SecurityLevel.S3 208 }; 209 210 relationalStore.getRdbStore(this.context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => { 211 store = rdbStore; 212 console.info('Get RdbStore successfully.') 213 }).catch((err: BusinessError) => { 214 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 215 }) 216 } 217} 218``` 219 220## relationalStore.deleteRdbStore 221 222deleteRdbStore(context: Context, name: string, callback: AsyncCallback<void>): void 223 224删除数据库文件,使用callback异步回调。 225 226删除成功后,建议将数据库对象置为null。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则调用此接口进行删库无效,必须使用 [deleteRdbStore<sup>10+</sup>](#relationalstoredeleterdbstore10) 接口进行删库。 227 228**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 229 230**参数:** 231 232| 参数名 | 类型 | 必填 | 说明 | 233| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 234| 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)。 | 235| name | string | 是 | 数据库名称。 | 236| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 237 238**错误码:** 239 240以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 241 242| **错误码ID** | **错误信息** | 243|-----------|---------------------------------------| 244| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 245| 14800000 | Inner error. | 246| 14800010 | Failed to open or delete database by invalid database path. | 247 248**示例:** 249 250FA模型示例: 251 252<!--code_no_check_fa--> 253```js 254import { featureAbility } from '@kit.AbilityKit'; 255import { BusinessError } from '@kit.BasicServicesKit'; 256 257let store: relationalStore.RdbStore | undefined = undefined; 258let context = featureAbility.getContext(); 259 260relationalStore.deleteRdbStore(context, "RdbTest.db", (err: BusinessError) => { 261 if (err) { 262 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 263 return; 264 } 265 store = undefined; 266 console.info('Delete RdbStore successfully.'); 267}) 268``` 269 270Stage模型示例: 271 272```ts 273import { UIAbility } from '@kit.AbilityKit'; 274import { window } from '@kit.ArkUI'; 275import { BusinessError } from '@kit.BasicServicesKit'; 276 277let store: relationalStore.RdbStore | undefined = undefined; 278 279class EntryAbility extends UIAbility { 280 onWindowStageCreate(windowStage: window.WindowStage){ 281 relationalStore.deleteRdbStore(this.context, "RdbTest.db", (err: BusinessError) => { 282 if (err) { 283 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 284 return; 285 } 286 store = undefined; 287 console.info('Delete RdbStore successfully.'); 288 }) 289 } 290} 291``` 292 293## relationalStore.deleteRdbStore 294 295deleteRdbStore(context: Context, name: string): Promise<void> 296 297使用指定的数据库文件配置删除数据库,使用Promise异步回调。 298 299删除成功后,建议将数据库对象置为null。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则调用此接口进行删库无效,必须使用 [deleteRdbStore<sup>10+</sup>](#relationalstoredeleterdbstore10-1) 接口进行删库。 300 301**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 302 303**参数** 304 305| 参数名 | 类型 | 必填 | 说明 | 306| ------- | ------- | ---- | ------------------------------------------------------------ | 307| 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)。 | 308| name | string | 是 | 数据库名称。 | 309 310**返回值**: 311 312| 类型 | 说明 | 313| ------------------- | ------------------------- | 314| Promise<void> | 无返回结果的Promise对象。 | 315 316**错误码:** 317 318以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 319 320| **错误码ID** | **错误信息** | 321|-----------|----------------------------------------------------------------------------------| 322| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 323| 14800000 | Inner error. | 324| 14800010 | Invalid database path. | 325 326**示例:** 327 328FA模型示例: 329 330<!--code_no_check_fa--> 331```js 332import { featureAbility } from '@kit.AbilityKit'; 333import { BusinessError } from '@kit.BasicServicesKit'; 334 335let store: relationalStore.RdbStore | undefined = undefined; 336let context = featureAbility.getContext(); 337 338relationalStore.deleteRdbStore(context, "RdbTest.db").then(()=>{ 339 store = undefined; 340 console.info('Delete RdbStore successfully.'); 341}).catch((err: BusinessError) => { 342 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 343}) 344``` 345 346Stage模型示例: 347 348```ts 349import { UIAbility } from '@kit.AbilityKit'; 350import { window } from '@kit.ArkUI'; 351import { BusinessError } from '@kit.BasicServicesKit'; 352 353let store: relationalStore.RdbStore | undefined = undefined; 354 355class EntryAbility extends UIAbility { 356 onWindowStageCreate(windowStage: window.WindowStage){ 357 relationalStore.deleteRdbStore(this.context, "RdbTest.db").then(()=>{ 358 store = undefined; 359 console.info('Delete RdbStore successfully.'); 360 }).catch((err: BusinessError) => { 361 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 362 }) 363 } 364} 365``` 366 367## relationalStore.deleteRdbStore<sup>10+</sup> 368 369deleteRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback\<void>): void 370 371使用指定的数据库文件配置删除数据库,使用callback异步回调。 372 373删除成功后,建议将数据库对象置为null。若数据库文件处于公共沙箱目录下,则删除数据库时必须使用该接口,当存在多个进程操作同一个数据库的情况,建议向其他进程发送数据库删除通知使其感知并处理。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则必须调用此接口进行删库。 374 375**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 376 377**参数:** 378 379| 参数名 | 类型 | 必填 | 说明 | 380| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 381| 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)。 | 382| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 383| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 384 385**错误码:** 386 387以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 388 389| **错误码ID** | **错误信息** | 390|-----------|----------| 391| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 392| 14800000 | Inner error. | 393| 14800010 | Failed to open or delete database by invalid database path. | 394| 14801001 | Only supported in stage mode. | 395| 14801002 | The data group id is not valid. | 396 397**示例:** 398 399FA模型示例: 400 401<!--code_no_check_fa--> 402```js 403import { featureAbility } from '@kit.AbilityKit'; 404import { BusinessError } from '@kit.BasicServicesKit'; 405 406let store: relationalStore.RdbStore | undefined = undefined; 407let context = featureAbility.getContext(); 408 409const STORE_CONFIG: relationalStore.StoreConfig = { 410 name: "RdbTest.db", 411 securityLevel: relationalStore.SecurityLevel.S3 412}; 413 414relationalStore.deleteRdbStore(context, STORE_CONFIG, (err: BusinessError) => { 415 if (err) { 416 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 417 return; 418 } 419 store = undefined; 420 console.info('Delete RdbStore successfully.'); 421}) 422``` 423 424Stage模型示例: 425 426```ts 427import { UIAbility } from '@kit.AbilityKit'; 428import { window } from '@kit.ArkUI'; 429import { BusinessError } from '@kit.BasicServicesKit'; 430 431let store: relationalStore.RdbStore | undefined = undefined; 432 433class EntryAbility extends UIAbility { 434 onWindowStageCreate(windowStage: window.WindowStage){ 435 const STORE_CONFIG: relationalStore.StoreConfig = { 436 name: "RdbTest.db", 437 securityLevel: relationalStore.SecurityLevel.S3 438 }; 439 relationalStore.deleteRdbStore(this.context, STORE_CONFIG, (err: BusinessError) => { 440 if (err) { 441 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 442 return; 443 } 444 store = undefined; 445 console.info('Delete RdbStore successfully.'); 446 }) 447 } 448} 449``` 450 451## relationalStore.deleteRdbStore<sup>10+</sup> 452 453deleteRdbStore(context: Context, config: StoreConfig): Promise\<void> 454 455使用指定的数据库文件配置删除数据库,使用Promise异步回调。 456 457删除成功后,建议将数据库对象置为null。若数据库文件处于公共沙箱目录下,则删除数据库时必须使用该接口,当存在多个进程操作同一个数据库的情况,建议向其他进程发送数据库删除通知使其感知并处理。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则必须调用此接口进行删库。 458 459**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 460 461**参数** 462 463| 参数名 | 类型 | 必填 | 说明 | 464| ------- | --------------------------- | ---- | ------------------------------------------------------------ | 465| 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)。 | 466| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 467 468**返回值**: 469 470| 类型 | 说明 | 471| ------------------- | ------------------------- | 472| Promise<void> | 无返回结果的Promise对象。 | 473 474**错误码:** 475 476以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 477 478| **错误码ID** | **错误信息** | 479|-----------|---------------------| 480| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 481| 801 | Capability not supported. | 482| 14800000 | Inner error. | 483| 14800010 | Invalid database path. | 484| 14801001 | Only supported in stage mode. | 485| 14801002 | The data group id is not valid. | 486 487 488**示例:** 489 490FA模型示例: 491 492<!--code_no_check_fa--> 493```js 494import { featureAbility } from "@kit.AbilityKit"; 495import { BusinessError } from '@kit.BasicServicesKit'; 496 497let store: relationalStore.RdbStore | undefined = undefined; 498let context = featureAbility.getContext(); 499 500const STORE_CONFIG: relationalStore.StoreConfig = { 501 name: "RdbTest.db", 502 securityLevel: relationalStore.SecurityLevel.S3 503}; 504 505relationalStore.deleteRdbStore(context, STORE_CONFIG).then(()=>{ 506 store = undefined; 507 console.info('Delete RdbStore successfully.'); 508}).catch((err: BusinessError) => { 509 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 510}) 511``` 512 513Stage模型示例: 514 515```ts 516import { UIAbility } from '@kit.AbilityKit'; 517import { window } from '@kit.ArkUI'; 518import { BusinessError } from '@kit.BasicServicesKit'; 519 520let store: relationalStore.RdbStore | undefined = undefined; 521 522class EntryAbility extends UIAbility { 523 onWindowStageCreate(windowStage: window.WindowStage){ 524 const STORE_CONFIG: relationalStore.StoreConfig = { 525 name: "RdbTest.db", 526 securityLevel: relationalStore.SecurityLevel.S3 527 }; 528 relationalStore.deleteRdbStore(this.context, STORE_CONFIG).then(()=>{ 529 store = undefined; 530 console.info('Delete RdbStore successfully.'); 531 }).catch((err: BusinessError) => { 532 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 533 }) 534 } 535} 536``` 537 538## StoreConfig 539 540管理关系数据库配置。 541 542| 名称 | 类型 | 必填 | 说明 | 543| ------------- | ------------- | ---- | --------------------------------------------------------- | 544| name | string | 是 | 数据库文件名,也是数据库唯一标识符。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 545| securityLevel | [SecurityLevel](#securitylevel) | 是 | 设置数据库安全级别。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core| 546| encrypt | boolean | 否 | 指定数据库是否加密,默认不加密。<br/> true:加密。<br/> false:非加密。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 547| dataGroupId<sup>10+</sup> | string | 否 | 应用组ID,需要向应用市场获取,暂不支持。<br/>**模型约束:** 此属性仅在Stage模型下可用。<br/>从API version 10开始,支持此可选参数。指定在此dataGroupId对应的沙箱路径下创建RdbStore实例,dataGroupId共沙箱的方式不支持多进程访问加密数据库,当此参数不填时,默认在本应用沙箱目录下创建RdbStore实例。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 548| customDir<sup>11+</sup> | string | 否 | 数据库自定义路径。<br/>**使用约束:** 数据库路径大小限制为128字节,如果超过该大小会开库失败,返回错误。<br/>从API version 11开始,支持此可选参数。数据库将在如下的目录结构中被创建:context.databaseDir + "/rdb/" + customDir,其中context.databaseDir是应用沙箱对应的路径,"/rdb/"表示创建的是关系型数据库,customDir表示自定义的路径。当此参数不填时,默认在本应用沙箱目录下创建RdbStore实例。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 549| autoCleanDirtyData<sup>11+</sup> | boolean | 否 | 指定是否自动清理云端删除后同步到本地的数据,true表示自动清理,false表示手动清理,默认自动清理。<br/>对于端云协同的数据库,当云端删除的数据同步到设备端时,可通过该参数设置设备端是否自动清理。手动清理可以通过[cleanDirtyData<sup>11+</sup>](#cleandirtydata11)接口清理。<br/>从API version 11开始,支持此可选参数。<br/>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 550| allowRebuild<sup>12+</sup> | boolean | 否 | 指定数据库是否支持损坏时自动重建,默认不重建。<br/>true:自动重建。<br/>false:不自动重建。<br/>从API version 12开始,支持此可选参数。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 551| isReadOnly<sup>12+</sup> | boolean | 否 | 指定数据库是否只读,默认为数据库可读写。<br/>true:只允许从数据库读取数据,不允许对数据库进行写操作,否则会返回错误码801。<br/>false:允许对数据库进行读写操作。<br/>从API version 12开始,支持此可选参数。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 552| pluginLibs<sup>12+</sup> | Array\<string> | 否 | 表示包含有fts(Full-Text Search,即全文搜索引擎)等能力的动态库名的数组。<br/>**使用约束:** <br/>1. 动态库名的数量限制最多为16个,如果超过该数量会开库失败,返回错误。<br/>2. 动态库名需为本应用沙箱路径下或系统路径下的动态库,如果动态库无法加载会开库失败,返回错误。<br/>3. 动态库名需为完整路径,用于被sqlite加载。<br/>样例:[context.bundleCodeDir+ "/libs/arm64/" + libtokenizer.so],其中context.bundleCodeDir是应用沙箱对应的路径,"/libs/arm64/"表示子目录,libtokenizer.so表示动态库的文件名。当此参数不填时,默认不加载动态库。<br/>4. 动态库需要包含其全部依赖,避免依赖项丢失导致无法运行。<br/>例如:在ndk工程中,使用默认编译参数构建libtokenizer.so,此动态库依赖c++标准库。在加载此动态库时,由于namespace与编译时不一致,链接到了错误的libc++_shared.so,导致`__emutls_get_address`符号找不到。要解决此问题,需在编译时静态链接c++标准库,具体请参见[NDK工程构建概述](../../napi/build-with-ndk-overview.md)。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 553 554## SecurityLevel 555 556数据库的安全级别枚举。请使用枚举名称而非枚举值。 557 558> **说明:** 559> 560> 若需要进行同步操作,数据库安全等级应不高于对端设备安全等级,具体可见[跨设备同步访问控制机制](../../database/sync-app-data-across-devices-overview.md#跨设备同步访问控制机制)。 561 562**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 563 564| 名称 | 值 | 说明 | 565| ---- | ---- | ------------------------------------------------------------ | 566| S1 | 1 | 表示数据库的安全级别为低级别,当数据泄露时会产生较低影响。例如,包含壁纸等系统数据的数据库。 | 567| S2 | 2 | 表示数据库的安全级别为中级别,当数据泄露时会产生较大影响。例如,包含录音、视频等用户生成数据或通话记录等信息的数据库。 | 568| S3 | 3 | 表示数据库的安全级别为高级别,当数据泄露时会产生重大影响。例如,包含用户运动、健康、位置等信息的数据库。 | 569| S4 | 4 | 表示数据库的安全级别为关键级别,当数据泄露时会产生严重影响。例如,包含认证凭据、财务数据等信息的数据库。 | 570 571## AssetStatus<sup>10+</sup> 572 573描述资产附件的状态枚举。请使用枚举名称而非枚举值。 574 575**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 576 577| 名称 | 值 | 说明 | 578| ------------------------------- | --- | -------------- | 579| ASSET_NORMAL | 1 | 表示资产状态正常。 | 580| ASSET_INSERT | 2 | 表示资产需要插入到云端。 | 581| ASSET_UPDATE | 3 | 表示资产需要更新到云端。 | 582| ASSET_DELETE | 4 | 表示资产需要在云端删除。 | 583| ASSET_ABNORMAL | 5 | 表示资产状态异常。 | 584| ASSET_DOWNLOADING | 6 | 表示资产正在下载到本地设备。 | 585 586## Asset<sup>10+</sup> 587 588记录资产附件(文件、图片、视频等类型文件)的相关信息。 589 590**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 591 592| 名称 | 类型 | 必填 | 说明 | 593| ----------- | --------------------------- | --- | ------------ | 594| name | string | 是 | 资产的名称。 | 595| uri | string | 是 | 资产的uri,在系统里的绝对路径。 | 596| path | string | 是 | 资产在应用沙箱里的路径。 | 597| createTime | string | 是 | 资产被创建出来的时间。 | 598| modifyTime | string | 是 | 资产最后一次被修改的时间。 | 599| size | string | 是 | 资产占用空间的大小。 | 600| status | [AssetStatus](#assetstatus10) | 否 | 资产的状态,默认值为ASSET_NORMAL。 | 601 602## Assets<sup>10+</sup> 603 604type Assets = Asset[] 605 606表示[Asset](#asset10)类型的数组。 607 608**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 609 610| 类型 | 说明 | 611| ------- | -------------------- | 612| [Asset](#asset10)[] | 表示Asset类型的数组。 | 613 614## ValueType 615 616type ValueType = null | number | string | boolean | Uint8Array | Asset | Assets | Float32Array | bigint 617 618用于表示允许的数据字段类型,接口参数具体类型根据其功能而定。 619 620**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 621 622| 类型 | 说明 | 623| ------- | -------------------- | 624| null<sup>10+</sup> | 表示值类型为空。 | 625| number | 表示值类型为数字。 | 626| string | 表示值类型为字符串。 | 627| boolean | 表示值类型为布尔值。 | 628| Uint8Array<sup>10+</sup> | 表示值类型为Uint8类型的数组。 | 629| Asset<sup>10+</sup> | 表示值类型为附件[Asset](#asset10)。<br/>当字段类型是Asset时,在创建表的sql语句中,类型应当为:ASSET。 | 630| Assets<sup>10+</sup> | 表示值类型为附件数组[Assets](#assets10)。<br/>当字段类型是Assets时,在创建表的sql语句中,类型应当为:ASSETS。 | 631| Float32Array<sup>12+</sup> | 表示值类型为浮点数组。<br/>当字段类型是Float32Array时,在创建表的sql语句中,类型应当为:floatvector(128)。 | 632| bigint<sup>12+</sup> | 表示值类型为任意长度的整数。<br/>当字段类型是bigint时,在创建表的sql语句中,类型应当为:UNLIMITED INT, 详见[通过关系型数据库实现数据持久化](../../database/data-persistence-by-rdb-store.md)。<br/>**说明:** bigint类型当前不支持比较大小,不支持如下谓词:between、notBetween、greaterThanlessThan、greaterThanOrEqualTo、lessThanOrEqualTo、orderByAsc、orderByDesc。<br/>bigint类型字段的数据写入时,需通过BigInt()方法或在数据尾部添加'n'的方式明确为bigint类型,如'let data = BigInt(1234)'或'let data = 1234n'。<br/>bigint字段如果写入number类型的数据,则查询该数据的返回类型为number,而非bigint。 | 633 634## ValuesBucket 635 636type ValuesBucket = Record<string, ValueType> 637 638用于存储键值对的类型。不支持Sendable跨线程传递。 639 640**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 641 642| 类型 | 说明 | 643| ---------------- | ---------------------------- | 644| Record<string, [ValueType](#valuetype)> | 表示键值对类型。键的类型为string,值的类型为[ValueType](#valuetype)。 | 645 646## PRIKeyType<sup>10+</sup> 647 648type PRIKeyType = number | string 649 650用于表示数据库表某一行主键的数据类型。 651 652**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 653 654| 类型 | 说明 | 655| ---------------- | ---------------------------------- | 656| number | 主键的类型可以是number。 | 657| string | 主键的类型可以是string。 | 658 659## UTCTime<sup>10+</sup> 660 661type UTCTime = Date 662 663用于表示UTC类型时间的数据类型。 664 665**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 666 667| 类型 | 说明 | 668| ---- | --------------- | 669| Date | UTC类型的时间。 | 670 671## ModifyTime<sup>10+</sup> 672 673type ModifyTime = Map<PRIKeyType, UTCTime> 674 675用于存储数据库表的主键和修改时间的数据类型。 676 677**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 678 679| 类型 | 说明 | 680| ------------------------------------------------------- | ------------------------------------------------------------ | 681| Map<[PRIKeyType](#prikeytype10), [UTCTime](#utctime10)> | 键表示是数据库表某一行的主键,值表示该行的最后修改时间,用UTC格式表示。 | 682 683## SyncMode 684 685指数据库同步模式。请使用枚举名称而非枚举值。 686 687| 名称 | 值 | 说明 | 688| -------------- | ---- | ---------------------------------- | 689| SYNC_MODE_PUSH | 0 | 表示数据从本地设备推送到远程设备。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 690| SYNC_MODE_PULL | 1 | 表示数据从远程设备拉至本地设备。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 691| SYNC_MODE_TIME_FIRST<sup>10+</sup> | 4 | 表示数据从修改时间较近的一端同步到修改时间较远的一端。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 692| SYNC_MODE_NATIVE_FIRST<sup>10+</sup> | 5 | 表示数据从本地设备同步到云端。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 693| SYNC_MODE_CLOUD_FIRST<sup>10+</sup> | 6 | 表示数据从云端同步到本地设备。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 694 695## Origin<sup>11+</sup> 696 697表示数据来源。请使用枚举名称而非枚举值。 698 699**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 700 701| 名称 | 值 | 说明 | 702| -------------- | ---- | ---------------------------------- | 703| LOCAL | 0 | 表示本地数据。 | 704| CLOUD | 1 | 表示云端同步的数据。 | 705| REMOTE | 2 | 表示端端同步的数据。 | 706 707## Field<sup>11+</sup> 708 709用于谓词查询条件的特殊字段。请使用枚举名称而非枚举值。 710 711**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 712 713| 名称 | 值 | 说明 | 714| -------------- | ---- | ---------------------------------- | 715| CURSOR_FIELD | '#_cursor' | 用于cursor查找的字段名。| 716| ORIGIN_FIELD | '#_origin' | 用于cursor查找时指定数据来源的字段名。 | 717| DELETED_FLAG_FIELD | '#_deleted_flag' | 用于cursor查找的结果集返回时填充的字段,表示云端删除的数据同步到本地后数据是否清理。<br>返回的结果集中,该字段对应的value为false表示数据未清理,true表示数据已清理。| 718| DATA_STATUS_FIELD<sup>12+</sup> | '#_data_status' | 用于cursor查找的结果集返回时填充的字段,返回的结果集中,该字段对应的0表示正常数据,1表示退出账号保留数据,2表示云侧同步删除,3表示退出账户删除数据。| 719| OWNER_FIELD | '#_cloud_owner' | 用于共享表中查找owner时,返回的结果集中填充的字段,表示当前共享记录的共享发起者。| 720| PRIVILEGE_FIELD | '#_cloud_privilege' | 用于共享表中查找共享数据权限时,返回的结果集中填充的字段,表示当前共享记录的允许的操作权限。| 721| SHARING_RESOURCE_FIELD | '#_sharing_resource_field' | 用于数据共享查找共享数据的共享资源时,返回的结果集中填充的字段,表示共享数据的共享资源标识。| 722 723## SubscribeType 724 725描述订阅类型。请使用枚举名称而非枚举值。 726 727| 名称 | 值 | 说明 | 728| --------------------- | ---- | ------------------ | 729| SUBSCRIBE_TYPE_REMOTE | 0 | 订阅远程数据更改。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 730| SUBSCRIBE_TYPE_CLOUD<sup>10+</sup> | 1 | 订阅云端数据更改。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 731| SUBSCRIBE_TYPE_CLOUD_DETAILS<sup>10+</sup> | 2 | 订阅云端数据更改详情。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 732| SUBSCRIBE_TYPE_LOCAL_DETAILS<sup>12+</sup> | 3 | 订阅本地数据更改详情。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 733 734## RebuildType<sup>12+</sup> 735 736描述数据库重建类型的枚举。请使用枚举名称而非枚举值。 737 738**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 739 740| 名称 | 值 | 说明 | 741| ------- | ---- |----------------------------------------------------------------------------------------------------------------| 742| NONE | 0 | 表示数据库未进行重建。 | 743| REBUILT | 1 | 表示数据库进行了重建并且生成了空数据库,需要应用重新建表并且恢复数据。 | 744| REPAIRED | 2 | 表示数据库进行了修复,恢复了未损坏的数据,<!--RP2-->当前只有[向量数据库](js-apis-data-relationalStore-sys.md#storeconfig)具备该能力。<!--RP2End--> | 745 746## ChangeType<sup>10+</sup> 747 748描述数据变更类型的枚举。请使用枚举名称而非枚举值。 749 750**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 751 752| 名称 | 值 | 说明 | 753| -------------------------- | --- | -------------------------- | 754| DATA_CHANGE | 0 | 表示是数据发生变更。 | 755| ASSET_CHANGE | 1 | 表示是资产附件发生了变更。 | 756 757## ChangeInfo<sup>10+</sup> 758 759记录端云同步过程详情。 760 761**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 762 763| 名称 | 类型 | 必填 | 说明 | 764| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 765| table | string | 是 | 表示发生变化的表的名称。 | 766| type | [ChangeType](#changetype10) | 是 | 表示发生变化的数据的类型,数据或者资产附件发生变化。 | 767| inserted | Array\<string\> \| Array\<number\> | 是 | 记录插入数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示插入数据的行号。 | 768| updated | Array\<string\> \| Array\<number\> | 是 | 记录更新数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示更新数据的行号。 | 769| deleted | Array\<string\> \| Array\<number\> | 是 | 记录删除数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示删除数据的行号。 | 770 771## DistributedType<sup>10+</sup> 772 773描述表的分布式类型的枚举。请使用枚举名称而非枚举值。 774 775| 名称 | 值 | 说明 | 776| ------------------ | --- | -------------------------------------------------------------------------------------------------- | 777| DISTRIBUTED_DEVICE | 0 | 表示在不同设备之间分布式的数据库表。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 778| DISTRIBUTED_CLOUD | 1 | 表示在设备和云端之间分布式的数据库表。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 779 780## DistributedConfig<sup>10+</sup> 781 782记录表的分布式配置信息。 783 784**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 785 786| 名称 | 类型 | 必填 | 说明 | 787| -------- | ------- | ---- | ------------------------------------------------------------ | 788| autoSync | boolean | 是 | 该值为true时,表示该表支持自动同步和手动同步;该值为false时,表示该表只支持手动同步,不支持自动同步。 | 789 790## ConflictResolution<sup>10+</sup> 791 792插入和修改接口的冲突解决模式。请使用枚举名称而非枚举值。 793 794**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 795 796| 名称 | 值 | 说明 | 797| -------------------- | ---- | ------------------------------------------------------------ | 798| ON_CONFLICT_NONE | 0 | 表示当冲突发生时,不做任何处理。 | 799| ON_CONFLICT_ROLLBACK | 1 | 表示当冲突发生时,中止SQL语句并回滚当前事务。 | 800| ON_CONFLICT_ABORT | 2 | 表示当冲突发生时,中止当前SQL语句,并撤销当前 SQL 语句所做的任何更改,但是由同一事务中先前的 SQL 语句引起的更改被保留并且事务保持活动状态。 | 801| ON_CONFLICT_FAIL | 3 | 表示当冲突发生时,中止当前 SQL 语句。但它不会撤销失败的 SQL 语句的先前更改,也不会结束事务。 | 802| ON_CONFLICT_IGNORE | 4 | 表示当冲突发生时,跳过包含违反约束的行并继续处理 SQL 语句的后续行。 | 803| ON_CONFLICT_REPLACE | 5 | 表示当冲突发生时,在插入或更新当前行之前删除导致约束违例的预先存在的行,并且命令会继续正常执行。 | 804 805## Progress<sup>10+</sup> 806 807描述端云同步过程的枚举。请使用枚举名称而非枚举值。 808 809**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 810 811| 名称 | 值 | 说明 | 812| ---------------- | ---- | ------------------------ | 813| SYNC_BEGIN | 0 | 表示端云同步过程开始。 | 814| SYNC_IN_PROGRESS | 1 | 表示正在端云同步过程中。 | 815| SYNC_FINISH | 2 | 表示端云同步过程已完成。 | 816 817## Statistic<sup>10+</sup> 818 819描述数据库表的端云同步过程的统计信息。 820 821**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 822 823| 名称 | 类型 | 必填 | 说明 | 824| ---------- | ------ | ---- | ---------------------------------------- | 825| total | number | 是 | 表示数据库表中需要端云同步的总行数。 | 826| successful | number | 是 | 表示数据库表中端云同步成功的行数。 | 827| failed | number | 是 | 表示数据库表中端云同步失败的行数。 | 828| remained | number | 是 | 表示数据库表中端云同步剩余未执行的行数。 | 829 830## TableDetails<sup>10+</sup> 831 832描述数据库表执行端云同步任务上传和下载的统计信息。 833 834**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 835 836| 名称 | 类型 | 必填 | 说明 | 837| -------- | ------------------------- | ---- | ------------------------------------------ | 838| upload | [Statistic](#statistic10) | 是 | 表示数据库表中端云同步上传过程的统计信息。 | 839| download | [Statistic](#statistic10) | 是 | 表示数据库表中端云同步下载过程的统计信息。 | 840 841## ProgressCode<sup>10+</sup> 842 843表示端云同步过程的状态。请使用枚举名称而非枚举值。 844 845**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 846 847| 名称 | 值 | 说明 | 848| --------------------- | ---- | ------------------------------------------------------------ | 849| SUCCESS | 0 | 表示端云同步过程成功。 | 850| UNKNOWN_ERROR | 1 | 表示端云同步过程遇到未知错误。 | 851| NETWORK_ERROR | 2 | 表示端云同步过程遇到网络错误。 | 852| CLOUD_DISABLED | 3 | 表示云端不可用。 | 853| LOCKED_BY_OTHERS | 4 | 表示有其他设备正在端云同步,本设备无法进行端云同步。<br>请确保无其他设备占用云端资源后,再使用本设备进行端云同步任务。 | 854| RECORD_LIMIT_EXCEEDED | 5 | 表示本次端云同步需要同步的条目或大小超出最大值。由云端配置最大值。 | 855| NO_SPACE_FOR_ASSET | 6 | 表示云空间剩余空间小于待同步的资产大小。 | 856| BLOCKED_BY_NETWORK_STRATEGY<sup>12+</sup> | 7 | 表示端云同步被网络策略限制。 | 857 858## ProgressDetails<sup>10+</sup> 859 860描述数据库整体执行端云同步任务上传和下载的统计信息。 861 862**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 863 864| 名称 | 类型 | 必填 | 说明 | 865| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 866| schedule | [Progress](#progress10) | 是 | 表示端云同步过程。 | 867| code | [ProgressCode](#progresscode10) | 是 | 表示端云同步过程的状态。 | 868| details | Record<string, [TableDetails](#tabledetails10)> | 是 | 表示端云同步各表的统计信息。<br>键表示表名,值表示该表的端云同步过程统计信息。 | 869 870## SqlExecutionInfo<sup>12+</sup> 871 872描述数据库执行的SQL语句的统计信息。 873 874**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 875 876| 名称 | 类型 | 只读 | 可选 |说明 | 877| -------- | ------------------------------------------------- | ---- | ---- | -------------------------------------------------------- | 878| sql<sup>12+</sup> | Array<string> | 是 | 否 | 表示执行的SQL语句的数组。当[batchInsert](#batchinsert)的参数太大时,可能有多个SQL。 | 879| totalTime<sup>12+</sup> | number | 是 | 否 | 表示执行SQL语句的总时间,单位为μs。 | 880| waitTime<sup>12+</sup> | number | 是 | 否 | 表示获取句柄的时间,单位为μs。 | 881| prepareTime<sup>12+</sup> | number | 是 | 否 | 表示准备SQL和绑定参数的时间,单位为μs。 | 882| executeTime<sup>12+</sup> | number | 是 | 否 | 表示执行SQL语句的时间,单位为μs。 | 883 884## RdbPredicates 885 886表示关系型数据库(RDB)的谓词。该类确定RDB中条件表达式的值是true还是false。谓词间支持多语句拼接,拼接时默认使用and()连接。不支持Sendable跨线程传递。 887 888### constructor 889 890constructor(name: string) 891 892构造函数。 893 894**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 895 896**参数:** 897 898| 参数名 | 类型 | 必填 | 说明 | 899| ------ | ------ | ---- | ------------ | 900| name | string | 是 | 数据库表名。 | 901 902**错误码:** 903 904以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 905 906| **错误码ID** | **错误信息** | 907| --------- |----------------------------------------------------------------------------------------------------------------| 908| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 909 910**示例:** 911 912```ts 913let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 914``` 915 916### inDevices 917 918inDevices(devices: Array<string>): RdbPredicates 919 920同步分布式数据库时连接到组网内指定的远程设备。 921 922> **说明:** 923> 924> 其中devices通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 925数据库同步时调用Sync接口,需要在入参谓词中调用inDevices接口选择设备。如果不调用inDevices接口即默认连接组网内所有的设备。 926 927**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 928 929**参数:** 930 931| 参数名 | 类型 | 必填 | 说明 | 932| ------- | ------------------- | ---- | -------------------------- | 933| devices | Array<string> | 是 | 指定的组网内的远程设备ID。 | 934 935**返回值**: 936 937| 类型 | 说明 | 938| ------------------------------------ | -------------------------- | 939| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 940 941**错误码:** 942 943以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 944 945| **错误码ID** | **错误信息** | 946| --------- |----------------------------------------------------------------------------------------------------------------| 947| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 948 949**示例:** 950 951```ts 952import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 953import { BusinessError } from '@kit.BasicServicesKit'; 954 955let dmInstance: distributedDeviceManager.DeviceManager; 956let deviceIds: Array<string> = []; 957 958try { 959 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 960 let devices: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 961 for (let i = 0; i < devices.length; i++) { 962 deviceIds[i] = devices[i].networkId!; 963 } 964} catch (err) { 965 let code = (err as BusinessError).code; 966 let message = (err as BusinessError).message 967 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 968} 969 970let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 971predicates.inDevices(deviceIds); 972``` 973 974### inAllDevices 975 976inAllDevices(): RdbPredicates 977 978同步分布式数据库时连接到组网内所有的远程设备。 979 980 981**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 982 983**返回值**: 984 985| 类型 | 说明 | 986| ------------------------------------ | -------------------------- | 987| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 988 989**示例:** 990 991```ts 992let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 993predicates.inAllDevices(); 994``` 995 996### equalTo 997 998equalTo(field: string, value: ValueType): RdbPredicates 999 1000配置谓词以匹配数据表的field列中值为value的字段。 1001 1002**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1003 1004**参数:** 1005 1006| 参数名 | 类型 | 必填 | 说明 | 1007| ------ | ----------------------- | ---- | ---------------------- | 1008| field | string | 是 | 数据库表中的列名。 | 1009| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1010 1011**返回值**: 1012 1013| 类型 | 说明 | 1014| ------------------------------------ | -------------------------- | 1015| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1016 1017**错误码:** 1018 1019以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1020 1021| **错误码ID** | **错误信息** | 1022| --------- |----------------------------------------------------------------------------------------------------------------| 1023| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1024 1025**示例:** 1026 1027```ts 1028// 匹配数据表的"NAME"列中值为"Lisa"的字段 1029let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1030predicates.equalTo("NAME", "Lisa"); 1031``` 1032 1033 1034### notEqualTo 1035 1036notEqualTo(field: string, value: ValueType): RdbPredicates 1037 1038配置谓词以匹配数据表的field列中值不为value的字段。 1039 1040**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1041 1042**参数:** 1043 1044| 参数名 | 类型 | 必填 | 说明 | 1045| ------ | ----------------------- | ---- | ---------------------- | 1046| field | string | 是 | 数据库表中的列名。 | 1047| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1048 1049**返回值**: 1050 1051| 类型 | 说明 | 1052| ------------------------------------ | -------------------------- | 1053| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1054 1055**错误码:** 1056 1057以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1058 1059| **错误码ID** | **错误信息** | 1060| --------- |----------------------------------------------------------------------------------------------------------------| 1061| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1062 1063**示例:** 1064 1065```ts 1066// 匹配数据表的"NAME"列中值不为"Lisa"的字段 1067let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1068predicates.notEqualTo("NAME", "Lisa"); 1069``` 1070 1071 1072### beginWrap 1073 1074beginWrap(): RdbPredicates 1075 1076向谓词添加左括号。 1077 1078**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1079 1080**返回值**: 1081 1082| 类型 | 说明 | 1083| ------------------------------------ | ------------------------- | 1084| [RdbPredicates](#rdbpredicates) | 返回带有左括号的Rdb谓词。 | 1085 1086**示例:** 1087 1088```ts 1089let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1090predicates.equalTo("NAME", "Lisa") 1091 .beginWrap() 1092 .equalTo("AGE", 18) 1093 .or() 1094 .equalTo("SALARY", 200.5) 1095 .endWrap() 1096``` 1097 1098### endWrap 1099 1100endWrap(): RdbPredicates 1101 1102向谓词添加右括号。 1103 1104**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1105 1106**返回值**: 1107 1108| 类型 | 说明 | 1109| ------------------------------------ | ------------------------- | 1110| [RdbPredicates](#rdbpredicates) | 返回带有右括号的Rdb谓词。 | 1111 1112**示例:** 1113 1114```ts 1115let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1116predicates.equalTo("NAME", "Lisa") 1117 .beginWrap() 1118 .equalTo("AGE", 18) 1119 .or() 1120 .equalTo("SALARY", 200.5) 1121 .endWrap() 1122``` 1123 1124### or 1125 1126or(): RdbPredicates 1127 1128将或条件添加到谓词中。 1129 1130**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1131 1132**返回值**: 1133 1134| 类型 | 说明 | 1135| ------------------------------------ | ------------------------- | 1136| [RdbPredicates](#rdbpredicates) | 返回带有或条件的Rdb谓词。 | 1137 1138**示例:** 1139 1140```ts 1141// 匹配数据表的"NAME"列中值为"Lisa"或"Rose"的字段 1142let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1143predicates.equalTo("NAME", "Lisa") 1144 .or() 1145 .equalTo("NAME", "Rose") 1146``` 1147 1148### and 1149 1150and(): RdbPredicates 1151 1152向谓词添加和条件。 1153 1154**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1155 1156**返回值**: 1157 1158| 类型 | 说明 | 1159| ------------------------------------ | ------------------------- | 1160| [RdbPredicates](#rdbpredicates) | 返回带有和条件的Rdb谓词。 | 1161 1162**示例:** 1163 1164```ts 1165// 匹配数据表的"NAME"列中值为"Lisa"且"SALARY"列中值为"200.5"的字段 1166let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1167predicates.equalTo("NAME", "Lisa") 1168 .and() 1169 .equalTo("SALARY", 200.5) 1170``` 1171 1172### contains 1173 1174contains(field: string, value: string): RdbPredicates 1175 1176配置谓词以匹配数据表的field列中包含value的字段。 1177 1178**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1179 1180**参数:** 1181 1182| 参数名 | 类型 | 必填 | 说明 | 1183| ------ | ------ | ---- | ---------------------- | 1184| field | string | 是 | 数据库表中的列名。 | 1185| value | string | 是 | 指示要与谓词匹配的值。 | 1186 1187**返回值**: 1188 1189| 类型 | 说明 | 1190| ------------------------------------ | -------------------------- | 1191| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1192 1193**错误码:** 1194 1195以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1196 1197| **错误码ID** | **错误信息** | 1198| --------- |----------------------------------------------------------------------------------------------------------------| 1199| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1200 1201**示例:** 1202 1203```ts 1204// 匹配数据表的"NAME"列中包含"os"的字段,如"Rose" 1205let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1206predicates.contains("NAME", "os"); 1207``` 1208 1209### beginsWith 1210 1211beginsWith(field: string, value: string): RdbPredicates 1212 1213配置谓词以匹配数据表的field列中以value开头的字段。 1214 1215**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1216 1217**参数:** 1218 1219| 参数名 | 类型 | 必填 | 说明 | 1220| ------ | ------ | ---- | ---------------------- | 1221| field | string | 是 | 数据库表中的列名。 | 1222| value | string | 是 | 指示要与谓词匹配的值。 | 1223 1224**返回值**: 1225 1226| 类型 | 说明 | 1227| ------------------------------------ | -------------------------- | 1228| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1229 1230**错误码:** 1231 1232以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1233 1234| **错误码ID** | **错误信息** | 1235| --------- |----------------------------------------------------------------------------------------------------------------| 1236| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1237 1238**示例:** 1239 1240```ts 1241// 匹配数据表的"NAME"列中以"Li"开头的字段,如"Lisa" 1242let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1243predicates.beginsWith("NAME", "Li"); 1244``` 1245 1246### endsWith 1247 1248endsWith(field: string, value: string): RdbPredicates 1249 1250配置谓词以匹配数据表的field列中以value结尾的字段。 1251 1252**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1253 1254**参数:** 1255 1256| 参数名 | 类型 | 必填 | 说明 | 1257| ------ | ------ | ---- | ---------------------- | 1258| field | string | 是 | 数据库表中的列名。 | 1259| value | string | 是 | 指示要与谓词匹配的值。 | 1260 1261**返回值**: 1262 1263| 类型 | 说明 | 1264| ------------------------------------ | -------------------------- | 1265| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1266 1267**错误码:** 1268 1269以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1270 1271| **错误码ID** | **错误信息** | 1272| --------- |----------------------------------------------------------------------------------------------------------------| 1273| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1274 1275**示例:** 1276 1277```ts 1278// 匹配数据表的"NAME"列中以"se"结尾的字段,如"Rose" 1279let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1280predicates.endsWith("NAME", "se"); 1281``` 1282 1283### isNull 1284 1285isNull(field: string): RdbPredicates 1286 1287配置谓词以匹配数据表的field列中值为null的字段。 1288 1289**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1290 1291**参数:** 1292 1293| 参数名 | 类型 | 必填 | 说明 | 1294| ------ | ------ | ---- | ------------------ | 1295| field | string | 是 | 数据库表中的列名。 | 1296 1297**返回值**: 1298 1299| 类型 | 说明 | 1300| ------------------------------------ | -------------------------- | 1301| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1302 1303**错误码:** 1304 1305以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1306 1307| **错误码ID** | **错误信息** | 1308| --------- |----------------------------------------------------------------------------------------------------------------| 1309| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1310 1311**示例**: 1312 1313```ts 1314let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1315predicates.isNull("NAME"); 1316``` 1317 1318### isNotNull 1319 1320isNotNull(field: string): RdbPredicates 1321 1322配置谓词以匹配数据表的field列中值不为null的字段。 1323 1324**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1325 1326**参数:** 1327 1328| 参数名 | 类型 | 必填 | 说明 | 1329| ------ | ------ | ---- | ------------------ | 1330| field | string | 是 | 数据库表中的列名。 | 1331 1332**返回值**: 1333 1334| 类型 | 说明 | 1335| ------------------------------------ | -------------------------- | 1336| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1337 1338**错误码:** 1339 1340以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1341 1342| **错误码ID** | **错误信息** | 1343| --------- |----------------------------------------------------------------------------------------------------------------| 1344| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1345 1346**示例:** 1347 1348```ts 1349let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1350predicates.isNotNull("NAME"); 1351``` 1352 1353### like 1354 1355like(field: string, value: string): RdbPredicates 1356 1357配置谓词以匹配数据表的field列中值类似于value的字段。 1358 1359**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1360 1361**参数:** 1362 1363| 参数名 | 类型 | 必填 | 说明 | 1364| ------ | ------ | ---- | ---------------------- | 1365| field | string | 是 | 数据库表中的列名。 | 1366| value | string | 是 | 指示要与谓词匹配的值。 | 1367 1368**返回值**: 1369 1370| 类型 | 说明 | 1371| ------------------------------------ | -------------------------- | 1372| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1373 1374**错误码:** 1375 1376以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1377 1378| **错误码ID** | **错误信息** | 1379| --------- |----------------------------------------------------------------------------------------------------------------| 1380| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1381 1382**示例:** 1383 1384```ts 1385// 匹配数据表的"NAME"列中值类似于"os"的字段,如"Rose" 1386let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1387predicates.like("NAME", "%os%"); 1388``` 1389 1390### glob 1391 1392glob(field: string, value: string): RdbPredicates 1393 1394配置谓词匹配数据字段为string的指定字段。 1395 1396**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1397 1398**参数:** 1399 1400| 参数名 | 类型 | 必填 | 说明 | 1401| ------ | ------ | ---- | ------------------------------------------------------------ | 1402| field | string | 是 | 数据库表中的列名。 | 1403| value | string | 是 | 指示要与谓词匹配的值。<br>支持通配符,*表示0个、1个或多个数字或字符,?表示1个数字或字符。 | 1404 1405**返回值**: 1406 1407| 类型 | 说明 | 1408| ------------------------------------ | -------------------------- | 1409| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1410 1411**错误码:** 1412 1413以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1414 1415| **错误码ID** | **错误信息** | 1416| --------- |----------------------------------------------------------------------------------------------------------------| 1417| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1418 1419**示例:** 1420 1421```ts 1422// 匹配数据表的"NAME"列中类型为string且值为"?h*g"的字段 1423let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1424predicates.glob("NAME", "?h*g"); 1425``` 1426 1427### between 1428 1429between(field: string, low: ValueType, high: ValueType): RdbPredicates 1430 1431配置谓词以匹配数据表的field列中值在给定范围内的字段(包含范围边界)。 1432 1433**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1434 1435**参数:** 1436 1437| 参数名 | 类型 | 必填 | 说明 | 1438| ------ | ----------------------- | ---- | -------------------------- | 1439| field | string | 是 | 数据库表中的列名。 | 1440| low | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最小值。 | 1441| high | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最大值。 | 1442 1443**返回值**: 1444 1445| 类型 | 说明 | 1446| ------------------------------------ | -------------------------- | 1447| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1448 1449**错误码:** 1450 1451以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1452 1453| **错误码ID** | **错误信息** | 1454| --------- |----------------------------------------------------------------------------------------------------------------| 1455| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1456 1457**示例:** 1458 1459```ts 1460// 匹配数据表的"AGE"列中大于等于10且小于等于50的值 1461let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1462predicates.between("AGE", 10, 50); 1463``` 1464 1465### notBetween 1466 1467notBetween(field: string, low: ValueType, high: ValueType): RdbPredicates 1468 1469配置谓词以匹配数据表的field列中值超出给定范围的字段(不包含范围边界)。 1470 1471**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1472 1473**参数:** 1474 1475| 参数名 | 类型 | 必填 | 说明 | 1476| ------ | ----------------------- | ---- | -------------------------- | 1477| field | string | 是 | 数据库表中的列名。 | 1478| low | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最小值。 | 1479| high | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的最大值。 | 1480 1481**返回值**: 1482 1483| 类型 | 说明 | 1484| ------------------------------------ | -------------------------- | 1485| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1486 1487**错误码:** 1488 1489以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1490 1491| **错误码ID** | **错误信息** | 1492| --------- |----------------------------------------------------------------------------------------------------------------| 1493| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1494 1495**示例:** 1496 1497```ts 1498// 匹配数据表的"AGE"列中小于10或大于50的值 1499let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1500predicates.notBetween("AGE", 10, 50); 1501``` 1502 1503### greaterThan 1504 1505greaterThan(field: string, value: ValueType): RdbPredicates 1506 1507配置谓词以匹配数据表的field列中值大于value的字段。 1508 1509**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1510 1511**参数:** 1512 1513| 参数名 | 类型 | 必填 | 说明 | 1514| ------ | ----------------------- | ---- | ---------------------- | 1515| field | string | 是 | 数据库表中的列名。 | 1516| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1517 1518**返回值**: 1519 1520| 类型 | 说明 | 1521| ------------------------------------ | -------------------------- | 1522| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1523 1524**错误码:** 1525 1526以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1527 1528| **错误码ID** | **错误信息** | 1529| --------- |----------------------------------------------------------------------------------------------------------------| 1530| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1531 1532**示例:** 1533 1534```ts 1535// 匹配数据表的"AGE"列中大于18的值 1536let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1537predicates.greaterThan("AGE", 18); 1538``` 1539 1540### lessThan 1541 1542lessThan(field: string, value: ValueType): RdbPredicates 1543 1544配置谓词以匹配数据表的field列中值小于value的字段。 1545 1546**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1547 1548**参数:** 1549 1550| 参数名 | 类型 | 必填 | 说明 | 1551| ------ | ----------------------- | ---- | ---------------------- | 1552| field | string | 是 | 数据库表中的列名。 | 1553| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1554 1555**返回值**: 1556 1557| 类型 | 说明 | 1558| ------------------------------------ | -------------------------- | 1559| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1560 1561**错误码:** 1562 1563以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1564 1565| **错误码ID** | **错误信息** | 1566| --------- |----------------------------------------------------------------------------------------------------------------| 1567| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1568 1569**示例:** 1570 1571```ts 1572// 匹配数据表的"AGE"列中小于20的值 1573let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1574predicates.lessThan("AGE", 20); 1575``` 1576 1577### greaterThanOrEqualTo 1578 1579greaterThanOrEqualTo(field: string, value: ValueType): RdbPredicates 1580 1581配置谓词以匹配数据表的field列中值大于或者等于value的字段。 1582 1583**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1584 1585**参数:** 1586 1587| 参数名 | 类型 | 必填 | 说明 | 1588| ------ | ----------------------- | ---- | ---------------------- | 1589| field | string | 是 | 数据库表中的列名。 | 1590| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1591 1592**返回值**: 1593 1594| 类型 | 说明 | 1595| ------------------------------------ | -------------------------- | 1596| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1597 1598**错误码:** 1599 1600以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1601 1602| **错误码ID** | **错误信息** | 1603| --------- |----------------------------------------------------------------------------------------------------------------| 1604| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1605 1606**示例:** 1607 1608```ts 1609// 匹配数据表的"AGE"列中大于等于18的值 1610let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1611predicates.greaterThanOrEqualTo("AGE", 18); 1612``` 1613 1614### lessThanOrEqualTo 1615 1616lessThanOrEqualTo(field: string, value: ValueType): RdbPredicates 1617 1618配置谓词以匹配数据表的field列中值小于或者等于value的字段。 1619 1620**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1621 1622**参数:** 1623 1624| 参数名 | 类型 | 必填 | 说明 | 1625| ------ | ----------------------- | ---- | ---------------------- | 1626| field | string | 是 | 数据库表中的列名。 | 1627| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1628 1629**返回值**: 1630 1631| 类型 | 说明 | 1632| ------------------------------------ | -------------------------- | 1633| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1634 1635**错误码:** 1636 1637以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1638 1639| **错误码ID** | **错误信息** | 1640| --------- |----------------------------------------------------------------------------------------------------------------| 1641| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1642 1643**示例:** 1644 1645```ts 1646// 匹配数据表的"AGE"列中小于等于20的值 1647let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1648predicates.lessThanOrEqualTo("AGE", 20); 1649``` 1650 1651### orderByAsc 1652 1653orderByAsc(field: string): RdbPredicates 1654 1655配置谓词以匹配数据表的field列中值按升序排序的列。 1656 1657**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1658 1659**参数:** 1660 1661| 参数名 | 类型 | 必填 | 说明 | 1662| ------ | ------ | ---- | ------------------ | 1663| field | string | 是 | 数据库表中的列名。 | 1664 1665**返回值**: 1666 1667| 类型 | 说明 | 1668| ------------------------------------ | -------------------------- | 1669| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1670 1671**错误码:** 1672 1673以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1674 1675| **错误码ID** | **错误信息** | 1676| --------- |----------------------------------------------------------------------------------------------------------------| 1677| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1678 1679**示例:** 1680 1681```ts 1682let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1683predicates.orderByAsc("NAME"); 1684``` 1685 1686### orderByDesc 1687 1688orderByDesc(field: string): RdbPredicates 1689 1690配置谓词以匹配数据表的field列中值按降序排序的列。 1691 1692**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1693 1694**参数:** 1695 1696| 参数名 | 类型 | 必填 | 说明 | 1697| ------ | ------ | ---- | ------------------ | 1698| field | string | 是 | 数据库表中的列名。 | 1699 1700**返回值**: 1701 1702| 类型 | 说明 | 1703| ------------------------------------ | -------------------------- | 1704| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1705 1706**错误码:** 1707 1708以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1709 1710| **错误码ID** | **错误信息** | 1711| --------- |----------------------------------------------------------------------------------------------------------------| 1712| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1713 1714**示例:** 1715 1716```ts 1717let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1718predicates.orderByDesc("AGE"); 1719``` 1720 1721### distinct 1722 1723distinct(): RdbPredicates 1724 1725配置谓词以过滤重复记录并仅保留其中一个。 1726 1727**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1728 1729**返回值**: 1730 1731| 类型 | 说明 | 1732| ------------------------------------ | ------------------------------ | 1733| [RdbPredicates](#rdbpredicates) | 返回可用于过滤重复记录的谓词。 | 1734 1735**示例:** 1736 1737```ts 1738let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1739predicates.equalTo("NAME", "Rose").distinct(); 1740``` 1741 1742### limitAs 1743 1744limitAs(value: number): RdbPredicates 1745 1746设置最大数据记录数的谓词。 1747 1748**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1749 1750**参数:** 1751 1752| 参数名 | 类型 | 必填 | 说明 | 1753| ------ | ------ | ---- | ---------------- | 1754| value | number | 是 | 最大数据记录数。 | 1755 1756**返回值**: 1757 1758| 类型 | 说明 | 1759| ------------------------------------ | ------------------------------------ | 1760| [RdbPredicates](#rdbpredicates) | 返回可用于设置最大数据记录数的谓词。 | 1761 1762**错误码:** 1763 1764以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1765 1766| **错误码ID** | **错误信息** | 1767| --------- |--------------------------| 1768| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1769 1770**示例:** 1771 1772```ts 1773let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1774predicates.equalTo("NAME", "Rose").limitAs(3); 1775``` 1776 1777### offsetAs 1778 1779offsetAs(rowOffset: number): RdbPredicates 1780 1781配置谓词以指定返回结果的起始位置。 1782 1783**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1784 1785**参数:** 1786 1787| 参数名 | 类型 | 必填 | 说明 | 1788| --------- | ------ | ---- | ---------------------------------- | 1789| rowOffset | number | 是 | 返回结果的起始位置,取值为正整数。 | 1790 1791**返回值**: 1792 1793| 类型 | 说明 | 1794| ------------------------------------ | ------------------------------------ | 1795| [RdbPredicates](#rdbpredicates) | 返回具有指定返回结果起始位置的谓词。 | 1796 1797**错误码:** 1798 1799以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1800 1801| **错误码ID** | **错误信息** | 1802| --------- |----------------------------------------------------------------------------------------------------------------| 1803| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1804 1805**示例:** 1806 1807```ts 1808let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1809predicates.equalTo("NAME", "Rose").offsetAs(3); 1810``` 1811 1812### groupBy 1813 1814groupBy(fields: Array<string>): RdbPredicates 1815 1816配置谓词按指定列分组查询结果。 1817 1818**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1819 1820**参数:** 1821 1822| 参数名 | 类型 | 必填 | 说明 | 1823| ------ | ------------------- | ---- | -------------------- | 1824| fields | Array<string> | 是 | 指定分组依赖的列名。 | 1825 1826**返回值**: 1827 1828| 类型 | 说明 | 1829| ------------------------------------ | ---------------------- | 1830| [RdbPredicates](#rdbpredicates) | 返回分组查询列的谓词。 | 1831 1832**错误码:** 1833 1834以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1835 1836| **错误码ID** | **错误信息** | 1837| --------- |----------------------------------------------------------------------------------------------------------------| 1838| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1839 1840**示例:** 1841 1842```ts 1843let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1844predicates.groupBy(["AGE", "NAME"]); 1845``` 1846 1847### indexedBy 1848 1849indexedBy(field: string): RdbPredicates 1850 1851配置谓词以指定索引列。 1852 1853**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1854 1855**参数:** 1856 1857| 参数名 | 类型 | 必填 | 说明 | 1858| ------ | ------ | ---- | -------------- | 1859| field | string | 是 | 索引列的名称。 | 1860 1861**返回值**: 1862 1863 1864| 类型 | 说明 | 1865| ------------------------------------ | ------------------------------------- | 1866| [RdbPredicates](#rdbpredicates) | 返回具有指定索引列的RdbPredicates。 | 1867 1868**错误码:** 1869 1870以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1871 1872| **错误码ID** | **错误信息** | 1873| --------- |----------------------------------------------------------------------------------------------------------------| 1874| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1875 1876**示例:** 1877 1878```ts 1879let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1880predicates.indexedBy("SALARY"); 1881``` 1882 1883### in 1884 1885in(field: string, value: Array<ValueType>): RdbPredicates 1886 1887配置谓词以匹配数据表的field列中值在给定范围内的字段。 1888 1889**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1890 1891**参数:** 1892 1893| 参数名 | 类型 | 必填 | 说明 | 1894| ------ | ------------------------------------ | ---- | --------------------------------------- | 1895| field | string | 是 | 数据库表中的列名。 | 1896| value | Array<[ValueType](#valuetype)> | 是 | 以ValueType型数组形式指定的要匹配的值。 | 1897 1898**返回值**: 1899 1900| 类型 | 说明 | 1901| ------------------------------------ | -------------------------- | 1902| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1903 1904**错误码:** 1905 1906以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1907 1908| **错误码ID** | **错误信息** | 1909| --------- |----------------------------------------------------------------------------------------------------------------| 1910| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1911 1912**示例:** 1913 1914```ts 1915// 匹配数据表的"AGE"列中在[18,20]中的值 1916let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1917predicates.in("AGE", [18, 20]); 1918``` 1919 1920### notIn 1921 1922notIn(field: string, value: Array<ValueType>): RdbPredicates 1923 1924将谓词配置为匹配数据字段为ValueType且值超出给定范围的指定字段。 1925 1926**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1927 1928**参数:** 1929 1930| 参数名 | 类型 | 必填 | 说明 | 1931| ------ | ------------------------------------ | ---- | ------------------------------------- | 1932| field | string | 是 | 数据库表中的列名。 | 1933| value | Array<[ValueType](#valuetype)> | 是 | 以ValueType数组形式指定的要匹配的值。 | 1934 1935**返回值**: 1936 1937| 类型 | 说明 | 1938| ------------------------------------ | -------------------------- | 1939| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1940 1941**错误码:** 1942 1943以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1944 1945| **错误码ID** | **错误信息** | 1946| --------- |----------------------------------------------------------------------------------------------------------------| 1947| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1948 1949**示例:** 1950 1951```ts 1952// 匹配数据表的"NAME"列中不在["Lisa", "Rose"]中的值 1953let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1954predicates.notIn("NAME", ["Lisa", "Rose"]); 1955``` 1956 1957### notContains<sup>12+</sup> 1958 1959notContains(field: string, value: string): RdbPredicates 1960 1961配置谓词以匹配数据表的field列中不包含value的字段。 1962 1963**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1964 1965**参数:** 1966 1967| 参数名 | 类型 | 必填 | 说明 | 1968| ------ | ------ | ---- | ---------------------- | 1969| field | string | 是 | 数据库表中的列名。 | 1970| value | string | 是 | 指示要与谓词匹配的值。 | 1971 1972**返回值**: 1973 1974| 类型 | 说明 | 1975| ------------------------------- | -------------------------- | 1976| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1977 1978**错误码:** 1979 1980以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1981 1982| **错误码ID** | **错误信息** | 1983| --------- |----------------------------------------------------------------------------------------------------------------| 1984| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1985 1986**示例:** 1987 1988```ts 1989// 匹配数据表的"NAME"列中不包含"os"的字段,如列表中的"Lisa" 1990let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1991predicates.notContains("NAME", "os"); 1992``` 1993 1994### notLike<sup>12+</sup> 1995 1996notLike(field: string, value: string): RdbPredicates 1997 1998配置谓词以匹配数据表的field列中值不存在类似于value的字段。 1999 2000**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2001 2002**参数:** 2003 2004| 参数名 | 类型 | 必填 | 说明 | 2005| ------ | ------ | ---- | ---------------------- | 2006| field | string | 是 | 数据库表中的列名。 | 2007| value | string | 是 | 指示要与谓词匹配的值。 | 2008 2009**返回值**: 2010 2011| 类型 | 说明 | 2012| ------------------------------- | -------------------------- | 2013| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 2014 2015**错误码:** 2016 2017以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2018 2019| **错误码ID** | **错误信息** | 2020| --------- |----------------------------------------------------------------------------------------------------------------| 2021| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2022 2023**示例:** 2024 2025```ts 2026// 匹配数据表的"NAME"列中不等于"os"的字段,如列表中的"Rose" 2027let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2028predicates.notLike("NAME", "os"); 2029``` 2030 2031 2032 2033## RdbStore 2034 2035提供管理关系数据库(RDB)方法的接口。 2036 2037在使用以下相关接口前,请使用[executeSql](#executesql)接口初始化数据库表结构和相关数据。 2038 2039### 属性 2040 2041**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2042 2043| 名称 | 类型 | 只读 | 可选 | 说明 | 2044| ------------ | ----------- | ---- | -------------------------------- | -------------------------------- | 2045| version<sup>10+</sup> | number | 否 | 否 | 设置和获取数据库版本,值为大于0的正整数。 | 2046| rebuilt<sup>12+</sup> | [RebuildType](#rebuildtype12) | 是 | 否 | 用于获取数据库是否进行过重建或修复。 | 2047 2048**错误码:** 2049 2050以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 2051 2052| **错误码ID** | **错误信息** | 2053|-----------| ------------------------------------------------------------ | 2054| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2055| 801 | Capability not supported. | 2056| 14800000 | Inner error. | 2057| 14800014 | Already closed. | 2058| 14800015 | The database does not respond. | 2059| 14800021 | SQLite: Generic error. | 2060| 14800023 | SQLite: Access permission denied. | 2061| 14800024 | SQLite: The database file is locked. | 2062| 14800025 | SQLite: A table in the database is locked. | 2063| 14800026 | SQLite: The database is out of memory. | 2064| 14800027 | SQLite: Attempt to write a readonly database. | 2065| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2066| 14800029 | SQLite: The database is full. | 2067| 14800030 | SQLite: Unable to open the database file. | 2068 2069**示例:** 2070 2071```ts 2072// 设置数据库版本 2073if(store != undefined) { 2074 (store as relationalStore.RdbStore).version = 3; 2075 // 获取数据库版本 2076 console.info(`RdbStore version is ${store.version}`); 2077 // 获取数据库是否重建 2078 console.info(`RdbStore rebuilt is ${store.rebuilt}`); 2079} 2080``` 2081 2082### insert 2083 2084insert(table: string, values: ValuesBucket, callback: AsyncCallback<number>):void 2085 2086向目标表中插入一行数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2087 2088**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2089 2090**参数:** 2091 2092| 参数名 | 类型 | 必填 | 说明 | 2093| -------- | ----------------------------- | ---- | ---------------------------------------------------------- | 2094| table | string | 是 | 指定的目标表名。 | 2095| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 2096| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回行ID;否则返回-1。 | 2097 2098**错误码:** 2099 2100以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2101 2102| **错误码ID** | **错误信息** | 2103|-----------| ------------------------------------------------------------ | 2104| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2105| 14800000 | Inner error. | 2106| 14800011 | Database corrupted. | 2107| 14800014 | Already closed. | 2108| 14800015 | The database does not respond. | 2109| 14800021 | SQLite: Generic error. | 2110| 14800022 | SQLite: Callback routine requested an abort. | 2111| 14800023 | SQLite: Access permission denied. | 2112| 14800024 | SQLite: The database file is locked. | 2113| 14800025 | SQLite: A table in the database is locked. | 2114| 14800026 | SQLite: The database is out of memory. | 2115| 14800027 | SQLite: Attempt to write a readonly database. | 2116| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2117| 14800029 | SQLite: The database is full. | 2118| 14800030 | SQLite: Unable to open the database file. | 2119| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2120| 14800032 | SQLite: Abort due to constraint violation. | 2121| 14800033 | SQLite: Data type mismatch. | 2122| 14800034 | SQLite: Library used incorrectly. | 2123| 14800047 | The WAL file size exceeds the default limit. | 2124 2125**示例:** 2126 2127```ts 2128let value1 = "Lisa"; 2129let value2 = 18; 2130let value3 = 100.5; 2131let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2132 2133// 以下三种方式可用 2134const valueBucket1: relationalStore.ValuesBucket = { 2135 'NAME': value1, 2136 'AGE': value2, 2137 'SALARY': value3, 2138 'CODES': value4, 2139}; 2140const valueBucket2: relationalStore.ValuesBucket = { 2141 NAME: value1, 2142 AGE: value2, 2143 SALARY: value3, 2144 CODES: value4, 2145}; 2146const valueBucket3: relationalStore.ValuesBucket = { 2147 "NAME": value1, 2148 "AGE": value2, 2149 "SALARY": value3, 2150 "CODES": value4, 2151}; 2152 2153if(store != undefined) { 2154 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1, (err: BusinessError, rowId: number) => { 2155 if (err) { 2156 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 2157 return; 2158 } 2159 console.info(`Insert is successful, rowId = ${rowId}`); 2160 }) 2161} 2162``` 2163 2164### insert<sup>10+</sup> 2165 2166insert(table: string, values: ValuesBucket, conflict: ConflictResolution, callback: AsyncCallback<number>):void 2167 2168向目标表中插入一行数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2169 2170**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2171 2172**参数:** 2173 2174| 参数名 | 类型 | 必填 | 说明 | 2175| -------- | ------------------------------------------- | ---- | ---------------------------------------------------------- | 2176| table | string | 是 | 指定的目标表名。 | 2177| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 2178| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。 | 2179| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回行ID;否则返回-1。 | 2180 2181**错误码:** 2182 2183以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2184 2185| **错误码ID** | **错误信息** | 2186|-----------| ---------------------------------------------------- | 2187| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2188| 14800000 | Inner error. | 2189| 14800011 | Database corrupted. | 2190| 14800014 | Already closed. | 2191| 14800015 | The database does not respond. | 2192| 14800021 | SQLite: Generic error. | 2193| 14800022 | SQLite: Callback routine requested an abort. | 2194| 14800023 | SQLite: Access permission denied. | 2195| 14800024 | SQLite: The database file is locked. | 2196| 14800025 | SQLite: A table in the database is locked. | 2197| 14800026 | SQLite: The database is out of memory. | 2198| 14800027 | SQLite: Attempt to write a readonly database. | 2199| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2200| 14800029 | SQLite: The database is full. | 2201| 14800030 | SQLite: Unable to open the database file. | 2202| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2203| 14800032 | SQLite: Abort due to constraint violation. | 2204| 14800033 | SQLite: Data type mismatch. | 2205| 14800034 | SQLite: Library used incorrectly. | 2206| 14800047 | The WAL file size exceeds the default limit. | 2207 2208**示例:** 2209 2210```ts 2211let value1 = "Lisa"; 2212let value2 = 18; 2213let value3 = 100.5; 2214let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2215 2216// 以下三种方式可用 2217const valueBucket1: relationalStore.ValuesBucket = { 2218 'NAME': value1, 2219 'AGE': value2, 2220 'SALARY': value3, 2221 'CODES': value4, 2222}; 2223const valueBucket2: relationalStore.ValuesBucket = { 2224 NAME: value1, 2225 AGE: value2, 2226 SALARY: value3, 2227 CODES: value4, 2228}; 2229const valueBucket3: relationalStore.ValuesBucket = { 2230 "NAME": value1, 2231 "AGE": value2, 2232 "SALARY": value3, 2233 "CODES": value4, 2234}; 2235 2236if(store != undefined) { 2237 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE, 2238 (err: BusinessError, rowId: number) => { 2239 if (err) { 2240 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 2241 return; 2242 } 2243 console.info(`Insert is successful, rowId = ${rowId}`); 2244 }) 2245} 2246``` 2247 2248### insert 2249 2250insert(table: string, values: ValuesBucket):Promise<number> 2251 2252向目标表中插入一行数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2253 2254**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2255 2256**参数:** 2257 2258| 参数名 | 类型 | 必填 | 说明 | 2259| ------ | ----------------------------- | ---- | -------------------------- | 2260| table | string | 是 | 指定的目标表名。 | 2261| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 2262 2263**返回值**: 2264 2265| 类型 | 说明 | 2266| --------------------- | ------------------------------------------------- | 2267| Promise<number> | Promise对象。如果操作成功,返回行ID;否则返回-1。 | 2268 2269**错误码:** 2270 2271以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2272 2273| **错误码ID** | **错误信息** | 2274|-----------| ------------------------------------------------------------ | 2275| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2276| 14800000 | Inner error. | 2277| 14800011 | Database corrupted. | 2278| 14800014 | Already closed. | 2279| 14800015 | The database does not respond. | 2280| 14800021 | SQLite: Generic error. | 2281| 14800022 | SQLite: Callback routine requested an abort. | 2282| 14800023 | SQLite: Access permission denied. | 2283| 14800024 | SQLite: The database file is locked. | 2284| 14800025 | SQLite: A table in the database is locked. | 2285| 14800026 | SQLite: The database is out of memory. | 2286| 14800027 | SQLite: Attempt to write a readonly database. | 2287| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2288| 14800029 | SQLite: The database is full. | 2289| 14800030 | SQLite: Unable to open the database file. | 2290| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2291| 14800032 | SQLite: Abort due to constraint violation. | 2292| 14800033 | SQLite: Data type mismatch. | 2293| 14800034 | SQLite: Library used incorrectly. | 2294| 14800047 | The WAL file size exceeds the default limit. | 2295 2296**示例:** 2297 2298```ts 2299import { BusinessError } from '@kit.BasicServicesKit'; 2300 2301let value1 = "Lisa"; 2302let value2 = 18; 2303let value3 = 100.5; 2304let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2305 2306// 以下三种方式可用 2307const valueBucket1: relationalStore.ValuesBucket = { 2308 'NAME': value1, 2309 'AGE': value2, 2310 'SALARY': value3, 2311 'CODES': value4, 2312}; 2313const valueBucket2: relationalStore.ValuesBucket = { 2314 NAME: value1, 2315 AGE: value2, 2316 SALARY: value3, 2317 CODES: value4, 2318}; 2319const valueBucket3: relationalStore.ValuesBucket = { 2320 "NAME": value1, 2321 "AGE": value2, 2322 "SALARY": value3, 2323 "CODES": value4, 2324}; 2325 2326if(store != undefined) { 2327 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1).then((rowId: number) => { 2328 console.info(`Insert is successful, rowId = ${rowId}`); 2329 }).catch((err: BusinessError) => { 2330 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 2331 }) 2332} 2333``` 2334 2335### insert<sup>10+</sup> 2336 2337insert(table: string, values: ValuesBucket, conflict: ConflictResolution):Promise<number> 2338 2339向目标表中插入一行数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2340 2341**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2342 2343**参数:** 2344 2345| 参数名 | 类型 | 必填 | 说明 | 2346| -------- | ------------------------------------------- | ---- | -------------------------- | 2347| table | string | 是 | 指定的目标表名。 | 2348| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 2349| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。 | 2350 2351**返回值**: 2352 2353| 类型 | 说明 | 2354| --------------------- | ------------------------------------------------- | 2355| Promise<number> | Promise对象。如果操作成功,返回行ID;否则返回-1。 | 2356 2357**错误码:** 2358 2359以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2360 2361| **错误码ID** | **错误信息** | 2362|-----------| ------------------------------------------------------------ | 2363| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2364| 14800000 | Inner error. | 2365| 14800011 | Database corrupted. | 2366| 14800014 | Already closed. | 2367| 14800015 | The database does not respond. | 2368| 14800021 | SQLite: Generic error. | 2369| 14800022 | SQLite: Callback routine requested an abort. | 2370| 14800023 | SQLite: Access permission denied. | 2371| 14800024 | SQLite: The database file is locked. | 2372| 14800025 | SQLite: A table in the database is locked. | 2373| 14800026 | SQLite: The database is out of memory. | 2374| 14800027 | SQLite: Attempt to write a readonly database. | 2375| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2376| 14800029 | SQLite: The database is full. | 2377| 14800030 | SQLite: Unable to open the database file. | 2378| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2379| 14800032 | SQLite: Abort due to constraint violation. | 2380| 14800033 | SQLite: Data type mismatch. | 2381| 14800034 | SQLite: Library used incorrectly. | 2382| 14800047 | The WAL file size exceeds the default limit. | 2383 2384**示例:** 2385 2386```ts 2387import { BusinessError } from '@kit.BasicServicesKit'; 2388 2389let value1 = "Lisa"; 2390let value2 = 18; 2391let value3 = 100.5; 2392let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2393 2394// 以下三种方式可用 2395const valueBucket1: relationalStore.ValuesBucket = { 2396 'NAME': value1, 2397 'AGE': value2, 2398 'SALARY': value3, 2399 'CODES': value4, 2400}; 2401const valueBucket2: relationalStore.ValuesBucket = { 2402 NAME: value1, 2403 AGE: value2, 2404 SALARY: value3, 2405 CODES: value4, 2406}; 2407const valueBucket3: relationalStore.ValuesBucket = { 2408 "NAME": value1, 2409 "AGE": value2, 2410 "SALARY": value3, 2411 "CODES": value4, 2412}; 2413 2414if(store != undefined) { 2415 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE).then((rowId: number) => { 2416 console.info(`Insert is successful, rowId = ${rowId}`); 2417 }).catch((err: BusinessError) => { 2418 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 2419 }) 2420} 2421``` 2422 2423### insertSync<sup>12+</sup> 2424 2425insertSync(table: string, values: ValuesBucket, conflict?: ConflictResolution):number 2426 2427向目标表中插入一行数据。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2428 2429**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2430 2431**参数:** 2432 2433| 参数名 | 类型 | 必填 | 说明 | 2434| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 2435| table | string | 是 | 指定的目标表名。 | 2436| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 2437| conflict | [ConflictResolution](#conflictresolution10) | 否 | 指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 2438 2439**返回值**: 2440 2441| 类型 | 说明 | 2442| ------ | ------------------------------------ | 2443| number | 如果操作成功,返回行ID;否则返回-1。 | 2444 2445**错误码:** 2446 2447以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2448 2449| **错误码ID** | **错误信息** | 2450| ------------ | ------------------------------------------------------------ | 2451| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2452| 14800000 | Inner error. | 2453| 14800011 | Database corrupted. | 2454| 14800014 | Already closed. | 2455| 14800015 | The database does not respond. | 2456| 14800021 | SQLite: Generic error. | 2457| 14800022 | SQLite: Callback routine requested an abort. | 2458| 14800023 | SQLite: Access permission denied. | 2459| 14800024 | SQLite: The database file is locked. | 2460| 14800025 | SQLite: A table in the database is locked. | 2461| 14800026 | SQLite: The database is out of memory. | 2462| 14800027 | SQLite: Attempt to write a readonly database. | 2463| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2464| 14800029 | SQLite: The database is full. | 2465| 14800030 | SQLite: Unable to open the database file. | 2466| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2467| 14800032 | SQLite: Abort due to constraint violation. | 2468| 14800033 | SQLite: Data type mismatch. | 2469| 14800034 | SQLite: Library used incorrectly. | 2470| 14800047 | The WAL file size exceeds the default limit. | 2471 2472**示例:** 2473 2474```ts 2475import { BusinessError } from '@kit.BasicServicesKit'; 2476 2477let value1 = "Lisa"; 2478let value2 = 18; 2479let value3 = 100.5; 2480let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2481 2482// 以下三种方式可用 2483const valueBucket1: relationalStore.ValuesBucket = { 2484 'NAME': value1, 2485 'AGE': value2, 2486 'SALARY': value3, 2487 'CODES': value4, 2488}; 2489const valueBucket2: relationalStore.ValuesBucket = { 2490 NAME: value1, 2491 AGE: value2, 2492 SALARY: value3, 2493 CODES: value4, 2494}; 2495const valueBucket3: relationalStore.ValuesBucket = { 2496 "NAME": value1, 2497 "AGE": value2, 2498 "SALARY": value3, 2499 "CODES": value4, 2500}; 2501 2502if(store != undefined) { 2503 try { 2504 let rowId : number = (store as relationalStore.RdbStore).insertSync("EMPLOYEE", valueBucket1, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE); 2505 console.info(`Insert is successful, rowId = ${rowId}`); 2506 } catch (error) { 2507 console.error(`Insert is failed, code is ${error.code},message is ${error.message}`); 2508 } 2509} 2510``` 2511 2512### insertSync<sup>12+</sup> 2513 2514insertSync(table: string, values: sendableRelationalStore.ValuesBucket, conflict?: ConflictResolution):number 2515 2516传入Sendable数据,向目标表中插入一行数据。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2517 2518**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2519 2520**参数:** 2521 2522| 参数名 | 类型 | 必填 | 说明 | 2523| -------- | ---------------------------------------------------------------------------------------------- | ---- | ------------------------------------------------------------------------------- | 2524| table | string | 是 | 指定的目标表名。 | 2525| values | [sendableRelationalStore.ValuesBucket](./js-apis-data-sendableRelationalStore.md#valuesbucket) | 是 | 表示要插入到表中的可跨线程传递数据。 | 2526| conflict | [ConflictResolution](#conflictresolution10) | 否 | 指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 2527 2528**返回值**: 2529 2530| 类型 | 说明 | 2531| ------ | ------------------------------------ | 2532| number | 如果操作成功,返回行ID;否则返回-1。 | 2533 2534**错误码:** 2535 2536以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2537 2538| **错误码ID** | **错误信息** | 2539| ------------ | ------------------------------------------------------------ | 2540| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2541| 14800000 | Inner error. | 2542| 14800011 | Database corrupted. | 2543| 14800014 | Already closed. | 2544| 14800015 | The database does not respond. | 2545| 14800021 | SQLite: Generic error. | 2546| 14800022 | SQLite: Callback routine requested an abort. | 2547| 14800023 | SQLite: Access permission denied. | 2548| 14800024 | SQLite: The database file is locked. | 2549| 14800025 | SQLite: A table in the database is locked. | 2550| 14800026 | SQLite: The database is out of memory. | 2551| 14800027 | SQLite: Attempt to write a readonly database. | 2552| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2553| 14800029 | SQLite: The database is full. | 2554| 14800030 | SQLite: Unable to open the database file. | 2555| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2556| 14800032 | SQLite: Abort due to constraint violation. | 2557| 14800033 | SQLite: Data type mismatch. | 2558| 14800034 | SQLite: Library used incorrectly. | 2559| 14800047 | The WAL file size exceeds the default limit. | 2560 2561**示例:** 2562 2563```ts 2564import { sendableRelationalStore } from '@kit.ArkData'; 2565 2566const valuesBucket: relationalStore.ValuesBucket = { 2567 "NAME": 'hangman', 2568 "AGE": 18, 2569 "SALARY": 100.5, 2570 "CODES": new Uint8Array([1,2,3]), 2571}; 2572const sendableValuesBucket = sendableRelationalStore.toSendableValuesBucket(valuesBucket); 2573 2574if(store != undefined) { 2575 try { 2576 let rowId : number = (store as relationalStore.RdbStore).insertSync("EMPLOYEE", sendableValuesBucket, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE); 2577 console.info(`Insert is successful, rowId = ${rowId}`); 2578 } catch (error) { 2579 console.error(`Insert is failed, code is ${error.code},message is ${error.message}`); 2580 } 2581} 2582``` 2583 2584### batchInsert 2585 2586batchInsert(table: string, values: Array<ValuesBucket>, callback: AsyncCallback<number>):void 2587 2588向目标表中插入一组数据,使用callback异步回调。 2589 2590**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2591 2592**参数:** 2593 2594| 参数名 | 类型 | 必填 | 说明 | 2595| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 2596| table | string | 是 | 指定的目标表名。 | 2597| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 2598| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回插入的数据个数,否则返回-1。 | 2599 2600**错误码:** 2601 2602以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2603 2604| **错误码ID** | **错误信息** | 2605|-----------| ------------------------------------------------------------ | 2606| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2607| 14800000 | Inner error. | 2608| 14800011 | Database corrupted. | 2609| 14800014 | Already closed. | 2610| 14800015 | The database does not respond. | 2611| 14800021 | SQLite: Generic error. | 2612| 14800022 | SQLite: Callback routine requested an abort. | 2613| 14800023 | SQLite: Access permission denied. | 2614| 14800024 | SQLite: The database file is locked. | 2615| 14800025 | SQLite: A table in the database is locked. | 2616| 14800026 | SQLite: The database is out of memory. | 2617| 14800027 | SQLite: Attempt to write a readonly database. | 2618| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2619| 14800029 | SQLite: The database is full. | 2620| 14800030 | SQLite: Unable to open the database file. | 2621| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2622| 14800032 | SQLite: Abort due to constraint violation. | 2623| 14800033 | SQLite: Data type mismatch. | 2624| 14800034 | SQLite: Library used incorrectly. | 2625| 14800047 | The WAL file size exceeds the default limit. | 2626 2627**示例:** 2628 2629```ts 2630 2631let value1 = "Lisa"; 2632let value2 = 18; 2633let value3 = 100.5; 2634let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2635let value5 = "Jack"; 2636let value6 = 19; 2637let value7 = 101.5; 2638let value8 = new Uint8Array([6, 7, 8, 9, 10]); 2639let value9 = "Tom"; 2640let value10 = 20; 2641let value11 = 102.5; 2642let value12 = new Uint8Array([11, 12, 13, 14, 15]); 2643 2644const valueBucket1: relationalStore.ValuesBucket = { 2645 'NAME': value1, 2646 'AGE': value2, 2647 'SALARY': value3, 2648 'CODES': value4, 2649}; 2650const valueBucket2: relationalStore.ValuesBucket = { 2651 'NAME': value5, 2652 'AGE': value6, 2653 'SALARY': value7, 2654 'CODES': value8, 2655}; 2656const valueBucket3: relationalStore.ValuesBucket = { 2657 'NAME': value9, 2658 'AGE': value10, 2659 'SALARY': value11, 2660 'CODES': value12, 2661}; 2662 2663let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 2664if(store != undefined) { 2665 (store as relationalStore.RdbStore).batchInsert("EMPLOYEE", valueBuckets, (err, insertNum) => { 2666 if (err) { 2667 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 2668 return; 2669 } 2670 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 2671 }) 2672} 2673``` 2674 2675### batchInsert 2676 2677batchInsert(table: string, values: Array<ValuesBucket>):Promise<number> 2678 2679向目标表中插入一组数据,使用Promise异步回调。 2680 2681**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2682 2683**参数:** 2684 2685| 参数名 | 类型 | 必填 | 说明 | 2686| ------ | ------------------------------------------ | ---- | ---------------------------- | 2687| table | string | 是 | 指定的目标表名。 | 2688| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 2689 2690**返回值**: 2691 2692| 类型 | 说明 | 2693| --------------------- | ----------------------------------------------------------- | 2694| Promise<number> | Promise对象。如果操作成功,返回插入的数据个数,否则返回-1。 | 2695 2696**错误码:** 2697 2698以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2699 2700| **错误码ID** | **错误信息** | 2701|-----------| ------------------------------------------------------------ | 2702| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2703| 14800000 | Inner error. | 2704| 14800011 | Database corrupted. | 2705| 14800014 | Already closed. | 2706| 14800015 | The database does not respond. | 2707| 14800021 | SQLite: Generic error. | 2708| 14800022 | SQLite: Callback routine requested an abort. | 2709| 14800023 | SQLite: Access permission denied. | 2710| 14800024 | SQLite: The database file is locked. | 2711| 14800025 | SQLite: A table in the database is locked. | 2712| 14800026 | SQLite: The database is out of memory. | 2713| 14800027 | SQLite: Attempt to write a readonly database. | 2714| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2715| 14800029 | SQLite: The database is full. | 2716| 14800030 | SQLite: Unable to open the database file. | 2717| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2718| 14800032 | SQLite: Abort due to constraint violation. | 2719| 14800033 | SQLite: Data type mismatch. | 2720| 14800034 | SQLite: Library used incorrectly. | 2721| 14800047 | The WAL file size exceeds the default limit. | 2722 2723**示例:** 2724 2725```ts 2726import { BusinessError } from '@kit.BasicServicesKit'; 2727 2728let value1 = "Lisa"; 2729let value2 = 18; 2730let value3 = 100.5; 2731let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2732let value5 = "Jack"; 2733let value6 = 19; 2734let value7 = 101.5; 2735let value8 = new Uint8Array([6, 7, 8, 9, 10]); 2736let value9 = "Tom"; 2737let value10 = 20; 2738let value11 = 102.5; 2739let value12 = new Uint8Array([11, 12, 13, 14, 15]); 2740 2741const valueBucket1: relationalStore.ValuesBucket = { 2742 'NAME': value1, 2743 'AGE': value2, 2744 'SALARY': value3, 2745 'CODES': value4, 2746}; 2747const valueBucket2: relationalStore.ValuesBucket = { 2748 'NAME': value5, 2749 'AGE': value6, 2750 'SALARY': value7, 2751 'CODES': value8, 2752}; 2753const valueBucket3: relationalStore.ValuesBucket = { 2754 'NAME': value9, 2755 'AGE': value10, 2756 'SALARY': value11, 2757 'CODES': value12, 2758}; 2759 2760let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 2761if(store != undefined) { 2762 (store as relationalStore.RdbStore).batchInsert("EMPLOYEE", valueBuckets).then((insertNum: number) => { 2763 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 2764 }).catch((err: BusinessError) => { 2765 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 2766 }) 2767} 2768``` 2769 2770### batchInsertSync<sup>12+</sup> 2771 2772batchInsertSync(table: string, values: Array<ValuesBucket>):number 2773 2774向目标表中插入一组数据。 2775 2776**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2777 2778**参数:** 2779 2780| 参数名 | 类型 | 必填 | 说明 | 2781| ------ | ------------------------------------------ | ---- | ---------------------------- | 2782| table | string | 是 | 指定的目标表名。 | 2783| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 2784 2785**返回值**: 2786 2787| 类型 | 说明 | 2788| ------ | ---------------------------------------------- | 2789| number | 如果操作成功,返回插入的数据个数,否则返回-1。 | 2790 2791**错误码:** 2792 2793以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2794 2795| **错误码ID** | **错误信息** | 2796| ------------ | ------------------------------------------------------------ | 2797| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2798| 14800000 | Inner error. | 2799| 14800011 | Database corrupted. | 2800| 14800014 | Already closed. | 2801| 14800015 | The database does not respond. | 2802| 14800021 | SQLite: Generic error. | 2803| 14800022 | SQLite: Callback routine requested an abort. | 2804| 14800023 | SQLite: Access permission denied. | 2805| 14800024 | SQLite: The database file is locked. | 2806| 14800025 | SQLite: A table in the database is locked. | 2807| 14800026 | SQLite: The database is out of memory. | 2808| 14800027 | SQLite: Attempt to write a readonly database. | 2809| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2810| 14800029 | SQLite: The database is full. | 2811| 14800030 | SQLite: Unable to open the database file. | 2812| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2813| 14800032 | SQLite: Abort due to constraint violation. | 2814| 14800033 | SQLite: Data type mismatch. | 2815| 14800034 | SQLite: Library used incorrectly. | 2816| 14800047 | The WAL file size exceeds the default limit. | 2817 2818**示例:** 2819 2820```ts 2821import { BusinessError } from '@kit.BasicServicesKit'; 2822 2823let value1 = "Lisa"; 2824let value2 = 18; 2825let value3 = 100.5; 2826let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2827let value5 = "Jack"; 2828let value6 = 19; 2829let value7 = 101.5; 2830let value8 = new Uint8Array([6, 7, 8, 9, 10]); 2831let value9 = "Tom"; 2832let value10 = 20; 2833let value11 = 102.5; 2834let value12 = new Uint8Array([11, 12, 13, 14, 15]); 2835 2836const valueBucket1: relationalStore.ValuesBucket = { 2837 'NAME': value1, 2838 'AGE': value2, 2839 'SALARY': value3, 2840 'CODES': value4, 2841}; 2842const valueBucket2: relationalStore.ValuesBucket = { 2843 'NAME': value5, 2844 'AGE': value6, 2845 'SALARY': value7, 2846 'CODES': value8, 2847}; 2848const valueBucket3: relationalStore.ValuesBucket = { 2849 'NAME': value9, 2850 'AGE': value10, 2851 'SALARY': value11, 2852 'CODES': value12, 2853}; 2854 2855let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 2856if(store != undefined) { 2857 try { 2858 let insertNum: number = (store as relationalStore.RdbStore).batchInsertSync("EMPLOYEE", valueBuckets); 2859 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 2860 } catch (err) { 2861 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 2862 } 2863} 2864``` 2865 2866### update 2867 2868update(values: ValuesBucket, predicates: RdbPredicates, callback: AsyncCallback<number>):void 2869 2870根据RdbPredicates的指定实例对象更新数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2871 2872**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2873 2874**参数:** 2875 2876| 参数名 | 类型 | 必填 | 说明 | 2877| ---------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 2878| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 2879| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 2880| callback | AsyncCallback<number> | 是 | 指定的callback回调方法。返回受影响的行数。 | 2881 2882**错误码:** 2883 2884以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2885 2886| **错误码ID** | **错误信息** | 2887|-----------| ------------------------------------------------------------ | 2888| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2889| 14800000 | Inner error. | 2890| 14800011 | Database corrupted. | 2891| 14800014 | Already closed. | 2892| 14800015 | The database does not respond. | 2893| 14800021 | SQLite: Generic error. | 2894| 14800022 | SQLite: Callback routine requested an abort. | 2895| 14800023 | SQLite: Access permission denied. | 2896| 14800024 | SQLite: The database file is locked. | 2897| 14800025 | SQLite: A table in the database is locked. | 2898| 14800026 | SQLite: The database is out of memory. | 2899| 14800027 | SQLite: Attempt to write a readonly database. | 2900| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2901| 14800029 | SQLite: The database is full. | 2902| 14800030 | SQLite: Unable to open the database file. | 2903| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2904| 14800032 | SQLite: Abort due to constraint violation. | 2905| 14800033 | SQLite: Data type mismatch. | 2906| 14800034 | SQLite: Library used incorrectly. | 2907| 14800047 | The WAL file size exceeds the default limit. | 2908 2909**示例:** 2910 2911```ts 2912 2913let value1 = "Rose"; 2914let value2 = 22; 2915let value3 = 200.5; 2916let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2917 2918// 以下三种方式可用 2919const valueBucket1: relationalStore.ValuesBucket = { 2920 'NAME': value1, 2921 'AGE': value2, 2922 'SALARY': value3, 2923 'CODES': value4, 2924}; 2925const valueBucket2: relationalStore.ValuesBucket = { 2926 NAME: value1, 2927 AGE: value2, 2928 SALARY: value3, 2929 CODES: value4, 2930}; 2931const valueBucket3: relationalStore.ValuesBucket = { 2932 "NAME": value1, 2933 "AGE": value2, 2934 "SALARY": value3, 2935 "CODES": value4, 2936}; 2937 2938let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2939predicates.equalTo("NAME", "Lisa"); 2940if(store != undefined) { 2941 (store as relationalStore.RdbStore).update(valueBucket1, predicates,(err, rows) => { 2942 if (err) { 2943 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 2944 return; 2945 } 2946 console.info(`Updated row count: ${rows}`); 2947 }) 2948} 2949``` 2950 2951### update<sup>10+</sup> 2952 2953update(values: ValuesBucket, predicates: RdbPredicates, conflict: ConflictResolution, callback: AsyncCallback<number>):void 2954 2955根据RdbPredicates的指定实例对象更新数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2956 2957**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2958 2959**参数:** 2960 2961| 参数名 | 类型 | 必填 | 说明 | 2962| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 2963| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 2964| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 2965| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。 | 2966| callback | AsyncCallback<number> | 是 | 指定的callback回调方法。返回受影响的行数。 | 2967 2968**错误码:** 2969 2970以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2971 2972| **错误码ID** | **错误信息** | 2973|-----------| ------------------------------------------------------------ | 2974| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2975| 14800000 | Inner error. | 2976| 14800011 | Database corrupted. | 2977| 14800014 | Already closed. | 2978| 14800015 | The database does not respond. | 2979| 14800021 | SQLite: Generic error. | 2980| 14800022 | SQLite: Callback routine requested an abort. | 2981| 14800023 | SQLite: Access permission denied. | 2982| 14800024 | SQLite: The database file is locked. | 2983| 14800025 | SQLite: A table in the database is locked. | 2984| 14800026 | SQLite: The database is out of memory. | 2985| 14800027 | SQLite: Attempt to write a readonly database. | 2986| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2987| 14800029 | SQLite: The database is full. | 2988| 14800030 | SQLite: Unable to open the database file. | 2989| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2990| 14800032 | SQLite: Abort due to constraint violation. | 2991| 14800033 | SQLite: Data type mismatch. | 2992| 14800034 | SQLite: Library used incorrectly. | 2993| 14800047 | The WAL file size exceeds the default limit. | 2994 2995**示例:** 2996 2997```ts 2998 2999let value1 = "Rose"; 3000let value2 = 22; 3001let value3 = 200.5; 3002let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3003 3004// 以下三种方式可用 3005const valueBucket1: relationalStore.ValuesBucket = { 3006 'NAME': value1, 3007 'AGE': value2, 3008 'SALARY': value3, 3009 'CODES': value4, 3010}; 3011const valueBucket2: relationalStore.ValuesBucket = { 3012 NAME: value1, 3013 AGE: value2, 3014 SALARY: value3, 3015 CODES: value4, 3016}; 3017const valueBucket3: relationalStore.ValuesBucket = { 3018 "NAME": value1, 3019 "AGE": value2, 3020 "SALARY": value3, 3021 "CODES": value4, 3022}; 3023 3024let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3025predicates.equalTo("NAME", "Lisa"); 3026if(store != undefined) { 3027 (store as relationalStore.RdbStore).update(valueBucket1, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE, (err, rows) => { 3028 if (err) { 3029 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 3030 return; 3031 } 3032 console.info(`Updated row count: ${rows}`); 3033 }) 3034} 3035``` 3036 3037### update 3038 3039update(values: ValuesBucket, predicates: RdbPredicates):Promise<number> 3040 3041根据RdbPredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3042 3043**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3044 3045**参数:** 3046 3047| 参数名 | 类型 | 必填 | 说明 | 3048| ------------ | ------------------------------------ | ---- | ------------------------------------------------------------ | 3049| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 3050| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 3051 3052**返回值**: 3053 3054| 类型 | 说明 | 3055| --------------------- | ----------------------------------------- | 3056| Promise<number> | 指定的Promise回调方法。返回受影响的行数。 | 3057 3058**错误码:** 3059 3060以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3061 3062| **错误码ID** | **错误信息** | 3063|-----------| ------------------------------------------------------------ | 3064| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3065| 14800000 | Inner error. | 3066| 14800011 | Database corrupted. | 3067| 14800014 | Already closed. | 3068| 14800015 | The database does not respond. | 3069| 14800021 | SQLite: Generic error. | 3070| 14800022 | SQLite: Callback routine requested an abort. | 3071| 14800023 | SQLite: Access permission denied. | 3072| 14800024 | SQLite: The database file is locked. | 3073| 14800025 | SQLite: A table in the database is locked. | 3074| 14800026 | SQLite: The database is out of memory. | 3075| 14800027 | SQLite: Attempt to write a readonly database. | 3076| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3077| 14800029 | SQLite: The database is full. | 3078| 14800030 | SQLite: Unable to open the database file. | 3079| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3080| 14800032 | SQLite: Abort due to constraint violation. | 3081| 14800033 | SQLite: Data type mismatch. | 3082| 14800034 | SQLite: Library used incorrectly. | 3083| 14800047 | The WAL file size exceeds the default limit. | 3084 3085**示例:** 3086 3087```ts 3088import { BusinessError } from '@kit.BasicServicesKit'; 3089 3090let value1 = "Rose"; 3091let value2 = 22; 3092let value3 = 200.5; 3093let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3094 3095// 以下三种方式可用 3096const valueBucket1: relationalStore.ValuesBucket = { 3097 'NAME': value1, 3098 'AGE': value2, 3099 'SALARY': value3, 3100 'CODES': value4, 3101}; 3102const valueBucket2: relationalStore.ValuesBucket = { 3103 NAME: value1, 3104 AGE: value2, 3105 SALARY: value3, 3106 CODES: value4, 3107}; 3108const valueBucket3: relationalStore.ValuesBucket = { 3109 "NAME": value1, 3110 "AGE": value2, 3111 "SALARY": value3, 3112 "CODES": value4, 3113}; 3114 3115let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3116predicates.equalTo("NAME", "Lisa"); 3117if(store != undefined) { 3118 (store as relationalStore.RdbStore).update(valueBucket1, predicates).then(async (rows: Number) => { 3119 console.info(`Updated row count: ${rows}`); 3120 }).catch((err: BusinessError) => { 3121 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 3122 }) 3123} 3124``` 3125 3126### update<sup>10+</sup> 3127 3128update(values: ValuesBucket, predicates: RdbPredicates, conflict: ConflictResolution):Promise<number> 3129 3130根据RdbPredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3131 3132**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3133 3134**参数:** 3135 3136| 参数名 | 类型 | 必填 | 说明 | 3137| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 3138| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 3139| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 3140| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。 | 3141 3142**返回值**: 3143 3144| 类型 | 说明 | 3145| --------------------- | ----------------------------------------- | 3146| Promise<number> | 指定的Promise回调方法。返回受影响的行数。 | 3147 3148**错误码:** 3149 3150以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3151 3152| **错误码ID** | **错误信息** | 3153|-----------| ------------------------------------------------------------ | 3154| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3155| 14800000 | Inner error. | 3156| 14800011 | Database corrupted. | 3157| 14800014 | Already closed. | 3158| 14800015 | The database does not respond. | 3159| 14800021 | SQLite: Generic error. | 3160| 14800022 | SQLite: Callback routine requested an abort. | 3161| 14800023 | SQLite: Access permission denied. | 3162| 14800024 | SQLite: The database file is locked. | 3163| 14800025 | SQLite: A table in the database is locked. | 3164| 14800026 | SQLite: The database is out of memory. | 3165| 14800027 | SQLite: Attempt to write a readonly database. | 3166| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3167| 14800029 | SQLite: The database is full. | 3168| 14800030 | SQLite: Unable to open the database file. | 3169| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3170| 14800032 | SQLite: Abort due to constraint violation. | 3171| 14800033 | SQLite: Data type mismatch. | 3172| 14800034 | SQLite: Library used incorrectly. | 3173| 14800047 | The WAL file size exceeds the default limit. | 3174 3175**示例:** 3176 3177```ts 3178import { BusinessError } from '@kit.BasicServicesKit'; 3179 3180let value1 = "Rose"; 3181let value2 = 22; 3182let value3 = 200.5; 3183let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3184 3185// 以下三种方式可用 3186const valueBucket1: relationalStore.ValuesBucket = { 3187 'NAME': value1, 3188 'AGE': value2, 3189 'SALARY': value3, 3190 'CODES': value4, 3191}; 3192const valueBucket2: relationalStore.ValuesBucket = { 3193 NAME: value1, 3194 AGE: value2, 3195 SALARY: value3, 3196 CODES: value4, 3197}; 3198const valueBucket3: relationalStore.ValuesBucket = { 3199 "NAME": value1, 3200 "AGE": value2, 3201 "SALARY": value3, 3202 "CODES": value4, 3203}; 3204 3205let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3206predicates.equalTo("NAME", "Lisa"); 3207if(store != undefined) { 3208 (store as relationalStore.RdbStore).update(valueBucket1, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE).then(async (rows: Number) => { 3209 console.info(`Updated row count: ${rows}`); 3210 }).catch((err: BusinessError) => { 3211 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 3212 }) 3213} 3214``` 3215 3216### updateSync<sup>12+</sup> 3217 3218updateSync(values: ValuesBucket, predicates: RdbPredicates, conflict?: ConflictResolution):number 3219 3220根据RdbPredicates的指定实例对象更新数据库中的数据。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3221 3222**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3223 3224**参数:** 3225 3226| 参数名 | 类型 | 必填 | 说明 | 3227| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 3228| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 3229| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 3230| conflict | [ConflictResolution](#conflictresolution10) | 否 | 指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 3231 3232**返回值**: 3233 3234| 类型 | 说明 | 3235| ------ | ------------------ | 3236| number | 返回受影响的行数。 | 3237 3238**错误码:** 3239 3240以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3241 3242| **错误码ID** | **错误信息** | 3243| ------------ | ------------------------------------------------------------ | 3244| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3245| 14800000 | Inner error. | 3246| 14800011 | Database corrupted. | 3247| 14800014 | Already closed. | 3248| 14800015 | The database does not respond. | 3249| 14800021 | SQLite: Generic error. | 3250| 14800022 | SQLite: Callback routine requested an abort. | 3251| 14800023 | SQLite: Access permission denied. | 3252| 14800024 | SQLite: The database file is locked. | 3253| 14800025 | SQLite: A table in the database is locked. | 3254| 14800026 | SQLite: The database is out of memory. | 3255| 14800027 | SQLite: Attempt to write a readonly database. | 3256| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3257| 14800029 | SQLite: The database is full. | 3258| 14800030 | SQLite: Unable to open the database file. | 3259| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3260| 14800032 | SQLite: Abort due to constraint violation. | 3261| 14800033 | SQLite: Data type mismatch. | 3262| 14800034 | SQLite: Library used incorrectly. | 3263| 14800047 | The WAL file size exceeds the default limit. | 3264 3265**示例:** 3266 3267```ts 3268import { BusinessError } from '@kit.BasicServicesKit'; 3269 3270let value1 = "Rose"; 3271let value2 = 22; 3272let value3 = 200.5; 3273let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3274 3275// 以下三种方式可用 3276const valueBucket1: relationalStore.ValuesBucket = { 3277 'NAME': value1, 3278 'AGE': value2, 3279 'SALARY': value3, 3280 'CODES': value4, 3281}; 3282const valueBucket2: relationalStore.ValuesBucket = { 3283 NAME: value1, 3284 AGE: value2, 3285 SALARY: value3, 3286 CODES: value4, 3287}; 3288const valueBucket3: relationalStore.ValuesBucket = { 3289 "NAME": value1, 3290 "AGE": value2, 3291 "SALARY": value3, 3292 "CODES": value4, 3293}; 3294 3295let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3296predicates.equalTo("NAME", "Lisa"); 3297if(store != undefined) { 3298 try { 3299 let rows: Number = (store as relationalStore.RdbStore).updateSync(valueBucket1, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE); 3300 console.info(`Updated row count: ${rows}`); 3301 } catch (error) { 3302 console.error(`Updated failed, code is ${error.code},message is ${error.message}`); 3303 } 3304} 3305``` 3306 3307### delete 3308 3309delete(predicates: RdbPredicates, callback: AsyncCallback<number>):void 3310 3311根据RdbPredicates的指定实例对象从数据库中删除数据,使用callback异步回调。 3312 3313**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3314 3315**参数:** 3316 3317| 参数名 | 类型 | 必填 | 说明 | 3318| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 3319| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 3320| callback | AsyncCallback<number> | 是 | 指定callback回调函数。返回受影响的行数。 | 3321 3322**错误码:** 3323 3324以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3325 3326| **错误码ID** | **错误信息** | 3327|-----------| ------------------------------------------------------------ | 3328| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3329| 14800000 | Inner error. | 3330| 14800011 | Database corrupted. | 3331| 14800014 | Already closed. | 3332| 14800015 | The database does not respond. | 3333| 14800021 | SQLite: Generic error. | 3334| 14800022 | SQLite: Callback routine requested an abort. | 3335| 14800023 | SQLite: Access permission denied. | 3336| 14800024 | SQLite: The database file is locked. | 3337| 14800025 | SQLite: A table in the database is locked. | 3338| 14800026 | SQLite: The database is out of memory. | 3339| 14800027 | SQLite: Attempt to write a readonly database. | 3340| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3341| 14800029 | SQLite: The database is full. | 3342| 14800030 | SQLite: Unable to open the database file. | 3343| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3344| 14800032 | SQLite: Abort due to constraint violation. | 3345| 14800033 | SQLite: Data type mismatch. | 3346| 14800034 | SQLite: Library used incorrectly. | 3347| 14800047 | The WAL file size exceeds the default limit. | 3348 3349**示例:** 3350 3351```ts 3352let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3353predicates.equalTo("NAME", "Lisa"); 3354if(store != undefined) { 3355 (store as relationalStore.RdbStore).delete(predicates, (err, rows) => { 3356 if (err) { 3357 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 3358 return; 3359 } 3360 console.info(`Delete rows: ${rows}`); 3361 }) 3362} 3363``` 3364 3365### delete 3366 3367delete(predicates: RdbPredicates):Promise<number> 3368 3369根据RdbPredicates的指定实例对象从数据库中删除数据,使用Promise异步回调。 3370 3371**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3372 3373**参数:** 3374 3375| 参数名 | 类型 | 必填 | 说明 | 3376| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 3377| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 3378 3379**返回值**: 3380 3381| 类型 | 说明 | 3382| --------------------- | ------------------------------- | 3383| Promise<number> | Promise对象。返回受影响的行数。 | 3384 3385**错误码:** 3386 3387以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3388 3389| **错误码ID** | **错误信息** | 3390|-----------| ------------------------------------------------------------ | 3391| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3392| 14800000 | Inner error. | 3393| 14800011 | Database corrupted. | 3394| 14800014 | Already closed. | 3395| 14800015 | The database does not respond. | 3396| 14800021 | SQLite: Generic error. | 3397| 14800022 | SQLite: Callback routine requested an abort. | 3398| 14800023 | SQLite: Access permission denied. | 3399| 14800024 | SQLite: The database file is locked. | 3400| 14800025 | SQLite: A table in the database is locked. | 3401| 14800026 | SQLite: The database is out of memory. | 3402| 14800027 | SQLite: Attempt to write a readonly database. | 3403| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3404| 14800029 | SQLite: The database is full. | 3405| 14800030 | SQLite: Unable to open the database file. | 3406| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3407| 14800032 | SQLite: Abort due to constraint violation. | 3408| 14800033 | SQLite: Data type mismatch. | 3409| 14800034 | SQLite: Library used incorrectly. | 3410| 14800047 | The WAL file size exceeds the default limit. | 3411 3412**示例:** 3413 3414```ts 3415import { BusinessError } from '@kit.BasicServicesKit'; 3416 3417let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3418predicates.equalTo("NAME", "Lisa"); 3419if(store != undefined) { 3420 (store as relationalStore.RdbStore).delete(predicates).then((rows: Number) => { 3421 console.info(`Delete rows: ${rows}`); 3422 }).catch((err: BusinessError) => { 3423 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 3424 }) 3425} 3426``` 3427 3428### deleteSync<sup>12+</sup> 3429 3430deleteSync(predicates: RdbPredicates):number 3431 3432根据RdbPredicates的指定实例对象从数据库中删除数据。 3433 3434**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3435 3436**参数:** 3437 3438| 参数名 | 类型 | 必填 | 说明 | 3439| ---------- | ------------------------------- | ---- | --------------------------------------- | 3440| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 3441 3442**返回值**: 3443 3444| 类型 | 说明 | 3445| ------ | ------------------ | 3446| number | 返回受影响的行数。 | 3447 3448**错误码:** 3449 3450以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3451 3452| **错误码ID** | **错误信息** | 3453| ------------ | ------------------------------------------------------------ | 3454| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3455| 14800000 | Inner error. | 3456| 14800011 | Database corrupted. | 3457| 14800014 | Already closed. | 3458| 14800015 | The database does not respond. | 3459| 14800021 | SQLite: Generic error. | 3460| 14800022 | SQLite: Callback routine requested an abort. | 3461| 14800023 | SQLite: Access permission denied. | 3462| 14800024 | SQLite: The database file is locked. | 3463| 14800025 | SQLite: A table in the database is locked. | 3464| 14800026 | SQLite: The database is out of memory. | 3465| 14800027 | SQLite: Attempt to write a readonly database. | 3466| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3467| 14800029 | SQLite: The database is full. | 3468| 14800030 | SQLite: Unable to open the database file. | 3469| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3470| 14800032 | SQLite: Abort due to constraint violation. | 3471| 14800033 | SQLite: Data type mismatch. | 3472| 14800034 | SQLite: Library used incorrectly. | 3473| 14800047 | The WAL file size exceeds the default limit. | 3474 3475**示例:** 3476 3477```ts 3478import { BusinessError } from '@kit.BasicServicesKit'; 3479 3480let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3481predicates.equalTo("NAME", "Lisa"); 3482if(store != undefined) { 3483 try { 3484 let rows: Number = (store as relationalStore.RdbStore).deleteSync(predicates) 3485 console.info(`Delete rows: ${rows}`); 3486 } catch (err) { 3487 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 3488 } 3489} 3490``` 3491 3492### query<sup>10+</sup> 3493 3494query(predicates: RdbPredicates, callback: AsyncCallback<ResultSet>):void 3495 3496根据指定条件查询数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3497 3498**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3499 3500**参数:** 3501 3502| 参数名 | 类型 | 必填 | 说明 | 3503| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 3504| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 3505| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 3506 3507**错误码:** 3508 3509以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 3510 3511| **错误码ID** | **错误信息** | 3512|-----------| ------------------------------------------------------------ | 3513| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3514| 14800000 | Inner error. | 3515| 14800014 | Already closed. | 3516| 14800015 | The database does not respond. | 3517 3518**示例:** 3519 3520```ts 3521let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3522predicates.equalTo("NAME", "Rose"); 3523if(store != undefined) { 3524 (store as relationalStore.RdbStore).query(predicates, (err, resultSet) => { 3525 if (err) { 3526 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 3527 return; 3528 } 3529 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 3530 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 3531 while (resultSet.goToNextRow()) { 3532 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 3533 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 3534 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 3535 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 3536 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 3537 } 3538 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 3539 resultSet.close(); 3540 }) 3541} 3542``` 3543 3544### query 3545 3546query(predicates: RdbPredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void 3547 3548根据指定条件查询数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3549 3550**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3551 3552**参数:** 3553 3554| 参数名 | 类型 | 必填 | 说明 | 3555| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 3556| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 3557| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 3558| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 3559 3560**错误码:** 3561 3562以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 3563 3564| **错误码ID** | **错误信息** | 3565|-----------| ------------------------------------------------------------ | 3566| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3567| 14800000 | Inner error. | 3568| 14800014 | Already closed. | 3569| 14800015 | The database does not respond. | 3570 3571**示例:** 3572 3573```ts 3574let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3575predicates.equalTo("NAME", "Rose"); 3576if(store != undefined) { 3577 (store as relationalStore.RdbStore).query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], (err, resultSet) => { 3578 if (err) { 3579 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 3580 return; 3581 } 3582 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 3583 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 3584 while (resultSet.goToNextRow()) { 3585 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 3586 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 3587 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 3588 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 3589 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 3590 } 3591 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 3592 resultSet.close(); 3593 }) 3594} 3595``` 3596 3597### query 3598 3599query(predicates: RdbPredicates, columns?: Array<string>):Promise<ResultSet> 3600 3601根据指定条件查询数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3602 3603**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3604 3605**参数:** 3606 3607| 参数名 | 类型 | 必填 | 说明 | 3608| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 3609| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 3610| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 3611 3612**错误码:** 3613 3614以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 3615 3616| **错误码ID** | **错误信息** | 3617|-----------| ------------------------------------------------------------ | 3618| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3619| 14800000 | Inner error. | 3620| 14800014 | Already closed. | 3621| 14800015 | The database does not respond. | 3622 3623**返回值**: 3624 3625| 类型 | 说明 | 3626| ------------------------------------------------------- | -------------------------------------------------- | 3627| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 3628 3629**示例:** 3630 3631```ts 3632import { BusinessError } from '@kit.BasicServicesKit'; 3633 3634let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3635predicates.equalTo("NAME", "Rose"); 3636if(store != undefined) { 3637 (store as relationalStore.RdbStore).query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((resultSet: relationalStore.ResultSet) => { 3638 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 3639 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 3640 while (resultSet.goToNextRow()) { 3641 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 3642 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 3643 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 3644 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 3645 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 3646 } 3647 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 3648 resultSet.close(); 3649 }).catch((err: BusinessError) => { 3650 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 3651 }) 3652} 3653``` 3654 3655### querySync<sup>12+</sup> 3656 3657querySync(predicates: RdbPredicates, columns?: Array<string>):ResultSet 3658 3659根据指定条件查询数据库中的数据。对query同步接口获得的resultSet进行操作时,若逻辑复杂且循环次数过多,可能造成freeze问题,建议将此步骤放到[taskpool](../apis-arkts/js-apis-taskpool.md)线程中执行。 3660 3661**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3662 3663**参数:** 3664 3665| 参数名 | 类型 | 必填 | 说明 | 3666| ---------- | ------------------------------- | ---- | ------------------------------------------------------------ | 3667| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 3668| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。默认值为空。 | 3669 3670**错误码:** 3671 3672以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 3673 3674| **错误码ID** | **错误信息** | 3675| ------------ | ------------------------------------------------------------ | 3676| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3677| 14800000 | Inner error. | 3678| 14800014 | Already closed. | 3679| 14800015 | The database does not respond. | 3680 3681**返回值**: 3682 3683| 类型 | 说明 | 3684| ----------------------- | ----------------------------------- | 3685| [ResultSet](#resultset) | 如果操作成功,则返回ResultSet对象。 | 3686 3687**示例:** 3688 3689```ts 3690import { BusinessError } from '@kit.BasicServicesKit'; 3691 3692let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3693predicates.equalTo("NAME", "Rose"); 3694if(store != undefined) { 3695 try { 3696 let resultSet: relationalStore.ResultSet = (store as relationalStore.RdbStore).querySync(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 3697 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 3698 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 3699 while (resultSet.goToNextRow()) { 3700 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 3701 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 3702 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 3703 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 3704 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 3705 } 3706 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 3707 resultSet.close(); 3708 } catch (err) { 3709 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 3710 } 3711} 3712``` 3713 3714### remoteQuery 3715 3716remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array<string> , callback: AsyncCallback<ResultSet>): void 3717 3718根据指定条件查询远程设备数据库中的数据。使用callback异步回调。 3719 3720> **说明:** 3721> 3722> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 3723 3724**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3725 3726**参数:** 3727 3728| 参数名 | 类型 | 必填 | 说明 | 3729| ---------- | -------------------------------------------- | ---- | --------------------------------------------------------- | 3730| device | string | 是 | 指定的远程设备ID。 | 3731| table | string | 是 | 指定的目标表名。 | 3732| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象,指定查询的条件。 | 3733| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 3734| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 3735 3736**错误码:** 3737 3738以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 3739 3740| **错误码ID** | **错误信息** | 3741|-----------| ------------------------------------------------------------ | 3742| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3743| 801 | Capability not supported. | 3744| 14800000 | Inner error. | 3745| 14800014 | Already closed. | 3746 3747**示例:** 3748 3749```ts 3750import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 3751import { BusinessError } from '@kit.BasicServicesKit'; 3752 3753let dmInstance: distributedDeviceManager.DeviceManager; 3754let deviceId: string | undefined = undefined; 3755 3756try { 3757 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 3758 let devices = dmInstance.getAvailableDeviceListSync(); 3759 if(deviceId != undefined) { 3760 deviceId = devices[0].networkId; 3761 } 3762} catch (err) { 3763 let code = (err as BusinessError).code; 3764 let message = (err as BusinessError).message; 3765 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 3766} 3767 3768let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 3769predicates.greaterThan("id", 0); 3770if(store != undefined && deviceId != undefined) { 3771 (store as relationalStore.RdbStore).remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((resultSet: relationalStore.ResultSet) => { 3772 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 3773 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 3774 while (resultSet.goToNextRow()) { 3775 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 3776 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 3777 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 3778 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 3779 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 3780 } 3781 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 3782 resultSet.close(); 3783 }).catch((err: BusinessError) => { 3784 console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`); 3785 }) 3786} 3787``` 3788 3789### remoteQuery 3790 3791remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array<string>): Promise<ResultSet> 3792 3793根据指定条件查询远程设备数据库中的数据。使用Promise异步回调。 3794 3795> **说明:** 3796> 3797> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 3798 3799**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3800 3801**参数:** 3802 3803| 参数名 | 类型 | 必填 | 说明 | 3804| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 3805| device | string | 是 | 指定的远程设备ID。 | 3806| table | string | 是 | 指定的目标表名。 | 3807| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象,指定查询的条件。 | 3808| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 3809 3810**返回值**: 3811 3812| 类型 | 说明 | 3813| ------------------------------------------------------------ | -------------------------------------------------- | 3814| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 3815 3816**错误码:** 3817 3818以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 3819 3820| **错误码ID** | **错误信息** | 3821|-----------| ------------------------------------------------------------ | 3822| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3823| 801 | Capability not supported. | 3824| 14800000 | Inner error. | 3825| 14800014 | Already closed. | 3826 3827**示例:** 3828 3829```ts 3830import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 3831import { BusinessError } from '@kit.BasicServicesKit'; 3832 3833let dmInstance: distributedDeviceManager.DeviceManager; 3834let deviceId: string | undefined = undefined; 3835 3836try { 3837 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 3838 let devices: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 3839 if(devices != undefined) { 3840 deviceId = devices[0].networkId; 3841 } 3842} catch (err) { 3843 let code = (err as BusinessError).code; 3844 let message = (err as BusinessError).message; 3845 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 3846} 3847 3848let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 3849predicates.greaterThan("id", 0); 3850if(store != undefined && deviceId != undefined) { 3851 (store as relationalStore.RdbStore).remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((resultSet: relationalStore.ResultSet) => { 3852 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 3853 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 3854 while (resultSet.goToNextRow()) { 3855 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 3856 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 3857 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 3858 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 3859 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 3860 } 3861 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 3862 resultSet.close(); 3863 }).catch((err: BusinessError) => { 3864 console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`); 3865 }) 3866} 3867``` 3868 3869### querySql<sup>10+</sup> 3870 3871querySql(sql: string, callback: AsyncCallback<ResultSet>):void 3872 3873根据指定SQL语句查询数据库中的数据,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用callback异步回调。 3874 3875**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3876 3877**参数:** 3878 3879| 参数名 | 类型 | 必填 | 说明 | 3880| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 3881| sql | string | 是 | 指定要执行的SQL语句。 | 3882| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 3883 3884**错误码:** 3885 3886以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 3887 3888| **错误码ID** | **错误信息** | 3889|-----------| ------------------------------------------------------------ | 3890| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3891| 14800000 | Inner error. | 3892| 14800014 | Already closed. | 3893| 14800015 | The database does not respond. | 3894 3895**示例:** 3896 3897```ts 3898if(store != undefined) { 3899 (store as relationalStore.RdbStore).querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'", (err, resultSet) => { 3900 if (err) { 3901 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 3902 return; 3903 } 3904 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 3905 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 3906 while (resultSet.goToNextRow()) { 3907 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 3908 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 3909 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 3910 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 3911 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 3912 } 3913 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 3914 resultSet.close(); 3915 }) 3916} 3917``` 3918 3919### querySql 3920 3921querySql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<ResultSet>):void 3922 3923根据指定SQL语句查询数据库中的数据,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用callback异步回调。 3924 3925**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3926 3927**参数:** 3928 3929| 参数名 | 类型 | 必填 | 说明 | 3930| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 3931| sql | string | 是 | 指定要执行的SQL语句。 | 3932| bindArgs | Array<[ValueType](#valuetype)> | 是 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数需为空数组。 | 3933| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 3934 3935**错误码:** 3936 3937以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 3938 3939| **错误码ID** | **错误信息** | 3940|-----------| ------------------------------------------------------------ | 3941| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3942| 14800000 | Inner error. | 3943| 14800014 | Already closed. | 3944| 14800015 | The database does not respond. | 3945 3946**示例:** 3947 3948```ts 3949if(store != undefined) { 3950 (store as relationalStore.RdbStore).querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], (err, resultSet) => { 3951 if (err) { 3952 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 3953 return; 3954 } 3955 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 3956 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 3957 while (resultSet.goToNextRow()) { 3958 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 3959 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 3960 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 3961 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 3962 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 3963 } 3964 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 3965 resultSet.close(); 3966 }) 3967} 3968``` 3969 3970### querySql 3971 3972querySql(sql: string, bindArgs?: Array<ValueType>):Promise<ResultSet> 3973 3974根据指定SQL语句查询数据库中的数据,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用Promise异步回调。 3975 3976**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3977 3978**参数:** 3979 3980| 参数名 | 类型 | 必填 | 说明 | 3981| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 3982| sql | string | 是 | 指定要执行的SQL语句。 | 3983| bindArgs | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 3984 3985**返回值**: 3986 3987| 类型 | 说明 | 3988| ------------------------------------------------------- | -------------------------------------------------- | 3989| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 3990 3991**错误码:** 3992 3993以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 3994 3995| **错误码ID** | **错误信息** | 3996|-----------| ------------------------------------------------------------ | 3997| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3998| 14800000 | Inner error. | 3999| 14800014 | Already closed. | 4000| 14800015 | The database does not respond. | 4001 4002**示例:** 4003 4004```ts 4005import { BusinessError } from '@kit.BasicServicesKit'; 4006 4007if(store != undefined) { 4008 (store as relationalStore.RdbStore).querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'").then((resultSet: relationalStore.ResultSet) => { 4009 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4010 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4011 while (resultSet.goToNextRow()) { 4012 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4013 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4014 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4015 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4016 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4017 } 4018 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4019 resultSet.close(); 4020 }).catch((err: BusinessError) => { 4021 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 4022 }) 4023} 4024``` 4025 4026### querySqlSync<sup>12+</sup> 4027 4028querySqlSync(sql: string, bindArgs?: Array<ValueType>):ResultSet 4029 4030根据指定SQL语句查询数据库中的数据,语句中的各种表达式和操作符之间的关系操作符号不超过1000个。对query同步接口获得的resultSet进行操作时,若逻辑复杂且循环次数过多,可能造成freeze问题,建议将此步骤放到[taskpool](../apis-arkts/js-apis-taskpool.md)线程中执行。 4031 4032**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4033 4034**参数:** 4035 4036| 参数名 | 类型 | 必填 | 说明 | 4037| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4038| sql | string | 是 | 指定要执行的SQL语句。 | 4039| bindArgs | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。默认值为空。 | 4040 4041**返回值**: 4042 4043| 类型 | 说明 | 4044| ----------------------- | ----------------------------------- | 4045| [ResultSet](#resultset) | 如果操作成功,则返回ResultSet对象。 | 4046 4047**错误码:** 4048 4049以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 4050 4051| **错误码ID** | **错误信息** | 4052| ------------ | ------------------------------------------------------------ | 4053| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4054| 14800000 | Inner error. | 4055| 14800014 | Already closed. | 4056| 14800015 | The database does not respond. | 4057 4058**示例:** 4059 4060```ts 4061import { BusinessError } from '@kit.BasicServicesKit'; 4062 4063if(store != undefined) { 4064 try { 4065 let resultSet: relationalStore.ResultSet = (store as relationalStore.RdbStore).querySqlSync("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'"); 4066 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4067 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4068 while (resultSet.goToNextRow()) { 4069 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4070 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4071 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4072 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4073 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4074 } 4075 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4076 resultSet.close(); 4077 } catch (err) { 4078 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 4079 } 4080} 4081``` 4082 4083### executeSql<sup>10+</sup> 4084 4085executeSql(sql: string, callback: AsyncCallback<void>):void 4086 4087执行包含指定参数但不返回值的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用callback异步回调。 4088 4089此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 4090 4091不支持分号分隔的多条语句。 4092 4093**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4094 4095**参数:** 4096 4097| 参数名 | 类型 | 必填 | 说明 | 4098| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4099| sql | string | 是 | 指定要执行的SQL语句。 | 4100| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 4101 4102**错误码:** 4103 4104以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4105 4106| **错误码ID** | **错误信息** | 4107|-----------| ------------------------------------------------------------ | 4108| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4109| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 4110| 14800000 | Inner error. | 4111| 14800011 | Database corrupted. | 4112| 14800014 | Already closed. | 4113| 14800015 | The database does not respond. | 4114| 14800021 | SQLite: Generic error. | 4115| 14800022 | SQLite: Callback routine requested an abort. | 4116| 14800023 | SQLite: Access permission denied. | 4117| 14800024 | SQLite: The database file is locked. | 4118| 14800025 | SQLite: A table in the database is locked. | 4119| 14800026 | SQLite: The database is out of memory. | 4120| 14800027 | SQLite: Attempt to write a readonly database. | 4121| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4122| 14800029 | SQLite: The database is full. | 4123| 14800030 | SQLite: Unable to open the database file. | 4124| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4125| 14800032 | SQLite: Abort due to constraint violation. | 4126| 14800033 | SQLite: Data type mismatch. | 4127| 14800034 | SQLite: Library used incorrectly. | 4128| 14800047 | The WAL file size exceeds the default limit. | 4129 4130**示例:** 4131 4132```ts 4133const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = 'zhangsan'" 4134if(store != undefined) { 4135 (store as relationalStore.RdbStore).executeSql(SQL_DELETE_TABLE, (err) => { 4136 if (err) { 4137 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 4138 return; 4139 } 4140 console.info('Delete table done.'); 4141 }) 4142} 4143``` 4144 4145### executeSql 4146 4147executeSql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<void>):void 4148 4149执行包含指定参数但不返回值的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用callback异步回调。 4150 4151此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 4152 4153不支持分号分隔的多条语句。 4154 4155**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4156 4157**参数:** 4158 4159| 参数名 | 类型 | 必填 | 说明 | 4160| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4161| sql | string | 是 | 指定要执行的SQL语句。 | 4162| bindArgs | Array<[ValueType](#valuetype)> | 是 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数需为空数组。 | 4163| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 4164 4165**错误码:** 4166 4167以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4168 4169| **错误码ID** | **错误信息** | 4170|-----------| ------------------------------------------------------------ | 4171| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4172| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 4173| 14800000 | Inner error. | 4174| 14800011 | Database corrupted. | 4175| 14800014 | Already closed. | 4176| 14800015 | The database does not respond. | 4177| 14800021 | SQLite: Generic error. | 4178| 14800022 | SQLite: Callback routine requested an abort. | 4179| 14800023 | SQLite: Access permission denied. | 4180| 14800024 | SQLite: The database file is locked. | 4181| 14800025 | SQLite: A table in the database is locked. | 4182| 14800026 | SQLite: The database is out of memory. | 4183| 14800027 | SQLite: Attempt to write a readonly database. | 4184| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4185| 14800029 | SQLite: The database is full. | 4186| 14800030 | SQLite: Unable to open the database file. | 4187| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4188| 14800032 | SQLite: Abort due to constraint violation. | 4189| 14800033 | SQLite: Data type mismatch. | 4190| 14800034 | SQLite: Library used incorrectly. | 4191| 14800047 | The WAL file size exceeds the default limit. | 4192 4193**示例:** 4194 4195```ts 4196const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = ?" 4197if(store != undefined) { 4198 (store as relationalStore.RdbStore).executeSql(SQL_DELETE_TABLE, ['zhangsan'], (err) => { 4199 if (err) { 4200 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 4201 return; 4202 } 4203 console.info('Delete table done.'); 4204 }) 4205} 4206``` 4207 4208### executeSql 4209 4210executeSql(sql: string, bindArgs?: Array<ValueType>):Promise<void> 4211 4212执行包含指定参数但不返回值的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用Promise异步回调。 4213 4214此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 4215 4216不支持分号分隔的多条语句。 4217 4218**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4219 4220**参数:** 4221 4222| 参数名 | 类型 | 必填 | 说明 | 4223| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4224| sql | string | 是 | 指定要执行的SQL语句。 | 4225| bindArgs | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 4226 4227**返回值**: 4228 4229| 类型 | 说明 | 4230| ------------------- | ------------------------- | 4231| Promise<void> | 无返回结果的Promise对象。 | 4232 4233**错误码:** 4234 4235以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4236 4237| **错误码ID** | **错误信息** | 4238|-----------| ------------------------------------------------------------ | 4239| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4240| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 4241| 14800000 | Inner error. | 4242| 14800011 | Database corrupted. | 4243| 14800014 | Already closed. | 4244| 14800015 | The database does not respond. | 4245| 14800021 | SQLite: Generic error. | 4246| 14800022 | SQLite: Callback routine requested an abort. | 4247| 14800023 | SQLite: Access permission denied. | 4248| 14800024 | SQLite: The database file is locked. | 4249| 14800025 | SQLite: A table in the database is locked. | 4250| 14800026 | SQLite: The database is out of memory. | 4251| 14800027 | SQLite: Attempt to write a readonly database. | 4252| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4253| 14800029 | SQLite: The database is full. | 4254| 14800030 | SQLite: Unable to open the database file. | 4255| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4256| 14800032 | SQLite: Abort due to constraint violation. | 4257| 14800033 | SQLite: Data type mismatch. | 4258| 14800034 | SQLite: Library used incorrectly. | 4259| 14800047 | The WAL file size exceeds the default limit. | 4260 4261**示例:** 4262 4263```ts 4264import { BusinessError } from '@kit.BasicServicesKit'; 4265 4266const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = 'zhangsan'" 4267if(store != undefined) { 4268 (store as relationalStore.RdbStore).executeSql(SQL_DELETE_TABLE).then(() => { 4269 console.info('Delete table done.'); 4270 }).catch((err: BusinessError) => { 4271 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 4272 }) 4273} 4274``` 4275 4276### execute<sup>12+</sup> 4277 4278execute(sql: string, args?: Array<ValueType>):Promise<ValueType> 4279 4280执行包含指定参数的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,返回值类型为ValueType,使用Promise异步回调。 4281 4282该接口支持执行增删改操作,支持执行PRAGMA语法的sql,支持对表的操作(建表、删表、修改表),返回结果类型由执行具体sql的结果决定。 4283 4284此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 4285 4286不支持分号分隔的多条语句。 4287 4288**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4289 4290**参数:** 4291 4292| 参数名 | 类型 | 必填 | 说明 | 4293| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4294| sql | string | 是 | 指定要执行的SQL语句。 | 4295| args | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 4296 4297**返回值**: 4298 4299| 类型 | 说明 | 4300| ------------------- | ------------------------- | 4301| Promise<[ValueType](#valuetype)> | Promise对象,返回sql执行后的结果。 | 4302 4303**错误码:** 4304 4305以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4306 4307| **错误码ID** | **错误信息** | 4308|-----------| ------------------------------------------------------------ | 4309| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4310| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 4311| 14800000 | Inner error. | 4312| 14800011 | Database corrupted. | 4313| 14800014 | Already closed. | 4314| 14800015 | The database does not respond. | 4315| 14800021 | SQLite: Generic error. | 4316| 14800022 | SQLite: Callback routine requested an abort. | 4317| 14800023 | SQLite: Access permission denied. | 4318| 14800024 | SQLite: The database file is locked. | 4319| 14800025 | SQLite: A table in the database is locked. | 4320| 14800026 | SQLite: The database is out of memory. | 4321| 14800027 | SQLite: Attempt to write a readonly database. | 4322| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4323| 14800029 | SQLite: The database is full. | 4324| 14800030 | SQLite: Unable to open the database file. | 4325| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4326| 14800032 | SQLite: Abort due to constraint violation. | 4327| 14800033 | SQLite: Data type mismatch. | 4328| 14800034 | SQLite: Library used incorrectly. | 4329| 14800047 | The WAL file size exceeds the default limit. | 4330 4331**示例:** 4332 4333```ts 4334import { BusinessError } from '@kit.BasicServicesKit'; 4335 4336// 校验数据库完整性 4337if(store != undefined) { 4338 const SQL_CHECK_INTEGRITY = 'PRAGMA integrity_check'; 4339 (store as relationalStore.RdbStore).execute(SQL_CHECK_INTEGRITY).then((data) => { 4340 console.info(`check result: ${data}`); 4341 }).catch((err: BusinessError) => { 4342 console.error(`check failed, code is ${err.code}, message is ${err.message}`); 4343 }) 4344} 4345 4346// 删除表中所有数据 4347if(store != undefined) { 4348 const SQL_DELETE_TABLE = 'DELETE FROM test'; 4349 (store as relationalStore.RdbStore).execute(SQL_DELETE_TABLE).then((data) => { 4350 console.info(`delete result: ${data}`); 4351 }).catch((err: BusinessError) => { 4352 console.error(`delete failed, code is ${err.code}, message is ${err.message}`); 4353 }) 4354} 4355 4356// 删表 4357if(store != undefined) { 4358 const SQL_DROP_TABLE = 'DROP TABLE test'; 4359 (store as relationalStore.RdbStore).execute(SQL_DROP_TABLE).then((data) => { 4360 console.info(`drop result: ${data}`); 4361 }).catch((err: BusinessError) => { 4362 console.error(`drop failed, code is ${err.code}, message is ${err.message}`); 4363 }) 4364} 4365``` 4366 4367### execute<sup>12+</sup> 4368 4369execute(sql: string, txId: number, args?: Array<ValueType>): Promise<ValueType> 4370 4371执行包含指定参数的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用Promise异步回调。 4372 4373<!--RP1--> 4374该接口仅支持[向量数据库](js-apis-data-relationalStore-sys.md#storeconfig)使用。<!--RP1End--> 4375 4376此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 4377 4378不支持分号分隔的多条语句。 4379 4380**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4381 4382**参数:** 4383 4384| 参数名 | 类型 | 必填 | 说明 | 4385| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4386| sql | string | 是 | 指定要执行的SQL语句。 | 4387| txId | number | 是 | 通过[beginTrans](#begintrans12)获取的事务ID,如果传0,该语句默认在单独事务内。 | 4388| args | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。该参数不填,或者填null或undefined,都认为是sql参数语句完整。 | 4389 4390**返回值**: 4391 4392| 类型 | 说明 | 4393| ------------------- | ------------------------- | 4394| Promise<[ValueType](#valuetype)> | Promise对象,返回null。 | 4395 4396**错误码:** 4397 4398以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4399 4400| **错误码ID** | **错误信息** | 4401|-----------| ------------------------------------------------------------ | 4402| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4403| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 4404| 14800000 | Inner error. | 4405| 14800011 | Database corrupted. | 4406| 14800014 | Already closed. | 4407| 14800015 | The database does not respond. | 4408| 14800021 | SQLite: Generic error. | 4409| 14800022 | SQLite: Callback routine requested an abort. | 4410| 14800023 | SQLite: Access permission denied. | 4411| 14800024 | SQLite: The database file is locked. | 4412| 14800025 | SQLite: A table in the database is locked. | 4413| 14800026 | SQLite: The database is out of memory. | 4414| 14800027 | SQLite: Attempt to write a readonly database. | 4415| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4416| 14800029 | SQLite: The database is full. | 4417| 14800030 | SQLite: Unable to open the database file. | 4418| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4419| 14800032 | SQLite: Abort due to constraint violation. | 4420| 14800033 | SQLite: Data type mismatch. | 4421| 14800034 | SQLite: Library used incorrectly. | 4422| 14800047 | The WAL file size exceeds the default limit. | 4423 4424**示例:** 4425 4426```ts 4427import { BusinessError } from '@kit.BasicServicesKit'; 4428if(store != null) { 4429 let txId : number; 4430 (store as relationalStore.RdbStore).beginTrans().then((txId : number) => { 4431 (store as relationalStore.RdbStore).execute("DELETE FROM TEST WHERE age = ? OR age = ?", txId, ["18", "20"]) 4432 .then(() => { 4433 (store as relationalStore.RdbStore).commit(txId); 4434 }) 4435 .catch((err: BusinessError) => { 4436 (store as relationalStore.RdbStore).rollback(txId) 4437 console.error(`execute sql failed, code is ${err.code},message is ${err.message}`); 4438 }); 4439 }); 4440} 4441``` 4442 4443### executeSync<sup>12+</sup> 4444 4445executeSync(sql: string, args?: Array<ValueType>): ValueType 4446 4447执行包含指定参数的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,返回值类型为ValueType。 4448 4449该接口支持执行增删改操作,支持执行PRAGMA语法的sql,支持对表的操作(建表、删表、修改表),返回结果类型由执行具体sql的结果决定。 4450 4451此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 4452 4453不支持分号分隔的多条语句。 4454 4455**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4456 4457**参数:** 4458 4459| 参数名 | 类型 | 必填 | 说明 | 4460| ------ | ------------------------------------ | ---- | ------------------------------------------------------------ | 4461| sql | string | 是 | 指定要执行的SQL语句。 | 4462| args | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。该参数不填,或者填null或undefined,都认为是sql参数语句完整。默认值为空。 | 4463 4464**返回值**: 4465 4466| 类型 | 说明 | 4467| ----------------------- | ------------------- | 4468| [ValueType](#valuetype) | 返回sql执行后的结果 | 4469 4470**错误码:** 4471 4472以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4473 4474| **错误码ID** | **错误信息** | 4475| ------------ | ------------------------------------------------------------ | 4476| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4477| 14800000 | Inner error. | 4478| 14800011 | Database corrupted. | 4479| 14800014 | Already closed. | 4480| 14800015 | The database does not respond. | 4481| 14800021 | SQLite: Generic error. | 4482| 14800022 | SQLite: Callback routine requested an abort. | 4483| 14800023 | SQLite: Access permission denied. | 4484| 14800024 | SQLite: The database file is locked. | 4485| 14800025 | SQLite: A table in the database is locked. | 4486| 14800026 | SQLite: The database is out of memory. | 4487| 14800027 | SQLite: Attempt to write a readonly database. | 4488| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4489| 14800029 | SQLite: The database is full. | 4490| 14800030 | SQLite: Unable to open the database file. | 4491| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4492| 14800032 | SQLite: Abort due to constraint violation. | 4493| 14800033 | SQLite: Data type mismatch. | 4494| 14800034 | SQLite: Library used incorrectly. | 4495| 14800047 | The WAL file size exceeds the default limit. | 4496 4497**示例:** 4498 4499```ts 4500import { BusinessError } from '@kit.BasicServicesKit'; 4501 4502// 校验数据库完整性 4503if(store != undefined) { 4504 const SQL_CHECK_INTEGRITY = 'PRAGMA integrity_check'; 4505 try { 4506 let data = (store as relationalStore.RdbStore).executeSync(SQL_CHECK_INTEGRITY) 4507 console.info(`check result: ${data}`); 4508 } catch (err) { 4509 console.error(`check failed, code is ${err.code}, message is ${err.message}`); 4510 } 4511} 4512 4513// 删除表中所有数据 4514if(store != undefined) { 4515 const SQL_DELETE_TABLE = 'DELETE FROM test'; 4516 try { 4517 let data = (store as relationalStore.RdbStore).executeSync(SQL_DELETE_TABLE) 4518 console.info(`delete result: ${data}`); 4519 } catch (err) { 4520 console.error(`delete failed, code is ${err.code}, message is ${err.message}`); 4521 } 4522} 4523 4524// 删表 4525if(store != undefined) { 4526 const SQL_DROP_TABLE = 'DROP TABLE test'; 4527 try { 4528 let data = (store as relationalStore.RdbStore).executeSync(SQL_DROP_TABLE) 4529 console.info(`drop result: ${data}`); 4530 } catch (err) { 4531 console.error(`drop failed, code is ${err.code}, message is ${err.message}`); 4532 } 4533} 4534``` 4535 4536### getModifyTime<sup>10+</sup> 4537 4538getModifyTime(table: string, columnName: string, primaryKeys: PRIKeyType[], callback: AsyncCallback<ModifyTime>): void 4539 4540获取数据库表中数据的最后修改时间,使用callback异步回调。 4541 4542**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4543 4544**参数:** 4545 4546| 参数名 | 类型 | 必填 | 说明 | 4547| ----------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | 4548| table | string | 是 | 指定要查询的数据库表的表名。 | 4549| columnName | string | 是 | 指定要查询的数据库表的列名。 | 4550| primaryKeys | [PRIKeyType](#prikeytype10)[] | 是 | 指定要查询的行的主键。<br>如果数据库表无主键,参数columnName需传入"rowid",此时primaryKeys为要查询的数据库表的行号。<br>如果数据库表无主键,参数columnName传入不为"rowid",返回对应的错误码。 | 4551| callback | AsyncCallback<[ModifyTime](#modifytime10)> | 是 | 指定callback回调函数。如果操作成功,则返回ModifyTime对象,表示数据的最后修改时间。 | 4552 4553**错误码:** 4554 4555以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4556 4557| **错误码ID** | **错误信息** | 4558|-----------| ------------------------------------------------------------ | 4559| 401 | Parameter error. Possible causes: 1. Need 3 - 4 parameter(s)! 2. The RdbStore must be not nullptr.3. The tablesNames must be not empty string. 4. The columnName must be not empty string. 5. The PRIKey must be number or string. | 4560| 801 | Capability not supported. | 4561| 14800000 | Inner error. | 4562| 14800011 | Database corrupted. | 4563| 14800014 | Already closed. | 4564| 14800015 | The database does not respond. | 4565| 14800021 | SQLite: Generic error. | 4566| 14800022 | SQLite: Callback routine requested an abort. | 4567| 14800023 | SQLite: Access permission denied. | 4568| 14800024 | SQLite: The database file is locked. | 4569| 14800025 | SQLite: A table in the database is locked. | 4570| 14800026 | SQLite: The database is out of memory. | 4571| 14800027 | SQLite: Attempt to write a readonly database. | 4572| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4573| 14800029 | SQLite: The database is full. | 4574| 14800030 | SQLite: Unable to open the database file. | 4575| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4576| 14800032 | SQLite: Abort due to constraint violation. | 4577| 14800033 | SQLite: Data type mismatch. | 4578| 14800034 | SQLite: Library used incorrectly. | 4579 4580**示例:** 4581 4582```ts 4583let PRIKey = [1, 4, 2, 3]; 4584if(store != undefined) { 4585 (store as relationalStore.RdbStore).getModifyTime("EMPLOYEE", "NAME", PRIKey, (err, modifyTime: relationalStore.ModifyTime) => { 4586 if (err) { 4587 console.error(`getModifyTime failed, code is ${err.code},message is ${err.message}`); 4588 return; 4589 } 4590 let size = modifyTime.size; 4591 }); 4592} 4593``` 4594 4595### getModifyTime<sup>10+</sup> 4596 4597getModifyTime(table: string, columnName: string, primaryKeys: PRIKeyType[]): Promise<ModifyTime> 4598 4599获取数据库表中数据的最后修改时间,使用Promise异步回调。 4600 4601**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4602 4603**参数:** 4604 4605| 参数名 | 类型 | 必填 | 说明 | 4606| ----------- | ----------------------------- | ---- | ------------------------------------------------------------ | 4607| table | string | 是 | 指定要查询的数据库表的表名。 | 4608| columnName | string | 是 | 指定要查询的数据库表的列名。 | 4609| primaryKeys | [PRIKeyType](#prikeytype10)[] | 是 | 指定要查询的行的主键。<br>如果数据库表无主键,参数columnName需传入"rowid",此时primaryKeys为要查询的数据库表的行号。<br>如果数据库表无主键,参数columnName传入不为"rowid",返回对应的错误码。 | 4610 4611**返回值**: 4612 4613| 类型 | 说明 | 4614| ------------------------------------------ | --------------------------------------------------------- | 4615| Promise<[ModifyTime](#modifytime10)> | 返回ModifyTime类型的Promise对象,表示数据最后的修改时间。 | 4616 4617**错误码:** 4618 4619以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4620 4621| **错误码ID** | **错误信息** | 4622|-----------| ------------------------------------------------------------ | 4623| 401 | Parameter error. Possible causes: 1. Need 3 - 4 parameter(s)! 2. The RdbStore must be not nullptr.3. The tablesNames must be not empty string. 4. The columnName must be not empty string. 5. The PRIKey must be number or string. | 4624| 801 | Capability not supported. | 4625| 14800000 | Inner error. | 4626| 14800011 | Database corrupted. | 4627| 14800014 | Already closed. | 4628| 14800015 | The database does not respond. | 4629| 14800021 | SQLite: Generic error. | 4630| 14800022 | SQLite: Callback routine requested an abort. | 4631| 14800023 | SQLite: Access permission denied. | 4632| 14800024 | SQLite: The database file is locked. | 4633| 14800025 | SQLite: A table in the database is locked. | 4634| 14800026 | SQLite: The database is out of memory. | 4635| 14800027 | SQLite: Attempt to write a readonly database. | 4636| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4637| 14800029 | SQLite: The database is full. | 4638| 14800030 | SQLite: Unable to open the database file. | 4639| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4640| 14800032 | SQLite: Abort due to constraint violation. | 4641| 14800033 | SQLite: Data type mismatch. | 4642| 14800034 | SQLite: Library used incorrectly. | 4643 4644**示例:** 4645 4646```ts 4647import { BusinessError } from '@kit.BasicServicesKit'; 4648 4649let PRIKey = [1, 2, 3]; 4650if(store != undefined) { 4651 (store as relationalStore.RdbStore).getModifyTime("EMPLOYEE", "NAME", PRIKey) 4652 .then((modifyTime: relationalStore.ModifyTime) => { 4653 let size = modifyTime.size; 4654 }) 4655 .catch((err: BusinessError) => { 4656 console.error(`getModifyTime failed, code is ${err.code},message is ${err.message}`); 4657 }); 4658} 4659``` 4660 4661### beginTransaction 4662 4663beginTransaction():void 4664 4665在开始执行SQL语句之前,开始事务。 4666此接口不允许嵌套事务,且不支持在多进程或多线程中使用。 4667 4668**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4669 4670**错误码:** 4671 4672以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4673 4674| **错误码ID** | **错误信息** | 4675|-----------| ------------------------------------------------------------ | 4676| 401 | Parameter error. The store must not be nullptr. | 4677| 14800000 | Inner error. | 4678| 14800011 | Database corrupted. | 4679| 14800014 | Already closed. | 4680| 14800015 | The database does not respond. | 4681| 14800021 | SQLite: Generic error. | 4682| 14800022 | SQLite: Callback routine requested an abort. | 4683| 14800023 | SQLite: Access permission denied. | 4684| 14800024 | SQLite: The database file is locked. | 4685| 14800025 | SQLite: A table in the database is locked. | 4686| 14800026 | SQLite: The database is out of memory. | 4687| 14800027 | SQLite: Attempt to write a readonly database. | 4688| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4689| 14800029 | SQLite: The database is full. | 4690| 14800030 | SQLite: Unable to open the database file. | 4691| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4692| 14800032 | SQLite: Abort due to constraint violation. | 4693| 14800033 | SQLite: Data type mismatch. | 4694| 14800034 | SQLite: Library used incorrectly. | 4695| 14800047 | The WAL file size exceeds the default limit. | 4696 4697**示例:** 4698 4699```ts 4700 4701let value1 = "Lisa"; 4702let value2 = 18; 4703let value3 = 100.5; 4704let value4 = new Uint8Array([1, 2, 3]); 4705 4706if(store != undefined) { 4707 (store as relationalStore.RdbStore).beginTransaction(); 4708 const valueBucket: relationalStore.ValuesBucket = { 4709 'NAME': value1, 4710 'AGE': value2, 4711 'SALARY': value3, 4712 'CODES': value4, 4713 }; 4714 (store as relationalStore.RdbStore).insert("test", valueBucket); 4715 (store as relationalStore.RdbStore).commit(); 4716} 4717``` 4718 4719### beginTrans<sup>12+</sup> 4720 4721beginTrans(): Promise<number> 4722 4723在开始执行SQL语句之前,开始事务,使用Promise异步回调。 4724 4725与[beginTransaction](#begintransaction)的区别在于:该接口会返回事务ID,[execute](#execute12-1)可以指定不同事务ID达到事务隔离目的。 4726 4727<!--RP1--> 4728该接口仅支持[向量数据库](js-apis-data-relationalStore-sys.md#storeconfig)使用。<!--RP1End--> 4729 4730**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4731 4732**返回值**: 4733 4734| 类型 | 说明 | 4735| ------------------- | ------------------------- | 4736| Promise<number> | Promise对象,返回事务ID。 | 4737 4738**错误码:** 4739 4740以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4741 4742| **错误码ID** | **错误信息** | 4743|-----------| ------------------------------------------------------------ | 4744| 401 | Parameter error. The store must not be nullptr. | 4745| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 4746| 14800000 | Inner error. | 4747| 14800011 | Database corrupted. | 4748| 14800014 | Already closed. | 4749| 14800015 | The database does not respond. | 4750| 14800021 | SQLite: Generic error. | 4751| 14800022 | SQLite: Callback routine requested an abort. | 4752| 14800023 | SQLite: Access permission denied. | 4753| 14800024 | SQLite: The database file is locked. | 4754| 14800025 | SQLite: A table in the database is locked. | 4755| 14800026 | SQLite: The database is out of memory. | 4756| 14800027 | SQLite: Attempt to write a readonly database. | 4757| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4758| 14800029 | SQLite: The database is full. | 4759| 14800030 | SQLite: Unable to open the database file. | 4760| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4761| 14800032 | SQLite: Abort due to constraint violation. | 4762| 14800033 | SQLite: Data type mismatch. | 4763| 14800034 | SQLite: Library used incorrectly. | 4764| 14800047 | The WAL file size exceeds the default limit. | 4765 4766**示例:** 4767 4768```ts 4769import { BusinessError } from '@kit.BasicServicesKit'; 4770if(store != null) { 4771 let txId : number; 4772 (store as relationalStore.RdbStore).beginTrans().then((txId : number) => { 4773 (store as relationalStore.RdbStore).execute("DELETE FROM TEST WHERE age = ? OR age = ?", txId, ["18", "20"]) 4774 .then(() => { 4775 (store as relationalStore.RdbStore).commit(txId); 4776 }) 4777 .catch((err: BusinessError) => { 4778 (store as relationalStore.RdbStore).rollback(txId) 4779 console.error(`execute sql failed, code is ${err.code},message is ${err.message}`); 4780 }); 4781 }); 4782} 4783``` 4784 4785### commit 4786 4787commit():void 4788 4789提交已执行的SQL语句,跟[beginTransaction](#begintransaction)配合使用。 4790此接口不允许嵌套事务,且不支持在多进程或多线程中使用。 4791 4792**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4793 4794**错误码:** 4795 4796以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4797 4798| **错误码ID** | **错误信息** | 4799|-----------| ------------------------------------------------------------ | 4800| 401 | Parameter error. The store must not be nullptr. | 4801| 14800000 | Inner error. | 4802| 14800011 | Database corrupted. | 4803| 14800014 | Already closed. | 4804| 14800015 | The database does not respond. | 4805| 14800021 | SQLite: Generic error. | 4806| 14800022 | SQLite: Callback routine requested an abort. | 4807| 14800023 | SQLite: Access permission denied. | 4808| 14800024 | SQLite: The database file is locked. | 4809| 14800025 | SQLite: A table in the database is locked. | 4810| 14800026 | SQLite: The database is out of memory. | 4811| 14800027 | SQLite: Attempt to write a readonly database. | 4812| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4813| 14800029 | SQLite: The database is full. | 4814| 14800030 | SQLite: Unable to open the database file. | 4815| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4816| 14800032 | SQLite: Abort due to constraint violation. | 4817| 14800033 | SQLite: Data type mismatch. | 4818| 14800034 | SQLite: Library used incorrectly. | 4819 4820**示例:** 4821 4822```ts 4823 4824let value1 = "Lisa"; 4825let value2 = 18; 4826let value3 = 100.5; 4827let value4 = new Uint8Array([1, 2, 3]); 4828 4829if(store != undefined) { 4830 (store as relationalStore.RdbStore).beginTransaction(); 4831 const valueBucket: relationalStore.ValuesBucket = { 4832 'NAME': value1, 4833 'AGE': value2, 4834 'SALARY': value3, 4835 'CODES': value4, 4836 }; 4837 (store as relationalStore.RdbStore).insert("test", valueBucket); 4838 (store as relationalStore.RdbStore).commit(); 4839} 4840``` 4841 4842### commit<sup>12+</sup> 4843 4844commit(txId : number):Promise<void> 4845 4846提交已执行的SQL语句,跟[beginTrans](#begintrans12)配合使用。 4847 4848<!--RP1--> 4849该接口仅支持[向量数据库](js-apis-data-relationalStore-sys.md#storeconfig)使用。<!--RP1End--> 4850 4851**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4852 4853**参数:** 4854 4855| 参数名 | 类型 | 必填 | 说明 | 4856| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4857| txId | number | 是 | 通过[beginTrans](#begintrans12)获取的事务ID。 | 4858 4859**返回值**: 4860 4861| 类型 | 说明 | 4862| ------------------- | ------------------------- | 4863| Promise<void> | 无返回结果的Promise对象。 | 4864 4865**错误码:** 4866 4867以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4868 4869| **错误码ID** | **错误信息** | 4870|-----------| ------------------------------------------------------------ | 4871| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4872| 14800000 | Inner error. | 4873| 14800011 | Database corrupted. | 4874| 14800014 | Already closed. | 4875| 14800015 | The database does not respond. | 4876| 14800021 | SQLite: Generic error. | 4877| 14800022 | SQLite: Callback routine requested an abort. | 4878| 14800023 | SQLite: Access permission denied. | 4879| 14800024 | SQLite: The database file is locked. | 4880| 14800025 | SQLite: A table in the database is locked. | 4881| 14800026 | SQLite: The database is out of memory. | 4882| 14800027 | SQLite: Attempt to write a readonly database. | 4883| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4884| 14800029 | SQLite: The database is full. | 4885| 14800030 | SQLite: Unable to open the database file. | 4886| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4887| 14800032 | SQLite: Abort due to constraint violation. | 4888| 14800033 | SQLite: Data type mismatch. | 4889| 14800034 | SQLite: Library used incorrectly. | 4890 4891**示例:** 4892 4893```ts 4894import { BusinessError } from '@kit.BasicServicesKit'; 4895if(store != null) { 4896 let txId : number; 4897 (store as relationalStore.RdbStore).beginTrans().then((txId : number) => { 4898 (store as relationalStore.RdbStore).execute("DELETE FROM TEST WHERE age = ? OR age = ?", txId, ["18", "20"]) 4899 .then(() => { 4900 (store as relationalStore.RdbStore).commit(txId); 4901 }) 4902 .catch((err: BusinessError) => { 4903 (store as relationalStore.RdbStore).rollback(txId) 4904 console.error(`execute sql failed, code is ${err.code},message is ${err.message}`); 4905 }); 4906 }); 4907} 4908``` 4909 4910### rollBack 4911 4912rollBack():void 4913 4914回滚已经执行的SQL语句。 4915此接口不允许嵌套事务,且不支持在多进程或多线程中使用。 4916 4917**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4918 4919**错误码:** 4920 4921以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4922 4923| **错误码ID** | **错误信息** | 4924|-----------| ------------------------------------------------------------ | 4925| 401 | Parameter error. The store must not be nullptr. | 4926| 14800000 | Inner error. | 4927| 14800011 | Database corrupted. | 4928| 14800014 | Already closed. | 4929| 14800015 | The database does not respond. | 4930| 14800021 | SQLite: Generic error. | 4931| 14800022 | SQLite: Callback routine requested an abort. | 4932| 14800023 | SQLite: Access permission denied. | 4933| 14800024 | SQLite: The database file is locked. | 4934| 14800025 | SQLite: A table in the database is locked. | 4935| 14800026 | SQLite: The database is out of memory. | 4936| 14800027 | SQLite: Attempt to write a readonly database. | 4937| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4938| 14800029 | SQLite: The database is full. | 4939| 14800030 | SQLite: Unable to open the database file. | 4940| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4941| 14800032 | SQLite: Abort due to constraint violation. | 4942| 14800033 | SQLite: Data type mismatch. | 4943| 14800034 | SQLite: Library used incorrectly. | 4944 4945**示例:** 4946 4947```ts 4948import { BusinessError } from '@kit.BasicServicesKit'; 4949 4950let value1 = "Lisa"; 4951let value2 = 18; 4952let value3 = 100.5; 4953let value4 = new Uint8Array([1, 2, 3]); 4954 4955if(store != undefined) { 4956 try { 4957 (store as relationalStore.RdbStore).beginTransaction() 4958 const valueBucket: relationalStore.ValuesBucket = { 4959 'NAME': value1, 4960 'AGE': value2, 4961 'SALARY': value3, 4962 'CODES': value4, 4963 }; 4964 (store as relationalStore.RdbStore).insert("test", valueBucket); 4965 (store as relationalStore.RdbStore).commit(); 4966 } catch (err) { 4967 let code = (err as BusinessError).code; 4968 let message = (err as BusinessError).message 4969 console.error(`Transaction failed, code is ${code},message is ${message}`); 4970 (store as relationalStore.RdbStore).rollBack(); 4971 } 4972} 4973``` 4974 4975### rollback<sup>12+</sup> 4976 4977rollback(txId : number):Promise<void> 4978 4979回滚已经执行的SQL语句,跟[beginTrans](#begintrans12)配合使用。 4980 4981<!--RP1--> 4982该接口仅支持[向量数据库](js-apis-data-relationalStore-sys.md#storeconfig)使用。<!--RP1End--> 4983 4984**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4985 4986**参数:** 4987 4988| 参数名 | 类型 | 必填 | 说明 | 4989| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4990| txId | number | 是 | 通过[beginTrans](#begintrans12)获取的事务ID。 | 4991 4992**返回值**: 4993 4994| 类型 | 说明 | 4995| ------------------- | ------------------------- | 4996| Promise<void> | 无返回结果的Promise对象。 | 4997 4998**错误码:** 4999 5000以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5001 5002| **错误码ID** | **错误信息** | 5003|-----------| ------------------------------------------------------------ | 5004| 401 | Parameter error. The store must not be nullptr. | 5005| 14800000 | Inner error. | 5006| 14800011 | Database corrupted. | 5007| 14800014 | Already closed. | 5008| 14800015 | The database does not respond. | 5009| 14800021 | SQLite: Generic error. | 5010| 14800022 | SQLite: Callback routine requested an abort. | 5011| 14800023 | SQLite: Access permission denied. | 5012| 14800024 | SQLite: The database file is locked. | 5013| 14800025 | SQLite: A table in the database is locked. | 5014| 14800026 | SQLite: The database is out of memory. | 5015| 14800027 | SQLite: Attempt to write a readonly database. | 5016| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5017| 14800029 | SQLite: The database is full. | 5018| 14800030 | SQLite: Unable to open the database file. | 5019| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5020| 14800032 | SQLite: Abort due to constraint violation. | 5021| 14800033 | SQLite: Data type mismatch. | 5022| 14800034 | SQLite: Library used incorrectly. | 5023 5024**示例:** 5025 5026```ts 5027import { BusinessError } from '@kit.BasicServicesKit'; 5028if(store != null) { 5029 let txId : number; 5030 (store as relationalStore.RdbStore).beginTrans().then((txId : number) => { 5031 (store as relationalStore.RdbStore).execute("DELETE FROM TEST WHERE age = ? OR age = ?", txId, ["18", "20"]) 5032 .then(() => { 5033 (store as relationalStore.RdbStore).commit(txId); 5034 }) 5035 .catch((err: BusinessError) => { 5036 (store as relationalStore.RdbStore).rollback(txId) 5037 console.error(`execute sql failed, code is ${err.code},message is ${err.message}`); 5038 }); 5039 }); 5040} 5041``` 5042 5043### backup 5044 5045backup(destName:string, callback: AsyncCallback<void>):void 5046 5047以指定名称备份数据库,使用callback异步回调。 5048 5049**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5050 5051**参数:** 5052 5053| 参数名 | 类型 | 必填 | 说明 | 5054| -------- | ------------------------- | ---- | ------------------------ | 5055| destName | string | 是 | 指定数据库的备份文件名。 | 5056| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 5057 5058**错误码:** 5059 5060以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5061 5062| **错误码ID** | **错误信息** | 5063|-----------| ------------------------------------------------------------ | 5064| 401 | Parameter error. The store must not be nullptr. | 5065| 14800000 | Inner error. | 5066| 14800010 | Invalid database path. | 5067| 14800011 | Database corrupted. | 5068| 14800014 | Already closed. | 5069| 14800015 | The database does not respond. | 5070| 14800021 | SQLite: Generic error. | 5071| 14800022 | SQLite: Callback routine requested an abort. | 5072| 14800023 | SQLite: Access permission denied. | 5073| 14800024 | SQLite: The database file is locked. | 5074| 14800025 | SQLite: A table in the database is locked. | 5075| 14800026 | SQLite: The database is out of memory. | 5076| 14800027 | SQLite: Attempt to write a readonly database. | 5077| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5078| 14800029 | SQLite: The database is full. | 5079| 14800030 | SQLite: Unable to open the database file. | 5080| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5081| 14800032 | SQLite: Abort due to constraint violation. | 5082| 14800033 | SQLite: Data type mismatch. | 5083| 14800034 | SQLite: Library used incorrectly. | 5084 5085**示例:** 5086 5087```ts 5088if(store != undefined) { 5089 (store as relationalStore.RdbStore).backup("dbBackup.db", (err) => { 5090 if (err) { 5091 console.error(`Backup failed, code is ${err.code},message is ${err.message}`); 5092 return; 5093 } 5094 console.info('Backup success.'); 5095 }) 5096} 5097``` 5098 5099### backup 5100 5101backup(destName:string): Promise<void> 5102 5103以指定名称备份数据库,使用Promise异步回调。 5104 5105**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5106 5107**参数:** 5108 5109| 参数名 | 类型 | 必填 | 说明 | 5110| -------- | ------ | ---- | ------------------------ | 5111| destName | string | 是 | 指定数据库的备份文件名。 | 5112 5113**返回值**: 5114 5115| 类型 | 说明 | 5116| ------------------- | ------------------------- | 5117| Promise<void> | 无返回结果的Promise对象。 | 5118 5119**错误码:** 5120 5121以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5122 5123| **错误码ID** | **错误信息** | 5124|-----------| ------------------------------------------------------------ | 5125| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5126| 14800000 | Inner error. | 5127| 14800011 | Database corrupted. | 5128| 14800014 | Already closed. | 5129| 14800015 | The database does not respond. | 5130| 14800021 | SQLite: Generic error. | 5131| 14800022 | SQLite: Callback routine requested an abort. | 5132| 14800023 | SQLite: Access permission denied. | 5133| 14800024 | SQLite: The database file is locked. | 5134| 14800025 | SQLite: A table in the database is locked. | 5135| 14800026 | SQLite: The database is out of memory. | 5136| 14800027 | SQLite: Attempt to write a readonly database. | 5137| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5138| 14800029 | SQLite: The database is full. | 5139| 14800030 | SQLite: Unable to open the database file. | 5140| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5141| 14800032 | SQLite: Abort due to constraint violation. | 5142| 14800033 | SQLite: Data type mismatch. | 5143| 14800034 | SQLite: Library used incorrectly. | 5144 5145**示例:** 5146 5147```ts 5148import { BusinessError } from '@kit.BasicServicesKit'; 5149 5150if(store != undefined) { 5151 let promiseBackup = (store as relationalStore.RdbStore).backup("dbBackup.db"); 5152 promiseBackup.then(() => { 5153 console.info('Backup success.'); 5154 }).catch((err: BusinessError) => { 5155 console.error(`Backup failed, code is ${err.code},message is ${err.message}`); 5156 }) 5157} 5158``` 5159 5160### restore 5161 5162restore(srcName:string, callback: AsyncCallback<void>):void 5163 5164从指定的数据库备份文件恢复数据库,使用callback异步回调。 5165 5166**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5167 5168**参数:** 5169 5170| 参数名 | 类型 | 必填 | 说明 | 5171| -------- | ------------------------- | ---- | ------------------------ | 5172| srcName | string | 是 | 指定数据库的备份文件名。 | 5173| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 5174 5175**错误码:** 5176 5177以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5178 5179| **错误码ID** | **错误信息** | 5180|-----------| ------------------------------------------------------------ | 5181| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5182| 14800000 | Inner error. | 5183| 14800011 | Database corrupted. | 5184| 14800014 | Already closed. | 5185| 14800015 | The database does not respond. | 5186| 14800021 | SQLite: Generic error. | 5187| 14800022 | SQLite: Callback routine requested an abort. | 5188| 14800023 | SQLite: Access permission denied. | 5189| 14800024 | SQLite: The database file is locked. | 5190| 14800025 | SQLite: A table in the database is locked. | 5191| 14800026 | SQLite: The database is out of memory. | 5192| 14800027 | SQLite: Attempt to write a readonly database. | 5193| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5194| 14800029 | SQLite: The database is full. | 5195| 14800030 | SQLite: Unable to open the database file. | 5196| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5197| 14800032 | SQLite: Abort due to constraint violation. | 5198| 14800033 | SQLite: Data type mismatch. | 5199| 14800034 | SQLite: Library used incorrectly. | 5200 5201**示例:** 5202 5203```ts 5204if(store != undefined) { 5205 (store as relationalStore.RdbStore).restore("dbBackup.db", (err) => { 5206 if (err) { 5207 console.error(`Restore failed, code is ${err.code},message is ${err.message}`); 5208 return; 5209 } 5210 console.info('Restore success.'); 5211 }) 5212} 5213``` 5214 5215### restore 5216 5217restore(srcName:string): Promise<void> 5218 5219从指定的数据库备份文件恢复数据库,使用Promise异步回调。 5220 5221**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5222 5223**参数:** 5224 5225| 参数名 | 类型 | 必填 | 说明 | 5226| ------- | ------ | ---- | ------------------------ | 5227| srcName | string | 是 | 指定数据库的备份文件名。 | 5228 5229**返回值**: 5230 5231| 类型 | 说明 | 5232| ------------------- | ------------------------- | 5233| Promise<void> | 无返回结果的Promise对象。 | 5234 5235**错误码:** 5236 5237以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5238 5239| **错误码ID** | **错误信息** | 5240|-----------| ------------------------------------------------------------ | 5241| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5242| 14800000 | Inner error. | 5243| 14800011 | Database corrupted. | 5244| 14800014 | Already closed. | 5245| 14800015 | The database does not respond. | 5246| 14800021 | SQLite: Generic error. | 5247| 14800022 | SQLite: Callback routine requested an abort. | 5248| 14800023 | SQLite: Access permission denied. | 5249| 14800024 | SQLite: The database file is locked. | 5250| 14800025 | SQLite: A table in the database is locked. | 5251| 14800026 | SQLite: The database is out of memory. | 5252| 14800027 | SQLite: Attempt to write a readonly database. | 5253| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5254| 14800029 | SQLite: The database is full. | 5255| 14800030 | SQLite: Unable to open the database file. | 5256| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5257| 14800032 | SQLite: Abort due to constraint violation. | 5258| 14800033 | SQLite: Data type mismatch. | 5259| 14800034 | SQLite: Library used incorrectly. | 5260 5261**示例:** 5262 5263```ts 5264import { BusinessError } from '@kit.BasicServicesKit'; 5265 5266if(store != undefined) { 5267 let promiseRestore = (store as relationalStore.RdbStore).restore("dbBackup.db"); 5268 promiseRestore.then(() => { 5269 console.info('Restore success.'); 5270 }).catch((err: BusinessError) => { 5271 console.error(`Restore failed, code is ${err.code},message is ${err.message}`); 5272 }) 5273} 5274``` 5275 5276### setDistributedTables 5277 5278setDistributedTables(tables: Array<string>, callback: AsyncCallback<void>): void 5279 5280设置分布式数据库表,使用callback异步回调。 5281 5282**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 5283 5284**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5285 5286**参数:** 5287 5288| 参数名 | 类型 | 必填 | 说明 | 5289| -------- | ------------------------- | ---- | ---------------------- | 5290| tables | Array<string> | 是 | 要设置的分布式数据库表表名。 | 5291| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 5292 5293**错误码:** 5294 5295以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5296 5297| **错误码ID** | **错误信息** | 5298|-----------| ------------------------------------------------------------ | 5299| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5300| 801 | Capability not supported. | 5301| 14800000 | Inner error. | 5302| 14800014 | Already closed. | 5303 5304**示例:** 5305 5306```ts 5307if(store != undefined) { 5308 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], (err) => { 5309 if (err) { 5310 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 5311 return; 5312 } 5313 console.info('SetDistributedTables successfully.'); 5314 }) 5315} 5316``` 5317 5318### setDistributedTables 5319 5320 setDistributedTables(tables: Array<string>): Promise<void> 5321 5322设置分布式数据库表,使用Promise异步回调。 5323 5324**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 5325 5326**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5327 5328**参数:** 5329 5330| 参数名 | 类型 | 必填 | 说明 | 5331| ------ | ------------------------ | ---- | ------------------------ | 5332| tables | ArrayArray<string> | 是 | 要设置的分布式数据库表表名。 | 5333 5334**返回值**: 5335 5336| 类型 | 说明 | 5337| ------------------- | ------------------------- | 5338| Promise<void> | 无返回结果的Promise对象。 | 5339 5340**错误码:** 5341 5342以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5343 5344| **错误码ID** | **错误信息** | 5345|-----------| ------------------------------------------------------------ | 5346| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5347| 801 | Capability not supported. | 5348| 14800000 | Inner error. | 5349| 14800014 | Already closed. | 5350 5351**示例:** 5352 5353```ts 5354import { BusinessError } from '@kit.BasicServicesKit'; 5355 5356if(store != undefined) { 5357 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"]).then(() => { 5358 console.info('SetDistributedTables successfully.'); 5359 }).catch((err: BusinessError) => { 5360 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 5361 }) 5362} 5363``` 5364 5365### setDistributedTables<sup>10+</sup> 5366 5367setDistributedTables(tables: Array<string>, type: DistributedType, callback: AsyncCallback<void>): void 5368 5369设置分布式数据库表,使用callback异步回调。 5370 5371**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 5372 5373**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5374 5375**参数:** 5376 5377| 参数名 | 类型 | 必填 | 说明 | 5378| -------- | ------------------------------------- | ---- | ---------------------------- | 5379| tables | Array<string> | 是 | 要设置的分布式数据库表表名。 | 5380| type | [DistributedType](#distributedtype10) | 是 | 表的分布式类型。 | 5381| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 5382 5383**错误码:** 5384 5385以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5386 5387| **错误码ID** | **错误信息** | 5388|-----------| ------------------------------------------------------------ | 5389| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5390| 801 | Capability not supported. | 5391| 14800000 | Inner error. | 5392| 14800014 | Already closed. | 5393| 14800051 | The type of the distributed table does not match. | 5394 5395**示例:** 5396 5397```ts 5398if(store != undefined) { 5399 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, (err) => { 5400 if (err) { 5401 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 5402 return; 5403 } 5404 console.info('SetDistributedTables successfully.'); 5405 }) 5406} 5407``` 5408 5409### setDistributedTables<sup>10+</sup> 5410 5411setDistributedTables(tables: Array<string>, type: DistributedType, config: DistributedConfig, callback: AsyncCallback<void>): void 5412 5413设置分布式数据库表,使用callback异步回调。 5414 5415**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 5416 5417**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5418 5419**参数:** 5420 5421| 参数名 | 类型 | 必填 | 说明 | 5422| -------- | ----------------------------------- | --- | --------------- | 5423| tables | Array<string> | 是 | 要设置的分布式数据库表表名。 | 5424| type | [DistributedType](#distributedtype10) | 是 | 表的分布式类型。 | 5425| config | [DistributedConfig](#distributedconfig10) | 是 | 表的分布式配置信息。 | 5426| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 5427 5428**错误码:** 5429 5430以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5431 5432| **错误码ID** | **错误信息** | 5433|-----------| ------------------------------------------------------------ | 5434| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5435| 801 | Capability not supported. | 5436| 14800000 | Inner error. | 5437| 14800014 | Already closed. | 5438| 14800051 | The type of the distributed table does not match. | 5439 5440**示例:** 5441 5442```ts 5443if(store != undefined) { 5444 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, { 5445 autoSync: true 5446 }, (err) => { 5447 if (err) { 5448 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 5449 return; 5450 } 5451 console.info('SetDistributedTables successfully.'); 5452 }) 5453} 5454``` 5455 5456### setDistributedTables<sup>10+</sup> 5457 5458 setDistributedTables(tables: Array<string>, type?: DistributedType, config?: DistributedConfig): Promise<void> 5459 5460设置分布式数据库表,使用Promise异步回调。 5461 5462**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 5463 5464**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5465 5466**参数:** 5467 5468| 参数名 | 类型 | 必填 | 说明 | 5469| ------ | ----------------------------------------- | ---- | ------------------------------------------------------------ | 5470| tables | Array<string> | 是 | 要设置的分布式数据库表表名。 | 5471| type | [DistributedType](#distributedtype10) | 否 | 表的分布式类型。默认值是relationalStore.DistributedType.DISTRIBUTED_DEVICE。 | 5472| config | [DistributedConfig](#distributedconfig10) | 否 | 表的分布式配置信息。不传入时默认autoSync为false,即只支持手动同步。 | 5473 5474**返回值**: 5475 5476| 类型 | 说明 | 5477| ------------------- | ------------------------- | 5478| Promise<void> | 无返回结果的Promise对象。 | 5479 5480**错误码:** 5481 5482以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5483 5484| **错误码ID** | **错误信息** | 5485|-----------| ------------------------------------------------------------ | 5486| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5487| 801 | Capability not supported. | 5488| 14800000 | Inner error. | 5489| 14800014 | Already closed. | 5490| 14800051 | The type of the distributed table does not match. | 5491 5492**示例:** 5493 5494```ts 5495import { BusinessError } from '@kit.BasicServicesKit'; 5496 5497if(store != undefined) { 5498 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, { 5499 autoSync: true 5500 }).then(() => { 5501 console.info('SetDistributedTables successfully.'); 5502 }).catch((err: BusinessError) => { 5503 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 5504 }) 5505} 5506``` 5507 5508### obtainDistributedTableName 5509 5510obtainDistributedTableName(device: string, table: string, callback: AsyncCallback<string>): void 5511 5512根据远程设备的本地表名获取指定远程设备的分布式表名。在查询远程设备数据库时,需要使用分布式表名, 使用callback异步回调。 5513 5514> **说明:** 5515> 5516> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 5517 5518**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 5519 5520**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5521 5522**参数:** 5523 5524| 参数名 | 类型 | 必填 | 说明 | 5525| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 5526| device | string | 是 | 远程设备ID 。 | 5527| table | string | 是 | 远程设备的本地表名。 | 5528| callback | AsyncCallback<string> | 是 | 指定的callback回调函数。如果操作成功,返回远程设备的分布式表名。 | 5529 5530**错误码:** 5531 5532以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5533 5534| **错误码ID** | **错误信息** | 5535|-----------| ------------------------------------------------------------ | 5536| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5537| 801 | Capability not supported. | 5538| 14800000 | Inner error. | 5539| 14800014 | Already closed. | 5540 5541**示例:** 5542 5543```ts 5544import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 5545import { BusinessError } from '@kit.BasicServicesKit'; 5546 5547let dmInstance: distributedDeviceManager.DeviceManager; 5548let deviceId: string | undefined = undefined; 5549 5550try { 5551 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 5552 let devices = dmInstance.getAvailableDeviceListSync(); 5553 deviceId = devices[0].networkId; 5554} catch (err) { 5555 let code = (err as BusinessError).code; 5556 let message = (err as BusinessError).message 5557 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 5558} 5559 5560if(store != undefined && deviceId != undefined) { 5561 (store as relationalStore.RdbStore).obtainDistributedTableName(deviceId, "EMPLOYEE", (err, tableName) => { 5562 if (err) { 5563 console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`); 5564 return; 5565 } 5566 console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`); 5567 }) 5568} 5569``` 5570 5571### obtainDistributedTableName 5572 5573 obtainDistributedTableName(device: string, table: string): Promise<string> 5574 5575根据远程设备的本地表名获取指定远程设备的分布式表名。在查询远程设备数据库时,需要使用分布式表名,使用Promise异步回调。 5576 5577> **说明:** 5578> 5579> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 5580 5581**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 5582 5583**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5584 5585**参数:** 5586 5587| 参数名 | 类型 | 必填 | 说明 | 5588| ------ | ------ | ---- | -------------------- | 5589| device | string | 是 | 远程设备ID。 | 5590| table | string | 是 | 远程设备的本地表名。 | 5591 5592**返回值**: 5593 5594| 类型 | 说明 | 5595| --------------------- | ----------------------------------------------------- | 5596| Promise<string> | Promise对象。如果操作成功,返回远程设备的分布式表名。 | 5597 5598**错误码:** 5599 5600以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5601 5602| **错误码ID** | **错误信息** | 5603|-----------| ------------------------------------------------------------ | 5604| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5605| 801 | Capability not supported. | 5606| 14800000 | Inner error. | 5607| 14800014 | Already closed. | 5608 5609**示例:** 5610 5611```ts 5612import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 5613import { BusinessError } from '@kit.BasicServicesKit'; 5614 5615let dmInstance: distributedDeviceManager.DeviceManager; 5616let deviceId: string | undefined = undefined; 5617 5618try { 5619 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 5620 let devices = dmInstance.getAvailableDeviceListSync(); 5621 deviceId = devices[0].networkId; 5622} catch (err) { 5623 let code = (err as BusinessError).code; 5624 let message = (err as BusinessError).message 5625 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 5626} 5627 5628if(store != undefined && deviceId != undefined) { 5629 (store as relationalStore.RdbStore).obtainDistributedTableName(deviceId, "EMPLOYEE").then((tableName: string) => { 5630 console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`); 5631 }).catch((err: BusinessError) => { 5632 console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`); 5633 }) 5634} 5635``` 5636 5637### sync 5638 5639sync(mode: SyncMode, predicates: RdbPredicates, callback: AsyncCallback<Array<[string, number]>>): void 5640 5641在设备之间同步数据, 使用callback异步回调。 5642 5643**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 5644 5645**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5646 5647**参数:** 5648 5649| 参数名 | 类型 | 必填 | 说明 | 5650| ---------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ | 5651| mode | [SyncMode](#syncmode) | 是 | 指同步模式。该值可以是relationalStore.SyncMode.SYNC_MODE_PUSH、relationalStore.SyncMode.SYNC_MODE_PULL。 | 5652| predicates | [RdbPredicates](#rdbpredicates) | 是 | 约束同步数据和设备。 | 5653| callback | AsyncCallback<Array<[string, number]>> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 | 5654 5655**错误码:** 5656 5657以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5658 5659| **错误码ID** | **错误信息** | 5660|-----------| ------------------------------------------------------------ | 5661| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5662| 801 | Capability not supported. | 5663| 14800000 | Inner error. | 5664| 14800014 | Already closed. | 5665 5666**示例:** 5667 5668```ts 5669import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 5670import { BusinessError } from '@kit.BasicServicesKit'; 5671 5672let dmInstance: distributedDeviceManager.DeviceManager; 5673let deviceIds: Array<string> = []; 5674 5675try { 5676 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 5677 let devices: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 5678 for (let i = 0; i < devices.length; i++) { 5679 deviceIds[i] = devices[i].networkId!; 5680 } 5681} catch (err) { 5682 let code = (err as BusinessError).code; 5683 let message = (err as BusinessError).message 5684 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 5685} 5686 5687let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 5688predicates.inDevices(deviceIds); 5689if(store != undefined) { 5690 (store as relationalStore.RdbStore).sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates, (err, result) => { 5691 if (err) { 5692 console.error(`Sync failed, code is ${err.code},message is ${err.message}`); 5693 return; 5694 } 5695 console.info('Sync done.'); 5696 for (let i = 0; i < result.length; i++) { 5697 console.info(`device= ${result[i][0]}, status= ${result[i][1]}`); 5698 } 5699 }) 5700} 5701``` 5702 5703### sync 5704 5705 sync(mode: SyncMode, predicates: RdbPredicates): Promise<Array<[string, number]>> 5706 5707在设备之间同步数据,使用Promise异步回调。 5708 5709**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 5710 5711**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5712 5713**参数:** 5714 5715| 参数名 | 类型 | 必填 | 说明 | 5716| ---------- | ------------------------------------ | ---- | ------------------------------ | 5717| mode | [SyncMode](#syncmode) | 是 | 指同步模式。该值可以是relationalStore.SyncMode.SYNC_MODE_PUSH、relationalStore.SyncMode.SYNC_MODE_PULL。 | 5718| predicates | [RdbPredicates](#rdbpredicates) | 是 | 约束同步数据和设备。 | 5719 5720**返回值**: 5721 5722| 类型 | 说明 | 5723| -------------------------------------------- | ------------------------------------------------------------ | 5724| Promise<Array<[string, number]>> | Promise对象,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 | 5725 5726**错误码:** 5727 5728以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5729 5730| **错误码ID** | **错误信息** | 5731|-----------| ------------------------------------------------------------ | 5732| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5733| 801 | Capability not supported. | 5734| 14800000 | Inner error. | 5735| 14800014 | Already closed. | 5736 5737**示例:** 5738 5739```ts 5740import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 5741import { BusinessError } from '@kit.BasicServicesKit'; 5742 5743let dmInstance: distributedDeviceManager.DeviceManager; 5744let deviceIds: Array<string> = []; 5745 5746try { 5747 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 5748 let devices: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 5749 for (let i = 0; i < devices.length; i++) { 5750 deviceIds[i] = devices[i].networkId!; 5751 } 5752} catch (err) { 5753 let code = (err as BusinessError).code; 5754 let message = (err as BusinessError).message 5755 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 5756} 5757 5758let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 5759predicates.inDevices(deviceIds); 5760if(store != undefined) { 5761 (store as relationalStore.RdbStore).sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates).then((result: Object[][]) => { 5762 console.info('Sync done.'); 5763 for (let i = 0; i < result.length; i++) { 5764 console.info(`device= ${result[i][0]}, status= ${result[i][1]}`); 5765 } 5766 }).catch((err: BusinessError) => { 5767 console.error(`Sync failed, code is ${err.code},message is ${err.message}`); 5768 }) 5769} 5770``` 5771 5772### cloudSync<sup>10+</sup> 5773 5774cloudSync(mode: SyncMode, progress: Callback<ProgressDetails>, callback: AsyncCallback<void>): void 5775 5776手动执行对所有分布式表的端云同步,使用callback异步回调。使用该接口需要实现云服务功能。 5777 5778**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 5779 5780**参数:** 5781 5782| 参数名 | 类型 | 必填 | 说明 | 5783| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 5784| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 5785| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 5786| callback | AsyncCallback<void> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。 | 5787 5788**错误码:** 5789 5790以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5791 5792| **错误码ID** | **错误信息** | 5793|-----------|-------| 5794| 401 | Parameter error. Possible causes: 1. Need 2 - 4 parameter(s). 2. The RdbStore must be not nullptr. 3. The mode must be a SyncMode of cloud. 4. The progress must be a callback type. 5. The callback must be a function. | 5795| 801 | Capability not supported. | 5796| 14800014 | Already closed. | 5797 5798**示例:** 5799 5800```ts 5801if(store != undefined) { 5802 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, (progressDetails) => { 5803 console.info(`Progess: ${progressDetails}`); 5804 }, (err) => { 5805 if (err) { 5806 console.error(`Cloud sync failed, code is ${err.code},message is ${err.message}`); 5807 return; 5808 } 5809 console.info('Cloud sync succeeded'); 5810 }); 5811} 5812``` 5813 5814### cloudSync<sup>10+</sup> 5815 5816cloudSync(mode: SyncMode, progress: Callback<ProgressDetails>): Promise<void> 5817 5818手动执行对所有分布式表的端云同步,使用Promise异步回调。使用该接口需要实现云服务功能。 5819 5820**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 5821 5822**参数:** 5823 5824| 参数名 | 类型 | 必填 | 说明 | 5825| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 5826| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 5827| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 5828 5829**返回值**: 5830 5831| 类型 | 说明 | 5832| ------------------- | --------------------------------------- | 5833| Promise<void> | Promise对象,用于向调用者发送同步结果。 | 5834 5835**错误码:** 5836 5837以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5838 5839| **错误码ID** | **错误信息** | 5840|-----------|------------------| 5841| 401 | Parameter error. Possible causes: 1. Need 2 - 4 parameter(s). 2. The RdbStore must be not nullptr. 3. The mode must be a SyncMode of cloud. 4. The progress must be a callback type. | 5842| 801 | Capability not supported. | 5843| 14800014 | Already closed. | 5844 5845**示例:** 5846 5847```ts 5848import { BusinessError } from '@kit.BasicServicesKit'; 5849 5850if(store != undefined) { 5851 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, (progressDetail: relationalStore.ProgressDetails) => { 5852 console.info(`progress: ${progressDetail}`); 5853 }).then(() => { 5854 console.info('Cloud sync succeeded'); 5855 }).catch((err: BusinessError) => { 5856 console.error(`cloudSync failed, code is ${err.code},message is ${err.message}`); 5857 }); 5858} 5859``` 5860 5861### cloudSync<sup>10+</sup> 5862 5863cloudSync(mode: SyncMode, tables: string[], progress: Callback<ProgressDetails>, callback: AsyncCallback<void>): void 5864 5865手动执行对指定表的端云同步,使用callback异步回调。使用该接口需要实现云服务功能。 5866 5867**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 5868 5869**参数:** 5870 5871| 参数名 | 类型 | 必填 | 说明 | 5872| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 5873| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 5874| tables | string[] | 是 | 指定同步的表名。 | 5875| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 5876| callback | AsyncCallback<void> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。 | 5877 5878**错误码:** 5879 5880以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5881 5882| **错误码ID** | **错误信息** | 5883|-----------|-------| 5884| 401 | Parameter error. Possible causes: 1. Need 2 - 4 parameter(s). 2. The RdbStore must be not nullptr. 3. The mode must be a SyncMode of cloud. 4. The tablesNames must be not empty. 5. The progress must be a callback type. 6.The callback must be a function.| 5885| 801 | Capability not supported. | 5886| 14800014 | Already closed. | 5887 5888**示例:** 5889 5890```ts 5891const tables = ["table1", "table2"]; 5892 5893if(store != undefined) { 5894 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, tables, (progressDetail: relationalStore.ProgressDetails) => { 5895 console.info(`Progess: ${progressDetail}`); 5896 }, (err) => { 5897 if (err) { 5898 console.error(`Cloud sync failed, code is ${err.code},message is ${err.message}`); 5899 return; 5900 } 5901 console.info('Cloud sync succeeded'); 5902 }); 5903}; 5904``` 5905 5906### cloudSync<sup>10+</sup> 5907 5908cloudSync(mode: SyncMode, tables: string[], progress: Callback<ProgressDetails>): Promise<void> 5909 5910手动执行对指定表的端云同步,使用Promise异步回调。使用该接口需要实现云服务功能。 5911 5912**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 5913 5914**参数:** 5915 5916| 参数名 | 类型 | 必填 | 说明 | 5917| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 5918| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 5919| tables | string[] | 是 | 指定同步的表名。 | 5920| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 5921 5922**返回值**: 5923 5924| 类型 | 说明 | 5925| ------------------- | --------------------------------------- | 5926| Promise<void> | Promise对象,用于向调用者发送同步结果。 | 5927 5928**错误码:** 5929 5930以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5931 5932| **错误码ID** | **错误信息** | 5933|-----------|---------------| 5934| 401 | Parameter error. Possible causes: 1. Need 2 - 4 parameter(s). 2. The RdbStore must be not nullptr. 3. The mode must be a SyncMode of cloud. 4. The tablesNames must be not empty. 5. The progress must be a callback type | 5935| 801 | Capability not supported. | 5936| 14800014 | Already closed. | 5937 5938**示例:** 5939 5940```ts 5941import { BusinessError } from '@kit.BasicServicesKit'; 5942 5943const tables = ["table1", "table2"]; 5944 5945if(store != undefined) { 5946 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, tables, (progressDetail: relationalStore.ProgressDetails) => { 5947 console.info(`progress: ${progressDetail}`); 5948 }).then(() => { 5949 console.info('Cloud sync succeeded'); 5950 }).catch((err: BusinessError) => { 5951 console.error(`cloudSync failed, code is ${err.code},message is ${err.message}`); 5952 }); 5953}; 5954``` 5955 5956### on('dataChange') 5957 5958on(event: 'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void 5959 5960注册数据库的数据变更的事件监听。当分布式数据库中的数据发生更改时,将调用回调。 5961 5962**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5963 5964**参数:** 5965 5966| 参数名 | 类型 | 必填 | 说明 | 5967| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 5968| event | string | 是 | 取值为'dataChange',表示数据更改。 | 5969| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 5970| observer | Callback<Array<string>> | 是 | 指分布式数据库中数据更改事件的观察者。Array<string>为数据库中的数据发生改变的对端设备ID。 | 5971 5972**错误码:** 5973 5974以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5975 5976| **错误码ID** | **错误信息** | 5977|-----------|-------------| 5978| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5979| 801 | Capability not supported. | 5980| 14800014 | Already closed. | 5981 5982**示例:** 5983 5984```ts 5985import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 5986import { BusinessError } from '@kit.BasicServicesKit'; 5987 5988let storeObserver = (devices: Array<string>) => { 5989 if (devices != undefined) { 5990 for (let i = 0; i < devices.length; i++) { 5991 console.info(`device= ${devices[i]} data changed`); 5992 } 5993 } 5994} 5995 5996try { 5997 if (store != undefined) { 5998 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver) 5999 } 6000} catch (err) { 6001 let code = (err as BusinessError).code; 6002 let message = (err as BusinessError).message 6003 console.error(`Register observer failed, code is ${code},message is ${message}`); 6004} 6005``` 6006 6007### on('dataChange')<sup>10+</sup> 6008 6009on(event: 'dataChange', type: SubscribeType, observer: Callback<Array<string>>\| Callback<Array<ChangeInfo>>): void 6010 6011注册数据库的数据变更的事件监听。当分布式数据库或本地数据库中的数据发生更改时,将调用回调。 6012 6013**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6014 6015**参数:** 6016 6017| 参数名 | 类型 | 必填 | 说明 | 6018| -------- | ----------------------------------- | ---- | ------------------------------------------- | 6019| event | string | 是 | 取值为'dataChange',表示数据更改。 | 6020| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 6021| observer | Callback<Array<string>> \| Callback<Array<[ChangeInfo](#changeinfo10)>> | 是 | 回调函数。<br>当type为SUBSCRIBE_TYPE_REMOTE,observer类型需为Callback<Array<string>>,其中Array<string>为数据库中的数据发生改变的对端设备ID。<br> 当type为SUBSCRIBE_TYPE_CLOUD,observer类型需为Callback<Array<string>>,其中Array<string>为数据库中的数据发生改变的云端账号。 <br> 当type为SUBSCRIBE_TYPE_CLOUD_DETAILS,observer类型需为Callback<Array<ChangeInfo>>,其中Array<ChangeInfo>为数据库端云同步过程的详情。<br>当type为SUBSCRIBE_TYPE_LOCAL_DETAILS,observer类型需为Callback<Array<ChangeInfo>>,其中Array<ChangeInfo>为本地数据库中的数据更改的详情。 | 6022 6023**错误码:** 6024 6025以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6026 6027| **错误码ID** | **错误信息** | 6028|-----------|-------------| 6029| 202 | Permission verification failed, application which is not a system application uses system API. | 6030| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6031| 801 | Capability not supported. | 6032| 14800014 | Already closed. | 6033 6034**示例1:type为SUBSCRIBE_TYPE_REMOTE** 6035 6036```ts 6037import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 6038import { BusinessError } from '@kit.BasicServicesKit'; 6039 6040let storeObserver = (devices: Array<string>) => { 6041 if (devices != undefined) { 6042 for (let i = 0; i < devices.length; i++) { 6043 console.info(`device= ${devices[i]} data changed`); 6044 } 6045 } 6046} 6047 6048try { 6049 if(store != undefined) { 6050 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 6051 } 6052} catch (err) { 6053 let code = (err as BusinessError).code; 6054 let message = (err as BusinessError).message; 6055 console.error(`Register observer failed, code is ${code},message is ${message}`); 6056} 6057``` 6058 6059**示例2:type为SUBSCRIBE_TYPE_LOCAL_DETAILS** 6060 6061```ts 6062import { BusinessError } from '@kit.BasicServicesKit'; 6063 6064let changeInfos = (changeInfos: Array<relationalStore.ChangeInfo>) => { 6065 for (let i = 0; i < changeInfos.length; i++) { 6066 console.info(`changeInfos = ${changeInfos[i]}`); 6067 } 6068} 6069 6070try { 6071 if(store != undefined) { 6072 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_LOCAL_DETAILS, changeInfos); 6073 } 6074} catch (err) { 6075 let code = (err as BusinessError).code; 6076 let message = (err as BusinessError).message; 6077 console.error(`on dataChange fail, code is ${code},message is ${message}`); 6078} 6079 6080let value1 = "Lisa"; 6081let value2 = 18; 6082let value3 = 100.5; 6083let value4 = new Uint8Array([1, 2, 3]); 6084 6085try { 6086 const valueBucket: relationalStore.ValuesBucket = { 6087 'name': value1, 6088 'age': value2, 6089 'salary': value3, 6090 'blobType': value4, 6091 }; 6092 6093 if(store != undefined) { 6094 (store as relationalStore.RdbStore).insert('test', valueBucket); 6095 } 6096} catch (err) { 6097 let code = (err as BusinessError).code; 6098 let message = (err as BusinessError).message; 6099 console.error(`insert fail, code is ${code},message is ${message}`); 6100} 6101``` 6102 6103### on<sup>10+</sup> 6104 6105on(event: string, interProcess: boolean, observer: Callback\<void>): void 6106 6107注册数据库的进程内或者进程间事件监听。当调用[emit](#emit10)接口时,将调用回调。 6108 6109**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6110 6111**参数:** 6112 6113| 参数名 | 类型 | 必填 | 说明 | 6114| ------------ | --------------- | ---- | ------------------------------------------------------------ | 6115| event | string | 是 | 订阅事件名称。 | 6116| interProcess | boolean | 是 | 指定是进程间还是本进程订阅。<br/> true:进程间。<br/> false:本进程。 | 6117| observer | Callback\<void> | 是 | 回调函数。 | 6118 6119**错误码:** 6120 6121以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6122 6123| **错误码ID** | **错误信息** | 6124|-----------|-------------| 6125| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6126| 801 | Capability not supported. | 6127| 14800000 | Inner error. | 6128| 14800014 | Already closed. | 6129| 14800050 | Failed to obtain subscription service. | 6130 6131**示例:** 6132 6133```ts 6134import { BusinessError } from '@kit.BasicServicesKit'; 6135 6136let storeObserver = () => { 6137 console.info(`storeObserver`); 6138} 6139 6140try { 6141 if(store != undefined) { 6142 (store as relationalStore.RdbStore).on('storeObserver', false, storeObserver); 6143 } 6144} catch (err) { 6145 let code = (err as BusinessError).code; 6146 let message = (err as BusinessError).message 6147 console.error(`Register observer failed, code is ${code},message is ${message}`); 6148} 6149``` 6150 6151### on('autoSyncProgress')<sup>11+</sup> 6152 6153on(event: 'autoSyncProgress', progress: Callback<ProgressDetails>): void 6154 6155在已打开端云同步,并且网络状态正常的条件下,注册自动同步进度通知,自动同步进行时调用回调。 6156 6157**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6158 6159**参数:** 6160 6161| 参数名 | 类型 | 必填 | 说明 | 6162| ------------ |---------------------------------| ---- |-----------------------------------| 6163| event | string | 是 | 取值为'autoSyncProgress',表示自动同步进度通知。 | 6164| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 回调函数。 | 6165 6166**错误码:** 6167 6168以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6169 6170| **错误码ID** | **错误信息** | 6171|-----------|--------| 6172| 401 | Parameter error. Possible causes: 1. Need 2 - 3 parameter(s)! 2. The RdbStore must be valid. 3. The event must be a not empty string. 4. The progress must be function. | 6173| 801 | Capability not supported. | 6174| 14800014 | Already closed. | 6175 6176**示例:** 6177 6178```ts 6179import { BusinessError } from '@kit.BasicServicesKit'; 6180 6181let progressDetail = (progressDetail: relationalStore.ProgressDetails) => { 6182 console.info(`progress: ${progressDetail}`); 6183} 6184 6185try { 6186 if(store != undefined) { 6187 (store as relationalStore.RdbStore).on('autoSyncProgress', progressDetail) 6188 } 6189} catch (err) { 6190 let code = (err as BusinessError).code; 6191 let message = (err as BusinessError).message 6192 console.error(`Register observer failed, code is ${code},message is ${message}`); 6193} 6194``` 6195 6196### on('statistics')<sup>12+</sup> 6197 6198on(event: 'statistics', observer: Callback<SqlExecutionInfo>): void 6199 6200订阅SQL统计信息。 6201 6202**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6203 6204**参数:** 6205 6206| 参数名 | 类型 | 必填 | 说明 | 6207| ------------ |---------------------------------| ---- |-----------------------------------| 6208| event | string | 是 | 订阅事件名称,取值为'statistics',表示sql执行时间的统计。 | 6209| observer | Callback<[SqlExecutionInfo](#sqlexecutioninfo12)> | 是 | 回调函数。用于返回数据库中SQL执行时间的统计信息。 | 6210 6211**错误码:** 6212 6213以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6214 6215| **错误码ID** | **错误信息** | 6216|-----------|--------| 6217| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 6218| 801 | Capability not supported. | 6219| 14800000 | Inner error. | 6220| 14800014 | Already closed. | 6221 6222**示例:** 6223 6224```ts 6225import { BusinessError } from '@kit.BasicServicesKit'; 6226 6227let sqlExecutionInfo = (sqlExecutionInfo: relationalStore.SqlExecutionInfo) => { 6228 console.info(`sql: ${sqlExecutionInfo.sql[0]}`); 6229 console.info(`totalTime: ${sqlExecutionInfo.totalTime}`); 6230 console.info(`waitTime: ${sqlExecutionInfo.waitTime}`); 6231 console.info(`prepareTime: ${sqlExecutionInfo.prepareTime}`); 6232 console.info(`executeTime: ${sqlExecutionInfo.executeTime}`); 6233} 6234 6235try { 6236 if(store != undefined) { 6237 (store as relationalStore.RdbStore).on('statistics', sqlExecutionInfo); 6238 } 6239} catch (err) { 6240 let code = (err as BusinessError).code; 6241 let message = (err as BusinessError).message; 6242 console.error(`Register observer failed, code is ${code},message is ${message}`); 6243} 6244 6245try { 6246 let value1 = "Lisa"; 6247 let value2 = 18; 6248 let value3 = 100.5; 6249 let value4 = new Uint8Array([1, 2, 3, 4, 5]); 6250 6251 const valueBucket: relationalStore.ValuesBucket = { 6252 'NAME': value1, 6253 'AGE': value2, 6254 'SALARY': value3, 6255 'CODES': value4, 6256 }; 6257 if(store != undefined) { 6258 (store as relationalStore.RdbStore).insert('test', valueBucket); 6259 } 6260} catch (err) { 6261 console.error(`insert fail, code:${err.code}, message: ${err.message}`); 6262} 6263``` 6264 6265### off('dataChange') 6266 6267off(event:'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void 6268 6269取消数据变更的事件监听。 6270 6271**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6272 6273**参数:** 6274 6275| 参数名 | 类型 | 必填 | 说明 | 6276| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6277| event | string | 是 | 取值为'dataChange',表示数据更改。 | 6278| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 6279| observer | Callback<Array<string>> | 是 | 指已注册的数据更改观察者。Array<string>为数据库中的数据发生改变的对端设备ID。 | 6280 6281**错误码:** 6282 6283以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6284 6285| **错误码ID** | **错误信息** | 6286|-----------|-------------| 6287| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6288| 801 | Capability not supported. | 6289| 14800014 | Already closed. | 6290 6291**示例:** 6292 6293```ts 6294import { BusinessError } from '@kit.BasicServicesKit'; 6295 6296let storeObserver = (devices: Array<string>) => { 6297 if (devices != undefined) { 6298 for (let i = 0; i < devices.length; i++) { 6299 console.info(`device= ${devices[i]} data changed`); 6300 } 6301 } 6302} 6303 6304try { 6305 if (store != undefined) { 6306 // 此处不能使用Lambda表达式 6307 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver) 6308 } 6309} catch (err) { 6310 let code = (err as BusinessError).code; 6311 let message = (err as BusinessError).message 6312 console.error(`Register observer failed, code is ${code},message is ${message}`); 6313} 6314 6315try { 6316 if(store != undefined) { 6317 (store as relationalStore.RdbStore).off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 6318 } 6319} catch (err) { 6320 let code = (err as BusinessError).code; 6321 let message = (err as BusinessError).message 6322 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 6323} 6324``` 6325 6326### off('dataChange')<sup>10+</sup> 6327 6328off(event:'dataChange', type: SubscribeType, observer?: Callback<Array<string>>\| Callback<Array<ChangeInfo>>): void 6329 6330取消数据变更的事件监听。 6331 6332**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6333 6334**参数:** 6335 6336| 参数名 | 类型 | 必填 | 说明 | 6337| -------- | ---------------------------------- | ---- | ------------------------------------------ | 6338| event | string | 是 | 取值为'dataChange',表示数据更改。 | 6339| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 6340| observer | Callback<Array<string>>\| Callback<Array<[ChangeInfo](#changeinfo10)>> | 否 | 回调函数。<br/>当type为SUBSCRIBE_TYPE_REMOTE,observer类型需为Callback<Array<string>>,其中Array<string>为数据库中的数据发生改变的对端设备ID。<br/> 当type为SUBSCRIBE_TYPE_CLOUD,observer类型需为Callback<Array<string>>,其中Array<string>为数据库中的数据发生改变的云端账号。 <br/> 当type为SUBSCRIBE_TYPE_CLOUD_DETAILS,observer类型需为Callback<Array<ChangeInfo>>,其中Array<ChangeInfo>为数据库端云同步过程的详情。<br>当type为SUBSCRIBE_TYPE_LOCAL_DETAILS,observer类型需为Callback<Array<ChangeInfo>>,其中Array<ChangeInfo>为本地数据库中的数据更改的详情。<br> 当observer没有传入时,表示取消当前type类型下所有数据变更的事件监听。 | 6341 6342**错误码:** 6343 6344以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6345 6346| **错误码ID** | **错误信息** | 6347|-----------|-------------| 6348| 202 | Permission verification failed, application which is not a system application uses system API. | 6349| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6350| 801 | Capability not supported. | 6351| 14800014 | Already closed. | 6352 6353**示例:** 6354 6355```ts 6356import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 6357import { BusinessError } from '@kit.BasicServicesKit'; 6358 6359let storeObserver = (devices: Array<string>) => { 6360 if (devices != undefined) { 6361 for (let i = 0; i < devices.length; i++) { 6362 console.info(`device= ${devices[i]} data changed`); 6363 } 6364 } 6365} 6366 6367try { 6368 if(store != undefined) { 6369 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 6370 } 6371} catch (err) { 6372 let code = (err as BusinessError).code; 6373 let message = (err as BusinessError).message; 6374 console.error(`Register observer failed, code is ${code},message is ${message}`); 6375} 6376 6377try { 6378 if(store != undefined) { 6379 (store as relationalStore.RdbStore).off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 6380 } 6381} catch (err) { 6382 let code = (err as BusinessError).code; 6383 let message = (err as BusinessError).message 6384 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 6385} 6386``` 6387 6388### off<sup>10+</sup> 6389 6390off(event: string, interProcess: boolean, observer?: Callback\<void>): void 6391 6392取消数据变更的事件监听。 6393 6394**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6395 6396**参数:** 6397 6398| 参数名 | 类型 | 必填 | 说明 | 6399| ------------ | --------------- | ---- | ------------------------------------------------------------ | 6400| event | string | 是 | 取消订阅事件名称。 | 6401| interProcess | boolean | 是 | 指定是进程间还是本进程取消订阅。<br/> true:进程间。<br/> false:本进程。 | 6402| observer | Callback\<void> | 否 | 该参数存在,则取消指定Callback监听回调,否则取消该event事件的所有监听回调。 | 6403 6404**错误码:** 6405 6406以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6407 6408| **错误码ID** | **错误信息** | 6409| ------------ | -------------------------------------- | 6410| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6411| 801 | Capability not supported. | 6412| 14800000 | Inner error. | 6413| 14800014 | Already closed. | 6414| 14800050 | Failed to obtain subscription service. | 6415 6416**示例:** 6417 6418```ts 6419import { BusinessError } from '@kit.BasicServicesKit'; 6420 6421let storeObserver = () => { 6422 console.info(`storeObserver`); 6423} 6424 6425try { 6426 if(store != undefined) { 6427 (store as relationalStore.RdbStore).on('storeObserver', false, storeObserver); 6428 } 6429} catch (err) { 6430 let code = (err as BusinessError).code; 6431 let message = (err as BusinessError).message 6432 console.error(`Register observer failed, code is ${code},message is ${message}`); 6433} 6434 6435try { 6436 if(store != undefined) { 6437 (store as relationalStore.RdbStore).off('storeObserver', false, storeObserver); 6438 } 6439} catch (err) { 6440 let code = (err as BusinessError).code; 6441 let message = (err as BusinessError).message 6442 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 6443} 6444``` 6445 6446### off('autoSyncProgress')<sup>11+</sup> 6447 6448off(event: 'autoSyncProgress', progress?: Callback<ProgressDetails>): void 6449 6450取消订阅自动同步进度的通知。 6451 6452**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6453 6454**参数:** 6455 6456| 参数名 | 类型 | 必填 | 说明 | 6457| ------------ |---------------------------------| ---- |------------------------------------------------------------------| 6458| event | string | 是 | 取值为'autoSyncProgress',表示自动同步进度通知。 | 6459| progress | Callback<[ProgressDetails](#progressdetails10)> | 否 | 指已注册的自动同步进度观察者。该参数存在,则取消订阅指定回调,该参数为null或undefined或不存在,则取消订阅所有回调。 | 6460 6461**错误码:** 6462 6463以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6464 6465| **错误码ID** | **错误信息** | 6466| ------------ |--------------------| 6467| 401 | Parameter error. Possible causes: 1. Need 1 - 3 parameter(s)! 2. The RdbStore must be valid. 3. The event must be a not empty string. 4. The progress must be function. | 6468| 801 | Capability not supported. | 6469| 14800014 | Already closed. | 6470 6471**示例:** 6472 6473```ts 6474import { BusinessError } from '@kit.BasicServicesKit'; 6475 6476let progressDetail = (progressDetail: relationalStore.ProgressDetails) => { 6477 console.info(`progress: ${progressDetail}`); 6478} 6479 6480try { 6481 if(store != undefined) { 6482 (store as relationalStore.RdbStore).on('autoSyncProgress', progressDetail) 6483 } 6484} catch (err) { 6485 let code = (err as BusinessError).code; 6486 let message = (err as BusinessError).message 6487 console.error(`Register observer failed, code is ${code},message is ${message}`); 6488} 6489 6490try { 6491 if(store != undefined) { 6492 (store as relationalStore.RdbStore).off('autoSyncProgress', progressDetail); 6493 } 6494} catch (err) { 6495 let code = (err as BusinessError).code; 6496 let message = (err as BusinessError).message; 6497 console.error(`Unregister failed, code is ${code},message is ${message}`); 6498} 6499``` 6500 6501### off('statistics')<sup>12+</sup> 6502 6503off(event: 'statistics', observer?: Callback<SqlExecutionInfo>): void 6504 6505取消订阅SQL统计信息。 6506 6507**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6508 6509**参数:** 6510 6511| 参数名 | 类型 | 必填 | 说明 | 6512| ------------ |---------------------------------| ---- |-----------------------------------| 6513| event | string | 是 | 取消订阅事件名称。取值为'statistics',表示sql执行时间的统计。 | 6514| observer | Callback<[SqlExecutionInfo](#sqlexecutioninfo12)> | 否 | 回调函数。该参数存在,则取消指定Callback监听回调,否则取消该event事件的所有监听回调。 | 6515 6516 6517**错误码:** 6518 6519以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6520 6521| **错误码ID** | **错误信息** | 6522|-----------|--------| 6523| 401 | Parameter error. | 6524| 801 | Capability not supported. | 6525| 14800000 | Inner error. | 6526| 14800014 | Already closed. | 6527 6528```ts 6529import { BusinessError } from '@kit.BasicServicesKit'; 6530 6531try { 6532 if(store != undefined) { 6533 (store as relationalStore.RdbStore).off('statistics'); 6534 } 6535} catch (err) { 6536 let code = (err as BusinessError).code; 6537 let message = (err as BusinessError).message; 6538 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 6539} 6540``` 6541 6542### emit<sup>10+</sup> 6543 6544emit(event: string): void 6545 6546通知通过[on](#on10)注册的进程间或者进程内监听事件。 6547 6548**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6549 6550**参数:** 6551 6552| 参数名 | 类型 | 必填 | 说明 | 6553| ------ | ------ | ---- | -------------------- | 6554| event | string | 是 | 通知订阅事件的名称。 | 6555 6556**错误码:** 6557 6558以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6559 6560| **错误码ID** | **错误信息** | 6561| --------- |---------------------------------------------------------------------------------------------------------------| 6562| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6563| 801 | Capability not supported. | 6564| 14800000 | Inner error. | 6565| 14800014 | Already closed. | 6566| 14800050 | Failed to obtain subscription service. | 6567 6568 6569**示例:** 6570 6571```ts 6572if(store != undefined) { 6573 (store as relationalStore.RdbStore).emit('storeObserver'); 6574} 6575``` 6576 6577### cleanDirtyData<sup>11+</sup> 6578 6579cleanDirtyData(table: string, cursor: number, callback: AsyncCallback<void>): void 6580 6581清理云端删除的数据同步到本地后,未自动清理的,且数据的游标(cursor)小于指定游标的数据。 6582 6583**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 6584 6585**参数:** 6586 6587| 参数名 | 类型 | 必填 | 说明 | 6588| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 6589| table | string | 是 | 表示当前数据库的表的名称。 | 6590| cursor | number | 是 | 整数类型,表示数据游标,小于此游标的脏数据将被清理。 | 6591| callback | AsyncCallback<void> | 是 | 指定的callback回调函数。 | 6592 6593**错误码:** 6594 6595以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 6596 6597| **错误码ID** | **错误信息** | 6598|-----------|---------------| 6599| 401 | Parameter error. Possible causes: 1. Need 1 - 3 parameter(s)! 2. The RdbStore must be not nullptr. 3. The tablesNames must be not empty string. 4. The cursor must be valid cursor. | 6600| 801 | Capability not supported. | 6601| 14800000 | Inner error. | 6602| 14800011 | Database corrupted. | 6603| 14800014 | Already closed. | 6604| 14800015 | The database does not respond. | 6605| 14800021 | SQLite: Generic error. | 6606| 14800022 | SQLite: Callback routine requested an abort. | 6607| 14800023 | SQLite: Access permission denied. | 6608| 14800024 | SQLite: The database file is locked. | 6609| 14800025 | SQLite: A table in the database is locked. | 6610| 14800026 | SQLite: The database is out of memory. | 6611| 14800027 | SQLite: Attempt to write a readonly database. | 6612| 14800028 | SQLite: Some kind of disk I/O error occurred. | 6613| 14800029 | SQLite: The database is full. | 6614| 14800030 | SQLite: Unable to open the database file. | 6615| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 6616| 14800032 | SQLite: Abort due to constraint violation. | 6617| 14800033 | SQLite: Data type mismatch. | 6618| 14800034 | SQLite: Library used incorrectly. | 6619 6620**示例:** 6621 6622```ts 6623if(store != undefined) { 6624 (store as relationalStore.RdbStore).cleanDirtyData('test_table', 100, (err) => { 6625 if (err) { 6626 console.error(`clean dirty data failed, code is ${err.code},message is ${err.message}`); 6627 return; 6628 } 6629 console.info('clean dirty data succeeded'); 6630 }) 6631} 6632``` 6633 6634### cleanDirtyData<sup>11+</sup> 6635 6636cleanDirtyData(table: string, callback: AsyncCallback<void>): void 6637 6638清理云端删除的数据同步到本地后,未自动清理的所有数据。 6639 6640**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 6641 6642**参数:** 6643 6644| 参数名 | 类型 | 必填 | 说明 | 6645| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 6646| table | string | 是 | 表示当前数据库的表的名称。 | 6647| callback | AsyncCallback<void> | 是 | 指定的callback回调函数。 | 6648 6649**错误码:** 6650 6651以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 6652 6653| **错误码ID** | **错误信息** | 6654|-----------|---------| 6655| 401 | Parameter error. Possible causes: 1. Need 1 - 3 parameter(s). 2. The RdbStore must be not nullptr. 3. The tablesNames must be not empty string. | 6656| 801 | Capability not supported. | 6657| 14800000 | Inner error. | 6658| 14800011 | Database corrupted. | 6659| 14800014 | Already closed. | 6660| 14800015 | The database does not respond. | 6661| 14800021 | SQLite: Generic error. | 6662| 14800022 | SQLite: Callback routine requested an abort. | 6663| 14800023 | SQLite: Access permission denied. | 6664| 14800024 | SQLite: The database file is locked. | 6665| 14800025 | SQLite: A table in the database is locked. | 6666| 14800026 | SQLite: The database is out of memory. | 6667| 14800027 | SQLite: Attempt to write a readonly database. | 6668| 14800028 | SQLite: Some kind of disk I/O error occurred. | 6669| 14800029 | SQLite: The database is full. | 6670| 14800030 | SQLite: Unable to open the database file. | 6671| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 6672| 14800032 | SQLite: Abort due to constraint violation. | 6673| 14800033 | SQLite: Data type mismatch. | 6674| 14800034 | SQLite: Library used incorrectly. | 6675 6676**示例:** 6677 6678```ts 6679if(store != undefined) { 6680 (store as relationalStore.RdbStore).cleanDirtyData('test_table', (err) => { 6681 if (err) { 6682 console.error(`clean dirty data failed, code is ${err.code},message is ${err.message}`); 6683 return; 6684 } 6685 console.info('clean dirty data succeeded'); 6686 }) 6687} 6688``` 6689 6690### cleanDirtyData<sup>11+</sup> 6691 6692cleanDirtyData(table: string, cursor?: number): Promise<void> 6693 6694清理云端删除的数据同步到本地后,未自动清理的,且数据的游标(cursor)小于指定游标的数据。若无cursor参数,将全部清理。 6695 6696**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 6697 6698**参数:** 6699 6700| 参数名 | 类型 | 必填 | 说明 | 6701| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 6702| table | string | 是 | 表示当前数据库的表的名称。 | 6703| cursor | number | 否 | 整数类型,表示数据游标,小于此游标的脏数据将被清理。当此参数不填时,清理当前表的所有脏数据。 | 6704 6705**返回值:** 6706| 参数名 | 说明 | 6707| -------- | ------------------------------------------------- | 6708| Promise\<void> | 无返回结果的Promise对象。 | 6709 6710**错误码:** 6711 6712以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 6713 6714| **错误码ID** | **错误信息** | 6715|-----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 6716| 401 | Parameter error. Possible causes: 1. Need 1 - 3 parameter(s)! 2. The RdbStore must be not nullptr. 3. The tablesNames must be not empty string. 4. The cursor must be valid cursor. | 6717| 801 | Capability not supported. | 6718| 14800000 | Inner error. | 6719| 14800011 | Database corrupted. | 6720| 14800014 | Already closed. | 6721| 14800015 | The database does not respond. | 6722| 14800021 | SQLite: Generic error. | 6723| 14800022 | SQLite: Callback routine requested an abort. | 6724| 14800023 | SQLite: Access permission denied. | 6725| 14800024 | SQLite: The database file is locked. | 6726| 14800025 | SQLite: A table in the database is locked. | 6727| 14800026 | SQLite: The database is out of memory. | 6728| 14800027 | SQLite: Attempt to write a readonly database. | 6729| 14800028 | SQLite: Some kind of disk I/O error occurred. | 6730| 14800029 | SQLite: The database is full. | 6731| 14800030 | SQLite: Unable to open the database file. | 6732| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 6733| 14800032 | SQLite: Abort due to constraint violation. | 6734| 14800033 | SQLite: Data type mismatch. | 6735| 14800034 | SQLite: Library used incorrectly. | 6736 6737**示例:** 6738 6739```ts 6740import { BusinessError } from '@kit.BasicServicesKit'; 6741 6742if(store != undefined) { 6743 (store as relationalStore.RdbStore).cleanDirtyData('test_table', 100).then(() => { 6744 console.info('clean dirty data succeeded'); 6745 }).catch ((err: BusinessError) => { 6746 console.error(`clean dirty data failed, code is ${err.code},message is ${err.message}`); 6747 }) 6748} 6749``` 6750 6751### attach<sup>12+</sup> 6752 6753attach(fullPath: string, attachName: string, waitTime?: number) : Promise<number> 6754 6755将一个数据库文件附加到当前数据库中,以便在SQL语句中可以直接访问附加数据库中的数据。 6756 6757数据库文件来自文件,且此API不支持附加加密数据库。调用attach接口后,数据库切换为非WAL模式,性能会存在一定的劣化。 6758 6759attach的时候,数据库会切换为非WAL模式,切换模式需要确保所有的ResultSet都已经Close,所有的写操作已经结束,否则会报错14800015。 6760 6761attach不能并发调用,可能出现未响应情况,报错14800015,需要重试。 6762 6763**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6764 6765**参数:** 6766 6767| 参数名 | 类型 | 必填 | 说明 | 6768| ----------- | ------ | --- | ------------ | 6769| fullPath | string | 是 | 表示要附加的数据库的路径。 | 6770| attachName | string | 是 | 表示附加后的数据库的别名。 | 6771| waitTime | number | 否 | 表示附加数据库文件的等待时长。默认值2s,最小值1s,最大值300s。 | 6772 6773**返回值:** 6774 6775| 类型 | 说明 | 6776| ---------------- | ---------------------------- | 6777| Promise<number> | Promise对象。返回附加数据库的数量。 | 6778 6779**错误码:** 6780 6781以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 6782 6783| **错误码ID** | **错误信息** | 6784|-----------| ------------------------------------------------------------ | 6785| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6786| 801 | Capability not supported. | 6787| 14800000 | Inner error. | 6788| 14800010 | Invalid database path. | 6789| 14800011 | Database corrupted. | 6790| 14800014 | Already closed. | 6791| 14800015 | The database does not respond. | 6792| 14800016 | The database is already attached. | 6793| 14800021 | SQLite: Generic error. | 6794| 14800022 | SQLite: Callback routine requested an abort. | 6795| 14800023 | SQLite: Access permission denied. | 6796| 14800024 | SQLite: The database file is locked. | 6797| 14800025 | SQLite: A table in the database is locked. | 6798| 14800026 | SQLite: The database is out of memory. | 6799| 14800027 | SQLite: Attempt to write a readonly database. | 6800| 14800028 | SQLite: Some kind of disk I/O error occurred. | 6801| 14800029 | SQLite: The database is full. | 6802| 14800030 | SQLite: Unable to open the database file. | 6803| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 6804| 14800032 | SQLite: Abort due to constraint violation. | 6805| 14800033 | SQLite: Data type mismatch. | 6806| 14800034 | SQLite: Library used incorrectly. | 6807 6808**示例:** 6809 6810```ts 6811// 非加密数据库附加非加密数据库。 6812import { BusinessError } from '@kit.BasicServicesKit'; 6813 6814if(store != undefined) { 6815 (store as relationalStore.RdbStore).attach("/path/rdbstore1.db", "attachDB").then((number: number) => { 6816 console.info('attach succeeded'); 6817 }).catch ((err: BusinessError) => { 6818 console.error(`attach failed, code is ${err.code},message is ${err.message}`); 6819 }) 6820} 6821``` 6822 6823### attach<sup>12+</sup> 6824 6825attach(context: Context, config: StoreConfig, attachName: string, waitTime?: number) : Promise<number> 6826 6827将一个当前应用的数据库附加到当前数据库中,以便在SQL语句中可以直接访问附加数据库中的数据。 6828 6829此API不支持加密数据库附加非加密数据库的场景。调用attach接口后,数据库切换为非WAL模式,性能会存在一定的劣化。 6830 6831attach的时候,数据库会切换为非WAL模式,切换模式需要确保所有的ResultSet都已经Close,所有的写操作已经结束,否则会报错14800015。 6832 6833attach不能并发调用,可能出现未响应情况,报错14800015,需要重试。 6834 6835**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6836 6837**参数:** 6838 6839| 参数名 | 类型 | 必填 | 说明 | 6840| ----------- | ------ | --- | ------------ | 6841| 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)。 | 6842| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 6843| attachName | string | 是 | 表示附加后的数据库的别名。 | 6844| waitTime | number | 否 | 表示附加数据库文件的等待时长。默认值2s,最小值1s,最大值300s。 | 6845 6846**返回值:** 6847 6848| 类型 | 说明 | 6849| ---------------- | ---------------------------- | 6850| Promise<number> | Promise对象。返回附加数据库的数量。 | 6851 6852**错误码:** 6853 6854以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 6855 6856| **错误码ID** | **错误信息** | 6857|-----------| ------------------------------------------------------------ | 6858| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6859| 801 | Capability not supported. | 6860| 14800000 | Inner error. | 6861| 14800010 | Invalid database path. | 6862| 14800011 | Database corrupted. | 6863| 14800014 | Already closed. | 6864| 14800015 | The database does not respond. | 6865| 14800016 | The database is already attached. | 6866| 14801001 | Only supported in stage mode. | 6867| 14801002 | The data group id is not valid. | 6868| 14800021 | SQLite: Generic error. | 6869| 14800022 | SQLite: Callback routine requested an abort. | 6870| 14800023 | SQLite: Access permission denied. | 6871| 14800024 | SQLite: The database file is locked. | 6872| 14800025 | SQLite: A table in the database is locked. | 6873| 14800026 | SQLite: The database is out of memory. | 6874| 14800027 | SQLite: Attempt to write a readonly database. | 6875| 14800028 | SQLite: Some kind of disk I/O error occurred. | 6876| 14800029 | SQLite: The database is full. | 6877| 14800030 | SQLite: Unable to open the database file. | 6878| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 6879| 14800032 | SQLite: Abort due to constraint violation. | 6880| 14800033 | SQLite: Data type mismatch. | 6881| 14800034 | SQLite: Library used incorrectly. | 6882 6883**示例1:非加密数据库附加非加密数据库** 6884 6885```ts 6886import { BusinessError } from '@kit.BasicServicesKit'; 6887 6888let attachStore: relationalStore.RdbStore | undefined = undefined; 6889 6890const STORE_CONFIG1: relationalStore.StoreConfig = { 6891 name: "rdbstore1.db", 6892 securityLevel: relationalStore.SecurityLevel.S3, 6893} 6894 6895relationalStore.getRdbStore(this.context, STORE_CONFIG1).then(async (rdbStore: relationalStore.RdbStore) => { 6896 attachStore = rdbStore; 6897 console.info('Get RdbStore successfully.') 6898}).catch((err: BusinessError) => { 6899 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 6900}) 6901 6902if(store != undefined) { 6903 (store as relationalStore.RdbStore).attach(this.context, STORE_CONFIG1, "attachDB").then((number: number) => { 6904 console.info(`attach succeeded, number is ${number}`); 6905 }).catch ((err: BusinessError) => { 6906 console.error(`attach failed, code is ${err.code},message is ${err.message}`); 6907 }) 6908} 6909``` 6910 6911**示例2:非加密数据库附加加密数据库** 6912 6913```ts 6914import { BusinessError } from '@kit.BasicServicesKit'; 6915 6916let attachStore: relationalStore.RdbStore | undefined = undefined; 6917 6918 6919const STORE_CONFIG2: relationalStore.StoreConfig = { 6920 name: "rdbstore2.db", 6921 encrypt: true, 6922 securityLevel: relationalStore.SecurityLevel.S3, 6923} 6924 6925relationalStore.getRdbStore(this.context, STORE_CONFIG2).then(async (rdbStore: relationalStore.RdbStore) => { 6926 attachStore = rdbStore; 6927 console.info('Get RdbStore successfully.') 6928}).catch((err: BusinessError) => { 6929 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 6930}) 6931 6932if(store != undefined) { 6933 (store as relationalStore.RdbStore).attach(this.context, STORE_CONFIG2, "attachDB2", 10).then((number: number) => { 6934 console.info(`attach succeeded, number is ${number}`); 6935 }).catch ((err: BusinessError) => { 6936 console.error(`attach failed, code is ${err.code},message is ${err.message}`); 6937 }) 6938} 6939``` 6940 6941### detach<sup>12+</sup> 6942 6943detach(attachName: string, waitTime?: number) : Promise<number> 6944 6945将附加的数据库从当前数据库中分离。 6946 6947当所有的附加的数据库被分离后,数据库会重新切换为WAL模式。 6948 6949在detach之前,所有的数据库操作要确保已经结束,所有的ResultSet已经Close。并且不能并发调用,可能出现未响应情况,需要重试。 6950 6951**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6952 6953**参数:** 6954 6955| 参数名 | 类型 | 必填 | 说明 | 6956| ----------- | ------ | --- | ------------ | 6957| attachName | string | 是 | 表示附加后的数据库的别名。 | 6958| waitTime | number | 否 | 表示分离数据库的等待时长。默认值2s,最小值1s,最大值300s。 | 6959 6960**返回值:** 6961 6962| 类型 | 说明 | 6963| ---------------- | ---------------------------- | 6964| Promise<number> | Promise对象。返回分离后剩余附加的数据库的数量。 | 6965 6966**错误码:** 6967 6968以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 6969 6970| **错误码ID** | **错误信息** | 6971|-----------|------------------------| 6972| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6973| 14800000 | Inner error. | 6974| 14800011 | Database corrupted. | 6975| 14800014 | Already closed. | 6976| 14800015 | The database does not respond. | 6977| 14800021 | SQLite: Generic error. | 6978| 14800022 | SQLite: Callback routine requested an abort. | 6979| 14800023 | SQLite: Access permission denied. | 6980| 14800024 | SQLite: The database file is locked. | 6981| 14800025 | SQLite: A table in the database is locked. | 6982| 14800026 | SQLite: The database is out of memory. | 6983| 14800027 | SQLite: Attempt to write a readonly database. | 6984| 14800028 | SQLite: Some kind of disk I/O error occurred. | 6985| 14800029 | SQLite: The database is full. | 6986| 14800030 | SQLite: Unable to open the database file. | 6987| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 6988| 14800032 | SQLite: Abort due to constraint violation. | 6989| 14800033 | SQLite: Data type mismatch. | 6990| 14800034 | SQLite: Library used incorrectly. | 6991 6992**示例:** 6993 6994```ts 6995import { BusinessError } from '@kit.BasicServicesKit'; 6996 6997if(store != undefined) { 6998 (store as relationalStore.RdbStore).detach("attachDB").then((number: number) => { 6999 console.info(`detach succeeded, number is ${number}`); 7000 }).catch ((err: BusinessError) => { 7001 console.error(`detach failed, code is ${err.code},message is ${err.message}`); 7002 }) 7003} 7004``` 7005 7006### lockRow<sup>12+</sup> 7007 7008lockRow(predicates: RdbPredicates):Promise<void> 7009 7010根据RdbPredicates的指定实例对象从数据库中锁定数据,锁定数据不执行端云同步,使用Promise异步回调。 7011 7012该接口只支持主键为基本类型的表、不支持共享表、无主键表和复合类型主键表。 7013该接口不支持依赖关系表之间的锁传递,如果表存在依赖关系,需要根据依赖关系手动调用该接口。 7014该接口不支持对已删除数据的操作。 7015 7016**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7017 7018**参数:** 7019 7020| 参数名 | 类型 | 必填 | 说明 | 7021| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 7022| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的锁定条件。 | 7023 7024**返回值**: 7025 7026| 类型 | 说明 | 7027| --------------------- | ------------------------------- | 7028| Promise<void> | 无返回结果的Promise对象。 | 7029 7030**错误码:** 7031 7032以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7033 7034| **错误码ID** | **错误信息** | 7035|-----------|----------------------------------------------------------------------------------------------| 7036| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7037| 14800000 | Inner error. | 7038| 14800011 | Database corrupted. | 7039| 14800014 | Already closed. | 7040| 14800015 | The database does not respond. | 7041| 14800018 | No data meets the condition. | 7042| 14800021 | SQLite: Generic error. | 7043| 14800022 | SQLite: Callback routine requested an abort. | 7044| 14800023 | SQLite: Access permission denied. | 7045| 14800024 | SQLite: The database file is locked. | 7046| 14800025 | SQLite: A table in the database is locked. | 7047| 14800026 | SQLite: The database is out of memory. | 7048| 14800027 | SQLite: Attempt to write a readonly database. | 7049| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7050| 14800029 | SQLite: The database is full. | 7051| 14800030 | SQLite: Unable to open the database file. | 7052| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7053| 14800032 | SQLite: Abort due to constraint violation. | 7054| 14800033 | SQLite: Data type mismatch. | 7055| 14800034 | SQLite: Library used incorrectly. | 7056 7057**示例:** 7058 7059```ts 7060import { BusinessError } from '@kit.BasicServicesKit'; 7061 7062let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 7063predicates.equalTo("NAME", "Lisa"); 7064if(store != undefined) { 7065 (store as relationalStore.RdbStore).lockRow(predicates).then(() => { 7066 console.info(`Lock success`); 7067 }).catch((err: BusinessError) => { 7068 console.error(`Lock failed, code is ${err.code},message is ${err.message}`); 7069 }) 7070} 7071``` 7072 7073### unlockRow<sup>12+</sup> 7074 7075unlockRow(predicates: RdbPredicates):Promise<void> 7076 7077根据RdbPredicates的指定实例对象从数据库中解锁数据,使用Promise异步回调。 7078 7079该接口只支持主键为基本类型的表、不支持共享表、无主键表和复合类型主键表。 7080该接口不支持依赖关系表之间的锁传递,如果表存在依赖关系,需要根据依赖关系手动调用该接口。 7081该接口不支持对已删除数据的操作。 7082 7083**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7084 7085**参数:** 7086 7087| 参数名 | 类型 | 必填 | 说明 | 7088| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 7089| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的锁定条件。 | 7090 7091**返回值**: 7092 7093| 类型 | 说明 | 7094| --------------------- | ------------------------------- | 7095| Promise<void> | 无返回结果的Promise对象。 | 7096 7097**错误码:** 7098 7099以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7100 7101| **错误码ID** | **错误信息** | 7102|-----------| ------------------------------------------------------------ | 7103| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7104| 14800000 | Inner error. | 7105| 14800011 | Database corrupted. | 7106| 14800014 | Already closed. | 7107| 14800015 | The database does not respond. | 7108| 14800018 | No data meets the condition. | 7109| 14800021 | SQLite: Generic error. | 7110| 14800022 | SQLite: Callback routine requested an abort. | 7111| 14800023 | SQLite: Access permission denied. | 7112| 14800024 | SQLite: The database file is locked. | 7113| 14800025 | SQLite: A table in the database is locked. | 7114| 14800026 | SQLite: The database is out of memory. | 7115| 14800027 | SQLite: Attempt to write a readonly database. | 7116| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7117| 14800029 | SQLite: The database is full. | 7118| 14800030 | SQLite: Unable to open the database file. | 7119| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7120| 14800032 | SQLite: Abort due to constraint violation. | 7121| 14800033 | SQLite: Data type mismatch. | 7122| 14800034 | SQLite: Library used incorrectly. | 7123 7124**示例:** 7125 7126```ts 7127import { BusinessError } from '@kit.BasicServicesKit'; 7128 7129let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 7130predicates.equalTo("NAME", "Lisa"); 7131if(store != undefined) { 7132 (store as relationalStore.RdbStore).unlockRow(predicates).then(() => { 7133 console.info(`Unlock success`); 7134 }).catch((err: BusinessError) => { 7135 console.error(`Unlock failed, code is ${err.code},message is ${err.message}`); 7136 }) 7137} 7138``` 7139 7140### queryLockedRow<sup>12+</sup> 7141 7142queryLockedRow(predicates: RdbPredicates, columns?: Array<string>):Promise<ResultSet> 7143 7144根据指定条件查询数据库中锁定的数据,使用Promise异步回调。 7145由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 7146 7147**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7148 7149**参数:** 7150 7151| 参数名 | 类型 | 必填 | 说明 | 7152| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 7153| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 7154| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 7155 7156**错误码:** 7157 7158以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7159 7160| **错误码ID** | **错误信息** | 7161|-----------| ------------------------------------------------------------ | 7162| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7163| 14800000 | Inner error. | 7164| 14800011 | Database corrupted. | 7165| 14800014 | Already closed. | 7166| 14800015 | The database does not respond. | 7167| 14800021 | SQLite: Generic error. | 7168| 14800022 | SQLite: Callback routine requested an abort. | 7169| 14800023 | SQLite: Access permission denied. | 7170| 14800024 | SQLite: The database file is locked. | 7171| 14800025 | SQLite: A table in the database is locked. | 7172| 14800026 | SQLite: The database is out of memory. | 7173| 14800027 | SQLite: Attempt to write a readonly database. | 7174| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7175| 14800029 | SQLite: The database is full. | 7176| 14800030 | SQLite: Unable to open the database file. | 7177| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7178| 14800032 | SQLite: Abort due to constraint violation. | 7179| 14800033 | SQLite: Data type mismatch. | 7180| 14800034 | SQLite: Library used incorrectly. | 7181 7182**返回值**: 7183 7184| 类型 | 说明 | 7185| ------------------------------------------------------- | -------------------------------------------------- | 7186| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 7187 7188**示例:** 7189 7190```ts 7191import { BusinessError } from '@kit.BasicServicesKit'; 7192 7193let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 7194predicates.equalTo("NAME", "Rose"); 7195if(store != undefined) { 7196 (store as relationalStore.RdbStore).queryLockedRow(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((resultSet: relationalStore.ResultSet) => { 7197 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 7198 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 7199 while (resultSet.goToNextRow()) { 7200 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 7201 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 7202 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 7203 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 7204 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 7205 } 7206 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 7207 resultSet.close(); 7208 }).catch((err: BusinessError) => { 7209 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 7210 }) 7211} 7212``` 7213### close<sup>12+</sup> 7214 7215close(): Promise<void> 7216 7217关闭数据库,使用Promise异步回调。 7218 7219**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7220 7221**返回值:** 7222 7223| 类型 | 说明 | 7224| ------------------- | ------------- | 7225| Promise<void> | Promise对象。 | 7226 7227**错误码:** 7228 7229以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 7230 7231| **错误码ID** | **错误信息** | 7232| ------------ | ----------------------------------------------- | 7233| 401 | Parameter error. The store must not be nullptr. | 7234| 14800000 | Inner error. | 7235 7236**示例:** 7237 7238```ts 7239import { BusinessError } from '@kit.BasicServicesKit'; 7240 7241if(store != undefined) { 7242 (store as relationalStore.RdbStore).close().then(() => { 7243 console.info(`close succeeded`); 7244 }).catch ((err: BusinessError) => { 7245 console.error(`close failed, code is ${err.code},message is ${err.message}`); 7246 }) 7247} 7248``` 7249 7250## ResultSet 7251 7252提供通过查询数据库生成的数据库结果集的访问方法。结果集是指用户调用关系型数据库查询接口之后返回的结果集合,提供了多种灵活的数据访问方式,以便用户获取各项数据。 7253 7254### 使用说明 7255 7256首先需要获取resultSet对象。 7257 7258**示例:** 7259 7260<!--code_no_check--> 7261```ts 7262let resultSet: relationalStore.ResultSet | undefined = undefined; 7263let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 7264predicates.equalTo("AGE", 18); 7265if(store != undefined) { 7266 (store as relationalStore.RdbStore).query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((result: relationalStore.ResultSet) => { 7267 resultSet = result; 7268 console.info(`resultSet columnNames: ${resultSet.columnNames}`); 7269 console.info(`resultSet columnCount: ${resultSet.columnCount}`); 7270 }); 7271} 7272``` 7273 7274### 属性 7275 7276**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7277 7278| 名称 | 类型 | 必填 | 说明 | 7279| ------------ | ------------------- | ---- | -------------------------------- | 7280| columnNames | Array<string> | 是 | 获取结果集中所有列的名称。 | 7281| columnCount | number | 是 | 获取结果集中的列数。 | 7282| rowCount | number | 是 | 获取结果集中的行数。 | 7283| rowIndex | number | 是 | 获取结果集当前行的索引。 | 7284| isAtFirstRow | boolean | 是 | 检查结果集是否位于第一行。 | 7285| isAtLastRow | boolean | 是 | 检查结果集是否位于最后一行。 | 7286| isEnded | boolean | 是 | 检查结果集是否位于最后一行之后。 | 7287| isStarted | boolean | 是 | 检查指针是否移动过。 | 7288| isClosed | boolean | 是 | 检查当前结果集是否关闭。 | 7289 7290### getColumnIndex 7291 7292getColumnIndex(columnName: string): number 7293 7294根据指定的列名获取列索引。 7295 7296**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7297 7298**参数:** 7299 7300| 参数名 | 类型 | 必填 | 说明 | 7301| ---------- | ------ | ---- | -------------------------- | 7302| columnName | string | 是 | 表示结果集中指定列的名称。 | 7303 7304**返回值:** 7305 7306| 类型 | 说明 | 7307| ------ | ------------------ | 7308| number | 返回指定列的索引。 | 7309 7310**错误码:** 7311 7312以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7313 7314| **错误码ID** | **错误信息** | 7315|-----------| ------------------------------------------------------------ | 7316| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7317| 14800000 | Inner error. | 7318| 14800011 | Database corrupted. | 7319| 14800013 | Column out of bounds. | 7320| 14800014 | Already closed. | 7321| 14800019 | The SQL must be a query statement. | 7322| 14800021 | SQLite: Generic error. | 7323| 14800022 | SQLite: Callback routine requested an abort. | 7324| 14800023 | SQLite: Access permission denied. | 7325| 14800024 | SQLite: The database file is locked. | 7326| 14800025 | SQLite: A table in the database is locked. | 7327| 14800026 | SQLite: The database is out of memory. | 7328| 14800027 | SQLite: Attempt to write a readonly database. | 7329| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7330| 14800029 | SQLite: The database is full. | 7331| 14800030 | SQLite: Unable to open the database file. | 7332| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7333| 14800032 | SQLite: Abort due to constraint violation. | 7334| 14800033 | SQLite: Data type mismatch. | 7335| 14800034 | SQLite: Library used incorrectly. | 7336 7337**示例:** 7338 7339```ts 7340if(resultSet != undefined) { 7341 const id = (resultSet as relationalStore.ResultSet).getLong((resultSet as relationalStore.ResultSet).getColumnIndex("ID")); 7342 const name = (resultSet as relationalStore.ResultSet).getString((resultSet as relationalStore.ResultSet).getColumnIndex("NAME")); 7343 const age = (resultSet as relationalStore.ResultSet).getLong((resultSet as relationalStore.ResultSet).getColumnIndex("AGE")); 7344 const salary = (resultSet as relationalStore.ResultSet).getDouble((resultSet as relationalStore.ResultSet).getColumnIndex("SALARY")); 7345} 7346``` 7347 7348### getColumnName 7349 7350getColumnName(columnIndex: number): string 7351 7352根据指定的列索引获取列名。 7353 7354**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7355 7356**参数:** 7357 7358| 参数名 | 类型 | 必填 | 说明 | 7359| ----------- | ------ | ---- | -------------------------- | 7360| columnIndex | number | 是 | 表示结果集中指定列的索引。 | 7361 7362**返回值:** 7363 7364| 类型 | 说明 | 7365| ------ | ------------------ | 7366| string | 返回指定列的名称。 | 7367 7368**错误码:** 7369 7370以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7371 7372| **错误码ID** | **错误信息** | 7373|-----------| ------------------------------------------------------------ | 7374| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7375| 14800000 | Inner error. | 7376| 14800011 | Database corrupted. | 7377| 14800013 | Column out of bounds. | 7378| 14800014 | Already closed. | 7379| 14800019 | The SQL must be a query statement. | 7380| 14800021 | SQLite: Generic error. | 7381| 14800022 | SQLite: Callback routine requested an abort. | 7382| 14800023 | SQLite: Access permission denied. | 7383| 14800024 | SQLite: The database file is locked. | 7384| 14800025 | SQLite: A table in the database is locked. | 7385| 14800026 | SQLite: The database is out of memory. | 7386| 14800027 | SQLite: Attempt to write a readonly database. | 7387| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7388| 14800029 | SQLite: The database is full. | 7389| 14800030 | SQLite: Unable to open the database file. | 7390| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7391| 14800032 | SQLite: Abort due to constraint violation. | 7392| 14800033 | SQLite: Data type mismatch. | 7393| 14800034 | SQLite: Library used incorrectly. | 7394 7395**示例:** 7396 7397```ts 7398if(resultSet != undefined) { 7399 const id = (resultSet as relationalStore.ResultSet).getColumnName(0); 7400 const name = (resultSet as relationalStore.ResultSet).getColumnName(1); 7401 const age = (resultSet as relationalStore.ResultSet).getColumnName(2); 7402} 7403``` 7404 7405### goTo 7406 7407goTo(offset:number): boolean 7408 7409向前或向后转至结果集的指定行,相对于其当前位置偏移。 7410 7411**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7412 7413**参数:** 7414 7415| 参数名 | 类型 | 必填 | 说明 | 7416| ------ | ------ | ---- | ---------------------------- | 7417| offset | number | 是 | 表示相对于当前位置的偏移量。 | 7418 7419**返回值:** 7420 7421| 类型 | 说明 | 7422| ------- | --------------------------------------------- | 7423| boolean | 如果成功移动结果集,则为true;否则返回false。 | 7424 7425**错误码:** 7426 7427以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7428 7429| **错误码ID** | **错误信息** | 7430|-----------| ------------------------------------------------------------ | 7431| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7432| 14800000 | Inner error. | 7433| 14800011 | Database corrupted. | 7434| 14800012 | Row out of bounds. | 7435| 14800014 | Already closed. | 7436| 14800019 | The SQL must be a query statement. | 7437| 14800021 | SQLite: Generic error. | 7438| 14800022 | SQLite: Callback routine requested an abort. | 7439| 14800023 | SQLite: Access permission denied. | 7440| 14800024 | SQLite: The database file is locked. | 7441| 14800025 | SQLite: A table in the database is locked. | 7442| 14800026 | SQLite: The database is out of memory. | 7443| 14800027 | SQLite: Attempt to write a readonly database. | 7444| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7445| 14800029 | SQLite: The database is full. | 7446| 14800030 | SQLite: Unable to open the database file. | 7447| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7448| 14800032 | SQLite: Abort due to constraint violation. | 7449| 14800033 | SQLite: Data type mismatch. | 7450| 14800034 | SQLite: Library used incorrectly. | 7451 7452**示例:** 7453 7454```ts 7455if(resultSet != undefined) { 7456 (resultSet as relationalStore.ResultSet).goTo(1); 7457} 7458``` 7459 7460### goToRow 7461 7462goToRow(position: number): boolean 7463 7464转到结果集的指定行。 7465 7466**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7467 7468**参数:** 7469 7470| 参数名 | 类型 | 必填 | 说明 | 7471| -------- | ------ | ---- | ------------------------ | 7472| position | number | 是 | 表示要移动到的指定位置。 | 7473 7474**返回值:** 7475 7476| 类型 | 说明 | 7477| ------- | --------------------------------------------- | 7478| boolean | 如果成功移动结果集,则为true;否则返回false。 | 7479 7480**错误码:** 7481 7482以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7483 7484| **错误码ID** | **错误信息** | 7485|-----------| ------------------------------------------------------------ | 7486| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7487| 14800000 | Inner error. | 7488| 14800011 | Database corrupted. | 7489| 14800012 | Row out of bounds. | 7490| 14800014 | Already closed. | 7491| 14800019 | The SQL must be a query statement. | 7492| 14800021 | SQLite: Generic error. | 7493| 14800022 | SQLite: Callback routine requested an abort. | 7494| 14800023 | SQLite: Access permission denied. | 7495| 14800024 | SQLite: The database file is locked. | 7496| 14800025 | SQLite: A table in the database is locked. | 7497| 14800026 | SQLite: The database is out of memory. | 7498| 14800027 | SQLite: Attempt to write a readonly database. | 7499| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7500| 14800029 | SQLite: The database is full. | 7501| 14800030 | SQLite: Unable to open the database file. | 7502| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7503| 14800032 | SQLite: Abort due to constraint violation. | 7504| 14800033 | SQLite: Data type mismatch. | 7505| 14800034 | SQLite: Library used incorrectly. | 7506 7507**示例:** 7508 7509```ts 7510if(resultSet != undefined) { 7511 (resultSet as relationalStore.ResultSet).goToRow(5); 7512} 7513``` 7514 7515### goToFirstRow 7516 7517goToFirstRow(): boolean 7518 7519 7520转到结果集的第一行。 7521 7522**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7523 7524**返回值:** 7525 7526| 类型 | 说明 | 7527| ------- | --------------------------------------------- | 7528| boolean | 如果成功移动结果集,则为true;否则返回false。 | 7529 7530**错误码:** 7531 7532以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7533 7534| **错误码ID** | **错误信息** | 7535|-----------| ------------------------------------------------------------ | 7536| 14800000 | Inner error. | 7537| 14800011 | Database corrupted. | 7538| 14800012 | Row out of bounds. | 7539| 14800014 | Already closed. | 7540| 14800019 | The SQL must be a query statement. | 7541| 14800021 | SQLite: Generic error. | 7542| 14800022 | SQLite: Callback routine requested an abort. | 7543| 14800023 | SQLite: Access permission denied. | 7544| 14800024 | SQLite: The database file is locked. | 7545| 14800025 | SQLite: A table in the database is locked. | 7546| 14800026 | SQLite: The database is out of memory. | 7547| 14800027 | SQLite: Attempt to write a readonly database. | 7548| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7549| 14800029 | SQLite: The database is full. | 7550| 14800030 | SQLite: Unable to open the database file. | 7551| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7552| 14800032 | SQLite: Abort due to constraint violation. | 7553| 14800033 | SQLite: Data type mismatch. | 7554| 14800034 | SQLite: Library used incorrectly. | 7555 7556**示例:** 7557 7558```ts 7559if(resultSet != undefined) { 7560 (resultSet as relationalStore.ResultSet).goToFirstRow(); 7561} 7562``` 7563 7564### goToLastRow 7565 7566goToLastRow(): boolean 7567 7568转到结果集的最后一行。 7569 7570**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7571 7572**返回值:** 7573 7574| 类型 | 说明 | 7575| ------- | --------------------------------------------- | 7576| boolean | 如果成功移动结果集,则为true;否则返回false。 | 7577 7578**错误码:** 7579 7580以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7581 7582| **错误码ID** | **错误信息** | 7583|-----------| ------------------------------------------------------------ | 7584| 14800000 | Inner error. | 7585| 14800011 | Database corrupted. | 7586| 14800012 | Row out of bounds. | 7587| 14800014 | Already closed. | 7588| 14800019 | The SQL must be a query statement. | 7589| 14800021 | SQLite: Generic error. | 7590| 14800022 | SQLite: Callback routine requested an abort. | 7591| 14800023 | SQLite: Access permission denied. | 7592| 14800024 | SQLite: The database file is locked. | 7593| 14800025 | SQLite: A table in the database is locked. | 7594| 14800026 | SQLite: The database is out of memory. | 7595| 14800027 | SQLite: Attempt to write a readonly database. | 7596| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7597| 14800029 | SQLite: The database is full. | 7598| 14800030 | SQLite: Unable to open the database file. | 7599| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7600| 14800032 | SQLite: Abort due to constraint violation. | 7601| 14800033 | SQLite: Data type mismatch. | 7602| 14800034 | SQLite: Library used incorrectly. | 7603 7604**示例:** 7605 7606```ts 7607if(resultSet != undefined) { 7608 (resultSet as relationalStore.ResultSet).goToLastRow(); 7609} 7610``` 7611 7612### goToNextRow 7613 7614goToNextRow(): boolean 7615 7616转到结果集的下一行。 7617 7618**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7619 7620**返回值:** 7621 7622| 类型 | 说明 | 7623| ------- | --------------------------------------------- | 7624| boolean | 如果成功移动结果集,则为true;否则返回false。 | 7625 7626**错误码:** 7627 7628以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7629 7630| **错误码ID** | **错误信息** | 7631|-----------| ------------------------------------------------------------ | 7632| 14800000 | Inner error. | 7633| 14800011 | Database corrupted. | 7634| 14800012 | Row out of bounds. | 7635| 14800014 | Already closed. | 7636| 14800019 | The SQL must be a query statement. | 7637| 14800021 | SQLite: Generic error. | 7638| 14800022 | SQLite: Callback routine requested an abort. | 7639| 14800023 | SQLite: Access permission denied. | 7640| 14800024 | SQLite: The database file is locked. | 7641| 14800025 | SQLite: A table in the database is locked. | 7642| 14800026 | SQLite: The database is out of memory. | 7643| 14800027 | SQLite: Attempt to write a readonly database. | 7644| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7645| 14800029 | SQLite: The database is full. | 7646| 14800030 | SQLite: Unable to open the database file. | 7647| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7648| 14800032 | SQLite: Abort due to constraint violation. | 7649| 14800033 | SQLite: Data type mismatch. | 7650| 14800034 | SQLite: Library used incorrectly. | 7651 7652**示例:** 7653 7654```ts 7655if(resultSet != undefined) { 7656 (resultSet as relationalStore.ResultSet).goToNextRow(); 7657} 7658``` 7659 7660### goToPreviousRow 7661 7662goToPreviousRow(): boolean 7663 7664转到结果集的上一行。 7665 7666**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7667 7668**返回值:** 7669 7670| 类型 | 说明 | 7671| ------- | --------------------------------------------- | 7672| boolean | 如果成功移动结果集,则为true;否则返回false。 | 7673 7674**错误码:** 7675 7676以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7677 7678| **错误码ID** | **错误信息** | 7679|-----------| ------------------------------------------------------------ | 7680| 14800000 | Inner error. | 7681| 14800011 | Database corrupted. | 7682| 14800012 | Row out of bounds. | 7683| 14800014 | Already closed. | 7684| 14800019 | The SQL must be a query statement. | 7685| 14800021 | SQLite: Generic error. | 7686| 14800022 | SQLite: Callback routine requested an abort. | 7687| 14800023 | SQLite: Access permission denied. | 7688| 14800024 | SQLite: The database file is locked. | 7689| 14800025 | SQLite: A table in the database is locked. | 7690| 14800026 | SQLite: The database is out of memory. | 7691| 14800027 | SQLite: Attempt to write a readonly database. | 7692| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7693| 14800029 | SQLite: The database is full. | 7694| 14800030 | SQLite: Unable to open the database file. | 7695| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7696| 14800032 | SQLite: Abort due to constraint violation. | 7697| 14800033 | SQLite: Data type mismatch. | 7698| 14800034 | SQLite: Library used incorrectly. | 7699 7700**示例:** 7701 7702```ts 7703if(resultSet != undefined) { 7704 (resultSet as relationalStore.ResultSet).goToPreviousRow(); 7705} 7706``` 7707 7708### getValue<sup>12+</sup> 7709 7710getValue(columnIndex: number): ValueType 7711 7712获取当前行中指定列的值,值类型如果是ValueType指定的任意类型,则会以对应类型返回指定类的值,否则返回14800000。 7713 7714**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7715 7716**参数:** 7717 7718| 参数名 | 类型 | 必填 | 说明 | 7719| ----------- | ------ | ---- | ----------------------- | 7720| columnIndex | number | 是 | 指定的列索引,从0开始。 | 7721 7722**返回值:** 7723 7724| 类型 | 说明 | 7725| ---------- | -------------------------------- | 7726| [ValueType](#valuetype) | 表示允许的数据字段类型。 | 7727 7728**错误码:** 7729 7730以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7731 7732| **错误码ID** | **错误信息** | 7733|-----------|---------| 7734| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7735| 14800000 | Inner error. | 7736| 14800011 | Database corrupted. | 7737| 14800012 | Row out of bounds. | 7738| 14800013 | Column out of bounds. | 7739| 14800014 | Already closed. | 7740| 14800021 | SQLite: Generic error. | 7741| 14800022 | SQLite: Callback routine requested an abort. | 7742| 14800023 | SQLite: Access permission denied. | 7743| 14800024 | SQLite: The database file is locked. | 7744| 14800025 | SQLite: A table in the database is locked. | 7745| 14800026 | SQLite: The database is out of memory. | 7746| 14800027 | SQLite: Attempt to write a readonly database. | 7747| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7748| 14800029 | SQLite: The database is full. | 7749| 14800030 | SQLite: Unable to open the database file. | 7750| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7751| 14800032 | SQLite: Abort due to constraint violation. | 7752| 14800033 | SQLite: Data type mismatch. | 7753| 14800034 | SQLite: Library used incorrectly. | 7754 7755**示例:** 7756 7757```ts 7758if(resultSet != undefined) { 7759 const codes = (resultSet as relationalStore.ResultSet).getValue((resultSet as relationalStore.ResultSet).getColumnIndex("BIGINT_COLUMN")); 7760} 7761``` 7762 7763### getBlob 7764 7765getBlob(columnIndex: number): Uint8Array 7766 7767 7768以字节数组的形式获取当前行中指定列的值,如果当前列的数据类型为INTEGER、DOUBLE、TEXT、BLOB类型,会转成字节数组类型返回指定值,如果该列内容为空时,会返回空字节数组,其他类型则返回14800000。 7769 7770**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7771 7772**参数:** 7773 7774| 参数名 | 类型 | 必填 | 说明 | 7775| ----------- | ------ | ---- | ----------------------- | 7776| columnIndex | number | 是 | 指定的列索引,从0开始。 | 7777 7778**返回值:** 7779 7780| 类型 | 说明 | 7781| ---------- | -------------------------------- | 7782| Uint8Array | 以字节数组的形式返回指定列的值。 | 7783 7784**错误码:** 7785 7786以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7787 7788| **错误码ID** | **错误信息** | 7789|-----------| ------------------------------------------------------------ | 7790| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7791| 14800000 | Inner error. | 7792| 14800011 | Database corrupted. | 7793| 14800012 | Row out of bounds. | 7794| 14800013 | Column out of bounds. | 7795| 14800014 | Already closed. | 7796| 14800021 | SQLite: Generic error. | 7797| 14800022 | SQLite: Callback routine requested an abort. | 7798| 14800023 | SQLite: Access permission denied. | 7799| 14800024 | SQLite: The database file is locked. | 7800| 14800025 | SQLite: A table in the database is locked. | 7801| 14800026 | SQLite: The database is out of memory. | 7802| 14800027 | SQLite: Attempt to write a readonly database. | 7803| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7804| 14800029 | SQLite: The database is full. | 7805| 14800030 | SQLite: Unable to open the database file. | 7806| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7807| 14800032 | SQLite: Abort due to constraint violation. | 7808| 14800033 | SQLite: Data type mismatch. | 7809| 14800034 | SQLite: Library used incorrectly. | 7810 7811**示例:** 7812 7813```ts 7814if(resultSet != undefined) { 7815 const codes = (resultSet as relationalStore.ResultSet).getBlob((resultSet as relationalStore.ResultSet).getColumnIndex("CODES")); 7816} 7817``` 7818 7819### getString 7820 7821getString(columnIndex: number): string 7822 7823以字符串形式获取当前行中指定列的值,如果当前列中的值为INTEGER、DOUBLE、TEXT、BLOB类型,会以字符串形式返回指定值,如果是当前列中的值为INTEGER,并且为空,则会返回空字符串"",其他类型则返回14800000。如果当前列中的值为DOUBLE类型,可能存在精度的丢失,建议使用[getDouble](#getdouble)接口获取。 7824 7825**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7826 7827**参数:** 7828 7829| 参数名 | 类型 | 必填 | 说明 | 7830| ----------- | ------ | ---- | ----------------------- | 7831| columnIndex | number | 是 | 指定的列索引,从0开始。 | 7832 7833**返回值:** 7834 7835| 类型 | 说明 | 7836| ------ | ---------------------------- | 7837| string | 以字符串形式返回指定列的值。 | 7838 7839**错误码:** 7840 7841以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7842 7843| **错误码ID** | **错误信息** | 7844|-----------| ------------------------------------------------------------ | 7845| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7846| 14800000 | Inner error. | 7847| 14800011 | Database corrupted. | 7848| 14800012 | Row out of bounds. | 7849| 14800013 | Column out of bounds. | 7850| 14800014 | Already closed. | 7851| 14800021 | SQLite: Generic error. | 7852| 14800022 | SQLite: Callback routine requested an abort. | 7853| 14800023 | SQLite: Access permission denied. | 7854| 14800024 | SQLite: The database file is locked. | 7855| 14800025 | SQLite: A table in the database is locked. | 7856| 14800026 | SQLite: The database is out of memory. | 7857| 14800027 | SQLite: Attempt to write a readonly database. | 7858| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7859| 14800029 | SQLite: The database is full. | 7860| 14800030 | SQLite: Unable to open the database file. | 7861| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7862| 14800032 | SQLite: Abort due to constraint violation. | 7863| 14800033 | SQLite: Data type mismatch. | 7864| 14800034 | SQLite: Library used incorrectly. | 7865 7866**示例:** 7867 7868```ts 7869if(resultSet != undefined) { 7870 const name = (resultSet as relationalStore.ResultSet).getString((resultSet as relationalStore.ResultSet).getColumnIndex("NAME")); 7871} 7872``` 7873 7874### getLong 7875 7876getLong(columnIndex: number): number 7877 7878以Long形式获取当前行中指定列的值,如果当前列的数据类型为INTEGER、DOUBLE、TEXT、BLOB类型,会转成Long类型返回指定值,如果该列内容为空时,会返回0,其他类型则返回14800000。 7879 7880**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7881 7882**参数:** 7883 7884| 参数名 | 类型 | 必填 | 说明 | 7885| ----------- | ------ | ---- | ----------------------- | 7886| columnIndex | number | 是 | 指定的列索引,从0开始。 | 7887 7888**返回值:** 7889 7890| 类型 | 说明 | 7891| ------ | ------------------------------------------------------------ | 7892| number | 以Long形式返回指定列的值。<br>该接口支持的数据范围是:Number.MIN_SAFE_INTEGER ~ Number.MAX_SAFE_INTEGER,若超出该范围,建议使用[getDouble](#getdouble)。 | 7893 7894**错误码:** 7895 7896以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7897 7898| **错误码ID** | **错误信息** | 7899|-----------| ------------------------------------------------------------ | 7900| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7901| 14800000 | Inner error. | 7902| 14800011 | Database corrupted. | 7903| 14800012 | Row out of bounds. | 7904| 14800013 | Column out of bounds. | 7905| 14800014 | Already closed. | 7906| 14800021 | SQLite: Generic error. | 7907| 14800022 | SQLite: Callback routine requested an abort. | 7908| 14800023 | SQLite: Access permission denied. | 7909| 14800024 | SQLite: The database file is locked. | 7910| 14800025 | SQLite: A table in the database is locked. | 7911| 14800026 | SQLite: The database is out of memory. | 7912| 14800027 | SQLite: Attempt to write a readonly database. | 7913| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7914| 14800029 | SQLite: The database is full. | 7915| 14800030 | SQLite: Unable to open the database file. | 7916| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7917| 14800032 | SQLite: Abort due to constraint violation. | 7918| 14800033 | SQLite: Data type mismatch. | 7919| 14800034 | SQLite: Library used incorrectly. | 7920 7921**示例:** 7922 7923```ts 7924if(resultSet != undefined) { 7925 const age = (resultSet as relationalStore.ResultSet).getLong((resultSet as relationalStore.ResultSet).getColumnIndex("AGE")); 7926 } 7927``` 7928 7929### getDouble 7930 7931getDouble(columnIndex: number): number 7932 7933以double形式获取当前行中指定列的值,如果当前列的数据类型为INTEGER、DOUBLE、TEXT、BLOB类型,会转成double类型返回指定值,如果该列内容为空时,会返回0.0,其他类型则返回14800000。 7934 7935**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7936 7937**参数:** 7938 7939| 参数名 | 类型 | 必填 | 说明 | 7940| ----------- | ------ | ---- | ----------------------- | 7941| columnIndex | number | 是 | 指定的列索引,从0开始。 | 7942 7943**返回值:** 7944 7945| 类型 | 说明 | 7946| ------ | ---------------------------- | 7947| number | 以double形式返回指定列的值。 | 7948 7949**错误码:** 7950 7951以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7952 7953| **错误码ID** | **错误信息** | 7954|-----------| ------------------------------------------------------------ | 7955| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7956| 14800000 | Inner error. | 7957| 14800011 | Database corrupted. | 7958| 14800012 | Row out of bounds. | 7959| 14800013 | Column out of bounds. | 7960| 14800014 | Already closed. | 7961| 14800021 | SQLite: Generic error. | 7962| 14800022 | SQLite: Callback routine requested an abort. | 7963| 14800023 | SQLite: Access permission denied. | 7964| 14800024 | SQLite: The database file is locked. | 7965| 14800025 | SQLite: A table in the database is locked. | 7966| 14800026 | SQLite: The database is out of memory. | 7967| 14800027 | SQLite: Attempt to write a readonly database. | 7968| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7969| 14800029 | SQLite: The database is full. | 7970| 14800030 | SQLite: Unable to open the database file. | 7971| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7972| 14800032 | SQLite: Abort due to constraint violation. | 7973| 14800033 | SQLite: Data type mismatch. | 7974| 14800034 | SQLite: Library used incorrectly. | 7975 7976**示例:** 7977 7978```ts 7979if(resultSet != undefined) { 7980 const salary = (resultSet as relationalStore.ResultSet).getDouble((resultSet as relationalStore.ResultSet).getColumnIndex("SALARY")); 7981} 7982``` 7983 7984### getAsset<sup>10+</sup> 7985 7986getAsset(columnIndex: number): Asset 7987 7988以[Asset](#asset10)形式获取当前行中指定列的值,如果当前列的数据类型为Asset类型,会以Asset类型返回指定值,如果当前列中的值为null时,会返回null,其他类型则返回14800000。 7989 7990**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7991 7992**参数:** 7993 7994| 参数名 | 类型 | 必填 | 说明 | 7995| ----------- | ------ | --- | ------------ | 7996| columnIndex | number | 是 | 指定的列索引,从0开始。 | 7997 7998**返回值:** 7999 8000| 类型 | 说明 | 8001| --------------- | -------------------------- | 8002| [Asset](#asset10) | 以Asset形式返回指定列的值。 | 8003 8004**错误码:** 8005 8006以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8007 8008| **错误码ID** | **错误信息** | 8009|-----------| ------------------------------------------------------------ | 8010| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8011| 14800000 | Inner error. | 8012| 14800011 | Database corrupted. | 8013| 14800012 | Row out of bounds. | 8014| 14800013 | Column out of bounds. | 8015| 14800014 | Already closed. | 8016| 14800021 | SQLite: Generic error. | 8017| 14800022 | SQLite: Callback routine requested an abort. | 8018| 14800023 | SQLite: Access permission denied. | 8019| 14800024 | SQLite: The database file is locked. | 8020| 14800025 | SQLite: A table in the database is locked. | 8021| 14800026 | SQLite: The database is out of memory. | 8022| 14800027 | SQLite: Attempt to write a readonly database. | 8023| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8024| 14800029 | SQLite: The database is full. | 8025| 14800030 | SQLite: Unable to open the database file. | 8026| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8027| 14800032 | SQLite: Abort due to constraint violation. | 8028| 14800033 | SQLite: Data type mismatch. | 8029| 14800034 | SQLite: Library used incorrectly. | 8030 8031**示例:** 8032 8033```ts 8034if(resultSet != undefined) { 8035 const doc = (resultSet as relationalStore.ResultSet).getAsset((resultSet as relationalStore.ResultSet).getColumnIndex("DOC")); 8036} 8037``` 8038 8039### getAssets<sup>10+</sup> 8040 8041getAssets(columnIndex: number): Assets 8042 8043以[Assets](#assets10)形式获取当前行中指定列的值,如果当前列的数据类型为Assets类型,会以Assets类型返回指定值,如果当前列中的值为null时,会返回null,其他类型则返回14800000。 8044 8045**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8046 8047**参数:** 8048 8049| 参数名 | 类型 | 必填 | 说明 | 8050| ----------- | ------ | --- | ------------ | 8051| columnIndex | number | 是 | 指定的列索引,从0开始。 | 8052 8053**返回值:** 8054 8055| 类型 | 说明 | 8056| ---------------- | ---------------------------- | 8057| [Assets](#assets10)| 以Assets形式返回指定列的值。 | 8058 8059**错误码:** 8060 8061以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8062 8063| **错误码ID** | **错误信息** | 8064|-----------| ------------------------------------------------------------ | 8065| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8066| 14800000 | Inner error. | 8067| 14800011 | Database corrupted. | 8068| 14800012 | Row out of bounds. | 8069| 14800013 | Column out of bounds. | 8070| 14800014 | Already closed. | 8071| 14800021 | SQLite: Generic error. | 8072| 14800022 | SQLite: Callback routine requested an abort. | 8073| 14800023 | SQLite: Access permission denied. | 8074| 14800024 | SQLite: The database file is locked. | 8075| 14800025 | SQLite: A table in the database is locked. | 8076| 14800026 | SQLite: The database is out of memory. | 8077| 14800027 | SQLite: Attempt to write a readonly database. | 8078| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8079| 14800029 | SQLite: The database is full. | 8080| 14800030 | SQLite: Unable to open the database file. | 8081| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8082| 14800032 | SQLite: Abort due to constraint violation. | 8083| 14800033 | SQLite: Data type mismatch. | 8084| 14800034 | SQLite: Library used incorrectly. | 8085 8086**示例:** 8087 8088```ts 8089if(resultSet != undefined) { 8090 const docs = (resultSet as relationalStore.ResultSet).getAssets((resultSet as relationalStore.ResultSet).getColumnIndex("DOCS")); 8091} 8092``` 8093 8094### getRow<sup>11+</sup> 8095 8096getRow(): ValuesBucket 8097 8098获取当前行。 8099 8100**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8101 8102**返回值:** 8103 8104| 类型 | 说明 | 8105| ---------------- | ---------------------------- | 8106| [ValuesBucket](#valuesbucket) | 返回指定行的值。 | 8107 8108**错误码:** 8109 8110以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8111 8112| **错误码ID** | **错误信息** | 8113|-----------| ------------------------------------------------------------ | 8114| 14800000 | Inner error. | 8115| 14800011 | Database corrupted. | 8116| 14800012 | Row out of bounds. | 8117| 14800013 | Column out of bounds. | 8118| 14800014 | Already closed. | 8119| 14800021 | SQLite: Generic error. | 8120| 14800022 | SQLite: Callback routine requested an abort. | 8121| 14800023 | SQLite: Access permission denied. | 8122| 14800024 | SQLite: The database file is locked. | 8123| 14800025 | SQLite: A table in the database is locked. | 8124| 14800026 | SQLite: The database is out of memory. | 8125| 14800027 | SQLite: Attempt to write a readonly database. | 8126| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8127| 14800029 | SQLite: The database is full. | 8128| 14800030 | SQLite: Unable to open the database file. | 8129| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8130| 14800032 | SQLite: Abort due to constraint violation. | 8131| 14800033 | SQLite: Data type mismatch. | 8132| 14800034 | SQLite: Library used incorrectly. | 8133 8134**示例:** 8135 8136```ts 8137if(resultSet != undefined) { 8138 const row = (resultSet as relationalStore.ResultSet).getRow(); 8139} 8140``` 8141 8142### getSendableRow<sup>12+</sup> 8143 8144getSendableRow(): sendableRelationalStore.ValuesBucket 8145 8146获取当前行数据的sendable形式,用于跨线程传递使用。 8147 8148**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8149 8150**返回值:** 8151 8152| 类型 | 说明 | 8153| ---------------------------------------------------------------------------------------------- | ---------------------------------------------- | 8154| [sendableRelationalStore.ValuesBucket](./js-apis-data-sendableRelationalStore.md#valuesbucket) | 当前行数据的sendable形式,用于跨线程传递使用。 | 8155 8156**错误码:** 8157 8158以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8159 8160| **错误码ID** | **错误信息** | 8161| ------------ | --------------------------------------------- | 8162| 14800000 | Inner error. | 8163| 14800011 | Database corrupted. | 8164| 14800012 | Row out of bounds. | 8165| 14800013 | Column out of bounds. | 8166| 14800014 | Already closed. | 8167| 14800021 | SQLite: Generic error. | 8168| 14800022 | SQLite: Callback routine requested an abort. | 8169| 14800023 | SQLite: Access permission denied. | 8170| 14800024 | SQLite: The database file is locked. | 8171| 14800025 | SQLite: A table in the database is locked. | 8172| 14800026 | SQLite: The database is out of memory. | 8173| 14800027 | SQLite: Attempt to write a readonly database. | 8174| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8175| 14800029 | SQLite: The database is full. | 8176| 14800030 | SQLite: Unable to open the database file. | 8177| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8178| 14800032 | SQLite: Abort due to constraint violation. | 8179| 14800033 | SQLite: Data type mismatch. | 8180| 14800034 | SQLite: Library used incorrectly. | 8181 8182**示例:** 8183 8184```ts 8185import { taskpool } from '@kit.ArkTS'; 8186import type ctx from '@ohos.app.ability.common'; 8187import { sendableRelationalStore } from '@kit.ArkData'; 8188 8189@Concurrent 8190async function getDataByName(name: string, context: ctx.UIAbilityContext) { 8191 const STORE_CONFIG: relationalStore.StoreConfig = { 8192 name: "RdbTest.db", 8193 securityLevel: relationalStore.SecurityLevel.S3 8194 }; 8195 const store = await relationalStore.getRdbStore(context, STORE_CONFIG); 8196 const predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 8197 predicates.equalTo("NAME", name); 8198 const resultSet = store.querySync(predicates); 8199 8200 if (resultSet.rowCount > 0) { 8201 resultSet.goToFirstRow(); 8202 const sendableValuesBucket = resultSet.getSendableRow(); 8203 return sendableValuesBucket; 8204 } else { 8205 return null; 8206 } 8207} 8208 8209async function run() { 8210 const task = new taskpool.Task(getDataByName, 'Lisa', this.context); 8211 const sendableValuesBucket = await taskpool.execute(task) as sendableRelationalStore.ValuesBucket; 8212 8213 if (sendableValuesBucket) { 8214 const columnCount = sendableValuesBucket.size; 8215 const age = sendableValuesBucket.get('age'); 8216 const name = sendableValuesBucket.get('name'); 8217 console.info(`Query data in taskpool succeeded, name is "${name}", age is "${age}"`) 8218 } 8219} 8220 8221run() 8222``` 8223 8224### isColumnNull 8225 8226isColumnNull(columnIndex: number): boolean 8227 8228检查当前行中指定列的值是否为null。 8229 8230**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8231 8232**参数:** 8233 8234| 参数名 | 类型 | 必填 | 说明 | 8235| ----------- | ------ | ---- | ----------------------- | 8236| columnIndex | number | 是 | 指定的列索引,从0开始。 | 8237 8238**返回值:** 8239 8240| 类型 | 说明 | 8241| ------- | --------------------------------------------------------- | 8242| boolean | 如果当前行中指定列的值为null,则返回true,否则返回false。 | 8243 8244**错误码:** 8245 8246以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8247 8248| **错误码ID** | **错误信息** | 8249|-----------| ------------------------------------------------------- | 8250| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8251| 14800000 | Inner error. | 8252| 14800011 | Database corrupted. | 8253| 14800012 | Row out of bounds. | 8254| 14800013 | Column out of bounds. | 8255| 14800014 | Already closed. | 8256| 14800021 | SQLite: Generic error. | 8257| 14800022 | SQLite: Callback routine requested an abort. | 8258| 14800023 | SQLite: Access permission denied. | 8259| 14800024 | SQLite: The database file is locked. | 8260| 14800025 | SQLite: A table in the database is locked. | 8261| 14800026 | SQLite: The database is out of memory. | 8262| 14800027 | SQLite: Attempt to write a readonly database. | 8263| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8264| 14800029 | SQLite: The database is full. | 8265| 14800030 | SQLite: Unable to open the database file. | 8266| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8267| 14800032 | SQLite: Abort due to constraint violation. | 8268| 14800033 | SQLite: Data type mismatch. | 8269| 14800034 | SQLite: Library used incorrectly. | 8270 8271**示例:** 8272 8273```ts 8274if(resultSet != undefined) { 8275 const isColumnNull = (resultSet as relationalStore.ResultSet).isColumnNull((resultSet as relationalStore.ResultSet).getColumnIndex("CODES")); 8276} 8277``` 8278 8279### close 8280 8281close(): void 8282 8283关闭结果集,若不关闭可能会引起fd泄露和内存泄露。 8284 8285**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8286 8287**示例:** 8288 8289```ts 8290if(resultSet != undefined) { 8291 (resultSet as relationalStore.ResultSet).close(); 8292} 8293``` 8294 8295**错误码:** 8296 8297以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 8298 8299| **错误码ID** | **错误信息** | 8300|-----------| ------------------------------------------------------------ | 8301| 14800000 | Inner error. | 8302| 14800012 | Row out of bounds. | 8303