1# @ohos.data.relationalStore (关系型数据库) 2<!--Kit: ArkData--> 3<!--Subsystem: DistributedDataManager--> 4<!--Owner: @baijidong--> 5<!--Designer: @widecode; @htt1997--> 6<!--Tester: @yippo; @logic42--> 7<!--Adviser: @ge-yafang--> 8 9关系型数据库(Relational Database,RDB)是一种基于关系模型来管理数据的数据库。关系型数据库基于SQLite组件提供了一套完整的对本地数据库进行管理的机制,对外提供了一系列的增、删、改、查等接口,也可以直接运行用户输入的SQL语句来满足复杂的场景需要。支持通过[ResultSet.getSendableRow](#getsendablerow12)方法获取Sendable数据,进行跨线程传递。 10 11为保证插入并读取数据成功,建议一条数据不超过2MB。如果数据超过2MB,插入操作将成功,读取操作将失败。 12 13大数据量场景下查询数据可能会导致耗时长甚至应用卡死,如有相关操作可参考文档[批量数据写数据库场景](../../arkts-utils/batch-database-operations-guide.md),且有建议如下: 14- 单次查询数据量不超过5000条。 15- 在[TaskPool](../apis-arkts/js-apis-taskpool.md)中查询。 16- 拼接SQL语句尽量简洁。 17- 合理地分批次查询。 18 19该模块提供以下关系型数据库相关的常用功能: 20 21- [RdbPredicates](#rdbpredicates):数据库中用来代表数据实体的性质、特征或者数据实体之间关系的词项,主要用来定义数据库的操作条件。 22- [RdbStore](#rdbstore):提供管理关系数据库(RDB)方法的接口。 23- [ResultSet](#resultset):提供用户调用关系型数据库查询接口之后返回的结果集合。 24- [Transaction](#transaction14):提供管理事务对象的接口。 25 26> **说明:** 27> 28> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 29 30## 导入模块 31 32```ts 33import { relationalStore } from '@kit.ArkData'; 34``` 35 36## relationalStore.getRdbStore 37 38getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback<RdbStore>): void 39 40创建或打开已有的关系型数据库,开发者可以根据自己的需求配置config参数,然后通过RdbStore调用相关接口执行数据操作。使用callback异步回调。 41 42对应沙箱路径下无数据库文件时,将创建数据库文件,文件创建位置详见[StoreConfig](#storeconfig)。对应路径下已有数据库文件时,将打开已有数据库文件。 43 44开发者在创建数据库时,应谨慎配置是否进行数据库加密的参数[encrypt](#storeconfig),数据库创建后,禁止对该参数进行修改。 45 46| 当前开库的加密类型 | 本设备上创建该数据库时的加密类型 | 结果 | 47| ------- | -------------------------------- | ---- | 48| 非加密 | 加密 | 将数据库以加密方式打开。 | 49| 加密 | 非加密 | 将数据库以非加密方式打开。 | 50 51getRdbStore目前不支持多线程并发操作。 52 53**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 54 55**参数:** 56 57| 参数名 | 类型 | 必填 | 说明 | 58| -------- | ---------------------------------------------- | ---- | ------------------------------------------------------------ | 59| 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)。 | 60| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 61| callback | AsyncCallback<[RdbStore](#rdbstore)> | 是 | 指定callback回调函数,返回RdbStore对象。 | 62 63**错误码:** 64 65以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 66 67| **错误码ID** | **错误信息** | 68|-----------|---------| 69| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 70| 14800000 | Inner error. | 71| 14800010 | Failed to open or delete the database by an invalid database path. | 72| 14800011 | Failed to open the database because it is corrupted. | 73| 14801001 | The operation is supported in the stage model only. | 74| 14801002 | Invalid data group ID. | 75| 14800017 | StoreConfig is changed. | 76| 14800020 | The secret key is corrupted or lost. | 77| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 78| 14800022 | SQLite: Callback routine requested an abort. | 79| 14800023 | SQLite: Access permission denied. | 80| 14800027 | SQLite: Attempt to write a readonly database. | 81| 14800028 | SQLite: Some kind of disk I/O error occurred. | 82| 14800029 | SQLite: The database is full. | 83| 14800030 | SQLite: Unable to open the database file. | 84 85**示例:** 86 87FA模型示例: 88 89<!--code_no_check_fa--> 90```js 91import { featureAbility } from '@kit.AbilityKit'; 92import { BusinessError } from '@kit.BasicServicesKit'; 93 94let store: relationalStore.RdbStore | undefined = undefined; 95let context = featureAbility.getContext(); 96 97const STORE_CONFIG: relationalStore.StoreConfig = { 98 name: "RdbTest.db", 99 securityLevel: relationalStore.SecurityLevel.S3 100}; 101 102relationalStore.getRdbStore(context, STORE_CONFIG, async (err: BusinessError, rdbStore: relationalStore.RdbStore) => { 103 if (err) { 104 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 105 return; 106 } 107 console.info('Get RdbStore successfully.'); 108 store = rdbStore; 109 // 成功获取到 rdbStore 后执行后续操作 110}); 111``` 112 113Stage模型示例: 114 115```ts 116import { UIAbility } from '@kit.AbilityKit'; 117import { window } from '@kit.ArkUI'; 118import { BusinessError } from '@kit.BasicServicesKit'; 119 120let store: relationalStore.RdbStore | undefined = undefined; 121 122class EntryAbility extends UIAbility { 123 onWindowStageCreate(windowStage: window.WindowStage) { 124 const STORE_CONFIG: relationalStore.StoreConfig = { 125 name: "RdbTest.db", 126 securityLevel: relationalStore.SecurityLevel.S3 127 }; 128 129 relationalStore.getRdbStore(this.context, STORE_CONFIG, async (err: BusinessError, rdbStore: relationalStore.RdbStore) => { 130 if (err) { 131 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 132 return; 133 } 134 console.info('Get RdbStore successfully.'); 135 store = rdbStore; 136 // 成功获取到 rdbStore 后执行后续操作 137 }); 138 } 139} 140``` 141 142## relationalStore.getRdbStore 143 144getRdbStore(context: Context, config: StoreConfig): Promise<RdbStore> 145 146创建或打开已有的关系型数据库,开发者可以根据自己的需求配置config参数,然后通过RdbStore调用相关接口执行数据操作。使用Promise异步回调。 147 148对应沙箱路径下无数据库文件时,将创建数据库文件,文件创建位置详见[StoreConfig](#storeconfig)。对应路径下已有数据库文件时,将打开已有数据库文件。 149 150开发者在创建数据库时,应谨慎配置是否进行数据库加密的参数[encrypt](#storeconfig),数据库创建后,禁止对该参数进行修改。 151 152| 当前开库的加密类型 | 本设备上创建该数据库时的加密类型 | 结果 | 153| ------- | -------------------------------- | ---- | 154| 非加密 | 加密 | 将数据库以加密方式打开。 | 155| 加密 | 非加密 | 将数据库以非加密方式打开。 | 156 157getRdbStore目前不支持多线程并发操作。 158 159**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 160 161**参数:** 162 163| 参数名 | 类型 | 必填 | 说明 | 164| ------- | -------------------------------- | ---- | ------------------------------------------------------------ | 165| 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)。 | 166| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 167 168**返回值**: 169 170| 类型 | 说明 | 171| ----------------------------------------- | --------------------------------- | 172| Promise<[RdbStore](#rdbstore)> | Promise对象。返回RdbStore对象。 | 173 174**错误码:** 175 176以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 177 178| **错误码ID** | **错误信息** | 179|-----------| ------------------------------------------------------------ | 180| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 181| 14800000 | Inner error. | 182| 14800010 | Failed to open or delete the database by an invalid database path. | 183| 14800011 | Failed to open the database because it is corrupted. | 184| 14801001 | The operation is supported in the stage model only. | 185| 14801002 | Invalid data group ID. | 186| 14800017 | StoreConfig is changed. | 187| 14800020 | The secret key is corrupted or lost. | 188| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 189| 14800022 | SQLite: Callback routine requested an abort. | 190| 14800023 | SQLite: Access permission denied. | 191| 14800027 | SQLite: Attempt to write a readonly database. | 192| 14800028 | SQLite: Some kind of disk I/O error occurred. | 193| 14800029 | SQLite: The database is full. | 194| 14800030 | SQLite: Unable to open the database file. | 195 196**示例:** 197 198FA模型示例: 199 200<!--code_no_check_fa--> 201```js 202import { featureAbility } from '@kit.AbilityKit'; 203import { BusinessError } from '@kit.BasicServicesKit'; 204 205let store: relationalStore.RdbStore | undefined = undefined; 206let context = featureAbility.getContext(); 207 208const STORE_CONFIG: relationalStore.StoreConfig = { 209 name: "RdbTest.db", 210 securityLevel: relationalStore.SecurityLevel.S3 211}; 212 213relationalStore.getRdbStore(context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => { 214 store = rdbStore; 215 console.info('Get RdbStore successfully.'); 216}).catch((err: BusinessError) => { 217 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 218}); 219``` 220 221Stage模型示例: 222 223```ts 224import { UIAbility } from '@kit.AbilityKit'; 225import { window } from '@kit.ArkUI'; 226import { BusinessError } from '@kit.BasicServicesKit'; 227 228let store: relationalStore.RdbStore | undefined = undefined; 229 230class EntryAbility extends UIAbility { 231 onWindowStageCreate(windowStage: window.WindowStage) { 232 const STORE_CONFIG: relationalStore.StoreConfig = { 233 name: "RdbTest.db", 234 securityLevel: relationalStore.SecurityLevel.S3 235 }; 236 237 relationalStore.getRdbStore(this.context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => { 238 store = rdbStore; 239 console.info('Get RdbStore successfully.'); 240 }).catch((err: BusinessError) => { 241 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 242 }); 243 } 244} 245``` 246 247## relationalStore.deleteRdbStore 248 249deleteRdbStore(context: Context, name: string, callback: AsyncCallback<void>): void 250 251删除数据库文件,使用callback异步回调。 252 253删除成功后,建议将数据库对象置为null。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则调用此接口进行删库无效,必须使用 [deleteRdbStore](#relationalstoredeleterdbstore10) 接口进行删库。 254 255当使用向量数据库时,在调用deleteRdbStore接口前,应当确保向量数据库已打开的RdbStore和ResultSet均已成功关闭。 256 257**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 258 259**参数:** 260 261| 参数名 | 类型 | 必填 | 说明 | 262| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 263| 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)。 | 264| name | string | 是 | 数据库名称。 | 265| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 266 267**错误码:** 268 269以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 270 271| **错误码ID** | **错误信息** | 272|-----------|---------------------------------------| 273| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 274| 14800000 | Inner error. | 275| 14800010 | Failed to open or delete the database by an invalid database path. | 276 277**示例:** 278 279FA模型示例: 280 281<!--code_no_check_fa--> 282```js 283import { featureAbility } from '@kit.AbilityKit'; 284import { BusinessError } from '@kit.BasicServicesKit'; 285 286let store: relationalStore.RdbStore | undefined = undefined; 287let context = featureAbility.getContext(); 288 289relationalStore.deleteRdbStore(context, "RdbTest.db", (err: BusinessError) => { 290 if (err) { 291 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 292 return; 293 } 294 store = undefined; 295 console.info('Delete RdbStore successfully.'); 296}); 297``` 298 299Stage模型示例: 300 301```ts 302import { UIAbility } from '@kit.AbilityKit'; 303import { window } from '@kit.ArkUI'; 304import { BusinessError } from '@kit.BasicServicesKit'; 305 306let store: relationalStore.RdbStore | undefined = undefined; 307 308class EntryAbility extends UIAbility { 309 onWindowStageCreate(windowStage: window.WindowStage) { 310 relationalStore.deleteRdbStore(this.context, "RdbTest.db", (err: BusinessError) => { 311 if (err) { 312 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 313 return; 314 } 315 store = undefined; 316 console.info('Delete RdbStore successfully.'); 317 }); 318 } 319} 320``` 321 322## relationalStore.deleteRdbStore 323 324deleteRdbStore(context: Context, name: string): Promise<void> 325 326使用指定的数据库文件配置删除数据库,使用Promise异步回调。 327 328删除成功后,建议将数据库对象置为null。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则调用此接口进行删库无效,必须使用 [deleteRdbStore](#relationalstoredeleterdbstore10-1) 接口进行删库。 329 330当使用向量数据库时,在调用deleteRdbStore接口前,应当确保向量数据库已打开的RdbStore和ResultSet均已成功关闭。 331 332**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 333 334**参数** 335 336| 参数名 | 类型 | 必填 | 说明 | 337| ------- | ------- | ---- | ------------------------------------------------------------ | 338| 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)。 | 339| name | string | 是 | 数据库名称。 | 340 341**返回值**: 342 343| 类型 | 说明 | 344| ------------------- | ------------------------- | 345| Promise<void> | 无返回结果的Promise对象。 | 346 347**错误码:** 348 349以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 350 351| **错误码ID** | **错误信息** | 352|-----------|----------------------------------------------------------------------------------| 353| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 354| 14800000 | Inner error. | 355| 14800010 | Failed to open or delete the database by an invalid database path. | 356 357**示例:** 358 359FA模型示例: 360 361<!--code_no_check_fa--> 362```js 363import { featureAbility } from '@kit.AbilityKit'; 364import { BusinessError } from '@kit.BasicServicesKit'; 365 366let store: relationalStore.RdbStore | undefined = undefined; 367let context = featureAbility.getContext(); 368 369relationalStore.deleteRdbStore(context, "RdbTest.db").then(() => { 370 store = undefined; 371 console.info('Delete RdbStore successfully.'); 372}).catch((err: BusinessError) => { 373 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 374}); 375``` 376 377Stage模型示例: 378 379```ts 380import { UIAbility } from '@kit.AbilityKit'; 381import { window } from '@kit.ArkUI'; 382import { BusinessError } from '@kit.BasicServicesKit'; 383 384let store: relationalStore.RdbStore | undefined = undefined; 385 386class EntryAbility extends UIAbility { 387 onWindowStageCreate(windowStage: window.WindowStage) { 388 relationalStore.deleteRdbStore(this.context, "RdbTest.db").then(() => { 389 store = undefined; 390 console.info('Delete RdbStore successfully.'); 391 }).catch((err: BusinessError) => { 392 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 393 }); 394 } 395} 396``` 397 398## relationalStore.deleteRdbStore<sup>10+</sup> 399 400deleteRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback\<void>): void 401 402使用指定的数据库文件配置删除数据库,使用callback异步回调。 403 404删除成功后,建议将数据库对象置为null。若数据库文件处于公共沙箱目录下,则删除数据库时必须使用该接口,当存在多个进程操作同一个数据库的情况,建议向其他进程发送数据库删除通知使其感知并处理。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则必须调用此接口进行删库。 405 406当使用向量数据库时,在调用deleteRdbStore接口前,应当确保向量数据库已打开的RdbStore和ResultSet均已成功关闭。 407 408**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 409 410**参数:** 411 412| 参数名 | 类型 | 必填 | 说明 | 413| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 414| 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)。 | 415| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 416| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 417 418**错误码:** 419 420以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 421 422| **错误码ID** | **错误信息** | 423|-----------|----------| 424| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 425| 14800000 | Inner error. | 426| 14800010 | Failed to open or delete the database by an invalid database path. | 427| 14801001 | The operation is supported in the stage model only. | 428| 14801002 | Invalid data group ID. | 429 430**示例:** 431 432FA模型示例: 433 434<!--code_no_check_fa--> 435```js 436import { featureAbility } from '@kit.AbilityKit'; 437import { BusinessError } from '@kit.BasicServicesKit'; 438 439let store: relationalStore.RdbStore | undefined = undefined; 440let context = featureAbility.getContext(); 441 442const STORE_CONFIG: relationalStore.StoreConfig = { 443 name: "RdbTest.db", 444 securityLevel: relationalStore.SecurityLevel.S3 445}; 446 447relationalStore.deleteRdbStore(context, STORE_CONFIG, (err: BusinessError) => { 448 if (err) { 449 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 450 return; 451 } 452 store = undefined; 453 console.info('Delete RdbStore successfully.'); 454}); 455``` 456 457Stage模型示例: 458 459```ts 460import { UIAbility } from '@kit.AbilityKit'; 461import { window } from '@kit.ArkUI'; 462import { BusinessError } from '@kit.BasicServicesKit'; 463 464let store: relationalStore.RdbStore | undefined = undefined; 465 466class EntryAbility extends UIAbility { 467 onWindowStageCreate(windowStage: window.WindowStage) { 468 const STORE_CONFIG: relationalStore.StoreConfig = { 469 name: "RdbTest.db", 470 securityLevel: relationalStore.SecurityLevel.S3 471 }; 472 relationalStore.deleteRdbStore(this.context, STORE_CONFIG, (err: BusinessError) => { 473 if (err) { 474 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 475 return; 476 } 477 store = undefined; 478 console.info('Delete RdbStore successfully.'); 479 }); 480 } 481} 482``` 483 484## relationalStore.deleteRdbStore<sup>10+</sup> 485 486deleteRdbStore(context: Context, config: StoreConfig): Promise\<void> 487 488使用指定的数据库文件配置删除数据库,使用Promise异步回调。 489 490删除成功后,建议将数据库对象置为null。若数据库文件处于公共沙箱目录下,则删除数据库时必须使用该接口,当存在多个进程操作同一个数据库的情况,建议向其他进程发送数据库删除通知使其感知并处理。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则必须调用此接口进行删库。 491 492当使用向量数据库时,在调用deleteRdbStore接口前,应当确保向量数据库已打开的RdbStore和ResultSet均已成功关闭。 493 494**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 495 496**参数** 497 498| 参数名 | 类型 | 必填 | 说明 | 499| ------- | --------------------------- | ---- | ------------------------------------------------------------ | 500| 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)。 | 501| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 502 503**返回值**: 504 505| 类型 | 说明 | 506| ------------------- | ------------------------- | 507| Promise<void> | 无返回结果的Promise对象。 | 508 509**错误码:** 510 511以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 512 513| **错误码ID** | **错误信息** | 514|-----------|---------------------| 515| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 516| 801 | Capability not supported. | 517| 14800000 | Inner error. | 518| 14800010 | Failed to open or delete the database by an invalid database path. | 519| 14801001 | The operation is supported in the stage model only. | 520| 14801002 | Invalid data group ID. | 521 522**示例:** 523 524FA模型示例: 525 526<!--code_no_check_fa--> 527```js 528import { featureAbility } from "@kit.AbilityKit"; 529import { BusinessError } from '@kit.BasicServicesKit'; 530 531let store: relationalStore.RdbStore | undefined = undefined; 532let context = featureAbility.getContext(); 533 534const STORE_CONFIG: relationalStore.StoreConfig = { 535 name: "RdbTest.db", 536 securityLevel: relationalStore.SecurityLevel.S3 537}; 538 539relationalStore.deleteRdbStore(context, STORE_CONFIG).then(() => { 540 store = undefined; 541 console.info('Delete RdbStore successfully.'); 542}).catch((err: BusinessError) => { 543 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 544}); 545``` 546 547Stage模型示例: 548 549```ts 550import { UIAbility } from '@kit.AbilityKit'; 551import { window } from '@kit.ArkUI'; 552import { BusinessError } from '@kit.BasicServicesKit'; 553 554let store: relationalStore.RdbStore | undefined = undefined; 555 556class EntryAbility extends UIAbility { 557 onWindowStageCreate(windowStage: window.WindowStage) { 558 const STORE_CONFIG: relationalStore.StoreConfig = { 559 name: "RdbTest.db", 560 securityLevel: relationalStore.SecurityLevel.S3 561 }; 562 relationalStore.deleteRdbStore(this.context, STORE_CONFIG).then(() => { 563 store = undefined; 564 console.info('Delete RdbStore successfully.'); 565 }).catch((err: BusinessError) => { 566 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 567 }); 568 } 569} 570``` 571## relationalStore.isVectorSupported<sup>18+</sup> 572 573isVectorSupported(): boolean 574 575判断系统是否提供向量数据库能力。 576 577**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 578 579**返回值**: 580 581| 类型 | 说明 | 582| ------- | ------------------------------------------------- | 583| boolean | 系统具备向量数据库能力时返回true,否则返回false。 | 584 585**示例:** 586 587``` 588let result = relationalStore.isVectorSupported(); 589``` 590 591## relationalStore.isTokenizerSupported<sup>18+</sup> 592 593isTokenizerSupported(tokenizer: Tokenizer): boolean 594 595判断当前平台是否支持传入的分词器,此为同步接口。 596 597如果当前平台支持传入的分词器时,此接口返回值为true;反之,返回值为false。 598 599**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 600 601**参数:** 602 603| 参数名 | 类型 | 必填 | 说明 | 604| ------- | --------------------- | ---- | ------------------------------------------------------------ | 605| tokenizer | [Tokenizer](#tokenizer17) | 是 | 需要被判断是否支持的分词器。 | 606 607**返回值:** 608 609| 类型 | 说明 | 610| ------------------- | ------------------------- | 611| boolean | true表示当前平台支持当前传入的分词器,false表示当前平台不支持当前传入的分词器。 | 612 613**错误码:** 614 615以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 616 617| **错误码ID** | **错误信息** | 618|-----------|---------------------| 619| 401 | Parameter error. Possible causes: Incorrect parameter types. | 620 621 622**示例:** 623 624```ts 625import { relationalStore } from '@kit.ArkData'; // 导入模块 626 627let customType = relationalStore.Tokenizer.CUSTOM_TOKENIZER; 628let customTypeSupported = relationalStore.isTokenizerSupported(customType); 629console.info("custom tokenizer supported on current platform: " + customTypeSupported); 630``` 631 632## StoreConfig 633 634管理关系数据库配置。 635 636| 名称 | 类型 | 必填 | 说明 | 637| ------------- | ------------- | ---- |-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 638| name | string | 是 | 数据库文件名,也是数据库唯一标识符。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 639| securityLevel | [SecurityLevel](#securitylevel) | 是 | 设置数据库安全级别。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 640| encrypt | boolean | 否 | 指定数据库是否加密,默认不加密。<br/> true:加密。<br/> false:非加密。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 641| dataGroupId<sup>10+</sup> | string | 否 | 应用组ID,<!--RP1-->暂不支持指定dataGroupId在对应的沙箱路径下创建RdbStore实例。<!--RP1End--><br/>**模型约束:** 此属性仅在Stage模型下可用。<br/>从API version 10开始,支持此可选参数。dataGroupId共沙箱的方式不支持多进程访问加密数据库,当此参数不填时,默认在本应用沙箱目录下创建RdbStore实例。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 642| customDir<sup>11+</sup> | string | 否 | 数据库自定义路径。<br/>**使用约束:** 数据库路径大小限制为128字节,如果超过该大小会开库失败,返回错误。<br/>从API version 11开始,支持此可选参数。数据库将在如下的目录结构中被创建:context.databaseDir + "/rdb/" + customDir,其中context.databaseDir是应用沙箱对应的路径,"/rdb/"表示创建的是关系型数据库,customDir表示自定义的路径。当此参数不填时,默认在本应用沙箱目录下创建RdbStore实例。从API version 18开始,如果同时配置了rootDir参数,将打开或删除如下路径数据库:rootDir + "/" + customDir + "/" + name。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 643| rootDir<sup>18+</sup> | string | 否 | 指定数据库根路径。<br/>从API version 18开始,支持此可选参数。将从如下目录打开或删除数据库:rootDir + "/" + customDir。通过设置此参数打开的数据库为只读模式,不允许对数据库进行写操作,否则返回错误码801。配置此参数打开或删除数据库时,应确保对应路径下数据库文件存在,并且有读取权限,否则返回错误码14800010。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 644| autoCleanDirtyData<sup>11+</sup> | boolean | 否 | 指定是否自动清理云端删除后同步到本地的数据,true表示自动清理,false表示手动清理,默认自动清理。<br/>对于端云协同的数据库,当云端删除的数据同步到设备端时,可通过该参数设置设备端是否自动清理。手动清理可以通过[cleanDirtyData<sup>11+</sup>](#cleandirtydata11)接口清理。<br/>从API version 11开始,支持此可选参数。<br/>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 645| allowRebuild<sup>12+</sup> | boolean | 否 | 指定数据库是否支持异常时自动删除,并重建一个空库空表,默认不删除。<br/>true:自动删除。<br/>false:不自动删除。<br/>从API version 12开始,支持此可选参数。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 646| isReadOnly<sup>12+</sup> | boolean | 否 | 指定数据库是否只读,默认为数据库可读写。<br/>true:只允许从数据库读取数据,不允许对数据库进行写操作,否则会返回错误码801。<br/>false:允许对数据库进行读写操作。<br/>从API version 12开始,支持此可选参数。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 647| 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 | 648| cryptoParam<sup>14+</sup> | [CryptoParam](#cryptoparam14) | 否 | 指定用户自定义的加密参数。<br/>当此参数不填时,使用默认的加密参数,见[CryptoParam](#cryptoparam14)各参数默认值。<br/>此配置只有在encrypt选项设置为真时才有效。<br/>从API version 14开始,支持此可选参数。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 649| vector<sup>18+</sup> | boolean | 否 | 指定数据库是否是向量数据库,true表示向量数据库,false表示关系型数据库,默认为false。<br/>向量数据库适用于存储和处理高维向量数据,关系型数据库适用于存储和处理结构化数据。<br/>当使用向量数据库时,在调用deleteRdbStore接口前,应当确保向量数据库已打开的RdbStore和ResultSet均已成功关闭。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 650| tokenizer<sup>17+</sup> | [Tokenizer](#tokenizer17) | 否 | 指定用户在fts场景下使用哪种分词器。<br/>当此参数不填时,则在fts下不支持中文以及多国语言分词,但仍可支持英文分词。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 651| persist<sup>18+</sup> | boolean | 否 | 指定数据库是否需要持久化。true表示持久化,false表示不持久化,即内存数据库。默认为true。<br/>内存数据库不支持加密、backup、restore、跨进程访问及分布式能力,securityLevel属性会被忽略。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 652| enableSemanticIndex<sup>20+</sup> | boolean | 否 | 指定数据库是否启用语义索引处理功能。true表示启用语义索引处理功能,false表示不启用。默认为false。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 653 654## SecurityLevel 655 656数据库的安全级别枚举。请使用枚举名称而非枚举值。数据库的安全等级仅支持由低向高设置,不支持由高向低设置。 657 658> **说明:** 659> 660> 若需要进行同步操作,数据库安全等级应不高于对端设备安全等级,具体可见[跨设备同步访问控制机制](../../database/sync-app-data-across-devices-overview.md#跨设备同步访问控制机制)。 661 662**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 663 664| 名称 | 值 | 说明 | 665| ---- | ---- | ------------------------------------------------------------ | 666| S1 | 1 | 表示数据库的安全级别为低级别,当数据泄露时会产生较低影响。例如,包含壁纸等系统数据的数据库。 | 667| S2 | 2 | 表示数据库的安全级别为中级别,当数据泄露时会产生较大影响。例如,包含录音、视频等用户生成数据或通话记录等信息的数据库。 | 668| S3 | 3 | 表示数据库的安全级别为高级别,当数据泄露时会产生重大影响。例如,包含用户运动、健康、位置等信息的数据库。 | 669| S4 | 4 | 表示数据库的安全级别为关键级别,当数据泄露时会产生严重影响。例如,包含认证凭据、财务数据等信息的数据库。 | 670 671## CryptoParam<sup>14+</sup> 672 673数据库加密参数配置。此配置只有在StoreConfig的encrypt选项设置为true时有效。 674 675**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 676 677| 名称 | 类型 | 必填 | 说明 | 678| ------------- | ------ | ---- | ------------------------------------------------------------ | 679| encryptionKey | Uint8Array | 是 | 指定数据库加/解密使用的密钥。<br/>如传入密钥为空,则由数据库负责生成并保存密钥,并使用生成的密钥打开数据库文件。<br/>使用完后用户需要将密钥内容全部置为零。 | 680| iterationCount | number | 否 | 整数类型,指定数据库PBKDF2算法的迭代次数,默认值为10000。<br/>迭代次数应当为大于零的整数,若非整数则向下取整。<br/>不指定此参数或指定为零时,使用默认值10000,并使用默认加密算法AES_256_GCM。 | 681| encryptionAlgo | [EncryptionAlgo](#encryptionalgo14) | 否 | 指定数据库加解密使用的加密算法。如不指定,默认值为 AES_256_GCM。 | 682| hmacAlgo | [HmacAlgo](#hmacalgo14) | 否 | 指定数据库加解密使用的HMAC算法。如不指定,默认值为SHA256。 | 683| kdfAlgo | [KdfAlgo](#kdfalgo14) | 否 | 指定数据库加解密使用的PBKDF2算法。如不指定,默认使用和HMAC算法相等的算法。 | 684| cryptoPageSize | number | 否 | 整数类型,指定数据库加解密使用的页大小。如不指定,默认值为1024字节。<br/>用户指定的页大小应为1024到65536范围内的整数,并且为2<sup>n</sup>。若指定值非整数,则向下取整。 | 685 686## EncryptionAlgo<sup>14+</sup> 687 688数据库的加密算法枚举。请使用枚举名称而非枚举值。 689 690**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 691 692| 名称 | 值 | 说明 | 693| ---- | ---- | ---- | 694| AES_256_GCM | 0 | AES_256_GCM加密算法。 | 695| AES_256_CBC | 1 | AES_256_CBC加密算法。 | 696 697## HmacAlgo<sup>14+</sup> 698 699数据库的HMAC算法枚举。请使用枚举名称而非枚举值。 700 701**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 702 703| 名称 | 值 | 说明 | 704| ---- | ---- | ---- | 705| SHA1 | 0 | HMAC_SHA1算法。 | 706| SHA256 | 1 | HMAC_SHA256算法。 | 707| SHA512 | 2 | HMAC_SHA512算法。 | 708 709## KdfAlgo<sup>14+</sup> 710 711数据库的PBKDF2算法枚举。请使用枚举名称而非枚举值。 712 713**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 714 715| 名称 | 值 | 说明 | 716| ---- | ---- | ---- | 717| KDF_SHA1 | 0 | PBKDF2_HMAC_SHA1算法。 | 718| KDF_SHA256 | 1 | PBKDF2_HMAC_SHA256算法。 | 719| KDF_SHA512 | 2 | PBKDF2_HMAC_SHA512算法。 | 720 721## Tokenizer<sup>17+</sup> 722 723描述fts(全文搜索)场景下使用的分词器枚举。请使用枚举名称而非枚举值。 724 725**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 726 727| 名称 | 值 | 说明 | 728| ------------------------------- | --- | -------------- | 729| NONE_TOKENIZER | 0 | 不使用分词器。 | 730| ICU_TOKENIZER | 1 | 表示使用icu分词器,支持中文以及多国语言。指定icu分词器时,可指定使用哪种语言,例如zh_CN表示中文,tr_TR表示土耳其语等。详细支持的语言种类,请查阅[ICU分词器](https://gitee.com/openharmony/third_party_icu/blob/master/icu4c/source/data/lang/zh.txt)。详细的语言缩写,请查阅该目录([ICU支持的语言缩写](https://gitee.com/openharmony/third_party_icu/tree/master/icu4c/source/data/locales))下的文件名。| 731| CUSTOM_TOKENIZER<sup>18+</sup> | 2 | 表示使用自研分词器,可支持中文(简体、繁体)、英文、阿拉伯数字。CUSTOM_TOKENIZER相比ICU_TOKENIZER在分词准确率、常驻内存占用上更有优势。 | 732 733在使用不同的分词器时,使用的创表语句会有所区别。 734 735**示例:** 736 737使用ICU_TOKENIZER分词器时,创建表的示例: 738 739```ts 740import { relationalStore } from '@kit.ArkData'; // 导入模块 741import { UIAbility } from '@kit.AbilityKit'; 742import { BusinessError } from '@kit.BasicServicesKit'; 743import { window } from '@kit.ArkUI'; 744 745// 此处示例在Stage模式、Ability中实现,使用者也可以在其他合理场景中使用 746class EntryAbility extends UIAbility { 747 async onWindowStageCreate(windowStage: window.WindowStage) { 748 let store: relationalStore.RdbStore | undefined = undefined; 749 const STORE_CONFIG: relationalStore.StoreConfig = { 750 name: "MyStore.db", 751 securityLevel: relationalStore.SecurityLevel.S3, 752 tokenizer: relationalStore.Tokenizer.ICU_TOKENIZER 753 }; 754 store = await relationalStore.getRdbStore(this.context, STORE_CONFIG); 755 756 const SQL_CREATE_TABLE = "CREATE VIRTUAL TABLE example USING fts4(name, content, tokenize=icu zh_CN)"; 757 if (store != undefined) { 758 (store as relationalStore.RdbStore).executeSql(SQL_CREATE_TABLE, (err) => { 759 if (err) { 760 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 761 return; 762 } 763 console.info('create virtual table done.'); 764 }); 765 } 766 } 767} 768``` 769 770使用CUSTOM_TOKENIZER分词器时,创建表的示例: 771 772```ts 773import { relationalStore } from '@kit.ArkData'; // 导入模块 774import { UIAbility } from '@kit.AbilityKit'; 775import { BusinessError } from '@kit.BasicServicesKit'; 776import { window } from '@kit.ArkUI'; 777 778// 此处示例在Stage模式、Ability中实现,使用者也可以在其他合理场景中使用 779class EntryAbility extends UIAbility { 780 async onWindowStageCreate(windowStage: window.WindowStage) { 781 let store: relationalStore.RdbStore | undefined = undefined; 782 const STORE_CONFIG: relationalStore.StoreConfig = { 783 name: "MyStore.db", 784 securityLevel: relationalStore.SecurityLevel.S3, 785 tokenizer: relationalStore.Tokenizer.CUSTOM_TOKENIZER 786 }; 787 store = await relationalStore.getRdbStore(this.context, STORE_CONFIG); 788 789 const SQL_CREATE_TABLE = "CREATE VIRTUAL TABLE example USING fts5(name, content, tokenize='customtokenizer')"; 790 if (store != undefined) { 791 (store as relationalStore.RdbStore).executeSql(SQL_CREATE_TABLE, (err) => { 792 if (err) { 793 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 794 return; 795 } 796 console.info('create virtual table done.'); 797 }); 798 } 799 } 800} 801``` 802 803## AssetStatus<sup>10+</sup> 804 805描述资产附件的状态枚举。请使用枚举名称而非枚举值。 806 807**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 808 809| 名称 | 值 | 说明 | 810| ------------------------------- | --- | -------------- | 811| ASSET_NORMAL | 1 | 表示资产状态正常。 | 812| ASSET_INSERT | 2 | 表示资产需要插入到云端。 | 813| ASSET_UPDATE | 3 | 表示资产需要更新到云端。 | 814| ASSET_DELETE | 4 | 表示资产需要在云端删除。 | 815| ASSET_ABNORMAL | 5 | 表示资产状态异常。 | 816| ASSET_DOWNLOADING | 6 | 表示资产正在下载到本地设备。 | 817 818## Asset<sup>10+</sup> 819 820记录资产附件(文件、图片、视频等类型文件)的相关信息。 821 822**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 823 824| 名称 | 类型 | 必填 | 说明 | 825| ----------- | --------------------------- | --- | ------------ | 826| name | string | 是 | 资产的名称。 | 827| uri | string | 是 | 资产的uri,在系统里的绝对路径。 | 828| path | string | 是 | 资产在应用沙箱里的路径。 | 829| createTime | string | 是 | 资产被创建出来的时间。 | 830| modifyTime | string | 是 | 资产最后一次被修改的时间。 | 831| size | string | 是 | 资产占用空间的大小。 | 832| status | [AssetStatus](#assetstatus10) | 否 | 资产的状态,默认值为ASSET_NORMAL。 | 833 834## Assets<sup>10+</sup> 835 836type Assets = Asset[] 837 838表示[Asset](#asset10)类型的数组。 839 840**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 841 842| 类型 | 说明 | 843| ------- | -------------------- | 844| [Asset](#asset10)[] | 表示Asset类型的数组。 | 845 846## ValueType 847 848type ValueType = null | number | string | boolean | Uint8Array | Asset | Assets | Float32Array | bigint 849 850用于表示允许的数据字段类型,接口参数具体类型根据其功能而定。 851 852**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 853 854| 类型 | 说明 | 855| ------- | -------------------- | 856| null<sup>10+</sup> | 表示值类型为空。 | 857| number | 表示值类型为数字。 | 858| string | 表示值类型为字符串。 | 859| boolean | 表示值类型为布尔值。 | 860| Uint8Array<sup>10+</sup> | 表示值类型为Uint8类型的数组。 | 861| Asset<sup>10+</sup> | 表示值类型为附件[Asset](#asset10)。<br/>当字段类型是Asset时,在创建表的sql语句中,类型应当为:ASSET。 | 862| Assets<sup>10+</sup> | 表示值类型为附件数组[Assets](#assets10)。<br/>当字段类型是Assets时,在创建表的sql语句中,类型应当为:ASSETS。 | 863| Float32Array<sup>12+</sup> | 表示值类型为浮点数组。<br/>当字段类型是Float32Array时,在创建表的sql语句中,类型应当为:floatvector(128)。 | 864| 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。 | 865 866## ValuesBucket 867 868type ValuesBucket = Record<string, ValueType> 869 870用于存储键值对的类型。不支持Sendable跨线程传递。 871 872**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 873 874| 类型 | 说明 | 875| ---------------- | ---------------------------- | 876| Record<string, [ValueType](#valuetype)> | 表示键值对类型。键的类型为string,值的类型为[ValueType](#valuetype)。 | 877 878## PRIKeyType<sup>10+</sup> 879 880type PRIKeyType = number | string 881 882用于表示数据库表某一行主键的数据类型。 883 884**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 885 886| 类型 | 说明 | 887| ---------------- | ---------------------------------- | 888| number | 主键的类型可以是number。 | 889| string | 主键的类型可以是string。 | 890 891## UTCTime<sup>10+</sup> 892 893type UTCTime = Date 894 895用于表示UTC类型时间的数据类型。 896 897**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 898 899| 类型 | 说明 | 900| ---- | --------------- | 901| Date | UTC类型的时间。 | 902 903## ModifyTime<sup>10+</sup> 904 905type ModifyTime = Map<PRIKeyType, UTCTime> 906 907用于存储数据库表的主键和修改时间的数据类型。 908 909**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 910 911| 类型 | 说明 | 912| ------------------------------------------------------- | ------------------------------------------------------------ | 913| Map<[PRIKeyType](#prikeytype10), [UTCTime](#utctime10)> | 键表示是数据库表某一行的主键,值表示该行的最后修改时间,用UTC格式表示。 | 914 915## SyncMode 916 917指数据库同步模式。请使用枚举名称而非枚举值。 918 919| 名称 | 值 | 说明 | 920| -------------- | ---- | ---------------------------------- | 921| SYNC_MODE_PUSH | 0 | 表示数据从本地设备推送到远程设备。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 922| SYNC_MODE_PULL | 1 | 表示数据从远程设备拉至本地设备。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 923| SYNC_MODE_TIME_FIRST<sup>10+</sup> | 4 | 表示数据从修改时间较近的一端同步到修改时间较远的一端。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 924| SYNC_MODE_NATIVE_FIRST<sup>10+</sup> | 5 | 表示数据从本地设备同步到云端。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 925| SYNC_MODE_CLOUD_FIRST<sup>10+</sup> | 6 | 表示数据从云端同步到本地设备。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 926 927## Origin<sup>11+</sup> 928 929表示数据来源。请使用枚举名称而非枚举值。 930 931**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 932 933| 名称 | 值 | 说明 | 934| -------------- | ---- | ---------------------------------- | 935| LOCAL | 0 | 表示本地数据。 | 936| CLOUD | 1 | 表示云端同步的数据。 | 937| REMOTE | 2 | 表示端端同步的数据。 | 938 939## Field<sup>11+</sup> 940 941用于谓词查询条件的特殊字段。请使用枚举名称而非枚举值。 942 943**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 944 945| 名称 | 值 | 说明 | 946| -------------- | ---- | ---------------------------------- | 947| CURSOR_FIELD | '#_cursor' | 用于cursor查找的字段名。| 948| ORIGIN_FIELD | '#_origin' | 用于cursor查找时指定数据来源的字段名。 | 949| DELETED_FLAG_FIELD | '#_deleted_flag' | 用于cursor查找的结果集返回时填充的字段,表示云端删除的数据同步到本地后数据是否清理。<br>返回的结果集中,该字段对应的value为false表示数据未清理,true表示数据已清理。| 950| DATA_STATUS_FIELD<sup>12+</sup> | '#_data_status' | 用于cursor查找的结果集返回时填充的字段,返回的结果集中,该字段对应的0表示正常数据,1表示退出账号保留数据,2表示云侧同步删除,3表示退出账户删除数据。| 951| OWNER_FIELD | '#_cloud_owner' | 用于共享表中查找owner时,返回的结果集中填充的字段,表示当前共享记录的共享发起者。| 952| PRIVILEGE_FIELD | '#_cloud_privilege' | 用于共享表中查找共享数据权限时,返回的结果集中填充的字段,表示当前共享记录的允许的操作权限。| 953| SHARING_RESOURCE_FIELD | '#_sharing_resource_field' | 用于数据共享查找共享数据的共享资源时,返回的结果集中填充的字段,表示共享数据的共享资源标识。| 954 955## SubscribeType 956 957描述订阅类型。请使用枚举名称而非枚举值。 958 959| 名称 | 值 | 说明 | 960| --------------------- | ---- | ------------------ | 961| SUBSCRIBE_TYPE_REMOTE | 0 | 订阅远程数据更改。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 962| SUBSCRIBE_TYPE_CLOUD<sup>10+</sup> | 1 | 订阅云端数据更改。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 963| SUBSCRIBE_TYPE_CLOUD_DETAILS<sup>10+</sup> | 2 | 订阅云端数据更改详情。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 964| SUBSCRIBE_TYPE_LOCAL_DETAILS<sup>12+</sup> | 3 | 订阅本地数据更改详情。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 965 966## RebuildType<sup>12+</sup> 967 968描述数据库重建类型的枚举。请使用枚举名称而非枚举值。 969 970**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 971 972| 名称 | 值 | 说明 | 973| ------- | ---- |----------------------------------------------------------------------------------------------------------------| 974| NONE | 0 | 表示数据库未进行重建。 | 975| REBUILT | 1 | 表示数据库进行了重建并且生成了空数据库,需要应用重新建表和恢复数据。 | 976| REPAIRED | 2 | 表示数据库进行了修复,恢复了未损坏的数据,当前只有[向量数据库](#storeconfig)具备该能力。 | 977 978## ChangeType<sup>10+</sup> 979 980描述数据变更类型的枚举。请使用枚举名称而非枚举值。 981 982**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 983 984| 名称 | 值 | 说明 | 985| -------------------------- | --- | -------------------------- | 986| DATA_CHANGE | 0 | 表示是数据发生变更。 | 987| ASSET_CHANGE | 1 | 表示是资产附件发生了变更。 | 988 989## ChangeInfo<sup>10+</sup> 990 991记录端云同步过程详情。 992 993**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 994 995| 名称 | 类型 | 必填 | 说明 | 996| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 997| table | string | 是 | 表示发生变化的表的名称。 | 998| type | [ChangeType](#changetype10) | 是 | 表示发生变化的数据的类型,数据或者资产附件发生变化。 | 999| inserted | Array\<string\> \| Array\<number\> | 是 | 记录插入数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示插入数据的行号。 | 1000| updated | Array\<string\> \| Array\<number\> | 是 | 记录更新数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示更新数据的行号。 | 1001| deleted | Array\<string\> \| Array\<number\> | 是 | 记录删除数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示删除数据的行号。 | 1002 1003## DistributedType<sup>10+</sup> 1004 1005描述表的分布式类型的枚举。请使用枚举名称而非枚举值。 1006 1007| 名称 | 值 | 说明 | 1008| ------------------ | --- | -------------------------------------------------------------------------------------------------- | 1009| DISTRIBUTED_DEVICE | 0 | 表示在不同设备之间分布式的数据库表。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 1010| DISTRIBUTED_CLOUD | 1 | 表示在设备和云端之间分布式的数据库表。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 1011 1012## DistributedConfig<sup>10+</sup> 1013 1014记录表的分布式配置信息。 1015 1016**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1017 1018| 名称 | 类型 | 必填 | 说明 | 1019| -------- | ------- | ---- | ------------------------------------------------------------ | 1020| autoSync | boolean | 是 | 该值为true时,表示该表支持自动同步和手动同步;该值为false时,表示该表只支持手动同步,不支持自动同步。 | 1021| asyncDownloadAsset<sup>18+</sup> | boolean | 否 | 表示当前数据库在端云同步时,同步或异步下载资产。true表示优先下载完所有数据后,使用异步任务下载资产;false表示同步下载资产;默认值为false。 | 1022| enableCloud<sup>18+</sup> | boolean | 否 | 表示当前数据库是否允许端云同步。true表示允许端云同步;false表示不允许端云同步。默认值为true。 | 1023 1024## ConflictResolution<sup>10+</sup> 1025 1026插入和修改接口的冲突解决模式。请使用枚举名称而非枚举值。 1027 1028**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1029 1030| 名称 | 值 | 说明 | 1031| -------------------- | ---- | ------------------------------------------------------------ | 1032| ON_CONFLICT_NONE | 0 | 表示当冲突发生时,不做任何处理。 | 1033| ON_CONFLICT_ROLLBACK | 1 | 表示当冲突发生时,中止SQL语句并回滚当前事务。 | 1034| ON_CONFLICT_ABORT | 2 | 表示当冲突发生时,中止当前SQL语句,并撤销当前 SQL 语句所做的任何更改,但是由同一事务中先前的 SQL 语句引起的更改被保留并且事务保持活动状态。 | 1035| ON_CONFLICT_FAIL | 3 | 表示当冲突发生时,中止当前 SQL 语句。但它不会撤销失败的 SQL 语句的先前更改,也不会结束事务。 | 1036| ON_CONFLICT_IGNORE | 4 | 表示当冲突发生时,跳过包含违反约束的行并继续处理 SQL 语句的后续行。 | 1037| ON_CONFLICT_REPLACE | 5 | 表示当冲突发生时,在插入或更新当前行之前删除导致约束违例的预先存在的行,并且命令会继续正常执行。 | 1038 1039## Progress<sup>10+</sup> 1040 1041描述端云同步过程的枚举。请使用枚举名称而非枚举值。 1042 1043**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1044 1045| 名称 | 值 | 说明 | 1046| ---------------- | ---- | ------------------------ | 1047| SYNC_BEGIN | 0 | 表示端云同步过程开始。 | 1048| SYNC_IN_PROGRESS | 1 | 表示正在端云同步过程中。 | 1049| SYNC_FINISH | 2 | 表示端云同步过程已完成。 | 1050 1051## Statistic<sup>10+</sup> 1052 1053描述数据库表的端云同步过程的统计信息。 1054 1055**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1056 1057| 名称 | 类型 | 必填 | 说明 | 1058| ---------- | ------ | ---- | ---------------------------------------- | 1059| total | number | 是 | 表示数据库表中需要端云同步的总行数。 | 1060| successful | number | 是 | 表示数据库表中端云同步成功的行数。 | 1061| failed | number | 是 | 表示数据库表中端云同步失败的行数。 | 1062| remained | number | 是 | 表示数据库表中端云同步剩余未执行的行数。 | 1063 1064## TableDetails<sup>10+</sup> 1065 1066描述数据库表执行端云同步任务上传和下载的统计信息。 1067 1068**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1069 1070| 名称 | 类型 | 必填 | 说明 | 1071| -------- | ------------------------- | ---- | ------------------------------------------ | 1072| upload | [Statistic](#statistic10) | 是 | 表示数据库表中端云同步上传过程的统计信息。 | 1073| download | [Statistic](#statistic10) | 是 | 表示数据库表中端云同步下载过程的统计信息。 | 1074 1075## ProgressCode<sup>10+</sup> 1076 1077表示端云同步过程的状态。请使用枚举名称而非枚举值。 1078 1079**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1080 1081| 名称 | 值 | 说明 | 1082| --------------------- | ---- | ------------------------------------------------------------ | 1083| SUCCESS | 0 | 表示端云同步过程成功。 | 1084| UNKNOWN_ERROR | 1 | 表示端云同步过程遇到未知错误。 | 1085| NETWORK_ERROR | 2 | 表示端云同步过程遇到网络错误。 | 1086| CLOUD_DISABLED | 3 | 表示云端不可用。 | 1087| LOCKED_BY_OTHERS | 4 | 表示有其他设备正在端云同步,本设备无法进行端云同步。<br>请确保无其他设备占用云端资源后,再使用本设备进行端云同步任务。 | 1088| RECORD_LIMIT_EXCEEDED | 5 | 表示本次端云同步需要同步的条目或大小超出最大值。由云端配置最大值。 | 1089| NO_SPACE_FOR_ASSET | 6 | 表示云空间剩余空间小于待同步的资产大小。 | 1090| BLOCKED_BY_NETWORK_STRATEGY<sup>12+</sup> | 7 | 表示端云同步被网络策略限制。 | 1091 1092## ProgressDetails<sup>10+</sup> 1093 1094描述数据库整体执行端云同步任务上传和下载的统计信息。 1095 1096**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1097 1098| 名称 | 类型 | 必填 | 说明 | 1099| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 1100| schedule | [Progress](#progress10) | 是 | 表示端云同步过程。 | 1101| code | [ProgressCode](#progresscode10) | 是 | 表示端云同步过程的状态。 | 1102| details | Record<string, [TableDetails](#tabledetails10)> | 是 | 表示端云同步各表的统计信息。<br>键表示表名,值表示该表的端云同步过程统计信息。 | 1103 1104## SqlExecutionInfo<sup>12+</sup> 1105 1106描述数据库执行的SQL语句的统计信息。 1107 1108**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1109 1110| 名称 | 类型 | 只读 | 可选 |说明 | 1111| -------- | ------------------------------------------------- | ---- | ---- | -------------------------------------------------------- | 1112| sql<sup>12+</sup> | Array<string> | 是 | 否 | 表示执行的SQL语句的数组。当[batchInsert](#batchinsert)的参数太大时,可能有多个SQL。 | 1113| totalTime<sup>12+</sup> | number | 是 | 否 | 表示执行SQL语句的总时间,单位为μs。 | 1114| waitTime<sup>12+</sup> | number | 是 | 否 | 表示获取句柄的时间,单位为μs。 | 1115| prepareTime<sup>12+</sup> | number | 是 | 否 | 表示准备SQL和绑定参数的时间,单位为μs。 | 1116| executeTime<sup>12+</sup> | number | 是 | 否 | 表示执行SQL语句的时间,单位为μs。 | 1117 1118## ExceptionMessage<sup>20+</sup> 1119 1120描述数据库执行的SQL语句的错误信息。 1121 1122**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1123 1124| 名称 | 类型 | 只读 | 可选 |说明 | 1125| -------- | ------------------------------------------------- | ---- | ---- | -------------------------------------------------------- | 1126| code<sup>20+</sup> | number | 是 | 否 | 表示执行SQL返回的错误码,对应的取值和含义请见[sqlite错误码](https://www.sqlite.org/rescode.html) | 1127| messgae<sup>20+</sup> | string | 是 | 否 | 表示执行SQL返回的错误信息。 | 1128| sql<sup>20+</sup> | string | 是 | 否 | 表示报错执行的SQL语句。 | 1129 1130## TransactionType<sup>14+</sup> 1131 1132描述创建事务对象的枚举。请使用枚举名称而非枚举值。 1133 1134**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1135 1136| 名称 | 值 | 说明 | 1137| ---------------- | ---- | ------------------------ | 1138| DEFERRED | 0 | 表示创建一个DEFERRED类型的事务对象,该类型的事务对象在创建时只会关闭自动提交而不会真正开始事务,只有在首次读或写操作时会真正开始一个读或写事务。 | 1139| IMMEDIATE | 1 | 表示创建一个IMMEDIATE类型的事务对象,该类型的事务对象在创建时会真正开始一个写事务;如果有别的写事务未提交,则会创建失败,返回错误码14800024。 | 1140| EXCLUSIVE | 2 | 表示创建一个EXCLUSIVE类型的事务对象,该类型的事务在WAL模式下和IMMEDIATE相同,但在其他日志模式下能够防止事务期间有其他连接读取数据库。 | 1141 1142## TransactionOptions<sup>14+</sup> 1143 1144事务对象的配置信息。 1145 1146**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1147 1148| 名称 | 类型 | 必填 | 说明 | 1149| ------------- | ------------- | ---- | --------------------------------------------------------- | 1150| transactionType | [TransactionType](#transactiontype14) | 否 | 事务类型。默认为DEFERRED。 | 1151 1152## ColumnType<sup>18+</sup> 1153 1154描述数据库列存储类型的枚举。请使用枚举名称而非枚举值。 1155 1156**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1157 1158| 名称 | 值 | 说明 | 1159| ------------- | ---- | ------------------------------------------------------------ | 1160| NULL | 0 | 表示列数据类型为NULL。 | 1161| INTEGER | 1 | 表示列数据类型为64位整数。可用于保存8位(包括布尔值)、16位、32位、64位整数。如果64位整数大于2^53或小于-2^53,需使用[getString](#getstring)将64位整数转换为字符串。 | 1162| REAL | 2 | 表示列类型为浮点数。 | 1163| TEXT | 3 | 表示列类型为字符串。 | 1164| BLOB | 4 | 表示列类型为Uint8Array。 | 1165| ASSET | 5 | 表示列类型为[Asset](#asset10)。 | 1166| ASSETS | 6 | 表示列类型为[Assets](#assets10)。 | 1167| FLOAT_VECTOR | 7 | 表示列类型为Float32Array。 | 1168| UNLIMITED_INT | 8 | 表示列类型为bigint。 | 1169 1170## RdbPredicates 1171 1172表示关系型数据库(RDB)的谓词。该类确定RDB中条件表达式的值是true还是false。谓词间支持多语句拼接,拼接时默认使用and()连接。不支持Sendable跨线程传递。 1173 1174### constructor 1175 1176constructor(name: string) 1177 1178构造函数。 1179 1180**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1181 1182**参数:** 1183 1184| 参数名 | 类型 | 必填 | 说明 | 1185| ------ | ------ | ---- | ------------ | 1186| name | string | 是 | 数据库表名。 | 1187 1188**错误码:** 1189 1190以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1191 1192| **错误码ID** | **错误信息** | 1193| --------- |----------------------------------------------------------------------------------------------------------------| 1194| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1195 1196**示例:** 1197 1198```ts 1199let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1200``` 1201 1202### inDevices 1203 1204inDevices(devices: Array<string>): RdbPredicates 1205 1206同步分布式数据库时连接到组网内指定的远程设备。 1207 1208> **说明:** 1209> 1210> 其中devices通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 1211数据库同步时调用Sync接口,需要在入参谓词中调用inDevices接口选择设备。如果不调用inDevices接口即默认连接组网内所有的设备。 1212 1213**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1214 1215**参数:** 1216 1217| 参数名 | 类型 | 必填 | 说明 | 1218| ------- | ------------------- | ---- | -------------------------- | 1219| devices | Array<string> | 是 | 指定的组网内的远程设备ID。 | 1220 1221**返回值**: 1222 1223| 类型 | 说明 | 1224| ------------------------------------ | -------------------------- | 1225| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1226 1227**错误码:** 1228 1229以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1230 1231| **错误码ID** | **错误信息** | 1232| --------- |----------------------------------------------------------------------------------------------------------------| 1233| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1234 1235**示例:** 1236 1237```ts 1238import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 1239import { BusinessError } from '@kit.BasicServicesKit'; 1240 1241let dmInstance: distributedDeviceManager.DeviceManager; 1242let deviceIds: Array<string> = []; 1243 1244try { 1245 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 1246 let devices: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 1247 for (let i = 0; i < devices.length; i++) { 1248 deviceIds[i] = devices[i].networkId!; 1249 } 1250} catch (err) { 1251 let code = (err as BusinessError).code; 1252 let message = (err as BusinessError).message; 1253 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 1254} 1255 1256let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1257predicates.inDevices(deviceIds); 1258``` 1259 1260### inAllDevices 1261 1262inAllDevices(): RdbPredicates 1263 1264同步分布式数据库时连接到组网内所有的远程设备。 1265 1266 1267**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1268 1269**返回值**: 1270 1271| 类型 | 说明 | 1272| ------------------------------------ | -------------------------- | 1273| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1274 1275**示例:** 1276 1277```ts 1278let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1279predicates.inAllDevices(); 1280``` 1281 1282### equalTo 1283 1284equalTo(field: string, value: ValueType): RdbPredicates 1285 1286配置谓词以匹配数据表的field列中值为value的字段。 1287 1288**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1289 1290**参数:** 1291 1292| 参数名 | 类型 | 必填 | 说明 | 1293| ------ | ----------------------- | ---- | ---------------------- | 1294| field | string | 是 | 数据库表中的列名。 | 1295| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 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 1314// 匹配数据表的"NAME"列中值为"Lisa"的字段 1315let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1316predicates.equalTo("NAME", "Lisa"); 1317``` 1318 1319 1320### notEqualTo 1321 1322notEqualTo(field: string, value: ValueType): RdbPredicates 1323 1324配置谓词以匹配数据表的field列中值不为value的字段。 1325 1326**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1327 1328**参数:** 1329 1330| 参数名 | 类型 | 必填 | 说明 | 1331| ------ | ----------------------- | ---- | ---------------------- | 1332| field | string | 是 | 数据库表中的列名。 | 1333| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1334 1335**返回值**: 1336 1337| 类型 | 说明 | 1338| ------------------------------------ | -------------------------- | 1339| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1340 1341**错误码:** 1342 1343以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1344 1345| **错误码ID** | **错误信息** | 1346| --------- |----------------------------------------------------------------------------------------------------------------| 1347| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1348 1349**示例:** 1350 1351```ts 1352// 匹配数据表的"NAME"列中值不为"Lisa"的字段 1353let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1354predicates.notEqualTo("NAME", "Lisa"); 1355``` 1356 1357 1358### beginWrap 1359 1360beginWrap(): RdbPredicates 1361 1362向谓词添加左括号。 1363 1364**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1365 1366**返回值**: 1367 1368| 类型 | 说明 | 1369| ------------------------------------ | ------------------------- | 1370| [RdbPredicates](#rdbpredicates) | 返回带有左括号的Rdb谓词。 | 1371 1372**示例:** 1373 1374```ts 1375let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1376predicates.equalTo("NAME", "Lisa") 1377 .beginWrap() 1378 .equalTo("AGE", 18) 1379 .or() 1380 .equalTo("SALARY", 200.5) 1381 .endWrap(); 1382``` 1383 1384### endWrap 1385 1386endWrap(): RdbPredicates 1387 1388向谓词添加右括号。 1389 1390**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1391 1392**返回值**: 1393 1394| 类型 | 说明 | 1395| ------------------------------------ | ------------------------- | 1396| [RdbPredicates](#rdbpredicates) | 返回带有右括号的Rdb谓词。 | 1397 1398**示例:** 1399 1400```ts 1401let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1402predicates.equalTo("NAME", "Lisa") 1403 .beginWrap() 1404 .equalTo("AGE", 18) 1405 .or() 1406 .equalTo("SALARY", 200.5) 1407 .endWrap(); 1408``` 1409 1410### or 1411 1412or(): RdbPredicates 1413 1414将或条件添加到谓词中。 1415 1416**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1417 1418**返回值**: 1419 1420| 类型 | 说明 | 1421| ------------------------------------ | ------------------------- | 1422| [RdbPredicates](#rdbpredicates) | 返回带有或条件的Rdb谓词。 | 1423 1424**示例:** 1425 1426```ts 1427// 匹配数据表的"NAME"列中值为"Lisa"或"Rose"的字段 1428let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1429predicates.equalTo("NAME", "Lisa") 1430 .or() 1431 .equalTo("NAME", "Rose"); 1432``` 1433 1434### and 1435 1436and(): RdbPredicates 1437 1438向谓词添加和条件。 1439 1440**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1441 1442**返回值**: 1443 1444| 类型 | 说明 | 1445| ------------------------------------ | ------------------------- | 1446| [RdbPredicates](#rdbpredicates) | 返回带有和条件的Rdb谓词。 | 1447 1448**示例:** 1449 1450```ts 1451// 匹配数据表的"NAME"列中值为"Lisa"且"SALARY"列中值为"200.5"的字段 1452let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1453predicates.equalTo("NAME", "Lisa") 1454 .and() 1455 .equalTo("SALARY", 200.5); 1456``` 1457 1458### contains 1459 1460contains(field: string, value: string): RdbPredicates 1461 1462配置谓词以匹配数据表的field列中包含value的字段。 1463 1464**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1465 1466**参数:** 1467 1468| 参数名 | 类型 | 必填 | 说明 | 1469| ------ | ------ | ---- | ---------------------- | 1470| field | string | 是 | 数据库表中的列名。 | 1471| value | string | 是 | 指示要与谓词匹配的值。 | 1472 1473**返回值**: 1474 1475| 类型 | 说明 | 1476| ------------------------------------ | -------------------------- | 1477| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1478 1479**错误码:** 1480 1481以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1482 1483| **错误码ID** | **错误信息** | 1484| --------- |----------------------------------------------------------------------------------------------------------------| 1485| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1486 1487**示例:** 1488 1489```ts 1490// 匹配数据表的"NAME"列中包含"os"的字段,如"Rose" 1491let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1492predicates.contains("NAME", "os"); 1493``` 1494 1495### beginsWith 1496 1497beginsWith(field: string, value: string): RdbPredicates 1498 1499配置谓词以匹配数据表的field列中以value开头的字段。 1500 1501**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1502 1503**参数:** 1504 1505| 参数名 | 类型 | 必填 | 说明 | 1506| ------ | ------ | ---- | ---------------------- | 1507| field | string | 是 | 数据库表中的列名。 | 1508| value | string | 是 | 指示要与谓词匹配的值。 | 1509 1510**返回值**: 1511 1512| 类型 | 说明 | 1513| ------------------------------------ | -------------------------- | 1514| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1515 1516**错误码:** 1517 1518以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1519 1520| **错误码ID** | **错误信息** | 1521| --------- |----------------------------------------------------------------------------------------------------------------| 1522| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1523 1524**示例:** 1525 1526```ts 1527// 匹配数据表的"NAME"列中以"Li"开头的字段,如"Lisa" 1528let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1529predicates.beginsWith("NAME", "Li"); 1530``` 1531 1532### endsWith 1533 1534endsWith(field: string, value: string): RdbPredicates 1535 1536配置谓词以匹配数据表的field列中以value结尾的字段。 1537 1538**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1539 1540**参数:** 1541 1542| 参数名 | 类型 | 必填 | 说明 | 1543| ------ | ------ | ---- | ---------------------- | 1544| field | string | 是 | 数据库表中的列名。 | 1545| value | string | 是 | 指示要与谓词匹配的值。 | 1546 1547**返回值**: 1548 1549| 类型 | 说明 | 1550| ------------------------------------ | -------------------------- | 1551| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1552 1553**错误码:** 1554 1555以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1556 1557| **错误码ID** | **错误信息** | 1558| --------- |----------------------------------------------------------------------------------------------------------------| 1559| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1560 1561**示例:** 1562 1563```ts 1564// 匹配数据表的"NAME"列中以"se"结尾的字段,如"Rose" 1565let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1566predicates.endsWith("NAME", "se"); 1567``` 1568 1569### isNull 1570 1571isNull(field: string): RdbPredicates 1572 1573配置谓词以匹配数据表的field列中值为null的字段。 1574 1575**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1576 1577**参数:** 1578 1579| 参数名 | 类型 | 必填 | 说明 | 1580| ------ | ------ | ---- | ------------------ | 1581| field | string | 是 | 数据库表中的列名。 | 1582 1583**返回值**: 1584 1585| 类型 | 说明 | 1586| ------------------------------------ | -------------------------- | 1587| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1588 1589**错误码:** 1590 1591以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1592 1593| **错误码ID** | **错误信息** | 1594| --------- |----------------------------------------------------------------------------------------------------------------| 1595| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1596 1597**示例**: 1598 1599```ts 1600let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1601predicates.isNull("NAME"); 1602``` 1603 1604### isNotNull 1605 1606isNotNull(field: string): RdbPredicates 1607 1608配置谓词以匹配数据表的field列中值不为null的字段。 1609 1610**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1611 1612**参数:** 1613 1614| 参数名 | 类型 | 必填 | 说明 | 1615| ------ | ------ | ---- | ------------------ | 1616| field | string | 是 | 数据库表中的列名。 | 1617 1618**返回值**: 1619 1620| 类型 | 说明 | 1621| ------------------------------------ | -------------------------- | 1622| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1623 1624**错误码:** 1625 1626以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1627 1628| **错误码ID** | **错误信息** | 1629| --------- |----------------------------------------------------------------------------------------------------------------| 1630| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1631 1632**示例:** 1633 1634```ts 1635let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1636predicates.isNotNull("NAME"); 1637``` 1638 1639### like 1640 1641like(field: string, value: string): RdbPredicates 1642 1643配置谓词以匹配数据表的field列中值类似于value的字段。 1644 1645**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1646 1647**参数:** 1648 1649| 参数名 | 类型 | 必填 | 说明 | 1650| ------ | ------ | ---- | ---------------------- | 1651| field | string | 是 | 数据库表中的列名。 | 1652| value | string | 是 | 指示要与谓词匹配的值。 | 1653 1654**返回值**: 1655 1656| 类型 | 说明 | 1657| ------------------------------------ | -------------------------- | 1658| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1659 1660**错误码:** 1661 1662以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1663 1664| **错误码ID** | **错误信息** | 1665| --------- |----------------------------------------------------------------------------------------------------------------| 1666| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1667 1668**示例:** 1669 1670```ts 1671// 匹配数据表的"NAME"列中值类似于"os"的字段,如"Rose" 1672let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1673predicates.like("NAME", "%os%"); 1674``` 1675 1676### glob 1677 1678glob(field: string, value: string): RdbPredicates 1679 1680配置谓词匹配数据字段为string的指定字段。 1681 1682**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1683 1684**参数:** 1685 1686| 参数名 | 类型 | 必填 | 说明 | 1687| ------ | ------ | ---- | ------------------------------------------------------------ | 1688| field | string | 是 | 数据库表中的列名。 | 1689| value | string | 是 | 指示要与谓词匹配的值。<br>支持通配符,*表示0个、1个或多个数字或字符,?表示1个数字或字符。 | 1690 1691**返回值**: 1692 1693| 类型 | 说明 | 1694| ------------------------------------ | -------------------------- | 1695| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1696 1697**错误码:** 1698 1699以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1700 1701| **错误码ID** | **错误信息** | 1702| --------- |----------------------------------------------------------------------------------------------------------------| 1703| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1704 1705**示例:** 1706 1707```ts 1708// 匹配数据表的"NAME"列中类型为string且值为"?h*g"的字段 1709let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1710predicates.glob("NAME", "?h*g"); 1711``` 1712 1713### between 1714 1715between(field: string, low: ValueType, high: ValueType): RdbPredicates 1716 1717配置谓词以匹配数据表的field列中值在给定范围内的字段(包含范围边界)。 1718 1719**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1720 1721**参数:** 1722 1723| 参数名 | 类型 | 必填 | 说明 | 1724| ------ | ----------------------- | ---- | -------------------------- | 1725| field | string | 是 | 数据库表中的列名。 | 1726| low | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最小值。 | 1727| high | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最大值。 | 1728 1729**返回值**: 1730 1731| 类型 | 说明 | 1732| ------------------------------------ | -------------------------- | 1733| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1734 1735**错误码:** 1736 1737以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1738 1739| **错误码ID** | **错误信息** | 1740| --------- |----------------------------------------------------------------------------------------------------------------| 1741| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1742 1743**示例:** 1744 1745```ts 1746// 匹配数据表的"AGE"列中大于等于10且小于等于50的值 1747let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1748predicates.between("AGE", 10, 50); 1749``` 1750 1751### notBetween 1752 1753notBetween(field: string, low: ValueType, high: ValueType): RdbPredicates 1754 1755配置谓词以匹配数据表的field列中值超出给定范围的字段(不包含范围边界)。 1756 1757**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1758 1759**参数:** 1760 1761| 参数名 | 类型 | 必填 | 说明 | 1762| ------ | ----------------------- | ---- | -------------------------- | 1763| field | string | 是 | 数据库表中的列名。 | 1764| low | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最小值。 | 1765| high | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的最大值。 | 1766 1767**返回值**: 1768 1769| 类型 | 说明 | 1770| ------------------------------------ | -------------------------- | 1771| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1772 1773**错误码:** 1774 1775以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1776 1777| **错误码ID** | **错误信息** | 1778| --------- |----------------------------------------------------------------------------------------------------------------| 1779| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1780 1781**示例:** 1782 1783```ts 1784// 匹配数据表的"AGE"列中小于10或大于50的值 1785let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1786predicates.notBetween("AGE", 10, 50); 1787``` 1788 1789### greaterThan 1790 1791greaterThan(field: string, value: ValueType): RdbPredicates 1792 1793配置谓词以匹配数据表的field列中值大于value的字段。 1794 1795**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1796 1797**参数:** 1798 1799| 参数名 | 类型 | 必填 | 说明 | 1800| ------ | ----------------------- | ---- | ---------------------- | 1801| field | string | 是 | 数据库表中的列名。 | 1802| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1803 1804**返回值**: 1805 1806| 类型 | 说明 | 1807| ------------------------------------ | -------------------------- | 1808| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1809 1810**错误码:** 1811 1812以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1813 1814| **错误码ID** | **错误信息** | 1815| --------- |----------------------------------------------------------------------------------------------------------------| 1816| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1817 1818**示例:** 1819 1820```ts 1821// 匹配数据表的"AGE"列中大于18的值 1822let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1823predicates.greaterThan("AGE", 18); 1824``` 1825 1826### lessThan 1827 1828lessThan(field: string, value: ValueType): RdbPredicates 1829 1830配置谓词以匹配数据表的field列中值小于value的字段。 1831 1832**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1833 1834**参数:** 1835 1836| 参数名 | 类型 | 必填 | 说明 | 1837| ------ | ----------------------- | ---- | ---------------------- | 1838| field | string | 是 | 数据库表中的列名。 | 1839| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1840 1841**返回值**: 1842 1843| 类型 | 说明 | 1844| ------------------------------------ | -------------------------- | 1845| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1846 1847**错误码:** 1848 1849以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1850 1851| **错误码ID** | **错误信息** | 1852| --------- |----------------------------------------------------------------------------------------------------------------| 1853| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1854 1855**示例:** 1856 1857```ts 1858// 匹配数据表的"AGE"列中小于20的值 1859let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1860predicates.lessThan("AGE", 20); 1861``` 1862 1863### greaterThanOrEqualTo 1864 1865greaterThanOrEqualTo(field: string, value: ValueType): RdbPredicates 1866 1867配置谓词以匹配数据表的field列中值大于或者等于value的字段。 1868 1869**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1870 1871**参数:** 1872 1873| 参数名 | 类型 | 必填 | 说明 | 1874| ------ | ----------------------- | ---- | ---------------------- | 1875| field | string | 是 | 数据库表中的列名。 | 1876| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1877 1878**返回值**: 1879 1880| 类型 | 说明 | 1881| ------------------------------------ | -------------------------- | 1882| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1883 1884**错误码:** 1885 1886以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1887 1888| **错误码ID** | **错误信息** | 1889| --------- |----------------------------------------------------------------------------------------------------------------| 1890| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1891 1892**示例:** 1893 1894```ts 1895// 匹配数据表的"AGE"列中大于等于18的值 1896let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1897predicates.greaterThanOrEqualTo("AGE", 18); 1898``` 1899 1900### lessThanOrEqualTo 1901 1902lessThanOrEqualTo(field: string, value: ValueType): RdbPredicates 1903 1904配置谓词以匹配数据表的field列中值小于或者等于value的字段。 1905 1906**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1907 1908**参数:** 1909 1910| 参数名 | 类型 | 必填 | 说明 | 1911| ------ | ----------------------- | ---- | ---------------------- | 1912| field | string | 是 | 数据库表中的列名。 | 1913| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1914 1915**返回值**: 1916 1917| 类型 | 说明 | 1918| ------------------------------------ | -------------------------- | 1919| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1920 1921**错误码:** 1922 1923以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1924 1925| **错误码ID** | **错误信息** | 1926| --------- |----------------------------------------------------------------------------------------------------------------| 1927| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1928 1929**示例:** 1930 1931```ts 1932// 匹配数据表的"AGE"列中小于等于20的值 1933let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1934predicates.lessThanOrEqualTo("AGE", 20); 1935``` 1936 1937### orderByAsc 1938 1939orderByAsc(field: string): RdbPredicates 1940 1941配置谓词以匹配数据表的field列中值按升序排序的列。 1942 1943**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1944 1945**参数:** 1946 1947| 参数名 | 类型 | 必填 | 说明 | 1948| ------ | ------ | ---- | ------------------ | 1949| field | string | 是 | 数据库表中的列名。 | 1950 1951**返回值**: 1952 1953| 类型 | 说明 | 1954| ------------------------------------ | -------------------------- | 1955| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1956 1957**错误码:** 1958 1959以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1960 1961| **错误码ID** | **错误信息** | 1962| --------- |----------------------------------------------------------------------------------------------------------------| 1963| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1964 1965**示例:** 1966 1967```ts 1968let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1969predicates.orderByAsc("NAME"); 1970``` 1971 1972### orderByDesc 1973 1974orderByDesc(field: string): RdbPredicates 1975 1976配置谓词以匹配数据表的field列中值按降序排序的列。 1977 1978**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1979 1980**参数:** 1981 1982| 参数名 | 类型 | 必填 | 说明 | 1983| ------ | ------ | ---- | ------------------ | 1984| field | string | 是 | 数据库表中的列名。 | 1985 1986**返回值**: 1987 1988| 类型 | 说明 | 1989| ------------------------------------ | -------------------------- | 1990| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1991 1992**错误码:** 1993 1994以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1995 1996| **错误码ID** | **错误信息** | 1997| --------- |----------------------------------------------------------------------------------------------------------------| 1998| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1999 2000**示例:** 2001 2002```ts 2003let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2004predicates.orderByDesc("AGE"); 2005``` 2006 2007### distinct 2008 2009distinct(): RdbPredicates 2010 2011配置谓词以过滤重复记录并仅保留其中一个。 2012 2013**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2014 2015**返回值**: 2016 2017| 类型 | 说明 | 2018| ------------------------------------ | ------------------------------ | 2019| [RdbPredicates](#rdbpredicates) | 返回可用于过滤重复记录的谓词。 | 2020 2021**示例:** 2022 2023```ts 2024let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2025predicates.equalTo("NAME", "Rose").distinct(); // 对NAME列值为Rose的结果集去重 2026``` 2027 2028### limitAs 2029 2030limitAs(value: number): RdbPredicates 2031 2032设置谓词的最大数据记录数量。 2033 2034**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2035 2036**参数:** 2037 2038| 参数名 | 类型 | 必填 | 说明 | 2039| ------ | ------ | ---- | ---------------- | 2040| value | number | 是 | 最大数据记录数,取值应为正整数,传入值小于等于0时,不会限制记录数量。 | 2041 2042**返回值**: 2043 2044| 类型 | 说明 | 2045| ------------------------------------ | ------------------------------------ | 2046| [RdbPredicates](#rdbpredicates) | 返回可用于设置最大数据记录数的谓词。 | 2047 2048**错误码:** 2049 2050以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2051 2052| **错误码ID** | **错误信息** | 2053| --------- |--------------------------| 2054| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2055 2056**示例:** 2057 2058```ts 2059let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2060predicates.equalTo("NAME", "Rose").limitAs(3); 2061``` 2062 2063### offsetAs 2064 2065offsetAs(rowOffset: number): RdbPredicates 2066 2067设置谓词查询结果返回的起始位置。需要同步调用limitAs接口指定查询数量,否则将无查询结果。如需查询指定偏移位置后的所有行,limitAs接口入参需小于等于0。 2068 2069**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2070 2071**参数:** 2072 2073| 参数名 | 类型 | 必填 | 说明 | 2074| --------- | ------ | ---- | ---------------------------------- | 2075| rowOffset | number | 是 | 指定查询结果的起始位置,默认初始位置为结果集的最前端。当rowOffset为负数时,起始位置为结果集的最前端。当rowOffset超出结果集最后位置时,查询结果为空。 | 2076 2077**返回值**: 2078 2079| 类型 | 说明 | 2080| ------------------------------------ | ------------------------------------ | 2081| [RdbPredicates](#rdbpredicates) | 返回具有指定返回结果起始位置的谓词。 | 2082 2083**错误码:** 2084 2085以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2086 2087| **错误码ID** | **错误信息** | 2088| --------- |----------------------------------------------------------------------------------------------------------------| 2089| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2090 2091**示例:** 2092 2093```ts 2094let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2095predicates.equalTo("NAME", "Rose").limitAs(-1).offsetAs(3); 2096``` 2097 2098### groupBy 2099 2100groupBy(fields: Array<string>): RdbPredicates 2101 2102配置谓词按指定列分组查询结果。 2103 2104**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2105 2106**参数:** 2107 2108| 参数名 | 类型 | 必填 | 说明 | 2109| ------ | ------------------- | ---- | -------------------- | 2110| fields | Array<string> | 是 | 指定分组依赖的列名。 | 2111 2112**返回值**: 2113 2114| 类型 | 说明 | 2115| ------------------------------------ | ---------------------- | 2116| [RdbPredicates](#rdbpredicates) | 返回分组查询列的谓词。 | 2117 2118**错误码:** 2119 2120以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2121 2122| **错误码ID** | **错误信息** | 2123| --------- |----------------------------------------------------------------------------------------------------------------| 2124| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2125 2126**示例:** 2127 2128```ts 2129let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2130predicates.groupBy(["AGE", "NAME"]); 2131``` 2132 2133### indexedBy 2134 2135indexedBy(field: string): RdbPredicates 2136 2137配置谓词以指定索引列。 2138 2139**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2140 2141**参数:** 2142 2143| 参数名 | 类型 | 必填 | 说明 | 2144| ------ | ------ | ---- | -------------- | 2145| field | string | 是 | 索引列的名称。 | 2146 2147**返回值**: 2148 2149 2150| 类型 | 说明 | 2151| ------------------------------------ | ------------------------------------- | 2152| [RdbPredicates](#rdbpredicates) | 返回具有指定索引列的RdbPredicates。 | 2153 2154**错误码:** 2155 2156以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2157 2158| **错误码ID** | **错误信息** | 2159| --------- |----------------------------------------------------------------------------------------------------------------| 2160| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2161 2162**示例:** 2163 2164```ts 2165let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2166predicates.indexedBy("SALARY"); 2167``` 2168 2169### in 2170 2171in(field: string, value: Array<ValueType>): RdbPredicates 2172 2173配置谓词以匹配数据表的field列中值在给定范围内的字段。 2174 2175**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2176 2177**参数:** 2178 2179| 参数名 | 类型 | 必填 | 说明 | 2180| ------ | ------------------------------------ | ---- | --------------------------------------- | 2181| field | string | 是 | 数据库表中的列名。 | 2182| value | Array<[ValueType](#valuetype)> | 是 | 以ValueType型数组形式指定的要匹配的值。 | 2183 2184**返回值**: 2185 2186| 类型 | 说明 | 2187| ------------------------------------ | -------------------------- | 2188| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 2189 2190**错误码:** 2191 2192以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2193 2194| **错误码ID** | **错误信息** | 2195| --------- |----------------------------------------------------------------------------------------------------------------| 2196| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2197 2198**示例:** 2199 2200```ts 2201// 匹配数据表的"AGE"列中在[18,20]中的值 2202let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2203predicates.in("AGE", [18, 20]); 2204``` 2205 2206### notIn 2207 2208notIn(field: string, value: Array<ValueType>): RdbPredicates 2209 2210将谓词配置为匹配数据字段为ValueType且值超出给定范围的指定字段。 2211 2212**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2213 2214**参数:** 2215 2216| 参数名 | 类型 | 必填 | 说明 | 2217| ------ | ------------------------------------ | ---- | ------------------------------------- | 2218| field | string | 是 | 数据库表中的列名。 | 2219| value | Array<[ValueType](#valuetype)> | 是 | 以ValueType数组形式指定的要匹配的值。 | 2220 2221**返回值**: 2222 2223| 类型 | 说明 | 2224| ------------------------------------ | -------------------------- | 2225| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 2226 2227**错误码:** 2228 2229以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2230 2231| **错误码ID** | **错误信息** | 2232| --------- |----------------------------------------------------------------------------------------------------------------| 2233| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2234 2235**示例:** 2236 2237```ts 2238// 匹配数据表的"NAME"列中不在["Lisa", "Rose"]中的值 2239let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2240predicates.notIn("NAME", ["Lisa", "Rose"]); 2241``` 2242 2243### notContains<sup>12+</sup> 2244 2245notContains(field: string, value: string): RdbPredicates 2246 2247配置谓词以匹配数据表的field列中不包含value的字段。 2248 2249**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2250 2251**参数:** 2252 2253| 参数名 | 类型 | 必填 | 说明 | 2254| ------ | ------ | ---- | ---------------------- | 2255| field | string | 是 | 数据库表中的列名。 | 2256| value | string | 是 | 指示要与谓词匹配的值。 | 2257 2258**返回值**: 2259 2260| 类型 | 说明 | 2261| ------------------------------- | -------------------------- | 2262| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 2263 2264**错误码:** 2265 2266以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2267 2268| **错误码ID** | **错误信息** | 2269| --------- |----------------------------------------------------------------------------------------------------------------| 2270| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2271 2272**示例:** 2273 2274```ts 2275// 匹配数据表的"NAME"列中不包含"os"的字段,如列表中的"Lisa" 2276let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2277predicates.notContains("NAME", "os"); 2278``` 2279 2280### notLike<sup>12+</sup> 2281 2282notLike(field: string, value: string): RdbPredicates 2283 2284配置谓词以匹配数据表的field列中值不存在类似于value的字段。 2285 2286**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2287 2288**参数:** 2289 2290| 参数名 | 类型 | 必填 | 说明 | 2291| ------ | ------ | ---- | ---------------------- | 2292| field | string | 是 | 数据库表中的列名。 | 2293| value | string | 是 | 指示要与谓词匹配的值。 | 2294 2295**返回值**: 2296 2297| 类型 | 说明 | 2298| ------------------------------- | -------------------------- | 2299| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 2300 2301**错误码:** 2302 2303以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2304 2305| **错误码ID** | **错误信息** | 2306| --------- |----------------------------------------------------------------------------------------------------------------| 2307| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2308 2309**示例:** 2310 2311```ts 2312// 匹配数据表的"NAME"列中不等于"os"的字段,如列表中的"Rose" 2313let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2314predicates.notLike("NAME", "os"); 2315``` 2316 2317### having<sup>20+</sup> 2318 2319having(conditions:string, args?: Array\<ValueType>): RdbPredicates 2320 2321筛选符合条件的分组数据。 2322 2323**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2324 2325**参数:** 2326 2327| 参数名 | 类型 | 必填 | 说明 | 2328| ------ | ------ | ---- | ---------------------- | 2329| conditions | string | 是 | 用于过滤使用[groupBy](#groupby)获得的数据,不能为空且必须与[groupBy](#groupby)配合使用。 2330| args | Array<[ValueType](#valuetype)> | 否 | 条件中使用的参数,用来替换条件语句中的占位符,不传时默认为空数组。 | 2331 2332**返回值**: 2333 2334| 类型 | 说明 | 2335| ------------------------------- | -------------------------- | 2336| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 2337 2338**错误码:** 2339 2340以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2341 2342| **错误码ID** | **错误信息** | 2343| --------- |----------------------------------------------------------------------------------------------------------------| 2344| 14800001 | Invalid args. Possible causes: 1. conditions are empty; 2. missing GROUP BY clause. | 2345 2346**示例1:** 2347 2348```ts 2349// 传递完整的条件 2350let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2351predicates.groupBy(["AGE"]); 2352predicates.having("NAME = zhangsan"); 2353``` 2354**示例2:** 2355 2356```ts 2357// 条件中使用占位符替代,args参数传入替换占位符的值 2358let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2359predicates.groupBy(["AGE"]); 2360predicates.having("NAME = ?", ["zhangsan"]); 2361``` 2362 2363## RdbStore 2364 2365提供管理关系数据库(RDB)方法的接口。 2366 2367在使用以下相关接口前,请使用[executeSql](#executesql)接口初始化数据库表结构和相关数据。 2368 2369### 属性 2370 2371**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2372 2373| 名称 | 类型 | 只读 | 可选 | 说明 | 2374| ------------ | ----------- | ---- | -------------------------------- | -------------------------------- | 2375| version<sup>10+</sup> | number | 否 | 否 | 设置和获取数据库版本,值为大于0的正整数。 | 2376| rebuilt<sup>12+</sup> | [RebuildType](#rebuildtype12) | 是 | 否 | 用于获取数据库是否进行过重建或修复。 | 2377 2378**错误码:** 2379 2380以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 2381 2382| **错误码ID** | **错误信息** | 2383|-----------| ------------------------------------------------------------ | 2384| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2385| 801 | Capability not supported. | 2386| 14800000 | Inner error. | 2387| 14800014 | The RdbStore or ResultSet is already closed. | 2388| 14800015 | The database does not respond. | 2389| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 2390| 14800023 | SQLite: Access permission denied. | 2391| 14800024 | SQLite: The database file is locked. | 2392| 14800025 | SQLite: A table in the database is locked. | 2393| 14800026 | SQLite: The database is out of memory. | 2394| 14800027 | SQLite: Attempt to write a readonly database. | 2395| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2396| 14800029 | SQLite: The database is full. | 2397| 14800030 | SQLite: Unable to open the database file. | 2398 2399**示例:** 2400 2401```ts 2402// 设置数据库版本 2403import { UIAbility } from '@kit.AbilityKit'; 2404import { BusinessError } from '@kit.BasicServicesKit'; 2405import { window } from '@kit.ArkUI'; 2406 2407let store: relationalStore.RdbStore | undefined = undefined; 2408 2409class EntryAbility extends UIAbility { 2410 onWindowStageCreate(windowStage: window.WindowStage) { 2411 const STORE_CONFIG: relationalStore.StoreConfig = { 2412 name: "RdbTest.db", 2413 securityLevel: relationalStore.SecurityLevel.S3 2414 }; 2415 const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB, IDENTITY UNLIMITED INT, ASSETDATA ASSET, ASSETSDATA ASSETS, FLOATARRAY floatvector(128))'; 2416 relationalStore.getRdbStore(this.context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => { 2417 store = rdbStore; 2418 await (store as relationalStore.RdbStore).executeSql(SQL_CREATE_TABLE); 2419 console.info('Get RdbStore successfully.'); 2420 }).catch((err: BusinessError) => { 2421 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 2422 }); 2423 2424 // 设置数据库版本 2425 if (store != undefined) { 2426 (store as relationalStore.RdbStore).version = 3; 2427 // 获取数据库版本 2428 console.info(`RdbStore version is ${store.version}`); 2429 // 获取数据库是否重建 2430 console.info(`RdbStore rebuilt is ${store.rebuilt}`); 2431 } 2432 } 2433} 2434``` 2435 2436### insert 2437 2438insert(table: string, values: ValuesBucket, callback: AsyncCallback<number>):void 2439 2440向目标表中插入一行数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2441 2442**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2443 2444**参数:** 2445 2446| 参数名 | 类型 | 必填 | 说明 | 2447| -------- | ----------------------------- | ---- | ---------------------------------------------------------- | 2448| table | string | 是 | 指定的目标表名。 | 2449| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 2450| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回行ID;否则返回-1。 | 2451 2452**错误码:** 2453 2454以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2455 2456| **错误码ID** | **错误信息** | 2457|-----------| ------------------------------------------------------------ | 2458| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2459| 14800000 | Inner error. | 2460| 14800011 | Failed to open the database because it is corrupted. | 2461| 14800014 | The RdbStore or ResultSet is already closed. | 2462| 14800015 | The database does not respond. | 2463| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 2464| 14800022 | SQLite: Callback routine requested an abort. | 2465| 14800023 | SQLite: Access permission denied. | 2466| 14800024 | SQLite: The database file is locked. | 2467| 14800025 | SQLite: A table in the database is locked. | 2468| 14800026 | SQLite: The database is out of memory. | 2469| 14800027 | SQLite: Attempt to write a readonly database. | 2470| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2471| 14800029 | SQLite: The database is full. | 2472| 14800030 | SQLite: Unable to open the database file. | 2473| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2474| 14800032 | SQLite: Abort due to constraint violation. | 2475| 14800033 | SQLite: Data type mismatch. | 2476| 14800034 | SQLite: Library used incorrectly. | 2477| 14800047 | The WAL file size exceeds the default limit. | 2478 2479**示例:** 2480 2481```ts 2482let value1 = "Lisa"; 2483let value2 = 18; 2484let value3 = 100.5; 2485let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2486 2487// 以下三种方式可用 2488const valueBucket1: relationalStore.ValuesBucket = { 2489 'NAME': value1, 2490 'AGE': value2, 2491 'SALARY': value3, 2492 'CODES': value4 2493}; 2494const valueBucket2: relationalStore.ValuesBucket = { 2495 NAME: value1, 2496 AGE: value2, 2497 SALARY: value3, 2498 CODES: value4 2499}; 2500const valueBucket3: relationalStore.ValuesBucket = { 2501 "NAME": value1, 2502 "AGE": value2, 2503 "SALARY": value3, 2504 "CODES": value4 2505}; 2506 2507if (store != undefined) { 2508 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1, (err: BusinessError, rowId: number) => { 2509 if (err) { 2510 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 2511 return; 2512 } 2513 console.info(`Insert is successful, rowId = ${rowId}`); 2514 }); 2515} 2516``` 2517 2518### insert<sup>10+</sup> 2519 2520insert(table: string, values: ValuesBucket, conflict: ConflictResolution, callback: AsyncCallback<number>):void 2521 2522向目标表中插入一行数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2523 2524**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2525 2526**参数:** 2527 2528| 参数名 | 类型 | 必填 | 说明 | 2529| -------- | ------------------------------------------- | ---- | ---------------------------------------------------------- | 2530| table | string | 是 | 指定的目标表名。 | 2531| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 2532| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。 | 2533| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回行ID;否则返回-1。 | 2534 2535**错误码:** 2536 2537以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2538 2539| **错误码ID** | **错误信息** | 2540|-----------| ---------------------------------------------------- | 2541| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2542| 14800000 | Inner error. | 2543| 14800011 | Failed to open the database because it is corrupted. | 2544| 14800014 | The RdbStore or ResultSet is already closed. | 2545| 14800015 | The database does not respond. | 2546| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 2547| 14800022 | SQLite: Callback routine requested an abort. | 2548| 14800023 | SQLite: Access permission denied. | 2549| 14800024 | SQLite: The database file is locked. | 2550| 14800025 | SQLite: A table in the database is locked. | 2551| 14800026 | SQLite: The database is out of memory. | 2552| 14800027 | SQLite: Attempt to write a readonly database. | 2553| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2554| 14800029 | SQLite: The database is full. | 2555| 14800030 | SQLite: Unable to open the database file. | 2556| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2557| 14800032 | SQLite: Abort due to constraint violation. | 2558| 14800033 | SQLite: Data type mismatch. | 2559| 14800034 | SQLite: Library used incorrectly. | 2560| 14800047 | The WAL file size exceeds the default limit. | 2561 2562**示例:** 2563 2564```ts 2565let value1 = "Lisa"; 2566let value2 = 18; 2567let value3 = 100.5; 2568let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2569 2570// 以下三种方式可用 2571const valueBucket1: relationalStore.ValuesBucket = { 2572 'NAME': value1, 2573 'AGE': value2, 2574 'SALARY': value3, 2575 'CODES': value4 2576}; 2577const valueBucket2: relationalStore.ValuesBucket = { 2578 NAME: value1, 2579 AGE: value2, 2580 SALARY: value3, 2581 CODES: value4 2582}; 2583const valueBucket3: relationalStore.ValuesBucket = { 2584 "NAME": value1, 2585 "AGE": value2, 2586 "SALARY": value3, 2587 "CODES": value4 2588}; 2589 2590if (store != undefined) { 2591 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE, 2592 (err: BusinessError, rowId: number) => { 2593 if (err) { 2594 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 2595 return; 2596 } 2597 console.info(`Insert is successful, rowId = ${rowId}`); 2598 }); 2599} 2600``` 2601 2602### insert 2603 2604insert(table: string, values: ValuesBucket):Promise<number> 2605 2606向目标表中插入一行数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2607 2608**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2609 2610**参数:** 2611 2612| 参数名 | 类型 | 必填 | 说明 | 2613| ------ | ----------------------------- | ---- | -------------------------- | 2614| table | string | 是 | 指定的目标表名。 | 2615| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 2616 2617**返回值**: 2618 2619| 类型 | 说明 | 2620| --------------------- | ------------------------------------------------- | 2621| Promise<number> | Promise对象。如果操作成功,返回行ID;否则返回-1。 | 2622 2623**错误码:** 2624 2625以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2626 2627| **错误码ID** | **错误信息** | 2628|-----------| ------------------------------------------------------------ | 2629| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2630| 14800000 | Inner error. | 2631| 14800011 | Failed to open the database because it is corrupted. | 2632| 14800014 | The RdbStore or ResultSet is already closed. | 2633| 14800015 | The database does not respond. | 2634| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 2635| 14800022 | SQLite: Callback routine requested an abort. | 2636| 14800023 | SQLite: Access permission denied. | 2637| 14800024 | SQLite: The database file is locked. | 2638| 14800025 | SQLite: A table in the database is locked. | 2639| 14800026 | SQLite: The database is out of memory. | 2640| 14800027 | SQLite: Attempt to write a readonly database. | 2641| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2642| 14800029 | SQLite: The database is full. | 2643| 14800030 | SQLite: Unable to open the database file. | 2644| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2645| 14800032 | SQLite: Abort due to constraint violation. | 2646| 14800033 | SQLite: Data type mismatch. | 2647| 14800034 | SQLite: Library used incorrectly. | 2648| 14800047 | The WAL file size exceeds the default limit. | 2649 2650**示例:** 2651 2652```ts 2653import { BusinessError } from '@kit.BasicServicesKit'; 2654 2655let value1 = "Lisa"; 2656let value2 = 18; 2657let value3 = 100.5; 2658let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2659 2660// 以下三种方式可用 2661const valueBucket1: relationalStore.ValuesBucket = { 2662 'NAME': value1, 2663 'AGE': value2, 2664 'SALARY': value3, 2665 'CODES': value4 2666}; 2667const valueBucket2: relationalStore.ValuesBucket = { 2668 NAME: value1, 2669 AGE: value2, 2670 SALARY: value3, 2671 CODES: value4 2672}; 2673const valueBucket3: relationalStore.ValuesBucket = { 2674 "NAME": value1, 2675 "AGE": value2, 2676 "SALARY": value3, 2677 "CODES": value4 2678}; 2679 2680if (store != undefined) { 2681 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1).then((rowId: number) => { 2682 console.info(`Insert is successful, rowId = ${rowId}`); 2683 }).catch((err: BusinessError) => { 2684 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 2685 }); 2686} 2687``` 2688 2689### insert<sup>10+</sup> 2690 2691insert(table: string, values: ValuesBucket, conflict: ConflictResolution):Promise<number> 2692 2693向目标表中插入一行数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2694 2695**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2696 2697**参数:** 2698 2699| 参数名 | 类型 | 必填 | 说明 | 2700| -------- | ------------------------------------------- | ---- | -------------------------- | 2701| table | string | 是 | 指定的目标表名。 | 2702| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 2703| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。 | 2704 2705**返回值**: 2706 2707| 类型 | 说明 | 2708| --------------------- | ------------------------------------------------- | 2709| Promise<number> | Promise对象。如果操作成功,返回行ID;否则返回-1。 | 2710 2711**错误码:** 2712 2713以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2714 2715| **错误码ID** | **错误信息** | 2716|-----------| ------------------------------------------------------------ | 2717| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2718| 14800000 | Inner error. | 2719| 14800011 | Failed to open the database because it is corrupted. | 2720| 14800014 | The RdbStore or ResultSet is already closed. | 2721| 14800015 | The database does not respond. | 2722| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 2723| 14800022 | SQLite: Callback routine requested an abort. | 2724| 14800023 | SQLite: Access permission denied. | 2725| 14800024 | SQLite: The database file is locked. | 2726| 14800025 | SQLite: A table in the database is locked. | 2727| 14800026 | SQLite: The database is out of memory. | 2728| 14800027 | SQLite: Attempt to write a readonly database. | 2729| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2730| 14800029 | SQLite: The database is full. | 2731| 14800030 | SQLite: Unable to open the database file. | 2732| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2733| 14800032 | SQLite: Abort due to constraint violation. | 2734| 14800033 | SQLite: Data type mismatch. | 2735| 14800034 | SQLite: Library used incorrectly. | 2736| 14800047 | The WAL file size exceeds the default limit. | 2737 2738**示例:** 2739 2740```ts 2741import { BusinessError } from '@kit.BasicServicesKit'; 2742 2743let value1 = "Lisa"; 2744let value2 = 18; 2745let value3 = 100.5; 2746let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2747 2748// 以下三种方式可用 2749const valueBucket1: relationalStore.ValuesBucket = { 2750 'NAME': value1, 2751 'AGE': value2, 2752 'SALARY': value3, 2753 'CODES': value4 2754}; 2755const valueBucket2: relationalStore.ValuesBucket = { 2756 NAME: value1, 2757 AGE: value2, 2758 SALARY: value3, 2759 CODES: value4 2760}; 2761const valueBucket3: relationalStore.ValuesBucket = { 2762 "NAME": value1, 2763 "AGE": value2, 2764 "SALARY": value3, 2765 "CODES": value4 2766}; 2767 2768if (store != undefined) { 2769 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE).then((rowId: number) => { 2770 console.info(`Insert is successful, rowId = ${rowId}`); 2771 }).catch((err: BusinessError) => { 2772 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 2773 }); 2774} 2775``` 2776 2777### insertSync<sup>12+</sup> 2778 2779insertSync(table: string, values: ValuesBucket, conflict?: ConflictResolution):number 2780 2781向目标表中插入一行数据。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2782 2783**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2784 2785**参数:** 2786 2787| 参数名 | 类型 | 必填 | 说明 | 2788| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 2789| table | string | 是 | 指定的目标表名。 | 2790| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 2791| conflict | [ConflictResolution](#conflictresolution10) | 否 | 指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 2792 2793**返回值**: 2794 2795| 类型 | 说明 | 2796| ------ | ------------------------------------ | 2797| number | 如果操作成功,返回行ID;否则返回-1。 | 2798 2799**错误码:** 2800 2801以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2802 2803| **错误码ID** | **错误信息** | 2804| ------------ | ------------------------------------------------------------ | 2805| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2806| 14800000 | Inner error. | 2807| 14800011 | Failed to open the database because it is corrupted. | 2808| 14800014 | The RdbStore or ResultSet is already closed. | 2809| 14800015 | The database does not respond. | 2810| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 2811| 14800022 | SQLite: Callback routine requested an abort. | 2812| 14800023 | SQLite: Access permission denied. | 2813| 14800024 | SQLite: The database file is locked. | 2814| 14800025 | SQLite: A table in the database is locked. | 2815| 14800026 | SQLite: The database is out of memory. | 2816| 14800027 | SQLite: Attempt to write a readonly database. | 2817| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2818| 14800029 | SQLite: The database is full. | 2819| 14800030 | SQLite: Unable to open the database file. | 2820| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2821| 14800032 | SQLite: Abort due to constraint violation. | 2822| 14800033 | SQLite: Data type mismatch. | 2823| 14800034 | SQLite: Library used incorrectly. | 2824| 14800047 | The WAL file size exceeds the default limit. | 2825 2826**示例:** 2827 2828```ts 2829import { BusinessError } from '@kit.BasicServicesKit'; 2830 2831let value1 = "Lisa"; 2832let value2 = 18; 2833let value3 = 100.5; 2834let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2835 2836// 以下三种方式可用 2837const valueBucket1: relationalStore.ValuesBucket = { 2838 'NAME': value1, 2839 'AGE': value2, 2840 'SALARY': value3, 2841 'CODES': value4 2842}; 2843const valueBucket2: relationalStore.ValuesBucket = { 2844 NAME: value1, 2845 AGE: value2, 2846 SALARY: value3, 2847 CODES: value4 2848}; 2849const valueBucket3: relationalStore.ValuesBucket = { 2850 "NAME": value1, 2851 "AGE": value2, 2852 "SALARY": value3, 2853 "CODES": value4 2854}; 2855 2856if (store != undefined) { 2857 try { 2858 let rowId: number = (store as relationalStore.RdbStore).insertSync("EMPLOYEE", valueBucket1, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE); 2859 console.info(`Insert is successful, rowId = ${rowId}`); 2860 } catch (error) { 2861 console.error(`Insert is failed, code is ${error.code},message is ${error.message}`); 2862 } 2863} 2864``` 2865 2866### insertSync<sup>12+</sup> 2867 2868insertSync(table: string, values: sendableRelationalStore.ValuesBucket, conflict?: ConflictResolution):number 2869 2870传入Sendable数据,向目标表中插入一行数据。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2871 2872**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2873 2874**参数:** 2875 2876| 参数名 | 类型 | 必填 | 说明 | 2877| -------- | ---------------------------------------------------------------------------------------------- | ---- | ------------------------------------------------------------------------------- | 2878| table | string | 是 | 指定的目标表名。 | 2879| values | [sendableRelationalStore.ValuesBucket](./js-apis-data-sendableRelationalStore.md#valuesbucket) | 是 | 表示要插入到表中的可跨线程传递数据。 | 2880| conflict | [ConflictResolution](#conflictresolution10) | 否 | 指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 2881 2882**返回值**: 2883 2884| 类型 | 说明 | 2885| ------ | ------------------------------------ | 2886| number | 如果操作成功,返回行ID;否则返回-1。 | 2887 2888**错误码:** 2889 2890以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2891 2892| **错误码ID** | **错误信息** | 2893| ------------ | ------------------------------------------------------------ | 2894| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2895| 14800000 | Inner error. | 2896| 14800011 | Failed to open the database because it is corrupted. | 2897| 14800014 | The RdbStore or ResultSet is already closed. | 2898| 14800015 | The database does not respond. | 2899| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 2900| 14800022 | SQLite: Callback routine requested an abort. | 2901| 14800023 | SQLite: Access permission denied. | 2902| 14800024 | SQLite: The database file is locked. | 2903| 14800025 | SQLite: A table in the database is locked. | 2904| 14800026 | SQLite: The database is out of memory. | 2905| 14800027 | SQLite: Attempt to write a readonly database. | 2906| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2907| 14800029 | SQLite: The database is full. | 2908| 14800030 | SQLite: Unable to open the database file. | 2909| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2910| 14800032 | SQLite: Abort due to constraint violation. | 2911| 14800033 | SQLite: Data type mismatch. | 2912| 14800034 | SQLite: Library used incorrectly. | 2913| 14800047 | The WAL file size exceeds the default limit. | 2914 2915**示例:** 2916 2917```ts 2918import { sendableRelationalStore } from '@kit.ArkData'; 2919 2920const valuesBucket: relationalStore.ValuesBucket = { 2921 "NAME": 'hangman', 2922 "AGE": 18, 2923 "SALARY": 100.5, 2924 "CODES": new Uint8Array([1, 2, 3]) 2925}; 2926const sendableValuesBucket = sendableRelationalStore.toSendableValuesBucket(valuesBucket); 2927 2928if (store != undefined) { 2929 try { 2930 let rowId: number = (store as relationalStore.RdbStore).insertSync("EMPLOYEE", sendableValuesBucket, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE); 2931 console.info(`Insert is successful, rowId = ${rowId}`); 2932 } catch (error) { 2933 console.error(`Insert is failed, code is ${error.code},message is ${error.message}`); 2934 } 2935} 2936``` 2937 2938### batchInsert 2939 2940batchInsert(table: string, values: Array<ValuesBucket>, callback: AsyncCallback<number>):void 2941 2942向目标表中插入一组数据,使用callback异步回调。 2943 2944从API version 20开始,支持[向量数据库](#storeconfig)。 2945 2946**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2947 2948**参数:** 2949 2950| 参数名 | 类型 | 必填 | 说明 | 2951| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 2952| table | string | 是 | 指定的目标表名。 | 2953| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 2954| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回插入的数据个数,否则返回-1。 | 2955 2956**错误码:** 2957 2958以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 2959 2960| **错误码ID** | **错误信息** | 2961|-----------| ------------------------------------------------------------ | 2962| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2963| 14800000 | Inner error. | 2964| 14800011 | Failed to open the database because it is corrupted. | 2965| 14800014 | The RdbStore or ResultSet is already closed. | 2966| 14800015 | The database does not respond. | 2967| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 2968| 14800022 | SQLite: Callback routine requested an abort. | 2969| 14800023 | SQLite: Access permission denied. | 2970| 14800024 | SQLite: The database file is locked. | 2971| 14800025 | SQLite: A table in the database is locked. | 2972| 14800026 | SQLite: The database is out of memory. | 2973| 14800027 | SQLite: Attempt to write a readonly database. | 2974| 14800028 | SQLite: Some kind of disk I/O error occurred. | 2975| 14800029 | SQLite: The database is full. | 2976| 14800030 | SQLite: Unable to open the database file. | 2977| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 2978| 14800032 | SQLite: Abort due to constraint violation. | 2979| 14800033 | SQLite: Data type mismatch. | 2980| 14800034 | SQLite: Library used incorrectly. | 2981| 14800047 | The WAL file size exceeds the default limit. | 2982 2983**示例:** 2984 2985```ts 2986let value1 = "Lisa"; 2987let value2 = 18; 2988let value3 = 100.5; 2989let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2990let value5 = "Jack"; 2991let value6 = 19; 2992let value7 = 101.5; 2993let value8 = new Uint8Array([6, 7, 8, 9, 10]); 2994let value9 = "Tom"; 2995let value10 = 20; 2996let value11 = 102.5; 2997let value12 = new Uint8Array([11, 12, 13, 14, 15]); 2998 2999const valueBucket1: relationalStore.ValuesBucket = { 3000 'NAME': value1, 3001 'AGE': value2, 3002 'SALARY': value3, 3003 'CODES': value4 3004}; 3005const valueBucket2: relationalStore.ValuesBucket = { 3006 'NAME': value5, 3007 'AGE': value6, 3008 'SALARY': value7, 3009 'CODES': value8 3010}; 3011const valueBucket3: relationalStore.ValuesBucket = { 3012 'NAME': value9, 3013 'AGE': value10, 3014 'SALARY': value11, 3015 'CODES': value12 3016}; 3017 3018let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 3019if (store != undefined) { 3020 (store as relationalStore.RdbStore).batchInsert("EMPLOYEE", valueBuckets, (err, insertNum) => { 3021 if (err) { 3022 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 3023 return; 3024 } 3025 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 3026 }) 3027} 3028``` 3029 3030### batchInsert 3031 3032batchInsert(table: string, values: Array<ValuesBucket>):Promise<number> 3033 3034向目标表中插入一组数据,使用Promise异步回调。 3035 3036从API version 20开始,该接口支持[向量数据库](#storeconfig)使用。 3037 3038**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3039 3040**参数:** 3041 3042| 参数名 | 类型 | 必填 | 说明 | 3043| ------ | ------------------------------------------ | ---- | ---------------------------- | 3044| table | string | 是 | 指定的目标表名。 | 3045| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。| 3046 3047**返回值**: 3048 3049| 类型 | 说明 | 3050| --------------------- | ----------------------------------------------------------- | 3051| Promise<number> | Promise对象。如果操作成功,返回插入的数据个数,否则返回-1。 | 3052 3053**错误码:** 3054 3055以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3056 3057| **错误码ID** | **错误信息** | 3058|-----------| ------------------------------------------------------------ | 3059| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3060| 14800000 | Inner error. | 3061| 14800011 | Failed to open the database because it is corrupted. | 3062| 14800014 | The RdbStore or ResultSet is already closed. | 3063| 14800015 | The database does not respond. | 3064| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3065| 14800022 | SQLite: Callback routine requested an abort. | 3066| 14800023 | SQLite: Access permission denied. | 3067| 14800024 | SQLite: The database file is locked. | 3068| 14800025 | SQLite: A table in the database is locked. | 3069| 14800026 | SQLite: The database is out of memory. | 3070| 14800027 | SQLite: Attempt to write a readonly database. | 3071| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3072| 14800029 | SQLite: The database is full. | 3073| 14800030 | SQLite: Unable to open the database file. | 3074| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3075| 14800032 | SQLite: Abort due to constraint violation. | 3076| 14800033 | SQLite: Data type mismatch. | 3077| 14800034 | SQLite: Library used incorrectly. | 3078| 14800047 | The WAL file size exceeds the default limit. | 3079 3080**示例:** 3081 3082关系型数据库: 3083 3084```ts 3085import { BusinessError } from '@kit.BasicServicesKit'; 3086 3087let value1 = "Lisa"; 3088let value2 = 18; 3089let value3 = 100.5; 3090let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3091let value5 = "Jack"; 3092let value6 = 19; 3093let value7 = 101.5; 3094let value8 = new Uint8Array([6, 7, 8, 9, 10]); 3095let value9 = "Tom"; 3096let value10 = 20; 3097let value11 = 102.5; 3098let value12 = new Uint8Array([11, 12, 13, 14, 15]); 3099 3100const valueBucket1: relationalStore.ValuesBucket = { 3101 'NAME': value1, 3102 'AGE': value2, 3103 'SALARY': value3, 3104 'CODES': value4 3105}; 3106const valueBucket2: relationalStore.ValuesBucket = { 3107 'NAME': value5, 3108 'AGE': value6, 3109 'SALARY': value7, 3110 'CODES': value8 3111}; 3112const valueBucket3: relationalStore.ValuesBucket = { 3113 'NAME': value9, 3114 'AGE': value10, 3115 'SALARY': value11, 3116 'CODES': value12 3117}; 3118 3119let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 3120if (store != undefined) { 3121 (store as relationalStore.RdbStore).batchInsert("EMPLOYEE", valueBuckets).then((insertNum: number) => { 3122 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 3123 }).catch((err: BusinessError) => { 3124 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 3125 }) 3126} 3127``` 3128 3129向量数据库: 3130 3131```ts 3132let createSql = "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT, data1 floatvector(2));"; 3133await store!.execute(createSql, 0, undefined); // 创建关系表,第二个参数0表示不开启显示事务,第三个参数undefined表示sql未使用绑定参数化 3134let floatVector = Float32Array.from([1.2, 2.3]); 3135let valueBucketArray = new Array<relationalStore.ValuesBucket>(); 3136for (let i = 0; i < 100; i++) { // 构造一个BucketArray用于写入 3137 const row : relationalStore.ValuesBucket = { 3138 "id" : i, 3139 "data1" : floatVector, 3140 } 3141 valueBucketArray.push(row); 3142} 3143await store!.batchInsert("test", valueBucketArray); // 执行批量写入 3144``` 3145 3146### batchInsertSync<sup>12+</sup> 3147 3148batchInsertSync(table: string, values: Array<ValuesBucket>):number 3149 3150向目标表中插入一组数据。 3151 3152**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3153 3154**参数:** 3155 3156| 参数名 | 类型 | 必填 | 说明 | 3157| ------ | ------------------------------------------ | ---- | ---------------------------- | 3158| table | string | 是 | 指定的目标表名。 | 3159| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 3160 3161**返回值**: 3162 3163| 类型 | 说明 | 3164| ------ | ---------------------------------------------- | 3165| number | 如果操作成功,返回插入的数据个数,否则返回-1。 | 3166 3167**错误码:** 3168 3169以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3170 3171| **错误码ID** | **错误信息** | 3172| ------------ | ------------------------------------------------------------ | 3173| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3174| 14800000 | Inner error. | 3175| 14800011 | Failed to open the database because it is corrupted. | 3176| 14800014 | The RdbStore or ResultSet is already closed. | 3177| 14800015 | The database does not respond. | 3178| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3179| 14800022 | SQLite: Callback routine requested an abort. | 3180| 14800023 | SQLite: Access permission denied. | 3181| 14800024 | SQLite: The database file is locked. | 3182| 14800025 | SQLite: A table in the database is locked. | 3183| 14800026 | SQLite: The database is out of memory. | 3184| 14800027 | SQLite: Attempt to write a readonly database. | 3185| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3186| 14800029 | SQLite: The database is full. | 3187| 14800030 | SQLite: Unable to open the database file. | 3188| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3189| 14800032 | SQLite: Abort due to constraint violation. | 3190| 14800033 | SQLite: Data type mismatch. | 3191| 14800034 | SQLite: Library used incorrectly. | 3192| 14800047 | The WAL file size exceeds the default limit. | 3193 3194**示例:** 3195 3196```ts 3197import { BusinessError } from '@kit.BasicServicesKit'; 3198 3199let value1 = "Lisa"; 3200let value2 = 18; 3201let value3 = 100.5; 3202let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3203let value5 = "Jack"; 3204let value6 = 19; 3205let value7 = 101.5; 3206let value8 = new Uint8Array([6, 7, 8, 9, 10]); 3207let value9 = "Tom"; 3208let value10 = 20; 3209let value11 = 102.5; 3210let value12 = new Uint8Array([11, 12, 13, 14, 15]); 3211 3212const valueBucket1: relationalStore.ValuesBucket = { 3213 'NAME': value1, 3214 'AGE': value2, 3215 'SALARY': value3, 3216 'CODES': value4 3217}; 3218const valueBucket2: relationalStore.ValuesBucket = { 3219 'NAME': value5, 3220 'AGE': value6, 3221 'SALARY': value7, 3222 'CODES': value8 3223}; 3224const valueBucket3: relationalStore.ValuesBucket = { 3225 'NAME': value9, 3226 'AGE': value10, 3227 'SALARY': value11, 3228 'CODES': value12 3229}; 3230 3231let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 3232if (store != undefined) { 3233 try { 3234 let insertNum: number = (store as relationalStore.RdbStore).batchInsertSync("EMPLOYEE", valueBuckets); 3235 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 3236 } catch (err) { 3237 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 3238 } 3239} 3240``` 3241 3242### batchInsertWithConflictResolution<sup>18+</sup> 3243 3244batchInsertWithConflictResolution(table: string, values: Array<ValuesBucket>, conflict: ConflictResolution): Promise<number> 3245 3246向目标表中插入一组数据,可以通过conflict参数指定冲突解决模式。使用Promise异步回调。 3247 3248**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3249 3250**参数:** 3251 3252| 参数名 | 类型 | 必填 | 说明 | 3253| ------ | ------------------------------------------ | ---- | ---------------------------- | 3254| table | string | 是 | 指定的目标表名。 | 3255| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 3256| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。 | 3257 3258**返回值**: 3259 3260| 类型 | 说明 | 3261| ------ | ---------------------------------------------- | 3262| Promise<number> | Promise对象。如果操作成功,返回插入的数据个数,否则返回-1。 | 3263 3264**错误码:** 3265 3266以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3267 3268| **错误码ID** | **错误信息** | 3269| ------------ | ------------------------------------------------------------ | 3270| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3271| 14800000 | Inner error. | 3272| 14800011 | Failed to open the database because it is corrupted. | 3273| 14800014 | The RdbStore or ResultSet is already closed. | 3274| 14800015 | The database does not respond. | 3275| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3276| 14800022 | SQLite: Callback routine requested an abort. | 3277| 14800023 | SQLite: Access permission denied. | 3278| 14800024 | SQLite: The database file is locked. | 3279| 14800025 | SQLite: A table in the database is locked. | 3280| 14800026 | SQLite: The database is out of memory. | 3281| 14800027 | SQLite: Attempt to write a readonly database. | 3282| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3283| 14800029 | SQLite: The database is full. | 3284| 14800030 | SQLite: Unable to open the database file. | 3285| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3286| 14800032 | SQLite: Abort due to constraint violation. | 3287| 14800033 | SQLite: Data type mismatch. | 3288| 14800034 | SQLite: Library used incorrectly. | 3289| 14800047 | The WAL file size exceeds the default limit. | 3290 3291**示例:** 3292 3293```ts 3294import { BusinessError } from '@kit.BasicServicesKit'; 3295 3296let value1 = "Lisa"; 3297let value2 = 18; 3298let value3 = 100.5; 3299let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3300let value5 = "Jack"; 3301let value6 = 19; 3302let value7 = 101.5; 3303let value8 = new Uint8Array([6, 7, 8, 9, 10]); 3304let value9 = "Tom"; 3305let value10 = 20; 3306let value11 = 102.5; 3307let value12 = new Uint8Array([11, 12, 13, 14, 15]); 3308 3309const valueBucket1: relationalStore.ValuesBucket = { 3310 'NAME': value1, 3311 'AGE': value2, 3312 'SALARY': value3, 3313 'CODES': value4 3314}; 3315const valueBucket2: relationalStore.ValuesBucket = { 3316 'NAME': value5, 3317 'AGE': value6, 3318 'SALARY': value7, 3319 'CODES': value8 3320}; 3321const valueBucket3: relationalStore.ValuesBucket = { 3322 'NAME': value9, 3323 'AGE': value10, 3324 'SALARY': value11, 3325 'CODES': value12 3326}; 3327 3328let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 3329if (store != undefined) { 3330 (store as relationalStore.RdbStore).batchInsertWithConflictResolution("EMPLOYEE", valueBuckets, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE).then((insertNum: number) => { 3331 console.info(`batchInsert is successful, insertNum = ${insertNum}`); 3332 }).catch((err: BusinessError) => { 3333 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 3334 }); 3335} 3336``` 3337 3338### batchInsertWithConflictResolutionSync<sup>18+</sup> 3339 3340batchInsertWithConflictResolutionSync(table: string, values: Array<ValuesBucket>, conflict: ConflictResolution): number 3341 3342向目标表中插入一组数据。 3343 3344**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3345 3346**参数:** 3347 3348| 参数名 | 类型 | 必填 | 说明 | 3349| ------ | ------------------------------------------ | ---- | ---------------------------- | 3350| table | string | 是 | 指定的目标表名。 | 3351| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 3352| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。 | 3353 3354**返回值**: 3355 3356| 类型 | 说明 | 3357| ------ | ---------------------------------------------- | 3358| number | 如果操作成功,返回插入的数据个数,否则返回-1。 | 3359 3360**错误码:** 3361 3362以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3363 3364| **错误码ID** | **错误信息** | 3365| ------------ | ------------------------------------------------------------ | 3366| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3367| 14800000 | Inner error. | 3368| 14800011 | Failed to open the database because it is corrupted. | 3369| 14800014 | The RdbStore or ResultSet is already closed. | 3370| 14800015 | The database does not respond. | 3371| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3372| 14800022 | SQLite: Callback routine requested an abort. | 3373| 14800023 | SQLite: Access permission denied. | 3374| 14800024 | SQLite: The database file is locked. | 3375| 14800025 | SQLite: A table in the database is locked. | 3376| 14800026 | SQLite: The database is out of memory. | 3377| 14800027 | SQLite: Attempt to write a readonly database. | 3378| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3379| 14800029 | SQLite: The database is full. | 3380| 14800030 | SQLite: Unable to open the database file. | 3381| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3382| 14800032 | SQLite: Abort due to constraint violation. | 3383| 14800033 | SQLite: Data type mismatch. | 3384| 14800034 | SQLite: Library used incorrectly. | 3385| 14800047 | The WAL file size exceeds the default limit. | 3386 3387**示例:** 3388 3389```ts 3390import { BusinessError } from '@kit.BasicServicesKit'; 3391 3392let value1 = "Lisa"; 3393let value2 = 18; 3394let value3 = 100.5; 3395let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3396let value5 = "Jack"; 3397let value6 = 19; 3398let value7 = 101.5; 3399let value8 = new Uint8Array([6, 7, 8, 9, 10]); 3400let value9 = "Tom"; 3401let value10 = 20; 3402let value11 = 102.5; 3403let value12 = new Uint8Array([11, 12, 13, 14, 15]); 3404 3405const valueBucket1: relationalStore.ValuesBucket = { 3406 'NAME': value1, 3407 'AGE': value2, 3408 'SALARY': value3, 3409 'CODES': value4 3410}; 3411const valueBucket2: relationalStore.ValuesBucket = { 3412 'NAME': value5, 3413 'AGE': value6, 3414 'SALARY': value7, 3415 'CODES': value8 3416}; 3417const valueBucket3: relationalStore.ValuesBucket = { 3418 'NAME': value9, 3419 'AGE': value10, 3420 'SALARY': value11, 3421 'CODES': value12 3422}; 3423 3424let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 3425if (store != undefined) { 3426 try { 3427 let insertNum: number = (store as relationalStore.RdbStore).batchInsertWithConflictResolutionSync("EMPLOYEE", valueBuckets, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE); 3428 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 3429 } catch (err) { 3430 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 3431 } 3432} 3433``` 3434 3435### update 3436 3437update(values: ValuesBucket, predicates: RdbPredicates, callback: AsyncCallback<number>):void 3438 3439根据RdbPredicates的指定实例对象更新数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3440 3441**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3442 3443**参数:** 3444 3445| 参数名 | 类型 | 必填 | 说明 | 3446| ---------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 3447| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 3448| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 3449| callback | AsyncCallback<number> | 是 | 指定的callback回调方法。返回受影响的行数。 | 3450 3451**错误码:** 3452 3453以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3454 3455| **错误码ID** | **错误信息** | 3456|-----------| ------------------------------------------------------------ | 3457| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3458| 14800000 | Inner error. | 3459| 14800011 | Failed to open the database because it is corrupted. | 3460| 14800014 | The RdbStore or ResultSet is already closed. | 3461| 14800015 | The database does not respond. | 3462| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3463| 14800022 | SQLite: Callback routine requested an abort. | 3464| 14800023 | SQLite: Access permission denied. | 3465| 14800024 | SQLite: The database file is locked. | 3466| 14800025 | SQLite: A table in the database is locked. | 3467| 14800026 | SQLite: The database is out of memory. | 3468| 14800027 | SQLite: Attempt to write a readonly database. | 3469| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3470| 14800029 | SQLite: The database is full. | 3471| 14800030 | SQLite: Unable to open the database file. | 3472| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3473| 14800032 | SQLite: Abort due to constraint violation. | 3474| 14800033 | SQLite: Data type mismatch. | 3475| 14800034 | SQLite: Library used incorrectly. | 3476| 14800047 | The WAL file size exceeds the default limit. | 3477 3478**示例:** 3479 3480```ts 3481let value1 = "Rose"; 3482let value2 = 22; 3483let value3 = 200.5; 3484let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3485 3486// 以下三种方式可用 3487const valueBucket1: relationalStore.ValuesBucket = { 3488 'NAME': value1, 3489 'AGE': value2, 3490 'SALARY': value3, 3491 'CODES': value4 3492}; 3493const valueBucket2: relationalStore.ValuesBucket = { 3494 NAME: value1, 3495 AGE: value2, 3496 SALARY: value3, 3497 CODES: value4 3498}; 3499const valueBucket3: relationalStore.ValuesBucket = { 3500 "NAME": value1, 3501 "AGE": value2, 3502 "SALARY": value3, 3503 "CODES": value4 3504}; 3505 3506let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3507predicates.equalTo("NAME", "Lisa"); 3508if (store != undefined) { 3509 (store as relationalStore.RdbStore).update(valueBucket1, predicates, (err, rows) => { 3510 if (err) { 3511 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 3512 return; 3513 } 3514 console.info(`Updated row count: ${rows}`); 3515 }); 3516} 3517``` 3518 3519### update<sup>10+</sup> 3520 3521update(values: ValuesBucket, predicates: RdbPredicates, conflict: ConflictResolution, callback: AsyncCallback<number>):void 3522 3523根据RdbPredicates的指定实例对象更新数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3524 3525**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3526 3527**参数:** 3528 3529| 参数名 | 类型 | 必填 | 说明 | 3530| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 3531| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 3532| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 3533| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。 | 3534| callback | AsyncCallback<number> | 是 | 指定的callback回调方法。返回受影响的行数。 | 3535 3536**错误码:** 3537 3538以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3539 3540| **错误码ID** | **错误信息** | 3541|-----------| ------------------------------------------------------------ | 3542| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3543| 14800000 | Inner error. | 3544| 14800011 | Failed to open the database because it is corrupted. | 3545| 14800014 | The RdbStore or ResultSet is already closed. | 3546| 14800015 | The database does not respond. | 3547| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3548| 14800022 | SQLite: Callback routine requested an abort. | 3549| 14800023 | SQLite: Access permission denied. | 3550| 14800024 | SQLite: The database file is locked. | 3551| 14800025 | SQLite: A table in the database is locked. | 3552| 14800026 | SQLite: The database is out of memory. | 3553| 14800027 | SQLite: Attempt to write a readonly database. | 3554| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3555| 14800029 | SQLite: The database is full. | 3556| 14800030 | SQLite: Unable to open the database file. | 3557| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3558| 14800032 | SQLite: Abort due to constraint violation. | 3559| 14800033 | SQLite: Data type mismatch. | 3560| 14800034 | SQLite: Library used incorrectly. | 3561| 14800047 | The WAL file size exceeds the default limit. | 3562 3563**示例:** 3564 3565```ts 3566let value1 = "Rose"; 3567let value2 = 22; 3568let value3 = 200.5; 3569let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3570 3571// 以下三种方式可用 3572const valueBucket1: relationalStore.ValuesBucket = { 3573 'NAME': value1, 3574 'AGE': value2, 3575 'SALARY': value3, 3576 'CODES': value4 3577}; 3578const valueBucket2: relationalStore.ValuesBucket = { 3579 NAME: value1, 3580 AGE: value2, 3581 SALARY: value3, 3582 CODES: value4 3583}; 3584const valueBucket3: relationalStore.ValuesBucket = { 3585 "NAME": value1, 3586 "AGE": value2, 3587 "SALARY": value3, 3588 "CODES": value4 3589}; 3590 3591let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3592predicates.equalTo("NAME", "Lisa"); 3593if (store != undefined) { 3594 (store as relationalStore.RdbStore).update(valueBucket1, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE, (err, rows) => { 3595 if (err) { 3596 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 3597 return; 3598 } 3599 console.info(`Updated row count: ${rows}`); 3600 }); 3601} 3602``` 3603 3604### update 3605 3606update(values: ValuesBucket, predicates: RdbPredicates):Promise<number> 3607 3608根据RdbPredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3609 3610**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3611 3612**参数:** 3613 3614| 参数名 | 类型 | 必填 | 说明 | 3615| ------------ | ------------------------------------ | ---- | ------------------------------------------------------------ | 3616| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 3617| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 3618 3619**返回值**: 3620 3621| 类型 | 说明 | 3622| --------------------- | ----------------------------------------- | 3623| Promise<number> | 指定的Promise回调方法。返回受影响的行数。 | 3624 3625**错误码:** 3626 3627以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3628 3629| **错误码ID** | **错误信息** | 3630|-----------| ------------------------------------------------------------ | 3631| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3632| 14800000 | Inner error. | 3633| 14800011 | Failed to open the database because it is corrupted. | 3634| 14800014 | The RdbStore or ResultSet is already closed. | 3635| 14800015 | The database does not respond. | 3636| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3637| 14800022 | SQLite: Callback routine requested an abort. | 3638| 14800023 | SQLite: Access permission denied. | 3639| 14800024 | SQLite: The database file is locked. | 3640| 14800025 | SQLite: A table in the database is locked. | 3641| 14800026 | SQLite: The database is out of memory. | 3642| 14800027 | SQLite: Attempt to write a readonly database. | 3643| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3644| 14800029 | SQLite: The database is full. | 3645| 14800030 | SQLite: Unable to open the database file. | 3646| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3647| 14800032 | SQLite: Abort due to constraint violation. | 3648| 14800033 | SQLite: Data type mismatch. | 3649| 14800034 | SQLite: Library used incorrectly. | 3650| 14800047 | The WAL file size exceeds the default limit. | 3651 3652**示例:** 3653 3654```ts 3655import { BusinessError } from '@kit.BasicServicesKit'; 3656 3657let value1 = "Rose"; 3658let value2 = 22; 3659let value3 = 200.5; 3660let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3661 3662// 以下三种方式可用 3663const valueBucket1: relationalStore.ValuesBucket = { 3664 'NAME': value1, 3665 'AGE': value2, 3666 'SALARY': value3, 3667 'CODES': value4 3668}; 3669const valueBucket2: relationalStore.ValuesBucket = { 3670 NAME: value1, 3671 AGE: value2, 3672 SALARY: value3, 3673 CODES: value4 3674}; 3675const valueBucket3: relationalStore.ValuesBucket = { 3676 "NAME": value1, 3677 "AGE": value2, 3678 "SALARY": value3, 3679 "CODES": value4 3680}; 3681 3682let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3683predicates.equalTo("NAME", "Lisa"); 3684if (store != undefined) { 3685 (store as relationalStore.RdbStore).update(valueBucket1, predicates).then(async (rows: Number) => { 3686 console.info(`Updated row count: ${rows}`); 3687 }).catch((err: BusinessError) => { 3688 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 3689 }); 3690} 3691``` 3692 3693### update<sup>10+</sup> 3694 3695update(values: ValuesBucket, predicates: RdbPredicates, conflict: ConflictResolution):Promise<number> 3696 3697根据RdbPredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3698 3699**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3700 3701**参数:** 3702 3703| 参数名 | 类型 | 必填 | 说明 | 3704| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 3705| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 3706| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 3707| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。 | 3708 3709**返回值**: 3710 3711| 类型 | 说明 | 3712| --------------------- | ----------------------------------------- | 3713| Promise<number> | 指定的Promise回调方法。返回受影响的行数。 | 3714 3715**错误码:** 3716 3717以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3718 3719| **错误码ID** | **错误信息** | 3720|-----------| ------------------------------------------------------------ | 3721| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3722| 14800000 | Inner error. | 3723| 14800011 | Failed to open the database because it is corrupted. | 3724| 14800014 | The RdbStore or ResultSet is already closed. | 3725| 14800015 | The database does not respond. | 3726| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3727| 14800022 | SQLite: Callback routine requested an abort. | 3728| 14800023 | SQLite: Access permission denied. | 3729| 14800024 | SQLite: The database file is locked. | 3730| 14800025 | SQLite: A table in the database is locked. | 3731| 14800026 | SQLite: The database is out of memory. | 3732| 14800027 | SQLite: Attempt to write a readonly database. | 3733| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3734| 14800029 | SQLite: The database is full. | 3735| 14800030 | SQLite: Unable to open the database file. | 3736| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3737| 14800032 | SQLite: Abort due to constraint violation. | 3738| 14800033 | SQLite: Data type mismatch. | 3739| 14800034 | SQLite: Library used incorrectly. | 3740| 14800047 | The WAL file size exceeds the default limit. | 3741 3742**示例:** 3743 3744```ts 3745import { BusinessError } from '@kit.BasicServicesKit'; 3746 3747let value1 = "Rose"; 3748let value2 = 22; 3749let value3 = 200.5; 3750let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3751 3752// 以下三种方式可用 3753const valueBucket1: relationalStore.ValuesBucket = { 3754 'NAME': value1, 3755 'AGE': value2, 3756 'SALARY': value3, 3757 'CODES': value4 3758}; 3759const valueBucket2: relationalStore.ValuesBucket = { 3760 NAME: value1, 3761 AGE: value2, 3762 SALARY: value3, 3763 CODES: value4 3764}; 3765const valueBucket3: relationalStore.ValuesBucket = { 3766 "NAME": value1, 3767 "AGE": value2, 3768 "SALARY": value3, 3769 "CODES": value4 3770}; 3771 3772let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3773predicates.equalTo("NAME", "Lisa"); 3774if (store != undefined) { 3775 (store as relationalStore.RdbStore).update(valueBucket1, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE).then(async (rows: Number) => { 3776 console.info(`Updated row count: ${rows}`); 3777 }).catch((err: BusinessError) => { 3778 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 3779 }); 3780} 3781``` 3782 3783### updateSync<sup>12+</sup> 3784 3785updateSync(values: ValuesBucket, predicates: RdbPredicates, conflict?: ConflictResolution):number 3786 3787根据RdbPredicates的指定实例对象更新数据库中的数据。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 3788 3789**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3790 3791**参数:** 3792 3793| 参数名 | 类型 | 必填 | 说明 | 3794| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 3795| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 3796| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 3797| conflict | [ConflictResolution](#conflictresolution10) | 否 | 指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 3798 3799**返回值**: 3800 3801| 类型 | 说明 | 3802| ------ | ------------------ | 3803| number | 返回受影响的行数。 | 3804 3805**错误码:** 3806 3807以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3808 3809| **错误码ID** | **错误信息** | 3810| ------------ | ------------------------------------------------------------ | 3811| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3812| 14800000 | Inner error. | 3813| 14800011 | Failed to open the database because it is corrupted. | 3814| 14800014 | The RdbStore or ResultSet is already closed. | 3815| 14800015 | The database does not respond. | 3816| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3817| 14800022 | SQLite: Callback routine requested an abort. | 3818| 14800023 | SQLite: Access permission denied. | 3819| 14800024 | SQLite: The database file is locked. | 3820| 14800025 | SQLite: A table in the database is locked. | 3821| 14800026 | SQLite: The database is out of memory. | 3822| 14800027 | SQLite: Attempt to write a readonly database. | 3823| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3824| 14800029 | SQLite: The database is full. | 3825| 14800030 | SQLite: Unable to open the database file. | 3826| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3827| 14800032 | SQLite: Abort due to constraint violation. | 3828| 14800033 | SQLite: Data type mismatch. | 3829| 14800034 | SQLite: Library used incorrectly. | 3830| 14800047 | The WAL file size exceeds the default limit. | 3831 3832**示例:** 3833 3834```ts 3835import { BusinessError } from '@kit.BasicServicesKit'; 3836 3837let value1 = "Rose"; 3838let value2 = 22; 3839let value3 = 200.5; 3840let value4 = new Uint8Array([1, 2, 3, 4, 5]); 3841 3842// 以下三种方式可用 3843const valueBucket1: relationalStore.ValuesBucket = { 3844 'NAME': value1, 3845 'AGE': value2, 3846 'SALARY': value3, 3847 'CODES': value4 3848}; 3849const valueBucket2: relationalStore.ValuesBucket = { 3850 NAME: value1, 3851 AGE: value2, 3852 SALARY: value3, 3853 CODES: value4 3854}; 3855const valueBucket3: relationalStore.ValuesBucket = { 3856 "NAME": value1, 3857 "AGE": value2, 3858 "SALARY": value3, 3859 "CODES": value4 3860}; 3861 3862let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3863predicates.equalTo("NAME", "Lisa"); 3864if (store != undefined) { 3865 try { 3866 let rows: Number = (store as relationalStore.RdbStore).updateSync(valueBucket1, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE); 3867 console.info(`Updated row count: ${rows}`); 3868 } catch (error) { 3869 console.error(`Updated failed, code is ${error.code},message is ${error.message}`); 3870 } 3871} 3872``` 3873 3874### delete 3875 3876delete(predicates: RdbPredicates, callback: AsyncCallback<number>):void 3877 3878根据RdbPredicates的指定实例对象从数据库中删除数据,使用callback异步回调。 3879 3880**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3881 3882**参数:** 3883 3884| 参数名 | 类型 | 必填 | 说明 | 3885| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 3886| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 3887| callback | AsyncCallback<number> | 是 | 指定callback回调函数。返回受影响的行数量。 | 3888 3889**错误码:** 3890 3891以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3892 3893| **错误码ID** | **错误信息** | 3894|-----------| ------------------------------------------------------------ | 3895| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3896| 14800000 | Inner error. | 3897| 14800011 | Failed to open the database because it is corrupted. | 3898| 14800014 | The RdbStore or ResultSet is already closed. | 3899| 14800015 | The database does not respond. | 3900| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3901| 14800022 | SQLite: Callback routine requested an abort. | 3902| 14800023 | SQLite: Access permission denied. | 3903| 14800024 | SQLite: The database file is locked. | 3904| 14800025 | SQLite: A table in the database is locked. | 3905| 14800026 | SQLite: The database is out of memory. | 3906| 14800027 | SQLite: Attempt to write a readonly database. | 3907| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3908| 14800029 | SQLite: The database is full. | 3909| 14800030 | SQLite: Unable to open the database file. | 3910| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3911| 14800032 | SQLite: Abort due to constraint violation. | 3912| 14800033 | SQLite: Data type mismatch. | 3913| 14800034 | SQLite: Library used incorrectly. | 3914| 14800047 | The WAL file size exceeds the default limit. | 3915 3916**示例:** 3917 3918```ts 3919let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3920predicates.equalTo("NAME", "Lisa"); 3921if (store != undefined) { 3922 (store as relationalStore.RdbStore).delete(predicates, (err, rows) => { 3923 if (err) { 3924 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 3925 return; 3926 } 3927 console.info(`Delete rows: ${rows}`); 3928 }); 3929} 3930``` 3931 3932### delete 3933 3934delete(predicates: RdbPredicates):Promise<number> 3935 3936根据RdbPredicates的指定实例对象从数据库中删除数据,使用Promise异步回调。 3937 3938**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3939 3940**参数:** 3941 3942| 参数名 | 类型 | 必填 | 说明 | 3943| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 3944| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 3945 3946**返回值**: 3947 3948| 类型 | 说明 | 3949| --------------------- | ------------------------------- | 3950| Promise<number> | Promise对象。返回受影响的行数量。 | 3951 3952**错误码:** 3953 3954以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 3955 3956| **错误码ID** | **错误信息** | 3957|-----------| ------------------------------------------------------------ | 3958| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3959| 14800000 | Inner error. | 3960| 14800011 | Failed to open the database because it is corrupted. | 3961| 14800014 | The RdbStore or ResultSet is already closed. | 3962| 14800015 | The database does not respond. | 3963| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 3964| 14800022 | SQLite: Callback routine requested an abort. | 3965| 14800023 | SQLite: Access permission denied. | 3966| 14800024 | SQLite: The database file is locked. | 3967| 14800025 | SQLite: A table in the database is locked. | 3968| 14800026 | SQLite: The database is out of memory. | 3969| 14800027 | SQLite: Attempt to write a readonly database. | 3970| 14800028 | SQLite: Some kind of disk I/O error occurred. | 3971| 14800029 | SQLite: The database is full. | 3972| 14800030 | SQLite: Unable to open the database file. | 3973| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 3974| 14800032 | SQLite: Abort due to constraint violation. | 3975| 14800033 | SQLite: Data type mismatch. | 3976| 14800034 | SQLite: Library used incorrectly. | 3977| 14800047 | The WAL file size exceeds the default limit. | 3978 3979**示例:** 3980 3981```ts 3982import { BusinessError } from '@kit.BasicServicesKit'; 3983 3984let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3985predicates.equalTo("NAME", "Lisa"); 3986if (store != undefined) { 3987 (store as relationalStore.RdbStore).delete(predicates).then((rows: Number) => { 3988 console.info(`Delete rows: ${rows}`); 3989 }).catch((err: BusinessError) => { 3990 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 3991 }); 3992} 3993``` 3994 3995### deleteSync<sup>12+</sup> 3996 3997deleteSync(predicates: RdbPredicates):number 3998 3999根据RdbPredicates的指定实例对象从数据库中删除数据。 4000 4001**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4002 4003**参数:** 4004 4005| 参数名 | 类型 | 必填 | 说明 | 4006| ---------- | ------------------------------- | ---- | --------------------------------------- | 4007| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 4008 4009**返回值**: 4010 4011| 类型 | 说明 | 4012| ------ | ------------------ | 4013| number | 返回受影响的行数量。 | 4014 4015**错误码:** 4016 4017以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4018 4019| **错误码ID** | **错误信息** | 4020| ------------ | ------------------------------------------------------------ | 4021| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4022| 14800000 | Inner error. | 4023| 14800011 | Failed to open the database because it is corrupted. | 4024| 14800014 | The RdbStore or ResultSet is already closed. | 4025| 14800015 | The database does not respond. | 4026| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 4027| 14800022 | SQLite: Callback routine requested an abort. | 4028| 14800023 | SQLite: Access permission denied. | 4029| 14800024 | SQLite: The database file is locked. | 4030| 14800025 | SQLite: A table in the database is locked. | 4031| 14800026 | SQLite: The database is out of memory. | 4032| 14800027 | SQLite: Attempt to write a readonly database. | 4033| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4034| 14800029 | SQLite: The database is full. | 4035| 14800030 | SQLite: Unable to open the database file. | 4036| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4037| 14800032 | SQLite: Abort due to constraint violation. | 4038| 14800033 | SQLite: Data type mismatch. | 4039| 14800034 | SQLite: Library used incorrectly. | 4040| 14800047 | The WAL file size exceeds the default limit. | 4041 4042**示例:** 4043 4044```ts 4045import { BusinessError } from '@kit.BasicServicesKit'; 4046 4047let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 4048predicates.equalTo("NAME", "Lisa"); 4049if (store != undefined) { 4050 try { 4051 let rows: Number = (store as relationalStore.RdbStore).deleteSync(predicates); 4052 console.info(`Delete rows: ${rows}`); 4053 } catch (err) { 4054 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 4055 } 4056} 4057``` 4058 4059### query<sup>10+</sup> 4060 4061query(predicates: RdbPredicates, callback: AsyncCallback<ResultSet>):void 4062 4063根据指定条件查询数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 4064 4065**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4066 4067**参数:** 4068 4069| 参数名 | 类型 | 必填 | 说明 | 4070| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 4071| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 4072| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 4073 4074**错误码:** 4075 4076以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 4077 4078| **错误码ID** | **错误信息** | 4079|-----------| ------------------------------------------------------------ | 4080| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4081| 14800000 | Inner error. | 4082| 14800014 | The RdbStore or ResultSet is already closed. | 4083| 14800015 | The database does not respond. | 4084 4085**示例:** 4086 4087```ts 4088let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 4089predicates.equalTo("NAME", "Rose"); 4090if (store != undefined) { 4091 (store as relationalStore.RdbStore).query(predicates, async (err, resultSet) => { 4092 if (err) { 4093 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 4094 return; 4095 } 4096 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4097 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4098 while (resultSet.goToNextRow()) { 4099 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4100 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4101 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4102 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4103 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4104 } 4105 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4106 resultSet.close(); 4107 }); 4108} 4109``` 4110 4111### query 4112 4113query(predicates: RdbPredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void 4114 4115根据指定条件查询数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 4116 4117**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4118 4119**参数:** 4120 4121| 参数名 | 类型 | 必填 | 说明 | 4122| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 4123| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 4124| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 4125| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 4126 4127**错误码:** 4128 4129以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 4130 4131| **错误码ID** | **错误信息** | 4132|-----------| ------------------------------------------------------------ | 4133| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4134| 14800000 | Inner error. | 4135| 14800014 | The RdbStore or ResultSet is already closed. | 4136| 14800015 | The database does not respond. | 4137 4138**示例:** 4139 4140```ts 4141let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 4142predicates.equalTo("NAME", "Rose"); 4143if (store != undefined) { 4144 (store as relationalStore.RdbStore).query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], async (err, resultSet) => { 4145 if (err) { 4146 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 4147 return; 4148 } 4149 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4150 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4151 while (resultSet.goToNextRow()) { 4152 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4153 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4154 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4155 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4156 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4157 } 4158 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4159 resultSet.close(); 4160 }); 4161} 4162``` 4163 4164### query 4165 4166query(predicates: RdbPredicates, columns?: Array<string>):Promise<ResultSet> 4167 4168根据指定条件查询数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 4169 4170**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4171 4172**参数:** 4173 4174| 参数名 | 类型 | 必填 | 说明 | 4175| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 4176| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 4177| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 4178 4179**错误码:** 4180 4181以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 4182 4183| **错误码ID** | **错误信息** | 4184|-----------| ------------------------------------------------------------ | 4185| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4186| 14800000 | Inner error. | 4187| 14800014 | The RdbStore or ResultSet is already closed. | 4188| 14800015 | The database does not respond. | 4189 4190**返回值**: 4191 4192| 类型 | 说明 | 4193| ------------------------------------------------------- | -------------------------------------------------- | 4194| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 4195 4196**示例:** 4197 4198```ts 4199import { BusinessError } from '@kit.BasicServicesKit'; 4200 4201let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 4202predicates.equalTo("NAME", "Rose"); 4203if (store != undefined) { 4204 (store as relationalStore.RdbStore).query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then(async (resultSet: relationalStore.ResultSet) => { 4205 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4206 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4207 while (resultSet.goToNextRow()) { 4208 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4209 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4210 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4211 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4212 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4213 } 4214 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4215 resultSet.close(); 4216 }).catch((err: BusinessError) => { 4217 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 4218 }); 4219} 4220``` 4221 4222### querySync<sup>12+</sup> 4223 4224querySync(predicates: RdbPredicates, columns?: Array<string>):ResultSet 4225 4226根据指定条件查询数据库中的数据。对query同步接口获得的resultSet进行操作时,若逻辑复杂且循环次数过多,可能造成freeze问题,建议将此步骤放到[taskpool](../apis-arkts/js-apis-taskpool.md)线程中执行。 4227 4228**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4229 4230**参数:** 4231 4232| 参数名 | 类型 | 必填 | 说明 | 4233| ---------- | ------------------------------- | ---- | ------------------------------------------------------------ | 4234| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 4235| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。默认值为空。 | 4236 4237**错误码:** 4238 4239以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 4240 4241| **错误码ID** | **错误信息** | 4242| ------------ | ------------------------------------------------------------ | 4243| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4244| 14800000 | Inner error. | 4245| 14800014 | The RdbStore or ResultSet is already closed. | 4246| 14800015 | The database does not respond. | 4247 4248**返回值**: 4249 4250| 类型 | 说明 | 4251| ----------------------- | ----------------------------------- | 4252| [ResultSet](#resultset) | 如果操作成功,则返回ResultSet对象。 | 4253 4254**示例:** 4255 4256```ts 4257import { BusinessError } from '@kit.BasicServicesKit'; 4258 4259let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 4260predicates.equalTo("NAME", "Rose"); 4261if (store != undefined) { 4262 try { 4263 let resultSet: relationalStore.ResultSet = (store as relationalStore.RdbStore).querySync(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 4264 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4265 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4266 while (resultSet.goToNextRow()) { 4267 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4268 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4269 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4270 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4271 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4272 } 4273 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4274 resultSet.close(); 4275 } catch (err) { 4276 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 4277 } 4278} 4279``` 4280 4281### remoteQuery 4282 4283remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array<string> , callback: AsyncCallback<ResultSet>): void 4284 4285根据指定条件查询远程设备数据库中的数据。使用callback异步回调。 4286 4287> **说明:** 4288> 4289> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 4290 4291**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4292 4293**参数:** 4294 4295| 参数名 | 类型 | 必填 | 说明 | 4296| ---------- | -------------------------------------------- | ---- | --------------------------------------------------------- | 4297| device | string | 是 | 指定的远程设备ID。 | 4298| table | string | 是 | 指定的目标表名。 | 4299| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象,指定查询的条件。 | 4300| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 4301| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 4302 4303**错误码:** 4304 4305以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.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. | 4311| 14800000 | Inner error. | 4312| 14800014 | The RdbStore or ResultSet is already closed. | 4313 4314**示例:** 4315 4316```ts 4317import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 4318import { BusinessError } from '@kit.BasicServicesKit'; 4319 4320let dmInstance: distributedDeviceManager.DeviceManager; 4321let deviceId: string | undefined = undefined; 4322 4323try { 4324 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 4325 let devices = dmInstance.getAvailableDeviceListSync(); 4326 if (deviceId != undefined) { 4327 deviceId = devices[0].networkId; 4328 } 4329} catch (err) { 4330 let code = (err as BusinessError).code; 4331 let message = (err as BusinessError).message; 4332 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 4333} 4334 4335let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 4336predicates.greaterThan("id", 0); 4337if (store != undefined && deviceId != undefined) { 4338 (store as relationalStore.RdbStore).remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then(async (resultSet: relationalStore.ResultSet) => { 4339 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4340 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4341 while (resultSet.goToNextRow()) { 4342 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4343 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4344 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4345 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4346 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4347 } 4348 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4349 resultSet.close(); 4350 }).catch((err: BusinessError) => { 4351 console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`); 4352 }); 4353} 4354``` 4355 4356### remoteQuery 4357 4358remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array<string>): Promise<ResultSet> 4359 4360根据指定条件查询远程设备数据库中的数据。使用Promise异步回调。 4361 4362> **说明:** 4363> 4364> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 4365 4366**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4367 4368**参数:** 4369 4370| 参数名 | 类型 | 必填 | 说明 | 4371| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 4372| device | string | 是 | 指定的远程设备ID。 | 4373| table | string | 是 | 指定的目标表名。 | 4374| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象,指定查询的条件。 | 4375| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 4376 4377**返回值**: 4378 4379| 类型 | 说明 | 4380| ------------------------------------------------------------ | -------------------------------------------------- | 4381| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 4382 4383**错误码:** 4384 4385以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 4386 4387| **错误码ID** | **错误信息** | 4388|-----------| ------------------------------------------------------------ | 4389| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4390| 801 | Capability not supported. | 4391| 14800000 | Inner error. | 4392| 14800014 | The RdbStore or ResultSet is already closed. | 4393 4394**示例:** 4395 4396```ts 4397import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 4398import { BusinessError } from '@kit.BasicServicesKit'; 4399 4400let dmInstance: distributedDeviceManager.DeviceManager; 4401let deviceId: string | undefined = undefined; 4402 4403try { 4404 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 4405 let devices: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 4406 if (devices != undefined) { 4407 deviceId = devices[0].networkId; 4408 } 4409} catch (err) { 4410 let code = (err as BusinessError).code; 4411 let message = (err as BusinessError).message; 4412 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 4413} 4414 4415let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 4416predicates.greaterThan("id", 0); 4417if (store != undefined && deviceId != undefined) { 4418 (store as relationalStore.RdbStore).remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then(async (resultSet: relationalStore.ResultSet) => { 4419 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4420 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4421 while (resultSet.goToNextRow()) { 4422 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4423 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4424 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4425 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4426 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4427 } 4428 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4429 resultSet.close(); 4430 }).catch((err: BusinessError) => { 4431 console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`); 4432 }); 4433} 4434``` 4435 4436### querySql<sup>10+</sup> 4437 4438querySql(sql: string, callback: AsyncCallback<ResultSet>):void 4439 4440根据指定SQL语句查询数据库中的数据,SQL语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用callback异步回调。 4441 4442该接口支持[向量数据库](#storeconfig)使用,当前支持的语法见[规格限制](../../database/data-persistence-by-vector-store.md#规格限制)。 4443 4444聚合函数不支持嵌套使用。 4445 4446**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4447 4448**参数:** 4449 4450| 参数名 | 类型 | 必填 | 说明 | 4451| -------- | -------------------------------------------- | ---- |---------------------------------------| 4452| sql | string | 是 | 指定要执行的SQL语句。 | 4453| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 4454 4455**错误码:** 4456 4457以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 4458 4459| **错误码ID** | **错误信息** | 4460|-----------| ------------------------------------------------------------ | 4461| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4462| 14800000 | Inner error. | 4463| 14800014 | The RdbStore or ResultSet is already closed. | 4464| 14800015 | The database does not respond. | 4465 4466**示例:** 4467 4468关系型数据库: 4469 4470```ts 4471if (store != undefined) { 4472 (store as relationalStore.RdbStore).querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'", async (err, resultSet) => { 4473 if (err) { 4474 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 4475 return; 4476 } 4477 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4478 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4479 while (resultSet.goToNextRow()) { 4480 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4481 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4482 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4483 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4484 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4485 } 4486 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4487 resultSet.close(); 4488 }); 4489} 4490``` 4491 4492向量数据库: 4493 4494```ts 4495// 相似度的计算符号是<->,余弦距离的计算符号是<=> 4496const querySql = "select id, repr <-> '[1.5,5.6]' as distance from test ORDER BY repr <-> '[1.5,5.6]' limit 10 offset 1;"; 4497let resultSet = await store.querySql(querySql); 4498 4499// 聚合查询,其中group by支持多列 4500const querySql1 = "select id, repr from test group by id, repr having max(repr<=>'[1.5,5.6]');"; 4501let resultSet1 = await store.querySql(querySql1); 4502 4503// 子查询,最大支持嵌套32层 4504const querySql2 = "select * from test where id in (select id from test1)"; 4505let resultSet2 = await store.querySql(querySql2); 4506``` 4507 4508### querySql 4509 4510querySql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<ResultSet>):void 4511 4512根据指定SQL语句查询数据库中的数据,SQL语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用callback异步回调。 4513 4514该接口支持[向量数据库](#storeconfig)使用,当前支持的语法见[规格限制](../../database/data-persistence-by-vector-store.md#规格限制)。 4515 4516聚合函数不支持嵌套使用。 4517 4518**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4519 4520**参数:** 4521 4522| 参数名 | 类型 | 必填 | 说明 | 4523| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 4524| sql | string | 是 | 指定要执行的SQL语句。 | 4525| bindArgs | Array<[ValueType](#valuetype)> | 是 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数需为空数组。 | 4526| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 4527 4528**错误码:** 4529 4530以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 4531 4532| **错误码ID** | **错误信息** | 4533|-----------| ------------------------------------------------------------ | 4534| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4535| 14800000 | Inner error. | 4536| 14800014 | The RdbStore or ResultSet is already closed. | 4537| 14800015 | The database does not respond. | 4538 4539**示例:** 4540 4541```ts 4542if (store != undefined) { 4543 (store as relationalStore.RdbStore).querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], async (err, resultSet) => { 4544 if (err) { 4545 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 4546 return; 4547 } 4548 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4549 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4550 while (resultSet.goToNextRow()) { 4551 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4552 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4553 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4554 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4555 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4556 } 4557 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4558 resultSet.close(); 4559 }); 4560} 4561``` 4562 4563### querySql 4564 4565querySql(sql: string, bindArgs?: Array<ValueType>):Promise<ResultSet> 4566 4567根据指定SQL语句查询数据库中的数据,SQL语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用Promise异步回调。 4568 4569该接口支持[向量数据库](#storeconfig)使用,当前支持的语法见[规格限制](../../database/data-persistence-by-vector-store.md#规格限制)。 4570 4571聚合函数不支持嵌套使用。 4572 4573**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4574 4575**参数:** 4576 4577| 参数名 | 类型 | 必填 | 说明 | 4578| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4579| sql | string | 是 | 指定要执行的SQL语句。 | 4580| bindArgs | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 4581 4582**返回值**: 4583 4584| 类型 | 说明 | 4585| ------------------------------------------------------- | -------------------------------------------------- | 4586| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 4587 4588**错误码:** 4589 4590以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 4591 4592| **错误码ID** | **错误信息** | 4593|-----------| ------------------------------------------------------------ | 4594| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4595| 14800000 | Inner error. | 4596| 14800014 | The RdbStore or ResultSet is already closed. | 4597| 14800015 | The database does not respond. | 4598 4599**示例:** 4600 4601关系型数据库: 4602 4603```ts 4604import { BusinessError } from '@kit.BasicServicesKit'; 4605 4606if (store != undefined) { 4607 (store as relationalStore.RdbStore).querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'").then(async (resultSet: relationalStore.ResultSet) => { 4608 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4609 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4610 while (resultSet.goToNextRow()) { 4611 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4612 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4613 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4614 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4615 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4616 } 4617 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4618 resultSet.close(); 4619 }).catch((err: BusinessError) => { 4620 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 4621 }); 4622} 4623``` 4624 4625向量数据库: 4626 4627```ts 4628// 查询id为1,与[1.5, 2.5]相似度小于0.5,且以相似度进行升序排序的前10条数据 4629const querySql = "select id, repr <-> ? as distance from test where id = ? and repr <-> ? < 0.5 ORDER BY repr <-> ? limit 10;"; 4630const vectorValue: Float32Array = new Float32Array([1.5, 2.5]); 4631let resultSet = await store.querySql(querySql, [vectorValue, 1, vectorValue, vectorValue]); 4632``` 4633 4634### querySqlSync<sup>12+</sup> 4635 4636querySqlSync(sql: string, bindArgs?: Array<ValueType>):ResultSet 4637 4638根据指定SQL语句查询数据库中的数据,SQL语句中的各种表达式和操作符之间的关系操作符号不超过1000个。对query同步接口获得的resultSet进行操作时,若逻辑复杂且循环次数过多,可能造成freeze问题,建议将此步骤放到[taskpool](../apis-arkts/js-apis-taskpool.md)线程中执行。 4639 4640**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4641 4642**参数:** 4643 4644| 参数名 | 类型 | 必填 | 说明 | 4645| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4646| sql | string | 是 | 指定要执行的SQL语句。 | 4647| bindArgs | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。默认值为空。 | 4648 4649**返回值**: 4650 4651| 类型 | 说明 | 4652| ----------------------- | ----------------------------------- | 4653| [ResultSet](#resultset) | 如果操作成功,则返回ResultSet对象。 | 4654 4655**错误码:** 4656 4657以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 4658 4659| **错误码ID** | **错误信息** | 4660| ------------ | ------------------------------------------------------------ | 4661| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4662| 14800000 | Inner error. | 4663| 14800014 | The RdbStore or ResultSet is already closed. | 4664| 14800015 | The database does not respond. | 4665 4666**示例:** 4667 4668```ts 4669import { BusinessError } from '@kit.BasicServicesKit'; 4670 4671if (store != undefined) { 4672 try { 4673 let resultSet: relationalStore.ResultSet = (store as relationalStore.RdbStore).querySqlSync("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'"); 4674 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 4675 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 4676 while (resultSet.goToNextRow()) { 4677 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 4678 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 4679 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 4680 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 4681 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 4682 } 4683 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 4684 resultSet.close(); 4685 } catch (err) { 4686 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 4687 } 4688} 4689``` 4690 4691### executeSql<sup>10+</sup> 4692 4693executeSql(sql: string, callback: AsyncCallback<void>):void 4694 4695执行包含指定参数但不返回值的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用callback异步回调。 4696 4697此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 4698 4699不支持分号分隔的多条语句。 4700 4701**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4702 4703**参数:** 4704 4705| 参数名 | 类型 | 必填 | 说明 | 4706| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4707| sql | string | 是 | 指定要执行的SQL语句。 | 4708| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 4709 4710**错误码:** 4711 4712以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4713 4714| **错误码ID** | **错误信息** | 4715|-----------| ------------------------------------------------------------ | 4716| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4717| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 4718| 14800000 | Inner error. | 4719| 14800011 | Failed to open the database because it is corrupted. | 4720| 14800014 | The RdbStore or ResultSet is already closed. | 4721| 14800015 | The database does not respond. | 4722| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 4723| 14800022 | SQLite: Callback routine requested an abort. | 4724| 14800023 | SQLite: Access permission denied. | 4725| 14800024 | SQLite: The database file is locked. | 4726| 14800025 | SQLite: A table in the database is locked. | 4727| 14800026 | SQLite: The database is out of memory. | 4728| 14800027 | SQLite: Attempt to write a readonly database. | 4729| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4730| 14800029 | SQLite: The database is full. | 4731| 14800030 | SQLite: Unable to open the database file. | 4732| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4733| 14800032 | SQLite: Abort due to constraint violation. | 4734| 14800033 | SQLite: Data type mismatch. | 4735| 14800034 | SQLite: Library used incorrectly. | 4736| 14800047 | The WAL file size exceeds the default limit. | 4737 4738**示例:** 4739 4740```ts 4741const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = 'zhangsan'"; 4742if (store != undefined) { 4743 (store as relationalStore.RdbStore).executeSql(SQL_DELETE_TABLE, (err) => { 4744 if (err) { 4745 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 4746 return; 4747 } 4748 console.info('Delete table done.'); 4749 }); 4750} 4751``` 4752 4753### executeSql 4754 4755executeSql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<void>):void 4756 4757执行包含指定参数但不返回值的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用callback异步回调。 4758 4759此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 4760 4761不支持分号分隔的多条语句。 4762 4763**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4764 4765**参数:** 4766 4767| 参数名 | 类型 | 必填 | 说明 | 4768| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4769| sql | string | 是 | 指定要执行的SQL语句。 | 4770| bindArgs | Array<[ValueType](#valuetype)> | 是 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数需为空数组。 | 4771| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 4772 4773**错误码:** 4774 4775以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4776 4777| **错误码ID** | **错误信息** | 4778|-----------| ------------------------------------------------------------ | 4779| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4780| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 4781| 14800000 | Inner error. | 4782| 14800011 | Failed to open the database because it is corrupted. | 4783| 14800014 | The RdbStore or ResultSet is already closed. | 4784| 14800015 | The database does not respond. | 4785| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 4786| 14800022 | SQLite: Callback routine requested an abort. | 4787| 14800023 | SQLite: Access permission denied. | 4788| 14800024 | SQLite: The database file is locked. | 4789| 14800025 | SQLite: A table in the database is locked. | 4790| 14800026 | SQLite: The database is out of memory. | 4791| 14800027 | SQLite: Attempt to write a readonly database. | 4792| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4793| 14800029 | SQLite: The database is full. | 4794| 14800030 | SQLite: Unable to open the database file. | 4795| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4796| 14800032 | SQLite: Abort due to constraint violation. | 4797| 14800033 | SQLite: Data type mismatch. | 4798| 14800034 | SQLite: Library used incorrectly. | 4799| 14800047 | The WAL file size exceeds the default limit. | 4800 4801**示例:** 4802 4803```ts 4804const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = ?"; 4805if (store != undefined) { 4806 (store as relationalStore.RdbStore).executeSql(SQL_DELETE_TABLE, ['zhangsan'], (err) => { 4807 if (err) { 4808 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 4809 return; 4810 } 4811 console.info('Delete table done.'); 4812 }); 4813} 4814``` 4815 4816### executeSql 4817 4818executeSql(sql: string, bindArgs?: Array<ValueType>):Promise<void> 4819 4820执行包含指定参数但不返回值的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用Promise异步回调。 4821 4822此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 4823 4824不支持分号分隔的多条语句。 4825 4826**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4827 4828**参数:** 4829 4830| 参数名 | 类型 | 必填 | 说明 | 4831| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4832| sql | string | 是 | 指定要执行的SQL语句。 | 4833| bindArgs | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 4834 4835**返回值**: 4836 4837| 类型 | 说明 | 4838| ------------------- | ------------------------- | 4839| Promise<void> | 无返回结果的Promise对象。 | 4840 4841**错误码:** 4842 4843以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4844 4845| **错误码ID** | **错误信息** | 4846|-----------| ------------------------------------------------------------ | 4847| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4848| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 4849| 14800000 | Inner error. | 4850| 14800011 | Failed to open the database because it is corrupted. | 4851| 14800014 | The RdbStore or ResultSet is already closed. | 4852| 14800015 | The database does not respond. | 4853| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 4854| 14800022 | SQLite: Callback routine requested an abort. | 4855| 14800023 | SQLite: Access permission denied. | 4856| 14800024 | SQLite: The database file is locked. | 4857| 14800025 | SQLite: A table in the database is locked. | 4858| 14800026 | SQLite: The database is out of memory. | 4859| 14800027 | SQLite: Attempt to write a readonly database. | 4860| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4861| 14800029 | SQLite: The database is full. | 4862| 14800030 | SQLite: Unable to open the database file. | 4863| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4864| 14800032 | SQLite: Abort due to constraint violation. | 4865| 14800033 | SQLite: Data type mismatch. | 4866| 14800034 | SQLite: Library used incorrectly. | 4867| 14800047 | The WAL file size exceeds the default limit. | 4868 4869**示例:** 4870 4871```ts 4872import { BusinessError } from '@kit.BasicServicesKit'; 4873 4874const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = 'zhangsan'"; 4875if (store != undefined) { 4876 (store as relationalStore.RdbStore).executeSql(SQL_DELETE_TABLE).then(() => { 4877 console.info('Delete table done.'); 4878 }).catch((err: BusinessError) => { 4879 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 4880 }); 4881} 4882``` 4883 4884### execute<sup>12+</sup> 4885 4886execute(sql: string, args?: Array<ValueType>):Promise<ValueType> 4887 4888执行包含指定参数的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,返回值类型为ValueType,使用Promise异步回调。 4889 4890该接口支持执行增删改操作,支持执行PRAGMA语法的sql,支持对表的操作(建表、删表、修改表),返回结果类型由执行具体sql的结果决定。 4891 4892此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 4893 4894向量数据库使用该接口执行插入操作,数据来源于子查询时,支持全字段插入,暂不支持部分字段插入。 4895 4896不支持分号分隔的多条语句。 4897 4898**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4899 4900**参数:** 4901 4902| 参数名 | 类型 | 必填 | 说明 | 4903| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 4904| sql | string | 是 | 指定要执行的SQL语句。 | 4905| args | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 4906 4907**返回值**: 4908 4909| 类型 | 说明 | 4910| ------------------- | ------------------------- | 4911| Promise<[ValueType](#valuetype)> | Promise对象,返回sql执行后的结果。 | 4912 4913**错误码:** 4914 4915以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 4916 4917| **错误码ID** | **错误信息** | 4918|-----------| ------------------------------------------------------------ | 4919| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 4920| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 4921| 14800000 | Inner error. | 4922| 14800011 | Failed to open the database because it is corrupted. | 4923| 14800014 | The RdbStore or ResultSet is already closed. | 4924| 14800015 | The database does not respond. | 4925| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 4926| 14800022 | SQLite: Callback routine requested an abort. | 4927| 14800023 | SQLite: Access permission denied. | 4928| 14800024 | SQLite: The database file is locked. | 4929| 14800025 | SQLite: A table in the database is locked. | 4930| 14800026 | SQLite: The database is out of memory. | 4931| 14800027 | SQLite: Attempt to write a readonly database. | 4932| 14800028 | SQLite: Some kind of disk I/O error occurred. | 4933| 14800029 | SQLite: The database is full. | 4934| 14800030 | SQLite: Unable to open the database file. | 4935| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 4936| 14800032 | SQLite: Abort due to constraint violation. | 4937| 14800033 | SQLite: Data type mismatch. | 4938| 14800034 | SQLite: Library used incorrectly. | 4939| 14800047 | The WAL file size exceeds the default limit. | 4940 4941**示例:** 4942 4943关系型数据库: 4944 4945```ts 4946import { BusinessError } from '@kit.BasicServicesKit'; 4947 4948// 校验数据库完整性 4949if (store != undefined) { 4950 const SQL_CHECK_INTEGRITY = 'PRAGMA integrity_check'; 4951 (store as relationalStore.RdbStore).execute(SQL_CHECK_INTEGRITY).then((data) => { 4952 console.info(`check result: ${data}`); 4953 }).catch((err: BusinessError) => { 4954 console.error(`check failed, code is ${err.code}, message is ${err.message}`); 4955 }); 4956} 4957 4958// 删除表中所有数据 4959if (store != undefined) { 4960 const SQL_DELETE_TABLE = 'DELETE FROM test'; 4961 (store as relationalStore.RdbStore).execute(SQL_DELETE_TABLE).then((data) => { 4962 console.info(`delete result: ${data}`); 4963 }).catch((err: BusinessError) => { 4964 console.error(`delete failed, code is ${err.code}, message is ${err.message}`); 4965 }); 4966} 4967 4968// 删表 4969if (store != undefined) { 4970 const SQL_DROP_TABLE = 'DROP TABLE test'; 4971 (store as relationalStore.RdbStore).execute(SQL_DROP_TABLE).then((data) => { 4972 console.info(`drop result: ${data}`); 4973 }).catch((err: BusinessError) => { 4974 console.error(`drop failed, code is ${err.code}, message is ${err.message}`); 4975 }); 4976} 4977``` 4978 4979向量数据库: 4980 4981```ts 4982// FLOATVECTOR(2)是维度为2的向量属性,后续操作repr需依照该维度进行。 4983let createSql = "CREATE TABLE test (ID INTEGER PRIMARY KEY,REPR FLOATVECTOR(2));"; 4984// 建表 4985await store!.execute(createSql); 4986// 使用参数绑定插入数据 4987let insertSql = "insert into test VALUES(?, ?);"; 4988const vectorValue: Float32Array = Float32Array.from([1.5, 6.6]); 4989await store!.execute(insertSql, [0, vectorValue]); 4990// 不使用绑定参数直接执行 4991await store!.execute("insert into test values(1, '[3.5, 1.8]');"); 4992``` 4993 4994### execute<sup>12+</sup> 4995 4996execute(sql: string, txId: number, args?: Array<ValueType>): Promise<ValueType> 4997 4998执行包含指定参数的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用Promise异步回调。 4999 5000该接口仅支持[向量数据库](#storeconfig)使用。使用该接口执行插入操作,数据来源于子查询时,支持全字段插入,暂不支持部分字段插入。 5001 5002此接口不支持执行查询,可以使用[querySql](#querysql10)接口代替。 5003 5004不支持分号分隔的多条语句。 5005 5006**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5007 5008**参数:** 5009 5010| 参数名 | 类型 | 必填 | 说明 | 5011| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 5012| sql | string | 是 | 指定要执行的SQL语句。 | 5013| txId | number | 是 | 通过[beginTrans](#begintrans12)获取的事务ID,如果传0,该语句默认在单独事务内。 | 5014| args | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。该参数不填,填null或者填undefined,都认为是sql参数语句完整。 | 5015 5016**返回值**: 5017 5018| 类型 | 说明 | 5019| ------------------- | ------------------------- | 5020| Promise<[ValueType](#valuetype)> | Promise对象,返回null。 | 5021 5022**错误码:** 5023 5024以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5025 5026| **错误码ID** | **错误信息** | 5027|-----------| ------------------------------------------------------------ | 5028| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5029| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 5030| 14800000 | Inner error. | 5031| 14800011 | Failed to open the database because it is corrupted. | 5032| 14800014 | The RdbStore or ResultSet is already closed. | 5033| 14800015 | The database does not respond. | 5034| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5035| 14800022 | SQLite: Callback routine requested an abort. | 5036| 14800023 | SQLite: Access permission denied. | 5037| 14800024 | SQLite: The database file is locked. | 5038| 14800025 | SQLite: A table in the database is locked. | 5039| 14800026 | SQLite: The database is out of memory. | 5040| 14800027 | SQLite: Attempt to write a readonly database. | 5041| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5042| 14800029 | SQLite: The database is full. | 5043| 14800030 | SQLite: Unable to open the database file. | 5044| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5045| 14800032 | SQLite: Abort due to constraint violation. | 5046| 14800033 | SQLite: Data type mismatch. | 5047| 14800034 | SQLite: Library used incorrectly. | 5048| 14800047 | The WAL file size exceeds the default limit. | 5049 5050**示例:** 5051 5052```ts 5053import { BusinessError } from '@kit.BasicServicesKit'; 5054if (store != null) { 5055 let txId: number; 5056 (store as relationalStore.RdbStore).beginTrans().then((txId: number) => { 5057 (store as relationalStore.RdbStore).execute("DELETE FROM TEST WHERE age = ? OR age = ?", txId, ["18", "20"]) 5058 .then(() => { 5059 (store as relationalStore.RdbStore).commit(txId); 5060 }) 5061 .catch((err: BusinessError) => { 5062 (store as relationalStore.RdbStore).rollback(txId); 5063 console.error(`execute sql failed, code is ${err.code},message is ${err.message}`); 5064 }); 5065 }); 5066} 5067``` 5068 5069### executeSync<sup>12+</sup> 5070 5071executeSync(sql: string, args?: Array<ValueType>): ValueType 5072 5073执行包含指定参数的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,返回值类型为ValueType。 5074 5075该接口支持执行增删改操作,支持执行PRAGMA语法的sql,支持对表的操作(建表、删表、修改表),返回结果类型由执行具体sql的结果决定。 5076 5077此接口不支持执行查询、附加数据库和事务操作,可以使用[querySql](#querysql10)、[query](#query10)、[attach](#attach12)、[beginTransaction](#begintransaction)、[commit](#commit)等接口代替。 5078 5079不支持分号分隔的多条语句。 5080 5081**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5082 5083**参数:** 5084 5085| 参数名 | 类型 | 必填 | 说明 | 5086| ------ | ------------------------------------ | ---- | ------------------------------------------------------------ | 5087| sql | string | 是 | 指定要执行的SQL语句。 | 5088| args | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。该参数不填,或者填null或undefined,都认为是sql参数语句完整。默认值为空。 | 5089 5090**返回值**: 5091 5092| 类型 | 说明 | 5093| ----------------------- | ------------------- | 5094| [ValueType](#valuetype) | 返回sql执行后的结果 | 5095 5096**错误码:** 5097 5098以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5099 5100| **错误码ID** | **错误信息** | 5101| ------------ | ------------------------------------------------------------ | 5102| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5103| 14800000 | Inner error. | 5104| 14800011 | Failed to open the database because it is corrupted. | 5105| 14800014 | The RdbStore or ResultSet is already closed. | 5106| 14800015 | The database does not respond. | 5107| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5108| 14800022 | SQLite: Callback routine requested an abort. | 5109| 14800023 | SQLite: Access permission denied. | 5110| 14800024 | SQLite: The database file is locked. | 5111| 14800025 | SQLite: A table in the database is locked. | 5112| 14800026 | SQLite: The database is out of memory. | 5113| 14800027 | SQLite: Attempt to write a readonly database. | 5114| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5115| 14800029 | SQLite: The database is full. | 5116| 14800030 | SQLite: Unable to open the database file. | 5117| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5118| 14800032 | SQLite: Abort due to constraint violation. | 5119| 14800033 | SQLite: Data type mismatch. | 5120| 14800034 | SQLite: Library used incorrectly. | 5121| 14800047 | The WAL file size exceeds the default limit. | 5122 5123**示例:** 5124 5125```ts 5126import { BusinessError } from '@kit.BasicServicesKit'; 5127 5128// 校验数据库完整性 5129if (store != undefined) { 5130 const SQL_CHECK_INTEGRITY = 'PRAGMA integrity_check'; 5131 try { 5132 let data = (store as relationalStore.RdbStore).executeSync(SQL_CHECK_INTEGRITY); 5133 console.info(`check result: ${data}`); 5134 } catch (err) { 5135 console.error(`check failed, code is ${err.code}, message is ${err.message}`); 5136 } 5137} 5138 5139// 删除表中所有数据 5140if (store != undefined) { 5141 const SQL_DELETE_TABLE = 'DELETE FROM test'; 5142 try { 5143 let data = (store as relationalStore.RdbStore).executeSync(SQL_DELETE_TABLE); 5144 console.info(`delete result: ${data}`); 5145 } catch (err) { 5146 console.error(`delete failed, code is ${err.code}, message is ${err.message}`); 5147 } 5148} 5149 5150// 删表 5151if (store != undefined) { 5152 const SQL_DROP_TABLE = 'DROP TABLE test'; 5153 try { 5154 let data = (store as relationalStore.RdbStore).executeSync(SQL_DROP_TABLE); 5155 console.info(`drop result: ${data}`); 5156 } catch (err) { 5157 console.error(`drop failed, code is ${err.code}, message is ${err.message}`); 5158 } 5159} 5160``` 5161 5162### getModifyTime<sup>10+</sup> 5163 5164getModifyTime(table: string, columnName: string, primaryKeys: PRIKeyType[], callback: AsyncCallback<ModifyTime>): void 5165 5166获取数据库表中数据的最后修改时间,使用callback异步回调。 5167 5168**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5169 5170**参数:** 5171 5172| 参数名 | 类型 | 必填 | 说明 | 5173| ----------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | 5174| table | string | 是 | 指定要查询的数据库表的表名。 | 5175| columnName | string | 是 | 指定要查询的数据库表的列名。 | 5176| primaryKeys | [PRIKeyType](#prikeytype10)[] | 是 | 指定要查询的行的主键。<br>如果数据库表无主键,参数columnName需传入"rowid",此时primaryKeys为要查询的数据库表的行号。<br>如果数据库表无主键,参数columnName传入不为"rowid",返回对应的错误码。 | 5177| callback | AsyncCallback<[ModifyTime](#modifytime10)> | 是 | 指定callback回调函数。如果操作成功,则返回ModifyTime对象,表示数据的最后修改时间。 | 5178 5179**错误码:** 5180 5181以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5182 5183| **错误码ID** | **错误信息** | 5184|-----------| ------------------------------------------------------------ | 5185| 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. | 5186| 801 | Capability not supported. | 5187| 14800000 | Inner error. | 5188| 14800011 | Failed to open the database because it is corrupted. | 5189| 14800014 | The RdbStore or ResultSet is already closed. | 5190| 14800015 | The database does not respond. | 5191| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5192| 14800022 | SQLite: Callback routine requested an abort. | 5193| 14800023 | SQLite: Access permission denied. | 5194| 14800024 | SQLite: The database file is locked. | 5195| 14800025 | SQLite: A table in the database is locked. | 5196| 14800026 | SQLite: The database is out of memory. | 5197| 14800027 | SQLite: Attempt to write a readonly database. | 5198| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5199| 14800029 | SQLite: The database is full. | 5200| 14800030 | SQLite: Unable to open the database file. | 5201| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5202| 14800032 | SQLite: Abort due to constraint violation. | 5203| 14800033 | SQLite: Data type mismatch. | 5204| 14800034 | SQLite: Library used incorrectly. | 5205 5206**示例:** 5207 5208```ts 5209let PRIKey = [1, 4, 2, 3]; 5210if (store != undefined) { 5211 (store as relationalStore.RdbStore).getModifyTime("EMPLOYEE", "NAME", PRIKey, (err, modifyTime: relationalStore.ModifyTime) => { 5212 if (err) { 5213 console.error(`getModifyTime failed, code is ${err.code},message is ${err.message}`); 5214 return; 5215 } 5216 let size = modifyTime.size; 5217 }); 5218} 5219``` 5220 5221### getModifyTime<sup>10+</sup> 5222 5223getModifyTime(table: string, columnName: string, primaryKeys: PRIKeyType[]): Promise<ModifyTime> 5224 5225获取数据库表中数据的最后修改时间,使用Promise异步回调。 5226 5227**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5228 5229**参数:** 5230 5231| 参数名 | 类型 | 必填 | 说明 | 5232| ----------- | ----------------------------- | ---- | ------------------------------------------------------------ | 5233| table | string | 是 | 指定要查询的数据库表的表名。 | 5234| columnName | string | 是 | 指定要查询的数据库表的列名。 | 5235| primaryKeys | [PRIKeyType](#prikeytype10)[] | 是 | 指定要查询的行的主键。<br>如果数据库表无主键,参数columnName需传入"rowid",此时primaryKeys为要查询的数据库表的行号。<br>如果数据库表无主键,参数columnName传入不为"rowid",返回对应的错误码。 | 5236 5237**返回值**: 5238 5239| 类型 | 说明 | 5240| ------------------------------------------ | --------------------------------------------------------- | 5241| Promise<[ModifyTime](#modifytime10)> | 返回ModifyTime类型的Promise对象,表示数据最后的修改时间。 | 5242 5243**错误码:** 5244 5245以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5246 5247| **错误码ID** | **错误信息** | 5248|-----------| ------------------------------------------------------------ | 5249| 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. | 5250| 801 | Capability not supported. | 5251| 14800000 | Inner error. | 5252| 14800011 | Failed to open the database because it is corrupted. | 5253| 14800014 | The RdbStore or ResultSet is already closed. | 5254| 14800015 | The database does not respond. | 5255| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5256| 14800022 | SQLite: Callback routine requested an abort. | 5257| 14800023 | SQLite: Access permission denied. | 5258| 14800024 | SQLite: The database file is locked. | 5259| 14800025 | SQLite: A table in the database is locked. | 5260| 14800026 | SQLite: The database is out of memory. | 5261| 14800027 | SQLite: Attempt to write a readonly database. | 5262| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5263| 14800029 | SQLite: The database is full. | 5264| 14800030 | SQLite: Unable to open the database file. | 5265| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5266| 14800032 | SQLite: Abort due to constraint violation. | 5267| 14800033 | SQLite: Data type mismatch. | 5268| 14800034 | SQLite: Library used incorrectly. | 5269 5270**示例:** 5271 5272```ts 5273import { BusinessError } from '@kit.BasicServicesKit'; 5274 5275let PRIKey = [1, 2, 3]; 5276if (store != undefined) { 5277 (store as relationalStore.RdbStore).getModifyTime("EMPLOYEE", "NAME", PRIKey) 5278 .then((modifyTime: relationalStore.ModifyTime) => { 5279 let size = modifyTime.size; 5280 }) 5281 .catch((err: BusinessError) => { 5282 console.error(`getModifyTime failed, code is ${err.code},message is ${err.message}`); 5283 }); 5284} 5285``` 5286 5287### beginTransaction 5288 5289beginTransaction():void 5290 5291在开始执行SQL语句之前,开始事务。 5292此接口不允许嵌套事务,且不支持在多进程或多线程中使用。 5293 5294**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5295 5296**错误码:** 5297 5298以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5299 5300| **错误码ID** | **错误信息** | 5301|-----------| ------------------------------------------------------------ | 5302| 401 | Parameter error. The store must not be nullptr. | 5303| 14800000 | Inner error. | 5304| 14800011 | Failed to open the database because it is corrupted. | 5305| 14800014 | The RdbStore or ResultSet is already closed. | 5306| 14800015 | The database does not respond. | 5307| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5308| 14800022 | SQLite: Callback routine requested an abort. | 5309| 14800023 | SQLite: Access permission denied. | 5310| 14800024 | SQLite: The database file is locked. | 5311| 14800025 | SQLite: A table in the database is locked. | 5312| 14800026 | SQLite: The database is out of memory. | 5313| 14800027 | SQLite: Attempt to write a readonly database. | 5314| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5315| 14800029 | SQLite: The database is full. | 5316| 14800030 | SQLite: Unable to open the database file. | 5317| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5318| 14800032 | SQLite: Abort due to constraint violation. | 5319| 14800033 | SQLite: Data type mismatch. | 5320| 14800034 | SQLite: Library used incorrectly. | 5321| 14800047 | The WAL file size exceeds the default limit. | 5322 5323**示例:** 5324 5325```ts 5326let value1 = "Lisa"; 5327let value2 = 18; 5328let value3 = 100.5; 5329let value4 = new Uint8Array([1, 2, 3]); 5330 5331if (store != undefined) { 5332 (store as relationalStore.RdbStore).beginTransaction(); 5333 const valueBucket: relationalStore.ValuesBucket = { 5334 'NAME': value1, 5335 'AGE': value2, 5336 'SALARY': value3, 5337 'CODES': value4 5338 }; 5339 (store as relationalStore.RdbStore).insert("test", valueBucket); 5340 (store as relationalStore.RdbStore).commit(); 5341} 5342``` 5343 5344### beginTrans<sup>12+</sup> 5345 5346beginTrans(): Promise<number> 5347 5348在开始执行SQL语句之前,开始事务,使用Promise异步回调。 5349 5350与[beginTransaction](#begintransaction)的区别在于:该接口会返回事务ID,[execute](#execute12-1)可以指定不同事务ID达到事务隔离目的。 5351 5352该接口仅支持[向量数据库](#storeconfig)使用。 5353 5354**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5355 5356**返回值**: 5357 5358| 类型 | 说明 | 5359| ------------------- | ------------------------- | 5360| Promise<number> | Promise对象,返回事务ID。 | 5361 5362**错误码:** 5363 5364以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5365 5366| **错误码ID** | **错误信息** | 5367|-----------| ------------------------------------------------------------ | 5368| 401 | Parameter error. The store must not be nullptr. | 5369| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 5370| 14800000 | Inner error. | 5371| 14800011 | Failed to open the database because it is corrupted. | 5372| 14800014 | The RdbStore or ResultSet is already closed. | 5373| 14800015 | The database does not respond. | 5374| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5375| 14800022 | SQLite: Callback routine requested an abort. | 5376| 14800023 | SQLite: Access permission denied. | 5377| 14800024 | SQLite: The database file is locked. | 5378| 14800025 | SQLite: A table in the database is locked. | 5379| 14800026 | SQLite: The database is out of memory. | 5380| 14800027 | SQLite: Attempt to write a readonly database. | 5381| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5382| 14800029 | SQLite: The database is full. | 5383| 14800030 | SQLite: Unable to open the database file. | 5384| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5385| 14800032 | SQLite: Abort due to constraint violation. | 5386| 14800033 | SQLite: Data type mismatch. | 5387| 14800034 | SQLite: Library used incorrectly. | 5388| 14800047 | The WAL file size exceeds the default limit. | 5389 5390**示例:** 5391 5392```ts 5393import { BusinessError } from '@kit.BasicServicesKit'; 5394if (store != null) { 5395 let txId: number; 5396 (store as relationalStore.RdbStore).beginTrans().then((txId: number) => { 5397 (store as relationalStore.RdbStore).execute("DELETE FROM TEST WHERE age = ? OR age = ?", txId, ["18", "20"]) 5398 .then(() => { 5399 (store as relationalStore.RdbStore).commit(txId); 5400 }) 5401 .catch((err: BusinessError) => { 5402 (store as relationalStore.RdbStore).rollback(txId); 5403 console.error(`execute sql failed, code is ${err.code},message is ${err.message}`); 5404 }); 5405 }); 5406} 5407``` 5408 5409### createTransaction<sup>14+</sup> 5410 5411createTransaction(options?: TransactionOptions): Promise<Transaction> 5412 5413创建一个事务对象并开始事务,使用Promise异步回调。 5414 5415与[beginTransaction](#begintransaction)的区别在于:createTransaction接口会返回一个事务对象,不同事务对象之间是隔离的。使用事务对象进行插入、删除或更新数据等操作,无法被注册数据变更通知[on('dataChange')](#ondatachange)监听到。 5416 5417一个store最多支持同时存在四个事务对象,超过后会返回14800015错误码,此时需要检查是否持有事务对象时间过长或并发事务过多,若确认无法通过上述优化解决问题,建议等待现有事务释放后,再尝试新建事务对象。 5418 5419优先使用createTransaction,不再推荐使用beginTransaction。 5420 5421**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5422 5423**参数:** 5424 5425| 参数名 | 类型 | 必填 | 说明 | 5426| ----------- | ----------------------------- | ---- | ------------------------------------------------------------ | 5427| options | [TransactionOptions](#transactionoptions14) | 否 | 表示事务对象的配置信息。 | 5428 5429**返回值**: 5430 5431| 类型 | 说明 | 5432| ------------------- | ------------------------- | 5433| Promise<[Transaction](#transaction14)> | Promise对象,返回事务对象。 | 5434 5435**错误码:** 5436 5437以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5438 5439| **错误码ID** | **错误信息** | 5440|-----------| ------------------------------------------------------------ | 5441| 14800000 | Inner error. | 5442| 14800011 | Failed to open the database because it is corrupted. | 5443| 14800014 | The RdbStore or ResultSet is already closed. | 5444| 14800015 | The database is busy. | 5445| 14800023 | SQLite: Access permission denied. | 5446| 14800024 | SQLite: The database file is locked. | 5447| 14800026 | SQLite: The database is out of memory. | 5448| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5449| 14800029 | SQLite: The database is full. | 5450| 14800030 | SQLite: Unable to open the database file. | 5451 5452**示例:** 5453 5454```ts 5455import { BusinessError } from '@kit.BasicServicesKit'; 5456 5457if (store != undefined) { 5458 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 5459 transaction.execute("DELETE FROM test WHERE age = ? OR age = ?", [21, 20]).then(() => { 5460 transaction.commit(); 5461 }).catch((e: BusinessError) => { 5462 transaction.rollback(); 5463 console.error(`execute sql failed, code is ${e.code},message is ${e.message}`); 5464 }); 5465 }).catch((err: BusinessError) => { 5466 console.error(`createTransaction faided, code is ${err.code},message is ${err.message}`); 5467 }); 5468} 5469``` 5470 5471### commit 5472 5473commit():void 5474 5475提交已执行的SQL语句,跟[beginTransaction](#begintransaction)配合使用。 5476此接口不允许嵌套事务,且不支持在多进程或多线程中使用。 5477 5478**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5479 5480**错误码:** 5481 5482以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5483 5484| **错误码ID** | **错误信息** | 5485|-----------| ------------------------------------------------------------ | 5486| 401 | Parameter error. The store must not be nullptr. | 5487| 14800000 | Inner error. | 5488| 14800011 | Failed to open the database because it is corrupted. | 5489| 14800014 | The RdbStore or ResultSet is already closed. | 5490| 14800015 | The database does not respond. | 5491| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5492| 14800022 | SQLite: Callback routine requested an abort. | 5493| 14800023 | SQLite: Access permission denied. | 5494| 14800024 | SQLite: The database file is locked. | 5495| 14800025 | SQLite: A table in the database is locked. | 5496| 14800026 | SQLite: The database is out of memory. | 5497| 14800027 | SQLite: Attempt to write a readonly database. | 5498| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5499| 14800029 | SQLite: The database is full. | 5500| 14800030 | SQLite: Unable to open the database file. | 5501| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5502| 14800032 | SQLite: Abort due to constraint violation. | 5503| 14800033 | SQLite: Data type mismatch. | 5504| 14800034 | SQLite: Library used incorrectly. | 5505 5506**示例:** 5507 5508```ts 5509let value1 = "Lisa"; 5510let value2 = 18; 5511let value3 = 100.5; 5512let value4 = new Uint8Array([1, 2, 3]); 5513 5514if (store != undefined) { 5515 (store as relationalStore.RdbStore).beginTransaction(); 5516 const valueBucket: relationalStore.ValuesBucket = { 5517 'NAME': value1, 5518 'AGE': value2, 5519 'SALARY': value3, 5520 'CODES': value4 5521 }; 5522 (store as relationalStore.RdbStore).insert("test", valueBucket); 5523 (store as relationalStore.RdbStore).commit(); 5524} 5525``` 5526 5527### commit<sup>12+</sup> 5528 5529commit(txId : number):Promise<void> 5530 5531提交已执行的SQL语句,跟[beginTrans](#begintrans12)配合使用。 5532 5533该接口仅支持[向量数据库](#storeconfig)使用。 5534 5535**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5536 5537**参数:** 5538 5539| 参数名 | 类型 | 必填 | 说明 | 5540| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 5541| txId | number | 是 | 通过[beginTrans](#begintrans12)获取的事务ID。 | 5542 5543**返回值**: 5544 5545| 类型 | 说明 | 5546| ------------------- | ------------------------- | 5547| Promise<void> | 无返回结果的Promise对象。 | 5548 5549**错误码:** 5550 5551以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5552 5553| **错误码ID** | **错误信息** | 5554|-----------| ------------------------------------------------------------ | 5555| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5556| 14800000 | Inner error. | 5557| 14800011 | Failed to open the database because it is corrupted. | 5558| 14800014 | The RdbStore or ResultSet is already closed. | 5559| 14800015 | The database does not respond. | 5560| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5561| 14800022 | SQLite: Callback routine requested an abort. | 5562| 14800023 | SQLite: Access permission denied. | 5563| 14800024 | SQLite: The database file is locked. | 5564| 14800025 | SQLite: A table in the database is locked. | 5565| 14800026 | SQLite: The database is out of memory. | 5566| 14800027 | SQLite: Attempt to write a readonly database. | 5567| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5568| 14800029 | SQLite: The database is full. | 5569| 14800030 | SQLite: Unable to open the database file. | 5570| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5571| 14800032 | SQLite: Abort due to constraint violation. | 5572| 14800033 | SQLite: Data type mismatch. | 5573| 14800034 | SQLite: Library used incorrectly. | 5574 5575**示例:** 5576 5577```ts 5578import { BusinessError } from '@kit.BasicServicesKit'; 5579if (store != null) { 5580 let txId: number; 5581 (store as relationalStore.RdbStore).beginTrans().then((txId: number) => { 5582 (store as relationalStore.RdbStore).execute("DELETE FROM TEST WHERE age = ? OR age = ?", txId, ["18", "20"]) 5583 .then(() => { 5584 (store as relationalStore.RdbStore).commit(txId); 5585 }) 5586 .catch((err: BusinessError) => { 5587 (store as relationalStore.RdbStore).rollback(txId); 5588 console.error(`execute sql failed, code is ${err.code},message is ${err.message}`); 5589 }); 5590 }); 5591} 5592``` 5593 5594### rollBack 5595 5596rollBack():void 5597 5598回滚已经执行的SQL语句。 5599此接口不允许嵌套事务,且不支持在多进程或多线程中使用。 5600 5601**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5602 5603**错误码:** 5604 5605以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5606 5607| **错误码ID** | **错误信息** | 5608|-----------| ------------------------------------------------------------ | 5609| 401 | Parameter error. The store must not be nullptr. | 5610| 14800000 | Inner error. | 5611| 14800011 | Failed to open the database because it is corrupted. | 5612| 14800014 | The RdbStore or ResultSet is already closed. | 5613| 14800015 | The database does not respond. | 5614| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5615| 14800022 | SQLite: Callback routine requested an abort. | 5616| 14800023 | SQLite: Access permission denied. | 5617| 14800024 | SQLite: The database file is locked. | 5618| 14800025 | SQLite: A table in the database is locked. | 5619| 14800026 | SQLite: The database is out of memory. | 5620| 14800027 | SQLite: Attempt to write a readonly database. | 5621| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5622| 14800029 | SQLite: The database is full. | 5623| 14800030 | SQLite: Unable to open the database file. | 5624| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5625| 14800032 | SQLite: Abort due to constraint violation. | 5626| 14800033 | SQLite: Data type mismatch. | 5627| 14800034 | SQLite: Library used incorrectly. | 5628 5629**示例:** 5630 5631```ts 5632import { BusinessError } from '@kit.BasicServicesKit'; 5633 5634let value1 = "Lisa"; 5635let value2 = 18; 5636let value3 = 100.5; 5637let value4 = new Uint8Array([1, 2, 3]); 5638 5639if (store != undefined) { 5640 try { 5641 (store as relationalStore.RdbStore).beginTransaction(); 5642 const valueBucket: relationalStore.ValuesBucket = { 5643 'NAME': value1, 5644 'AGE': value2, 5645 'SALARY': value3, 5646 'CODES': value4 5647 }; 5648 (store as relationalStore.RdbStore).insert("test", valueBucket); 5649 (store as relationalStore.RdbStore).commit(); 5650 } catch (err) { 5651 let code = (err as BusinessError).code; 5652 let message = (err as BusinessError).message; 5653 console.error(`Transaction failed, code is ${code},message is ${message}`); 5654 (store as relationalStore.RdbStore).rollBack(); 5655 } 5656} 5657``` 5658 5659### rollback<sup>12+</sup> 5660 5661rollback(txId : number):Promise<void> 5662 5663回滚已经执行的SQL语句,跟[beginTrans](#begintrans12)配合使用。 5664 5665该接口仅支持[向量数据库](#storeconfig)使用。 5666 5667**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5668 5669**参数:** 5670 5671| 参数名 | 类型 | 必填 | 说明 | 5672| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 5673| txId | number | 是 | 通过[beginTrans](#begintrans12)获取的事务ID。 | 5674 5675**返回值**: 5676 5677| 类型 | 说明 | 5678| ------------------- | ------------------------- | 5679| Promise<void> | 无返回结果的Promise对象。 | 5680 5681**错误码:** 5682 5683以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5684 5685| **错误码ID** | **错误信息** | 5686|-----------| ------------------------------------------------------------ | 5687| 401 | Parameter error. The store must not be nullptr. | 5688| 14800000 | Inner error. | 5689| 14800011 | Failed to open the database because it is corrupted. | 5690| 14800014 | The RdbStore or ResultSet is already closed. | 5691| 14800015 | The database does not respond. | 5692| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5693| 14800022 | SQLite: Callback routine requested an abort. | 5694| 14800023 | SQLite: Access permission denied. | 5695| 14800024 | SQLite: The database file is locked. | 5696| 14800025 | SQLite: A table in the database is locked. | 5697| 14800026 | SQLite: The database is out of memory. | 5698| 14800027 | SQLite: Attempt to write a readonly database. | 5699| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5700| 14800029 | SQLite: The database is full. | 5701| 14800030 | SQLite: Unable to open the database file. | 5702| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5703| 14800032 | SQLite: Abort due to constraint violation. | 5704| 14800033 | SQLite: Data type mismatch. | 5705| 14800034 | SQLite: Library used incorrectly. | 5706 5707**示例:** 5708 5709```ts 5710import { BusinessError } from '@kit.BasicServicesKit'; 5711if (store != null) { 5712 let txId: number; 5713 (store as relationalStore.RdbStore).beginTrans().then((txId: number) => { 5714 (store as relationalStore.RdbStore).execute("DELETE FROM TEST WHERE age = ? OR age = ?", txId, ["18", "20"]) 5715 .then(() => { 5716 (store as relationalStore.RdbStore).commit(txId); 5717 }) 5718 .catch((err: BusinessError) => { 5719 (store as relationalStore.RdbStore).rollback(txId); 5720 console.error(`execute sql failed, code is ${err.code},message is ${err.message}`); 5721 }); 5722 }); 5723} 5724``` 5725 5726### backup 5727 5728backup(destName:string, callback: AsyncCallback<void>):void 5729 5730以指定名称备份数据库,使用callback异步回调。 5731 5732该接口支持[向量数据库](#storeconfig)使用。 5733 5734**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5735 5736**参数:** 5737 5738| 参数名 | 类型 | 必填 | 说明 | 5739| -------- | ------------------------- | ---- | ------------------------ | 5740| destName | string | 是 | 指定数据库的备份文件名。 | 5741| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 5742 5743**错误码:** 5744 5745以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5746 5747| **错误码ID** | **错误信息** | 5748|-----------| ------------------------------------------------------------ | 5749| 401 | Parameter error. The store must not be nullptr. | 5750| 14800000 | Inner error. | 5751| 14800010 | Failed to open or delete the database by an invalid database path. | 5752| 14800011 | Failed to open the database because it is corrupted. | 5753| 14800014 | The RdbStore or ResultSet is already closed. | 5754| 14800015 | The database does not respond. | 5755| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5756| 14800022 | SQLite: Callback routine requested an abort. | 5757| 14800023 | SQLite: Access permission denied. | 5758| 14800024 | SQLite: The database file is locked. | 5759| 14800025 | SQLite: A table in the database is locked. | 5760| 14800026 | SQLite: The database is out of memory. | 5761| 14800027 | SQLite: Attempt to write a readonly database. | 5762| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5763| 14800029 | SQLite: The database is full. | 5764| 14800030 | SQLite: Unable to open the database file. | 5765| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5766| 14800032 | SQLite: Abort due to constraint violation. | 5767| 14800033 | SQLite: Data type mismatch. | 5768| 14800034 | SQLite: Library used incorrectly. | 5769 5770**示例:** 5771 5772```ts 5773if (store != undefined) { 5774 (store as relationalStore.RdbStore).backup("dbBackup.db", (err) => { 5775 if (err) { 5776 console.error(`Backup failed, code is ${err.code},message is ${err.message}`); 5777 return; 5778 } 5779 console.info('Backup success.'); 5780 }); 5781} 5782``` 5783 5784### backup 5785 5786backup(destName:string): Promise<void> 5787 5788以指定名称备份数据库,使用Promise异步回调。 5789 5790该接口支持[向量数据库](#storeconfig)使用。 5791 5792**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5793 5794**参数:** 5795 5796| 参数名 | 类型 | 必填 | 说明 | 5797| -------- | ------ | ---- | ------------------------ | 5798| destName | string | 是 | 指定数据库的备份文件名。 | 5799 5800**返回值**: 5801 5802| 类型 | 说明 | 5803| ------------------- | ------------------------- | 5804| Promise<void> | 无返回结果的Promise对象。 | 5805 5806**错误码:** 5807 5808以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5809 5810| **错误码ID** | **错误信息** | 5811|-----------| ------------------------------------------------------------ | 5812| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5813| 14800000 | Inner error. | 5814| 14800011 | Failed to open the database because it is corrupted. | 5815| 14800014 | The RdbStore or ResultSet is already closed. | 5816| 14800015 | The database does not respond. | 5817| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5818| 14800022 | SQLite: Callback routine requested an abort. | 5819| 14800023 | SQLite: Access permission denied. | 5820| 14800024 | SQLite: The database file is locked. | 5821| 14800025 | SQLite: A table in the database is locked. | 5822| 14800026 | SQLite: The database is out of memory. | 5823| 14800027 | SQLite: Attempt to write a readonly database. | 5824| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5825| 14800029 | SQLite: The database is full. | 5826| 14800030 | SQLite: Unable to open the database file. | 5827| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5828| 14800032 | SQLite: Abort due to constraint violation. | 5829| 14800033 | SQLite: Data type mismatch. | 5830| 14800034 | SQLite: Library used incorrectly. | 5831 5832**示例:** 5833 5834```ts 5835import { BusinessError } from '@kit.BasicServicesKit'; 5836 5837if (store != undefined) { 5838 let promiseBackup = (store as relationalStore.RdbStore).backup("dbBackup.db"); 5839 promiseBackup.then(() => { 5840 console.info('Backup success.'); 5841 }).catch((err: BusinessError) => { 5842 console.error(`Backup failed, code is ${err.code},message is ${err.message}`); 5843 }); 5844} 5845``` 5846 5847### restore 5848 5849restore(srcName:string, callback: AsyncCallback<void>):void 5850 5851从指定的数据库备份文件恢复数据库,使用callback异步回调。 5852 5853该接口支持[向量数据库](#storeconfig)使用。 5854 5855**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5856 5857**参数:** 5858 5859| 参数名 | 类型 | 必填 | 说明 | 5860| -------- | ------------------------- | ---- | ------------------------ | 5861| srcName | string | 是 | 指定数据库的备份文件名。 | 5862| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 5863 5864**错误码:** 5865 5866以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5867 5868| **错误码ID** | **错误信息** | 5869|-----------| ------------------------------------------------------------ | 5870| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5871| 14800000 | Inner error. | 5872| 14800011 | Failed to open the database because it is corrupted. | 5873| 14800014 | The RdbStore or ResultSet is already closed. | 5874| 14800015 | The database does not respond. | 5875| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5876| 14800022 | SQLite: Callback routine requested an abort. | 5877| 14800023 | SQLite: Access permission denied. | 5878| 14800024 | SQLite: The database file is locked. | 5879| 14800025 | SQLite: A table in the database is locked. | 5880| 14800026 | SQLite: The database is out of memory. | 5881| 14800027 | SQLite: Attempt to write a readonly database. | 5882| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5883| 14800029 | SQLite: The database is full. | 5884| 14800030 | SQLite: Unable to open the database file. | 5885| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5886| 14800032 | SQLite: Abort due to constraint violation. | 5887| 14800033 | SQLite: Data type mismatch. | 5888| 14800034 | SQLite: Library used incorrectly. | 5889 5890**示例:** 5891 5892```ts 5893if (store != undefined) { 5894 (store as relationalStore.RdbStore).restore("dbBackup.db", (err) => { 5895 if (err) { 5896 console.error(`Restore failed, code is ${err.code},message is ${err.message}`); 5897 return; 5898 } 5899 console.info('Restore success.'); 5900 }); 5901} 5902``` 5903 5904### restore 5905 5906restore(srcName:string): Promise<void> 5907 5908从指定的数据库备份文件恢复数据库,使用Promise异步回调。 5909 5910该接口支持[向量数据库](#storeconfig)使用。 5911 5912**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5913 5914**参数:** 5915 5916| 参数名 | 类型 | 必填 | 说明 | 5917| ------- | ------ | ---- | ------------------------ | 5918| srcName | string | 是 | 指定数据库的备份文件名。 | 5919 5920**返回值**: 5921 5922| 类型 | 说明 | 5923| ------------------- | ------------------------- | 5924| Promise<void> | 无返回结果的Promise对象。 | 5925 5926**错误码:** 5927 5928以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 5929 5930| **错误码ID** | **错误信息** | 5931|-----------| ------------------------------------------------------------ | 5932| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5933| 14800000 | Inner error. | 5934| 14800011 | Failed to open the database because it is corrupted. | 5935| 14800014 | The RdbStore or ResultSet is already closed. | 5936| 14800015 | The database does not respond. | 5937| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 5938| 14800022 | SQLite: Callback routine requested an abort. | 5939| 14800023 | SQLite: Access permission denied. | 5940| 14800024 | SQLite: The database file is locked. | 5941| 14800025 | SQLite: A table in the database is locked. | 5942| 14800026 | SQLite: The database is out of memory. | 5943| 14800027 | SQLite: Attempt to write a readonly database. | 5944| 14800028 | SQLite: Some kind of disk I/O error occurred. | 5945| 14800029 | SQLite: The database is full. | 5946| 14800030 | SQLite: Unable to open the database file. | 5947| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 5948| 14800032 | SQLite: Abort due to constraint violation. | 5949| 14800033 | SQLite: Data type mismatch. | 5950| 14800034 | SQLite: Library used incorrectly. | 5951 5952**示例:** 5953 5954```ts 5955import { BusinessError } from '@kit.BasicServicesKit'; 5956 5957if (store != undefined) { 5958 let promiseRestore = (store as relationalStore.RdbStore).restore("dbBackup.db"); 5959 promiseRestore.then(() => { 5960 console.info('Restore success.'); 5961 }).catch((err: BusinessError) => { 5962 console.error(`Restore failed, code is ${err.code},message is ${err.message}`); 5963 }); 5964} 5965``` 5966 5967### setDistributedTables 5968 5969setDistributedTables(tables: Array<string>, callback: AsyncCallback<void>): void 5970 5971设置分布式数据库表,使用callback异步回调。 5972 5973**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 5974 5975**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5976 5977**参数:** 5978 5979| 参数名 | 类型 | 必填 | 说明 | 5980| -------- | ------------------------- | ---- | ---------------------- | 5981| tables | Array<string> | 是 | 要设置的分布式数据库的表名。 | 5982| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 5983 5984**错误码:** 5985 5986以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 5987 5988| **错误码ID** | **错误信息** | 5989|-----------| ------------------------------------------------------------ | 5990| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 5991| 801 | Capability not supported. | 5992| 14800000 | Inner error. | 5993| 14800014 | The RdbStore or ResultSet is already closed. | 5994 5995**示例:** 5996 5997```ts 5998if (store != undefined) { 5999 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], (err) => { 6000 if (err) { 6001 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 6002 return; 6003 } 6004 console.info('SetDistributedTables successfully.'); 6005 }); 6006} 6007``` 6008 6009### setDistributedTables 6010 6011 setDistributedTables(tables: Array<string>): Promise<void> 6012 6013设置分布式数据库表,使用Promise异步回调。 6014 6015**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 6016 6017**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6018 6019**参数:** 6020 6021| 参数名 | 类型 | 必填 | 说明 | 6022| ------ | ------------------------ | ---- | ------------------------ | 6023| tables | Array<string> | 是 | 要设置的分布式数据库的表名。 | 6024 6025**返回值**: 6026 6027| 类型 | 说明 | 6028| ------------------- | ------------------------- | 6029| Promise<void> | 无返回结果的Promise对象。 | 6030 6031**错误码:** 6032 6033以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6034 6035| **错误码ID** | **错误信息** | 6036|-----------| ------------------------------------------------------------ | 6037| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6038| 801 | Capability not supported. | 6039| 14800000 | Inner error. | 6040| 14800014 | The RdbStore or ResultSet is already closed. | 6041 6042**示例:** 6043 6044```ts 6045import { BusinessError } from '@kit.BasicServicesKit'; 6046 6047if (store != undefined) { 6048 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"]).then(() => { 6049 console.info('SetDistributedTables successfully.'); 6050 }).catch((err: BusinessError) => { 6051 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 6052 }); 6053} 6054``` 6055 6056### setDistributedTables<sup>10+</sup> 6057 6058setDistributedTables(tables: Array<string>, type: DistributedType, callback: AsyncCallback<void>): void 6059 6060设置分布式数据库表,使用callback异步回调。 6061 6062**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 6063 6064**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6065 6066**参数:** 6067 6068| 参数名 | 类型 | 必填 | 说明 | 6069| -------- | ------------------------------------- | ---- | ---------------------------- | 6070| tables | Array<string> | 是 | 要设置的分布式数据库的表名。 | 6071| type | [DistributedType](#distributedtype10) | 是 | 表的分布式类型。 | 6072| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 6073 6074**错误码:** 6075 6076以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6077 6078| **错误码ID** | **错误信息** | 6079|-----------| ------------------------------------------------------------ | 6080| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6081| 801 | Capability not supported. | 6082| 14800000 | Inner error. | 6083| 14800014 | The RdbStore or ResultSet is already closed. | 6084| 14800051 | The type of the distributed table does not match. | 6085 6086**示例:** 6087 6088```ts 6089if (store != undefined) { 6090 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, (err) => { 6091 if (err) { 6092 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 6093 return; 6094 } 6095 console.info('SetDistributedTables successfully.'); 6096 }); 6097} 6098``` 6099 6100### setDistributedTables<sup>10+</sup> 6101 6102setDistributedTables(tables: Array<string>, type: DistributedType, config: DistributedConfig, callback: AsyncCallback<void>): void 6103 6104设置分布式数据库表,使用callback异步回调。 6105 6106**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 6107 6108**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6109 6110**参数:** 6111 6112| 参数名 | 类型 | 必填 | 说明 | 6113| -------- | ----------------------------------- | --- |-----------------| 6114| tables | Array<string> | 是 | 要设置的分布式数据库的表名。 | 6115| type | [DistributedType](#distributedtype10) | 是 | 表的分布式类型。 | 6116| config | [DistributedConfig](#distributedconfig10) | 是 | 表的分布式配置信息。 | 6117| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 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 | The RdbStore or ResultSet is already closed. | 6129| 14800051 | The type of the distributed table does not match. | 6130 6131**示例:** 6132 6133```ts 6134if (store != undefined) { 6135 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, { 6136 autoSync: true 6137 }, (err) => { 6138 if (err) { 6139 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 6140 return; 6141 } 6142 console.info('SetDistributedTables successfully.'); 6143 }); 6144} 6145``` 6146 6147### setDistributedTables<sup>10+</sup> 6148 6149 setDistributedTables(tables: Array<string>, type?: DistributedType, config?: DistributedConfig): Promise<void> 6150 6151设置分布式数据库表,使用Promise异步回调。 6152 6153**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 6154 6155**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6156 6157**参数:** 6158 6159| 参数名 | 类型 | 必填 | 说明 | 6160| ------ | ----------------------------------------- | ---- |-----------------------------------------------------------------| 6161| tables | Array<string> | 是 | 要设置的分布式数据库的表名。 | 6162| type | [DistributedType](#distributedtype10) | 否 | 表的分布式类型。默认值是relationalStore.DistributedType.DISTRIBUTED_DEVICE。 | 6163| config | [DistributedConfig](#distributedconfig10) | 否 | 表的分布式配置信息。不传入时默认autoSync为false,即只支持手动同步。 | 6164 6165**返回值**: 6166 6167| 类型 | 说明 | 6168| ------------------- | ------------------------- | 6169| Promise<void> | 无返回结果的Promise对象。 | 6170 6171**错误码:** 6172 6173以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6174 6175| **错误码ID** | **错误信息** | 6176|-----------| ------------------------------------------------------------ | 6177| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6178| 801 | Capability not supported. | 6179| 14800000 | Inner error. | 6180| 14800014 | The RdbStore or ResultSet is already closed. | 6181| 14800051 | The type of the distributed table does not match. | 6182 6183**示例:** 6184 6185```ts 6186import { BusinessError } from '@kit.BasicServicesKit'; 6187 6188if (store != undefined) { 6189 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, { 6190 autoSync: true 6191 }).then(() => { 6192 console.info('SetDistributedTables successfully.'); 6193 }).catch((err: BusinessError) => { 6194 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 6195 }); 6196} 6197``` 6198 6199### obtainDistributedTableName 6200 6201obtainDistributedTableName(device: string, table: string, callback: AsyncCallback<string>): void 6202 6203根据远程设备的本地表名获取指定远程设备的分布式表名。在查询远程设备数据库时,需要使用分布式表名,使用callback异步回调。 6204 6205> **说明:** 6206> 6207> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 6208 6209**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 6210 6211**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6212 6213**参数:** 6214 6215| 参数名 | 类型 | 必填 | 说明 | 6216| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 6217| device | string | 是 | 远程设备ID 。 | 6218| table | string | 是 | 远程设备的本地表名。 | 6219| callback | AsyncCallback<string> | 是 | 指定的callback回调函数。如果操作成功,返回远程设备的分布式表名。 | 6220 6221**错误码:** 6222 6223以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6224 6225| **错误码ID** | **错误信息** | 6226|-----------| ------------------------------------------------------------ | 6227| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6228| 801 | Capability not supported. | 6229| 14800000 | Inner error. | 6230| 14800014 | The RdbStore or ResultSet is already closed. | 6231 6232**示例:** 6233 6234```ts 6235import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 6236import { BusinessError } from '@kit.BasicServicesKit'; 6237 6238let dmInstance: distributedDeviceManager.DeviceManager; 6239let deviceId: string | undefined = undefined; 6240 6241try { 6242 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 6243 let devices = dmInstance.getAvailableDeviceListSync(); 6244 deviceId = devices[0].networkId; 6245} catch (err) { 6246 let code = (err as BusinessError).code; 6247 let message = (err as BusinessError).message; 6248 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 6249} 6250 6251if (store != undefined && deviceId != undefined) { 6252 (store as relationalStore.RdbStore).obtainDistributedTableName(deviceId, "EMPLOYEE", (err, tableName) => { 6253 if (err) { 6254 console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`); 6255 return; 6256 } 6257 console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`); 6258 }); 6259} 6260``` 6261 6262### obtainDistributedTableName 6263 6264 obtainDistributedTableName(device: string, table: string): Promise<string> 6265 6266根据远程设备的本地表名获取指定远程设备的分布式表名。在查询远程设备数据库时,需要使用分布式表名,使用Promise异步回调。 6267 6268> **说明:** 6269> 6270> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 6271 6272**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 6273 6274**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6275 6276**参数:** 6277 6278| 参数名 | 类型 | 必填 | 说明 | 6279| ------ | ------ | ---- | -------------------- | 6280| device | string | 是 | 远程设备ID。 | 6281| table | string | 是 | 远程设备的本地表名。 | 6282 6283**返回值**: 6284 6285| 类型 | 说明 | 6286| --------------------- | ----------------------------------------------------- | 6287| Promise<string> | Promise对象。如果操作成功,返回远程设备的分布式表名。 | 6288 6289**错误码:** 6290 6291以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6292 6293| **错误码ID** | **错误信息** | 6294|-----------| ------------------------------------------------------------ | 6295| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6296| 801 | Capability not supported. | 6297| 14800000 | Inner error. | 6298| 14800014 | The RdbStore or ResultSet is already closed. | 6299 6300**示例:** 6301 6302```ts 6303import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 6304import { BusinessError } from '@kit.BasicServicesKit'; 6305 6306let dmInstance: distributedDeviceManager.DeviceManager; 6307let deviceId: string | undefined = undefined; 6308 6309try { 6310 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 6311 let devices = dmInstance.getAvailableDeviceListSync(); 6312 deviceId = devices[0].networkId; 6313} catch (err) { 6314 let code = (err as BusinessError).code; 6315 let message = (err as BusinessError).message; 6316 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 6317} 6318 6319if (store != undefined && deviceId != undefined) { 6320 (store as relationalStore.RdbStore).obtainDistributedTableName(deviceId, "EMPLOYEE").then((tableName: string) => { 6321 console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`); 6322 }).catch((err: BusinessError) => { 6323 console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`); 6324 }); 6325} 6326``` 6327 6328### sync 6329 6330sync(mode: SyncMode, predicates: RdbPredicates, callback: AsyncCallback<Array<[string, number]>>): void 6331 6332在设备之间同步数据,使用callback异步回调。 6333 6334**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 6335 6336**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6337 6338**参数:** 6339 6340| 参数名 | 类型 | 必填 | 说明 | 6341| ---------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ | 6342| mode | [SyncMode](#syncmode) | 是 | 指同步模式。该值可以是relationalStore.SyncMode.SYNC_MODE_PUSH、relationalStore.SyncMode.SYNC_MODE_PULL。 | 6343| predicates | [RdbPredicates](#rdbpredicates) | 是 | 约束同步数据和设备。 | 6344| callback | AsyncCallback<Array<[string, number]>> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 | 6345 6346**错误码:** 6347 6348以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6349 6350| **错误码ID** | **错误信息** | 6351|-----------| ------------------------------------------------------------ | 6352| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6353| 801 | Capability not supported. | 6354| 14800000 | Inner error. | 6355| 14800014 | The RdbStore or ResultSet is already closed. | 6356 6357**示例:** 6358 6359```ts 6360import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 6361import { BusinessError } from '@kit.BasicServicesKit'; 6362 6363let dmInstance: distributedDeviceManager.DeviceManager; 6364let deviceIds: Array<string> = []; 6365 6366try { 6367 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 6368 let devices: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 6369 for (let i = 0; i < devices.length; i++) { 6370 deviceIds[i] = devices[i].networkId!; 6371 } 6372} catch (err) { 6373 let code = (err as BusinessError).code; 6374 let message = (err as BusinessError).message; 6375 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 6376} 6377 6378let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 6379predicates.inDevices(deviceIds); 6380if (store != undefined) { 6381 (store as relationalStore.RdbStore).sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates, (err, result) => { 6382 if (err) { 6383 console.error(`Sync failed, code is ${err.code},message is ${err.message}`); 6384 return; 6385 } 6386 console.info('Sync done.'); 6387 for (let i = 0; i < result.length; i++) { 6388 console.info(`device= ${result[i][0]}, status= ${result[i][1]}`); 6389 } 6390 }); 6391} 6392``` 6393 6394### sync 6395 6396 sync(mode: SyncMode, predicates: RdbPredicates): Promise<Array<[string, number]>> 6397 6398在设备之间同步数据,使用Promise异步回调。 6399 6400**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 6401 6402**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6403 6404**参数:** 6405 6406| 参数名 | 类型 | 必填 | 说明 | 6407| ---------- | ------------------------------------ | ---- | ------------------------------ | 6408| mode | [SyncMode](#syncmode) | 是 | 指同步模式。该值可以是relationalStore.SyncMode.SYNC_MODE_PUSH、relationalStore.SyncMode.SYNC_MODE_PULL。 | 6409| predicates | [RdbPredicates](#rdbpredicates) | 是 | 约束同步数据和设备。 | 6410 6411**返回值**: 6412 6413| 类型 | 说明 | 6414| -------------------------------------------- | ------------------------------------------------------------ | 6415| Promise<Array<[string, number]>> | Promise对象,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 | 6416 6417**错误码:** 6418 6419以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6420 6421| **错误码ID** | **错误信息** | 6422|-----------| ------------------------------------------------------------ | 6423| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6424| 801 | Capability not supported. | 6425| 14800000 | Inner error. | 6426| 14800014 | The RdbStore or ResultSet is already closed. | 6427 6428**示例:** 6429 6430```ts 6431import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 6432import { BusinessError } from '@kit.BasicServicesKit'; 6433 6434let dmInstance: distributedDeviceManager.DeviceManager; 6435let deviceIds: Array<string> = []; 6436 6437try { 6438 dmInstance = distributedDeviceManager.createDeviceManager("com.example.appdatamgrverify"); 6439 let devices: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 6440 for (let i = 0; i < devices.length; i++) { 6441 deviceIds[i] = devices[i].networkId!; 6442 } 6443} catch (err) { 6444 let code = (err as BusinessError).code; 6445 let message = (err as BusinessError).message; 6446 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 6447} 6448 6449let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 6450predicates.inDevices(deviceIds); 6451if (store != undefined) { 6452 (store as relationalStore.RdbStore).sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates).then((result: Object[][]) => { 6453 console.info('Sync done.'); 6454 for (let i = 0; i < result.length; i++) { 6455 console.info(`device= ${result[i][0]}, status= ${result[i][1]}`); 6456 } 6457 }).catch((err: BusinessError) => { 6458 console.error(`Sync failed, code is ${err.code},message is ${err.message}`); 6459 }); 6460} 6461``` 6462 6463### cloudSync<sup>10+</sup> 6464 6465cloudSync(mode: SyncMode, progress: Callback<ProgressDetails>, callback: AsyncCallback<void>): void 6466 6467手动执行对所有分布式表的端云同步,使用callback异步回调。使用该接口需要实现云服务功能。 6468 6469**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 6470 6471**参数:** 6472 6473| 参数名 | 类型 | 必填 | 说明 | 6474| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 6475| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 6476| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 6477| callback | AsyncCallback<void> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。 | 6478 6479**错误码:** 6480 6481以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6482 6483| **错误码ID** | **错误信息** | 6484|-----------|-------| 6485| 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. | 6486| 801 | Capability not supported. | 6487| 14800014 | The RdbStore or ResultSet is already closed. | 6488 6489**示例:** 6490 6491```ts 6492if (store != undefined) { 6493 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, (progressDetails) => { 6494 console.info(`Progess: ${progressDetails}`); 6495 }, (err) => { 6496 if (err) { 6497 console.error(`Cloud sync failed, code is ${err.code},message is ${err.message}`); 6498 return; 6499 } 6500 console.info('Cloud sync succeeded'); 6501 }); 6502} 6503``` 6504 6505### cloudSync<sup>10+</sup> 6506 6507cloudSync(mode: SyncMode, progress: Callback<ProgressDetails>): Promise<void> 6508 6509手动执行对所有分布式表的端云同步,使用Promise异步回调。使用该接口需要实现云服务功能。 6510 6511**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 6512 6513**参数:** 6514 6515| 参数名 | 类型 | 必填 | 说明 | 6516| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 6517| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 6518| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 6519 6520**返回值**: 6521 6522| 类型 | 说明 | 6523| ------------------- | --------------------------------------- | 6524| Promise<void> | Promise对象,用于向调用者发送同步结果。 | 6525 6526**错误码:** 6527 6528以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6529 6530| **错误码ID** | **错误信息** | 6531|-----------|------------------| 6532| 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. | 6533| 801 | Capability not supported. | 6534| 14800014 | The RdbStore or ResultSet is already closed. | 6535 6536**示例:** 6537 6538```ts 6539import { BusinessError } from '@kit.BasicServicesKit'; 6540 6541if (store != undefined) { 6542 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, (progressDetail: relationalStore.ProgressDetails) => { 6543 console.info(`progress: ${progressDetail}`); 6544 }).then(() => { 6545 console.info('Cloud sync succeeded'); 6546 }).catch((err: BusinessError) => { 6547 console.error(`cloudSync failed, code is ${err.code},message is ${err.message}`); 6548 }); 6549} 6550``` 6551 6552### cloudSync<sup>10+</sup> 6553 6554cloudSync(mode: SyncMode, tables: string[], progress: Callback<ProgressDetails>, callback: AsyncCallback<void>): void 6555 6556手动执行对指定表的端云同步,使用callback异步回调。使用该接口需要实现云服务功能。 6557 6558**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 6559 6560**参数:** 6561 6562| 参数名 | 类型 | 必填 | 说明 | 6563| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 6564| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 6565| tables | string[] | 是 | 指定同步的表名。 | 6566| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 6567| callback | AsyncCallback<void> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。 | 6568 6569**错误码:** 6570 6571以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6572 6573| **错误码ID** | **错误信息** | 6574|-----------|-------| 6575| 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.| 6576| 801 | Capability not supported. | 6577| 14800014 | The RdbStore or ResultSet is already closed. | 6578 6579**示例:** 6580 6581```ts 6582const tables = ["table1", "table2"]; 6583 6584if (store != undefined) { 6585 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, tables, (progressDetail: relationalStore.ProgressDetails) => { 6586 console.info(`Progess: ${progressDetail}`); 6587 }, (err) => { 6588 if (err) { 6589 console.error(`Cloud sync failed, code is ${err.code},message is ${err.message}`); 6590 return; 6591 } 6592 console.info('Cloud sync succeeded'); 6593 }); 6594}; 6595``` 6596 6597### cloudSync<sup>10+</sup> 6598 6599cloudSync(mode: SyncMode, tables: string[], progress: Callback<ProgressDetails>): Promise<void> 6600 6601手动执行对指定表的端云同步,使用Promise异步回调。使用该接口需要实现云服务功能。 6602 6603**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 6604 6605**参数:** 6606 6607| 参数名 | 类型 | 必填 | 说明 | 6608| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 6609| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 6610| tables | string[] | 是 | 指定同步的表名。 | 6611| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 6612 6613**返回值**: 6614 6615| 类型 | 说明 | 6616| ------------------- | --------------------------------------- | 6617| Promise<void> | Promise对象,用于向调用者发送同步结果。 | 6618 6619**错误码:** 6620 6621以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6622 6623| **错误码ID** | **错误信息** | 6624|-----------|---------------| 6625| 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 | 6626| 801 | Capability not supported. | 6627| 14800014 | The RdbStore or ResultSet is already closed. | 6628 6629**示例:** 6630 6631```ts 6632import { BusinessError } from '@kit.BasicServicesKit'; 6633 6634const tables = ["table1", "table2"]; 6635 6636if (store != undefined) { 6637 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, tables, (progressDetail: relationalStore.ProgressDetails) => { 6638 console.info(`progress: ${progressDetail}`); 6639 }).then(() => { 6640 console.info('Cloud sync succeeded'); 6641 }).catch((err: BusinessError) => { 6642 console.error(`cloudSync failed, code is ${err.code},message is ${err.message}`); 6643 }); 6644}; 6645``` 6646 6647### on('dataChange') 6648 6649on(event: 'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void 6650 6651注册数据库的数据变更的事件监听。当分布式数据库中的数据发生更改时,将调用回调。 6652 6653**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6654 6655**参数:** 6656 6657| 参数名 | 类型 | 必填 | 说明 | 6658| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 6659| event | string | 是 | 取值为'dataChange',表示数据更改。 | 6660| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 6661| observer | Callback<Array<string>> | 是 | 指分布式数据库中数据更改事件的观察者。Array<string>为数据库中的数据发生改变的对端设备ID。 | 6662 6663**错误码:** 6664 6665以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6666 6667| **错误码ID** | **错误信息** | 6668|-----------|-------------| 6669| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6670| 801 | Capability not supported. | 6671| 14800014 | The RdbStore or ResultSet is already closed. | 6672 6673**示例:** 6674 6675```ts 6676import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 6677import { BusinessError } from '@kit.BasicServicesKit'; 6678 6679let storeObserver = (devices: Array<string>) => { 6680 if (devices != undefined) { 6681 for (let i = 0; i < devices.length; i++) { 6682 console.info(`device= ${devices[i]} data changed`); 6683 } 6684 } 6685}; 6686 6687try { 6688 if (store != undefined) { 6689 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 6690 } 6691} catch (err) { 6692 let code = (err as BusinessError).code; 6693 let message = (err as BusinessError).message; 6694 console.error(`Register observer failed, code is ${code},message is ${message}`); 6695} 6696``` 6697 6698### on('dataChange')<sup>10+</sup> 6699 6700on(event: 'dataChange', type: SubscribeType, observer: Callback<Array<string>>\| Callback<Array<ChangeInfo>>): void 6701 6702注册数据库的数据变更的事件监听。当分布式数据库或本地数据库中的数据发生更改时,将调用回调。 6703 6704**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6705 6706**参数:** 6707 6708| 参数名 | 类型 | 必填 | 说明 | 6709| -------- | ----------------------------------- | ---- | ------------------------------------------- | 6710| event | string | 是 | 取值为'dataChange',表示数据更改。 | 6711| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 6712| 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>为本地数据库中的数据更改的详情。 | 6713 6714**错误码:** 6715 6716以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6717 6718| **错误码ID** | **错误信息** | 6719|-----------|-------------| 6720| 202 | Permission verification failed, application which is not a system application uses system API. | 6721| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6722| 801 | Capability not supported. | 6723| 14800014 | The RdbStore or ResultSet is already closed. | 6724 6725**示例1:type为SUBSCRIBE_TYPE_REMOTE** 6726 6727```ts 6728import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 6729import { BusinessError } from '@kit.BasicServicesKit'; 6730 6731let storeObserver = (devices: Array<string>) => { 6732 if (devices != undefined) { 6733 for (let i = 0; i < devices.length; i++) { 6734 console.info(`device= ${devices[i]} data changed`); 6735 } 6736 } 6737}; 6738 6739try { 6740 if (store != undefined) { 6741 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 6742 } 6743} catch (err) { 6744 let code = (err as BusinessError).code; 6745 let message = (err as BusinessError).message; 6746 console.error(`Register observer failed, code is ${code},message is ${message}`); 6747} 6748``` 6749 6750**示例2:type为SUBSCRIBE_TYPE_LOCAL_DETAILS** 6751 6752```ts 6753import { BusinessError } from '@kit.BasicServicesKit'; 6754 6755let changeInfos = (changeInfos: Array<relationalStore.ChangeInfo>) => { 6756 for (let i = 0; i < changeInfos.length; i++) { 6757 console.info(`changeInfos = ${changeInfos[i]}`); 6758 } 6759}; 6760 6761try { 6762 if (store != undefined) { 6763 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_LOCAL_DETAILS, changeInfos); 6764 } 6765} catch (err) { 6766 let code = (err as BusinessError).code; 6767 let message = (err as BusinessError).message; 6768 console.error(`on dataChange fail, code is ${code},message is ${message}`); 6769} 6770 6771let value1 = "Lisa"; 6772let value2 = 18; 6773let value3 = 100.5; 6774let value4 = new Uint8Array([1, 2, 3]); 6775 6776try { 6777 const valueBucket: relationalStore.ValuesBucket = { 6778 'name': value1, 6779 'age': value2, 6780 'salary': value3, 6781 'blobType': value4 6782 }; 6783 6784 if (store != undefined) { 6785 (store as relationalStore.RdbStore).insert('test', valueBucket); 6786 } 6787} catch (err) { 6788 let code = (err as BusinessError).code; 6789 let message = (err as BusinessError).message; 6790 console.error(`insert fail, code is ${code},message is ${message}`); 6791} 6792``` 6793 6794### on<sup>10+</sup> 6795 6796on(event: string, interProcess: boolean, observer: Callback\<void>): void 6797 6798注册数据库的进程内或者进程间事件监听。当调用[emit](#emit10)接口时,将调用回调。 6799 6800**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6801 6802**参数:** 6803 6804| 参数名 | 类型 | 必填 | 说明 | 6805| ------------ | --------------- | ---- | ------------------------------------------------------------ | 6806| event | string | 是 | 订阅事件名称,与emit接口触发事件时的名称一致。 | 6807| interProcess | boolean | 是 | 指定是进程间还是本进程订阅。<br/> true:进程间。<br/> false:本进程。 | 6808| observer | Callback\<void> | 是 | 回调函数。 | 6809 6810**错误码:** 6811 6812以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6813 6814| **错误码ID** | **错误信息** | 6815|-----------|-------------| 6816| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 6817| 801 | Capability not supported. | 6818| 14800000 | Inner error. | 6819| 14800014 | The RdbStore or ResultSet is already closed. | 6820| 14800050 | Failed to obtain the subscription service. | 6821 6822**示例:** 6823 6824```ts 6825import { BusinessError } from '@kit.BasicServicesKit'; 6826 6827let storeObserver = () => { 6828 console.info(`storeObserver`); 6829}; 6830 6831try { 6832 if (store != undefined) { 6833 (store as relationalStore.RdbStore).on('storeObserver', false, storeObserver); 6834 } 6835} catch (err) { 6836 let code = (err as BusinessError).code; 6837 let message = (err as BusinessError).message; 6838 console.error(`Register observer failed, code is ${code},message is ${message}`); 6839} 6840``` 6841 6842### on('autoSyncProgress')<sup>11+</sup> 6843 6844on(event: 'autoSyncProgress', progress: Callback<ProgressDetails>): void 6845 6846在已打开端云同步,并且网络状态正常的条件下,注册自动同步进度通知,自动同步进行时调用回调。 6847 6848**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6849 6850**参数:** 6851 6852| 参数名 | 类型 | 必填 | 说明 | 6853| ------------ |---------------------------------| ---- |-----------------------------------| 6854| event | string | 是 | 取值为'autoSyncProgress',表示自动同步进度通知。 | 6855| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 回调函数。 | 6856 6857**错误码:** 6858 6859以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6860 6861| **错误码ID** | **错误信息** | 6862|-----------|--------| 6863| 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. | 6864| 801 | Capability not supported. | 6865| 14800014 | The RdbStore or ResultSet is already closed. | 6866 6867**示例:** 6868 6869```ts 6870import { BusinessError } from '@kit.BasicServicesKit'; 6871 6872let progressDetail = (progressDetail: relationalStore.ProgressDetails) => { 6873 console.info(`progress: ${progressDetail}`); 6874}; 6875 6876try { 6877 if (store != undefined) { 6878 (store as relationalStore.RdbStore).on('autoSyncProgress', progressDetail); 6879 } 6880} catch (err) { 6881 let code = (err as BusinessError).code; 6882 let message = (err as BusinessError).message; 6883 console.error(`Register observer failed, code is ${code},message is ${message}`); 6884} 6885``` 6886 6887### on('statistics')<sup>12+</sup> 6888 6889on(event: 'statistics', observer: Callback<SqlExecutionInfo>): void 6890 6891订阅SQL统计信息。 6892 6893**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6894 6895**参数:** 6896 6897| 参数名 | 类型 | 必填 | 说明 | 6898| ------------ |---------------------------------| ---- |-----------------------------------| 6899| event | string | 是 | 订阅事件名称,取值为'statistics',表示sql执行时间的统计。 | 6900| observer | Callback<[SqlExecutionInfo](#sqlexecutioninfo12)> | 是 | 回调函数。用于返回数据库中SQL执行时间的统计信息。 | 6901 6902**错误码:** 6903 6904以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6905 6906| **错误码ID** | **错误信息** | 6907|-----------|--------| 6908| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. | 6909| 801 | Capability not supported. | 6910| 14800000 | Inner error. | 6911| 14800014 | The RdbStore or ResultSet is already closed. | 6912 6913**示例:** 6914 6915```ts 6916import { BusinessError } from '@kit.BasicServicesKit'; 6917 6918let sqlExecutionInfo = (sqlExecutionInfo: relationalStore.SqlExecutionInfo) => { 6919 console.info(`sql: ${sqlExecutionInfo.sql[0]}`); 6920 console.info(`totalTime: ${sqlExecutionInfo.totalTime}`); 6921 console.info(`waitTime: ${sqlExecutionInfo.waitTime}`); 6922 console.info(`prepareTime: ${sqlExecutionInfo.prepareTime}`); 6923 console.info(`executeTime: ${sqlExecutionInfo.executeTime}`); 6924}; 6925 6926try { 6927 if (store != undefined) { 6928 (store as relationalStore.RdbStore).on('statistics', sqlExecutionInfo); 6929 } 6930} catch (err) { 6931 let code = (err as BusinessError).code; 6932 let message = (err as BusinessError).message; 6933 console.error(`Register observer failed, code is ${code},message is ${message}`); 6934} 6935 6936try { 6937 let value1 = "Lisa"; 6938 let value2 = 18; 6939 let value3 = 100.5; 6940 let value4 = new Uint8Array([1, 2, 3, 4, 5]); 6941 6942 const valueBucket: relationalStore.ValuesBucket = { 6943 'NAME': value1, 6944 'AGE': value2, 6945 'SALARY': value3, 6946 'CODES': value4 6947 }; 6948 if (store != undefined) { 6949 (store as relationalStore.RdbStore).insert('test', valueBucket); 6950 } 6951} catch (err) { 6952 console.error(`insert fail, code:${err.code}, message: ${err.message}`); 6953} 6954``` 6955 6956### on('sqliteErrorOccurred')<sup>20+</sup> 6957 6958on(event: 'sqliteErrorOccurred', observer: Callback<ExceptionMessage>): void 6959 6960记录执行SQL语句时的异常日志。 6961 6962**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 6963 6964**参数:** 6965 6966| 参数名 | 类型 | 必填 | 说明 | 6967| ------------ |---------------------------------| ---- |-----------------------------------| 6968| event | string | 是 | 订阅事件名称,取值为'sqliteErrorOccurred',记录SQL语句执行过程中的错误信息。 | 6969| observer | Callback<[ExceptionMessage](#exceptionmessage20)> | 是 | 回调函数。用于返回SQL执行时出现的异常信息。 | 6970 6971**错误码:** 6972 6973以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 6974 6975| **错误码ID** | **错误信息** | 6976|-----------|--------| 6977| 801 | Capability not supported. | 6978| 14800014 | The RdbStore or ResultSet is already closed. | 6979 6980**示例:** 6981 6982```ts 6983import { BusinessError } from '@kit.BasicServicesKit'; 6984 6985async test() 6986{ 6987 try { 6988 if (store != undefined) { 6989 let exceptionMessage: relationalStore.ExceptionMessage; 6990 store.on('sqliteErrorOccurred', exceptionMessage => { 6991 let sqliteCode = exceptionMessage.code; 6992 let sqliteMessage = exceptionMessage.message; 6993 let errSQL = exceptionMessage.sql; 6994 console.info(`error log is ${sqliteCode}, errMessage is ${sqliteMessage}, errSQL is ${errSQL}`); 6995 }) 6996 } 6997 } catch (err) { 6998 let code = (err as BusinessError).code; 6999 let message = (err as BusinessError).message; 7000 console.error(`Register observer failed, code is ${code},message is ${message}`); 7001 } 7002 const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 7003 "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL)"; 7004 try { 7005 let value = new Uint8Array([1, 2, 3, 4, 5]); 7006 const valueBucket: relationalStore.ValuesBucket = { 7007 'name': "Lisa", 7008 'age': 18, 7009 'salary': 100.5, 7010 'codes': value, 7011 }; 7012 await store.executeSql(CREATE_TABLE_TEST); 7013 if (store != undefined) { 7014 (store as relationalStore.RdbStore).insert('test', valueBucket); 7015 } 7016 } catch (err) { 7017 console.error(`Insert fail, code:${err.code}, message: ${err.message}`); 7018 } 7019} 7020``` 7021 7022### off('dataChange') 7023 7024off(event:'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void 7025 7026取消数据变更的事件监听。 7027 7028**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7029 7030**参数:** 7031 7032| 参数名 | 类型 | 必填 | 说明 | 7033| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 7034| event | string | 是 | 取值为'dataChange',表示数据更改。 | 7035| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 7036| observer | Callback<Array<string>> | 是 | 指已注册的数据更改观察者。Array<string>为数据库中的数据发生改变的对端设备ID。 | 7037 7038**错误码:** 7039 7040以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 7041 7042| **错误码ID** | **错误信息** | 7043|-----------|-------------| 7044| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7045| 801 | Capability not supported. | 7046| 14800014 | The RdbStore or ResultSet is already closed. | 7047 7048**示例:** 7049 7050```ts 7051import { BusinessError } from '@kit.BasicServicesKit'; 7052 7053let storeObserver = (devices: Array<string>) => { 7054 if (devices != undefined) { 7055 for (let i = 0; i < devices.length; i++) { 7056 console.info(`device= ${devices[i]} data changed`); 7057 } 7058 } 7059}; 7060 7061try { 7062 if (store != undefined) { 7063 // 此处不能使用Lambda表达式 7064 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 7065 } 7066} catch (err) { 7067 let code = (err as BusinessError).code; 7068 let message = (err as BusinessError).message; 7069 console.error(`Register observer failed, code is ${code},message is ${message}`); 7070} 7071 7072try { 7073 if (store != undefined) { 7074 (store as relationalStore.RdbStore).off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 7075 } 7076} catch (err) { 7077 let code = (err as BusinessError).code; 7078 let message = (err as BusinessError).message; 7079 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 7080} 7081``` 7082 7083### off('dataChange')<sup>10+</sup> 7084 7085off(event:'dataChange', type: SubscribeType, observer?: Callback<Array<string>>\| Callback<Array<ChangeInfo>>): void 7086 7087取消数据变更的事件监听。 7088 7089**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7090 7091**参数:** 7092 7093| 参数名 | 类型 | 必填 | 说明 | 7094| -------- | ---------------------------------- | ---- | ------------------------------------------ | 7095| event | string | 是 | 取值为'dataChange',表示数据更改。 | 7096| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 7097| 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类型下所有数据变更的事件监听。 | 7098 7099**错误码:** 7100 7101以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 7102 7103| **错误码ID** | **错误信息** | 7104|-----------|-------------| 7105| 202 | Permission verification failed, application which is not a system application uses system API. | 7106| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7107| 801 | Capability not supported. | 7108| 14800014 | The RdbStore or ResultSet is already closed. | 7109 7110**示例:** 7111 7112```ts 7113import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 7114import { BusinessError } from '@kit.BasicServicesKit'; 7115 7116let storeObserver = (devices: Array<string>) => { 7117 if (devices != undefined) { 7118 for (let i = 0; i < devices.length; i++) { 7119 console.info(`device= ${devices[i]} data changed`); 7120 } 7121 } 7122}; 7123 7124try { 7125 if (store != undefined) { 7126 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 7127 } 7128} catch (err) { 7129 let code = (err as BusinessError).code; 7130 let message = (err as BusinessError).message; 7131 console.error(`Register observer failed, code is ${code},message is ${message}`); 7132} 7133 7134try { 7135 if (store != undefined) { 7136 (store as relationalStore.RdbStore).off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 7137 } 7138} catch (err) { 7139 let code = (err as BusinessError).code; 7140 let message = (err as BusinessError).message; 7141 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 7142} 7143``` 7144 7145### off<sup>10+</sup> 7146 7147off(event: string, interProcess: boolean, observer?: Callback\<void>): void 7148 7149取消数据变更的事件监听。 7150 7151**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7152 7153**参数:** 7154 7155| 参数名 | 类型 | 必填 | 说明 | 7156| ------------ | --------------- | ---- | ------------------------------------------------------------ | 7157| event | string | 是 | 取消订阅事件名称。事件名称与on接口调用时订阅事件的名称一致。 | 7158| interProcess | boolean | 是 | 指定是进程间还是本进程取消订阅。<br/> true:进程间。<br/> false:本进程。 | 7159| observer | Callback\<void> | 否 | 该参数存在,则取消指定Callback监听回调,否则取消该event事件的所有监听回调。 | 7160 7161**错误码:** 7162 7163以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 7164 7165| **错误码ID** | **错误信息** | 7166| ------------ | -------------------------------------- | 7167| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7168| 801 | Capability not supported. | 7169| 14800000 | Inner error. | 7170| 14800014 | The RdbStore or ResultSet is already closed. | 7171| 14800050 | Failed to obtain the subscription service. | 7172 7173**示例:** 7174 7175```ts 7176import { BusinessError } from '@kit.BasicServicesKit'; 7177 7178let storeObserver = () => { 7179 console.info(`storeObserver`); 7180}; 7181 7182try { 7183 if (store != undefined) { 7184 (store as relationalStore.RdbStore).on('storeObserver', false, storeObserver); 7185 } 7186} catch (err) { 7187 let code = (err as BusinessError).code; 7188 let message = (err as BusinessError).message; 7189 console.error(`Register observer failed, code is ${code},message is ${message}`); 7190} 7191 7192try { 7193 if (store != undefined) { 7194 (store as relationalStore.RdbStore).off('storeObserver', false, storeObserver); 7195 } 7196} catch (err) { 7197 let code = (err as BusinessError).code; 7198 let message = (err as BusinessError).message; 7199 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 7200} 7201``` 7202 7203### off('autoSyncProgress')<sup>11+</sup> 7204 7205off(event: 'autoSyncProgress', progress?: Callback<ProgressDetails>): void 7206 7207取消订阅自动同步进度的通知。 7208 7209**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7210 7211**参数:** 7212 7213| 参数名 | 类型 | 必填 | 说明 | 7214| ------------ |---------------------------------| ---- |------------------------------------------------------------------| 7215| event | string | 是 | 取值为'autoSyncProgress',表示自动同步进度通知。 | 7216| progress | Callback<[ProgressDetails](#progressdetails10)> | 否 | 指已注册的自动同步进度观察者。该参数存在,则取消订阅指定回调,该参数为null或undefined或不存在,则取消订阅所有回调。 | 7217 7218**错误码:** 7219 7220以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 7221 7222| **错误码ID** | **错误信息** | 7223| ------------ |--------------------| 7224| 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. | 7225| 801 | Capability not supported. | 7226| 14800014 | The RdbStore or ResultSet is already closed. | 7227 7228**示例:** 7229 7230```ts 7231import { BusinessError } from '@kit.BasicServicesKit'; 7232 7233let progressDetail = (progressDetail: relationalStore.ProgressDetails) => { 7234 console.info(`progress: ${progressDetail}`); 7235}; 7236 7237try { 7238 if (store != undefined) { 7239 (store as relationalStore.RdbStore).on('autoSyncProgress', progressDetail); 7240 } 7241} catch (err) { 7242 let code = (err as BusinessError).code; 7243 let message = (err as BusinessError).message; 7244 console.error(`Register observer failed, code is ${code},message is ${message}`); 7245} 7246 7247try { 7248 if (store != undefined) { 7249 (store as relationalStore.RdbStore).off('autoSyncProgress', progressDetail); 7250 } 7251} catch (err) { 7252 let code = (err as BusinessError).code; 7253 let message = (err as BusinessError).message; 7254 console.error(`Unregister failed, code is ${code},message is ${message}`); 7255} 7256``` 7257 7258### off('statistics')<sup>12+</sup> 7259 7260off(event: 'statistics', observer?: Callback<SqlExecutionInfo>): void 7261 7262取消订阅SQL统计信息。 7263 7264**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7265 7266**参数:** 7267 7268| 参数名 | 类型 | 必填 | 说明 | 7269| ------------ |---------------------------------| ---- |-----------------------------------| 7270| event | string | 是 | 取消订阅事件名称。取值为'statistics',表示sql执行时间的统计。 | 7271| observer | Callback<[SqlExecutionInfo](#sqlexecutioninfo12)> | 否 | 回调函数。该参数存在,则取消指定Callback监听回调,否则取消该event事件的所有监听回调。 | 7272 7273 7274**错误码:** 7275 7276以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 7277 7278| **错误码ID** | **错误信息** | 7279|-----------|--------| 7280| 401 | Parameter error. | 7281| 801 | Capability not supported. | 7282| 14800000 | Inner error. | 7283| 14800014 | The RdbStore or ResultSet is already closed. | 7284 7285```ts 7286import { BusinessError } from '@kit.BasicServicesKit'; 7287 7288try { 7289 if (store != undefined) { 7290 (store as relationalStore.RdbStore).off('statistics'); 7291 } 7292} catch (err) { 7293 let code = (err as BusinessError).code; 7294 let message = (err as BusinessError).message; 7295 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 7296} 7297``` 7298 7299### off('sqliteErrorOccurred')<sup>20+</sup> 7300 7301off(event: 'sqliteErrorOccurred', observer: Callback<ExceptionMessage>): void 7302 7303停止记录SQL执行过程中的异常日志。 7304 7305**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7306 7307**参数:** 7308 7309| 参数名 | 类型 | 必填 | 说明 | 7310| ------------ |---------------------------------| ---- |-----------------------------------| 7311| event | string | 是 | 取消订阅事件名称,取值为'sqliteErrorOccurred',记录SQL语句执行过程中的错误信息。 | 7312| observer | Callback<[ExceptionMessage](#exceptionmessage20)> | 是 | 回调函数。该参数存在,则取消指定Callback监听回调,否则取消该event事件的所有监听回调。 | 7313 7314**错误码:** 7315 7316以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 7317 7318| **错误码ID** | **错误信息** | 7319|-----------|--------| 7320| 801 | Capability not supported. | 7321| 14800014 | The RdbStore or ResultSet is already closed. | 7322 7323**示例:** 7324 7325```ts 7326import { BusinessError } from '@kit.BasicServicesKit'; 7327 7328try { 7329 if (store != undefined) { 7330 (store as relationalStore.RdbStore).off('sqliteErrorOccurred'); 7331 } 7332} catch (err) { 7333 let code = (err as BusinessError).code; 7334 let message = (err as BusinessError).message; 7335 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 7336} 7337``` 7338 7339 7340### emit<sup>10+</sup> 7341 7342emit(event: string): void 7343 7344通知通过[on](#on10)注册的进程间或者进程内监听事件。 7345 7346**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7347 7348**参数:** 7349 7350| 参数名 | 类型 | 必填 | 说明 | 7351| ------ | ------ | ---- | -------------------- | 7352| event | string | 是 | 通知订阅事件的名称,可自定义事件名称,不能与系统已有事件[dataChange](#ondatachange),[autoSyncProgress](#onautosyncprogress11),[statistics](#onstatistics12)名称重复。 | 7353 7354**错误码:** 7355 7356以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 7357 7358| **错误码ID** | **错误信息** | 7359| --------- |---------------------------------------------------------------------------------------------------------------| 7360| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7361| 801 | Capability not supported. | 7362| 14800000 | Inner error. | 7363| 14800014 | The RdbStore or ResultSet is already closed. | 7364| 14800050 | Failed to obtain the subscription service. | 7365 7366 7367**示例:** 7368 7369```ts 7370if (store != undefined) { 7371 (store as relationalStore.RdbStore).emit('storeObserver'); 7372} 7373``` 7374 7375### cleanDirtyData<sup>11+</sup> 7376 7377cleanDirtyData(table: string, cursor: number, callback: AsyncCallback<void>): void 7378 7379清理云端删除的数据同步到本地后,未自动清理的,且数据的游标(cursor)小于指定游标的数据。 7380 7381**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 7382 7383**参数:** 7384 7385| 参数名 | 类型 | 必填 | 说明 | 7386| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 7387| table | string | 是 | 表示当前数据库的表的名称。 | 7388| cursor | number | 是 | 整数类型,表示数据游标,小于此游标的脏数据将被清理。 | 7389| callback | AsyncCallback<void> | 是 | 指定的callback回调函数。 | 7390 7391**错误码:** 7392 7393以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7394 7395| **错误码ID** | **错误信息** | 7396|-----------|---------------| 7397| 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. | 7398| 801 | Capability not supported. | 7399| 14800000 | Inner error. | 7400| 14800011 | Failed to open the database because it is corrupted. | 7401| 14800014 | The RdbStore or ResultSet is already closed. | 7402| 14800015 | The database does not respond. | 7403| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 7404| 14800022 | SQLite: Callback routine requested an abort. | 7405| 14800023 | SQLite: Access permission denied. | 7406| 14800024 | SQLite: The database file is locked. | 7407| 14800025 | SQLite: A table in the database is locked. | 7408| 14800026 | SQLite: The database is out of memory. | 7409| 14800027 | SQLite: Attempt to write a readonly database. | 7410| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7411| 14800029 | SQLite: The database is full. | 7412| 14800030 | SQLite: Unable to open the database file. | 7413| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7414| 14800032 | SQLite: Abort due to constraint violation. | 7415| 14800033 | SQLite: Data type mismatch. | 7416| 14800034 | SQLite: Library used incorrectly. | 7417 7418**示例:** 7419 7420```ts 7421if (store != undefined) { 7422 (store as relationalStore.RdbStore).cleanDirtyData('test_table', 100, (err) => { 7423 if (err) { 7424 console.error(`clean dirty data failed, code is ${err.code},message is ${err.message}`); 7425 return; 7426 } 7427 console.info('clean dirty data succeeded'); 7428 }); 7429} 7430``` 7431 7432### cleanDirtyData<sup>11+</sup> 7433 7434cleanDirtyData(table: string, callback: AsyncCallback<void>): void 7435 7436清理云端删除的数据同步到本地后,未自动清理的所有数据。 7437 7438**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 7439 7440**参数:** 7441 7442| 参数名 | 类型 | 必填 | 说明 | 7443| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 7444| table | string | 是 | 表示当前数据库的表的名称。 | 7445| callback | AsyncCallback<void> | 是 | 指定的callback回调函数。 | 7446 7447**错误码:** 7448 7449以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7450 7451| **错误码ID** | **错误信息** | 7452|-----------|---------| 7453| 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. | 7454| 801 | Capability not supported. | 7455| 14800000 | Inner error. | 7456| 14800011 | Failed to open the database because it is corrupted. | 7457| 14800014 | The RdbStore or ResultSet is already closed. | 7458| 14800015 | The database does not respond. | 7459| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 7460| 14800022 | SQLite: Callback routine requested an abort. | 7461| 14800023 | SQLite: Access permission denied. | 7462| 14800024 | SQLite: The database file is locked. | 7463| 14800025 | SQLite: A table in the database is locked. | 7464| 14800026 | SQLite: The database is out of memory. | 7465| 14800027 | SQLite: Attempt to write a readonly database. | 7466| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7467| 14800029 | SQLite: The database is full. | 7468| 14800030 | SQLite: Unable to open the database file. | 7469| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7470| 14800032 | SQLite: Abort due to constraint violation. | 7471| 14800033 | SQLite: Data type mismatch. | 7472| 14800034 | SQLite: Library used incorrectly. | 7473 7474**示例:** 7475 7476```ts 7477if (store != undefined) { 7478 (store as relationalStore.RdbStore).cleanDirtyData('test_table', (err) => { 7479 if (err) { 7480 console.error(`clean dirty data failed, code is ${err.code},message is ${err.message}`); 7481 return; 7482 } 7483 console.info('clean dirty data succeeded'); 7484 }); 7485} 7486``` 7487 7488### cleanDirtyData<sup>11+</sup> 7489 7490cleanDirtyData(table: string, cursor?: number): Promise<void> 7491 7492清理云端删除的数据同步到本地后,未自动清理的,且数据的游标(cursor)小于指定游标的数据。若无cursor参数,将全部清理。 7493 7494**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 7495 7496**参数:** 7497 7498| 参数名 | 类型 | 必填 | 说明 | 7499| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 7500| table | string | 是 | 表示当前数据库的表的名称。 | 7501| cursor | number | 否 | 整数类型,表示数据游标,小于此游标的脏数据将被清理。当此参数不填时,清理当前表的所有脏数据。 | 7502 7503**返回值:** 7504| 参数名 | 说明 | 7505| -------- | ------------------------------------------------- | 7506| Promise\<void> | 无返回结果的Promise对象。 | 7507 7508**错误码:** 7509 7510以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7511 7512| **错误码ID** | **错误信息** | 7513|-----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 7514| 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. | 7515| 801 | Capability not supported. | 7516| 14800000 | Inner error. | 7517| 14800011 | Failed to open the database because it is corrupted. | 7518| 14800014 | The RdbStore or ResultSet is already closed. | 7519| 14800015 | The database does not respond. | 7520| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 7521| 14800022 | SQLite: Callback routine requested an abort. | 7522| 14800023 | SQLite: Access permission denied. | 7523| 14800024 | SQLite: The database file is locked. | 7524| 14800025 | SQLite: A table in the database is locked. | 7525| 14800026 | SQLite: The database is out of memory. | 7526| 14800027 | SQLite: Attempt to write a readonly database. | 7527| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7528| 14800029 | SQLite: The database is full. | 7529| 14800030 | SQLite: Unable to open the database file. | 7530| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7531| 14800032 | SQLite: Abort due to constraint violation. | 7532| 14800033 | SQLite: Data type mismatch. | 7533| 14800034 | SQLite: Library used incorrectly. | 7534 7535**示例:** 7536 7537```ts 7538import { BusinessError } from '@kit.BasicServicesKit'; 7539 7540if (store != undefined) { 7541 (store as relationalStore.RdbStore).cleanDirtyData('test_table', 100).then(() => { 7542 console.info('clean dirty data succeeded'); 7543 }).catch((err: BusinessError) => { 7544 console.error(`clean dirty data failed, code is ${err.code},message is ${err.message}`); 7545 }); 7546} 7547``` 7548 7549### attach<sup>12+</sup> 7550 7551attach(fullPath: string, attachName: string, waitTime?: number) : Promise<number> 7552 7553将一个数据库文件附加到当前数据库中,以便在SQL语句中可以直接访问附加数据库中的数据。 7554 7555数据库文件来自文件,且此API不支持附加加密数据库。调用attach接口后,数据库切换为非WAL模式,性能会存在一定的劣化。 7556 7557attach时,数据库会切换为非WAL模式,切换模式需要确保所有的ResultSet都已经Close,所有的写操作已经结束,否则会报错14800015。 7558 7559attach不能并发调用,否则可能出现未响应情况并报错14800015,需要重试。 7560 7561**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7562 7563**参数:** 7564 7565| 参数名 | 类型 | 必填 | 说明 | 7566| ----------- | ------ | --- | ------------ | 7567| fullPath | string | 是 | 表示要附加的数据库的路径。 | 7568| attachName | string | 是 | 表示附加后的数据库的别名。 | 7569| waitTime | number | 否 | 表示附加数据库文件的等待时长。默认值2s,最小值1s,最大值300s。 | 7570 7571**返回值:** 7572 7573| 类型 | 说明 | 7574| ---------------- | ---------------------------- | 7575| Promise<number> | Promise对象。返回附加数据库的数量。 | 7576 7577**错误码:** 7578 7579以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7580 7581| **错误码ID** | **错误信息** | 7582|-----------| ------------------------------------------------------------ | 7583| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7584| 801 | Capability not supported. | 7585| 14800000 | Inner error. | 7586| 14800010 | Failed to open or delete the database by an invalid database path. | 7587| 14800011 | Failed to open the database because it is corrupted. | 7588| 14800014 | The RdbStore or ResultSet is already closed. | 7589| 14800015 | The database does not respond. | 7590| 14800016 | The database alias already exists. | 7591| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 7592| 14800022 | SQLite: Callback routine requested an abort. | 7593| 14800023 | SQLite: Access permission denied. | 7594| 14800024 | SQLite: The database file is locked. | 7595| 14800025 | SQLite: A table in the database is locked. | 7596| 14800026 | SQLite: The database is out of memory. | 7597| 14800027 | SQLite: Attempt to write a readonly database. | 7598| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7599| 14800029 | SQLite: The database is full. | 7600| 14800030 | SQLite: Unable to open the database file. | 7601| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7602| 14800032 | SQLite: Abort due to constraint violation. | 7603| 14800033 | SQLite: Data type mismatch. | 7604| 14800034 | SQLite: Library used incorrectly. | 7605 7606**示例:** 7607 7608```ts 7609// 非加密数据库附加非加密数据库。 7610import { BusinessError } from '@kit.BasicServicesKit'; 7611 7612if (store != undefined) { 7613 (store as relationalStore.RdbStore).attach("/path/rdbstore1.db", "attachDB").then((number: number) => { 7614 console.info('attach succeeded'); 7615 }).catch((err: BusinessError) => { 7616 console.error(`attach failed, code is ${err.code},message is ${err.message}`); 7617 }); 7618} 7619``` 7620 7621### attach<sup>12+</sup> 7622 7623attach(context: Context, config: StoreConfig, attachName: string, waitTime?: number) : Promise<number> 7624 7625将一个当前应用的数据库附加到当前数据库中,以便在SQL语句中可以直接访问附加数据库中的数据。 7626 7627此API不支持加密数据库附加非加密数据库。调用attach接口后,数据库切换为非WAL模式,性能会存在一定的劣化。 7628 7629attach时,数据库会切换为非WAL模式,切换模式需要确保所有的ResultSet都已经Close,所有的写操作已经结束,否则会报错14800015。 7630 7631attach不能并发调用,否则可能出现未响应情况并报错14800015,需要重试。除此之外,attach附加加密数据库时,可能受到并发的影响,出现解密失败的情况,报错14800011,需要显式指定加密参数并重试。 7632 7633**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7634 7635**参数:** 7636 7637| 参数名 | 类型 | 必填 | 说明 | 7638| ----------- | ------ | --- | ------------ | 7639| 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)。 | 7640| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 7641| attachName | string | 是 | 表示附加后的数据库的别名。 | 7642| waitTime | number | 否 | 表示附加数据库文件的等待时长。默认值2s,最小值1s,最大值300s。 | 7643 7644**返回值:** 7645 7646| 类型 | 说明 | 7647| ---------------- | ---------------------------- | 7648| Promise<number> | Promise对象。返回附加数据库的数量。 | 7649 7650**错误码:** 7651 7652以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7653 7654| **错误码ID** | **错误信息** | 7655|-----------| ------------------------------------------------------------ | 7656| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7657| 801 | Capability not supported. | 7658| 14800000 | Inner error. | 7659| 14800010 | Failed to open or delete the database by an invalid database path. | 7660| 14800011 | Failed to open the database because it is corrupted. | 7661| 14800014 | The RdbStore or ResultSet is already closed. | 7662| 14800015 | The database does not respond. | 7663| 14800016 | The database alias already exists. | 7664| 14801001 | The operation is supported in the stage model only. | 7665| 14801002 | Invalid data group ID. | 7666| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 7667| 14800022 | SQLite: Callback routine requested an abort. | 7668| 14800023 | SQLite: Access permission denied. | 7669| 14800024 | SQLite: The database file is locked. | 7670| 14800025 | SQLite: A table in the database is locked. | 7671| 14800026 | SQLite: The database is out of memory. | 7672| 14800027 | SQLite: Attempt to write a readonly database. | 7673| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7674| 14800029 | SQLite: The database is full. | 7675| 14800030 | SQLite: Unable to open the database file. | 7676| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7677| 14800032 | SQLite: Abort due to constraint violation. | 7678| 14800033 | SQLite: Data type mismatch. | 7679| 14800034 | SQLite: Library used incorrectly. | 7680 7681**示例1:非加密数据库附加非加密数据库** 7682 7683```ts 7684import { BusinessError } from '@kit.BasicServicesKit'; 7685 7686let attachStore: relationalStore.RdbStore | undefined = undefined; 7687 7688const STORE_CONFIG1: relationalStore.StoreConfig = { 7689 name: "rdbstore1.db", 7690 securityLevel: relationalStore.SecurityLevel.S3 7691}; 7692 7693relationalStore.getRdbStore(this.context, STORE_CONFIG1).then(async (rdbStore: relationalStore.RdbStore) => { 7694 attachStore = rdbStore; 7695 console.info('Get RdbStore successfully.'); 7696}).catch((err: BusinessError) => { 7697 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 7698}); 7699 7700if (store != undefined) { 7701 (store as relationalStore.RdbStore).attach(this.context, STORE_CONFIG1, "attachDB").then((number: number) => { 7702 console.info(`attach succeeded, number is ${number}`); 7703 }).catch((err: BusinessError) => { 7704 console.error(`attach failed, code is ${err.code},message is ${err.message}`); 7705 }); 7706} 7707``` 7708 7709**示例2:非加密数据库附加加密数据库** 7710 7711```ts 7712import { BusinessError } from '@kit.BasicServicesKit'; 7713 7714let attachStore: relationalStore.RdbStore | undefined = undefined; 7715 7716const STORE_CONFIG2: relationalStore.StoreConfig = { 7717 name: "rdbstore2.db", 7718 encrypt: true, 7719 securityLevel: relationalStore.SecurityLevel.S3 7720}; 7721 7722relationalStore.getRdbStore(this.context, STORE_CONFIG2).then(async (rdbStore: relationalStore.RdbStore) => { 7723 attachStore = rdbStore; 7724 console.info('Get RdbStore successfully.'); 7725}).catch((err: BusinessError) => { 7726 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 7727}); 7728 7729if (store != undefined) { 7730 (store as relationalStore.RdbStore).attach(this.context, STORE_CONFIG2, "attachDB2", 10).then((number: number) => { 7731 console.info(`attach succeeded, number is ${number}`); 7732 }).catch((err: BusinessError) => { 7733 console.error(`attach failed, code is ${err.code},message is ${err.message}`); 7734 }); 7735} 7736``` 7737 7738### detach<sup>12+</sup> 7739 7740detach(attachName: string, waitTime?: number) : Promise<number> 7741 7742将附加的数据库从当前数据库中分离。 7743 7744当所有的附加的数据库被分离后,数据库会重新切换为WAL模式。 7745 7746在detach之前,所有的数据库操作要确保已经结束,所有的ResultSet已经Close。并且不能并发调用,可能出现未响应情况,需要重试。 7747 7748**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7749 7750**参数:** 7751 7752| 参数名 | 类型 | 必填 | 说明 | 7753| ----------- | ------ | --- | ------------ | 7754| attachName | string | 是 | 表示附加后的数据库的别名。 | 7755| waitTime | number | 否 | 表示分离数据库的等待时长。默认值2s,最小值1s,最大值300s。 | 7756 7757**返回值:** 7758 7759| 类型 | 说明 | 7760| ---------------- | ---------------------------- | 7761| Promise<number> | Promise对象。返回分离后剩余附加的数据库的数量。 | 7762 7763**错误码:** 7764 7765以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7766 7767| **错误码ID** | **错误信息** | 7768|-----------|------------------------| 7769| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7770| 14800000 | Inner error. | 7771| 14800011 | Failed to open the database because it is corrupted. | 7772| 14800014 | The RdbStore or ResultSet is already closed. | 7773| 14800015 | The database does not respond. | 7774| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 7775| 14800022 | SQLite: Callback routine requested an abort. | 7776| 14800023 | SQLite: Access permission denied. | 7777| 14800024 | SQLite: The database file is locked. | 7778| 14800025 | SQLite: A table in the database is locked. | 7779| 14800026 | SQLite: The database is out of memory. | 7780| 14800027 | SQLite: Attempt to write a readonly database. | 7781| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7782| 14800029 | SQLite: The database is full. | 7783| 14800030 | SQLite: Unable to open the database file. | 7784| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7785| 14800032 | SQLite: Abort due to constraint violation. | 7786| 14800033 | SQLite: Data type mismatch. | 7787| 14800034 | SQLite: Library used incorrectly. | 7788 7789**示例:** 7790 7791```ts 7792import { BusinessError } from '@kit.BasicServicesKit'; 7793 7794if (store != undefined) { 7795 (store as relationalStore.RdbStore).detach("attachDB").then((number: number) => { 7796 console.info(`detach succeeded, number is ${number}`); 7797 }).catch((err: BusinessError) => { 7798 console.error(`detach failed, code is ${err.code},message is ${err.message}`); 7799 }); 7800} 7801``` 7802 7803### lockRow<sup>12+</sup> 7804 7805lockRow(predicates: RdbPredicates):Promise<void> 7806 7807根据RdbPredicates的指定实例对象从数据库中锁定数据,锁定数据不执行端云同步,使用Promise异步回调。 7808 7809该接口只支持主键为基本类型的表、不支持共享表、无主键表和复合类型主键表。 7810该接口不支持依赖关系表之间的锁传递,如果表存在依赖关系,需要根据依赖关系手动调用该接口。 7811该接口不支持对已删除数据的操作。 7812 7813**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7814 7815**参数:** 7816 7817| 参数名 | 类型 | 必填 | 说明 | 7818| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 7819| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的锁定条件。 | 7820 7821**返回值**: 7822 7823| 类型 | 说明 | 7824| --------------------- | ------------------------------- | 7825| Promise<void> | 无返回结果的Promise对象。 | 7826 7827**错误码:** 7828 7829以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7830 7831| **错误码ID** | **错误信息** | 7832|-----------|----------------------------------------------------------------------------------------------| 7833| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7834| 14800000 | Inner error. | 7835| 14800011 | Failed to open the database because it is corrupted. | 7836| 14800014 | The RdbStore or ResultSet is already closed. | 7837| 14800015 | The database does not respond. | 7838| 14800018 | No data meets the condition. | 7839| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 7840| 14800022 | SQLite: Callback routine requested an abort. | 7841| 14800023 | SQLite: Access permission denied. | 7842| 14800024 | SQLite: The database file is locked. | 7843| 14800025 | SQLite: A table in the database is locked. | 7844| 14800026 | SQLite: The database is out of memory. | 7845| 14800027 | SQLite: Attempt to write a readonly database. | 7846| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7847| 14800029 | SQLite: The database is full. | 7848| 14800030 | SQLite: Unable to open the database file. | 7849| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7850| 14800032 | SQLite: Abort due to constraint violation. | 7851| 14800033 | SQLite: Data type mismatch. | 7852| 14800034 | SQLite: Library used incorrectly. | 7853 7854**示例:** 7855 7856```ts 7857import { BusinessError } from '@kit.BasicServicesKit'; 7858 7859let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 7860predicates.equalTo("NAME", "Lisa"); 7861if (store != undefined) { 7862 (store as relationalStore.RdbStore).lockRow(predicates).then(() => { 7863 console.info(`Lock success`); 7864 }).catch((err: BusinessError) => { 7865 console.error(`Lock failed, code is ${err.code},message is ${err.message}`); 7866 }); 7867} 7868``` 7869 7870### unlockRow<sup>12+</sup> 7871 7872unlockRow(predicates: RdbPredicates):Promise<void> 7873 7874根据RdbPredicates的指定实例对象从数据库中解锁数据,使用Promise异步回调。 7875 7876该接口只支持主键为基本类型的表、不支持共享表、无主键表和复合类型主键表。 7877该接口不支持依赖关系表之间的锁传递,如果表存在依赖关系,需要根据依赖关系手动调用该接口。 7878该接口不支持对已删除数据的操作。 7879 7880**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7881 7882**参数:** 7883 7884| 参数名 | 类型 | 必填 | 说明 | 7885| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 7886| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的锁定条件。 | 7887 7888**返回值**: 7889 7890| 类型 | 说明 | 7891| --------------------- | ------------------------------- | 7892| Promise<void> | 无返回结果的Promise对象。 | 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 | Failed to open the database because it is corrupted. | 7903| 14800014 | The RdbStore or ResultSet is already closed. | 7904| 14800015 | The database does not respond. | 7905| 14800018 | No data meets the condition. | 7906| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 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 7924import { BusinessError } from '@kit.BasicServicesKit'; 7925 7926let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 7927predicates.equalTo("NAME", "Lisa"); 7928if (store != undefined) { 7929 (store as relationalStore.RdbStore).unlockRow(predicates).then(() => { 7930 console.info(`Unlock success`); 7931 }).catch((err: BusinessError) => { 7932 console.error(`Unlock failed, code is ${err.code},message is ${err.message}`); 7933 }); 7934} 7935``` 7936 7937### queryLockedRow<sup>12+</sup> 7938 7939queryLockedRow(predicates: RdbPredicates, columns?: Array<string>):Promise<ResultSet> 7940 7941根据指定条件查询数据库中锁定的数据,使用Promise异步回调。 7942由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 7943 7944**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 7945 7946**参数:** 7947 7948| 参数名 | 类型 | 必填 | 说明 | 7949| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 7950| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 7951| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 7952 7953**错误码:** 7954 7955以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 7956 7957| **错误码ID** | **错误信息** | 7958|-----------| ------------------------------------------------------------ | 7959| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 7960| 14800000 | Inner error. | 7961| 14800011 | Failed to open the database because it is corrupted. | 7962| 14800014 | The RdbStore or ResultSet is already closed. | 7963| 14800015 | The database does not respond. | 7964| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 7965| 14800022 | SQLite: Callback routine requested an abort. | 7966| 14800023 | SQLite: Access permission denied. | 7967| 14800024 | SQLite: The database file is locked. | 7968| 14800025 | SQLite: A table in the database is locked. | 7969| 14800026 | SQLite: The database is out of memory. | 7970| 14800027 | SQLite: Attempt to write a readonly database. | 7971| 14800028 | SQLite: Some kind of disk I/O error occurred. | 7972| 14800029 | SQLite: The database is full. | 7973| 14800030 | SQLite: Unable to open the database file. | 7974| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 7975| 14800032 | SQLite: Abort due to constraint violation. | 7976| 14800033 | SQLite: Data type mismatch. | 7977| 14800034 | SQLite: Library used incorrectly. | 7978 7979**返回值**: 7980 7981| 类型 | 说明 | 7982| ------------------------------------------------------- | -------------------------------------------------- | 7983| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 7984 7985**示例:** 7986 7987```ts 7988import { BusinessError } from '@kit.BasicServicesKit'; 7989 7990let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 7991predicates.equalTo("NAME", "Rose"); 7992if (store != undefined) { 7993 (store as relationalStore.RdbStore).queryLockedRow(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then(async (resultSet: relationalStore.ResultSet) => { 7994 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 7995 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 7996 while (resultSet.goToNextRow()) { 7997 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 7998 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 7999 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 8000 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 8001 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 8002 } 8003 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 8004 resultSet.close(); 8005 }).catch((err: BusinessError) => { 8006 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 8007 }); 8008} 8009``` 8010### close<sup>12+</sup> 8011 8012close(): Promise<void> 8013 8014关闭数据库,使用Promise异步回调。 8015 8016**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8017 8018**返回值:** 8019 8020| 类型 | 说明 | 8021| ------------------- | ------------- | 8022| Promise<void> | Promise对象。 | 8023 8024**错误码:** 8025 8026以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 8027 8028| **错误码ID** | **错误信息** | 8029| ------------ | ----------------------------------------------- | 8030| 401 | Parameter error. The store must not be nullptr. | 8031| 14800000 | Inner error. | 8032 8033**示例:** 8034 8035```ts 8036import { BusinessError } from '@kit.BasicServicesKit'; 8037 8038if (store != undefined) { 8039 (store as relationalStore.RdbStore).close().then(() => { 8040 console.info(`close succeeded`); 8041 }).catch((err: BusinessError) => { 8042 console.error(`close failed, code is ${err.code},message is ${err.message}`); 8043 }); 8044} 8045``` 8046 8047## ResultSet 8048 8049提供通过查询数据库生成的数据库结果集的访问方法。结果集是指用户调用关系型数据库查询接口之后返回的结果集合,提供了多种灵活的数据访问方式,以便用户获取各项数据。 8050 8051下列API示例中,都需先使用[query](#query14)、[querySql](#querysql14)、[remoteQuery](#remotequery-1)、[queryLockedRow](#querylockedrow12)中任一方法获取到ResultSet实例,再通过此实例调用对应方法。 8052 8053### 属性 8054 8055**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8056 8057| 名称 | 类型 | 必填 | 说明 | 8058| ------------ | ------------------- | ---- | -------------------------------- | 8059| columnNames | Array<string> | 是 | 获取结果集中所有列的名称。 | 8060| columnCount | number | 是 | 获取结果集中列的数量。 | 8061| rowCount | number | 是 | 获取结果集中行的数量。 | 8062| rowIndex | number | 是 | 获取结果集当前行的索引位置,默认值为-1。索引位置下标从0开始。 | 8063| isAtFirstRow | boolean | 是 | 检查结果集指针是否位于第一行(行索引为0),true表示位于第一行,false表示不位于第一行。 | 8064| isAtLastRow | boolean | 是 | 检查结果集指针是否位于最后一行,true表示位于最后一行,false表示不位于最后一行。 | 8065| isEnded | boolean | 是 | 检查结果集指针是否位于最后一行之后,true表示位于最后一行之后,false表示不位于最后一行之后。 | 8066| isStarted | boolean | 是 | 检查指针是否移动过,true表示指针已移动过,false表示指针未移动过。 | 8067| isClosed | boolean | 是 | 检查当前结果集是否关闭,true表示结果集已关闭,false表示结果集未关闭。 | 8068 8069### getColumnIndex 8070 8071getColumnIndex(columnName: string): number 8072 8073根据指定的列名获取列索引。 8074 8075**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8076 8077**参数:** 8078 8079| 参数名 | 类型 | 必填 | 说明 | 8080| ---------- | ------ | ---- | -------------------------- | 8081| columnName | string | 是 | 表示结果集中指定列的名称。 | 8082 8083**返回值:** 8084 8085| 类型 | 说明 | 8086| ------ | ------------------ | 8087| number | 返回指定列的索引。 | 8088 8089**错误码:** 8090 8091以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8092 8093| **错误码ID** | **错误信息** | 8094|-----------| ------------------------------------------------------------ | 8095| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8096| 14800000 | Inner error. | 8097| 14800011 | Failed to open the database because it is corrupted. | 8098| 14800013 | Resultset is empty or column index is out of bounds. | 8099| 14800014 | The RdbStore or ResultSet is already closed. | 8100| 14800019 | The SQL must be a query statement. | 8101| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8102| 14800022 | SQLite: Callback routine requested an abort. | 8103| 14800023 | SQLite: Access permission denied. | 8104| 14800024 | SQLite: The database file is locked. | 8105| 14800025 | SQLite: A table in the database is locked. | 8106| 14800026 | SQLite: The database is out of memory. | 8107| 14800027 | SQLite: Attempt to write a readonly database. | 8108| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8109| 14800029 | SQLite: The database is full. | 8110| 14800030 | SQLite: Unable to open the database file. | 8111| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8112| 14800032 | SQLite: Abort due to constraint violation. | 8113| 14800033 | SQLite: Data type mismatch. | 8114| 14800034 | SQLite: Library used incorrectly. | 8115 8116**示例:** 8117 8118```ts 8119if (resultSet != undefined) { 8120 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 8121 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 8122 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 8123 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 8124} 8125``` 8126 8127### getColumnName 8128 8129getColumnName(columnIndex: number): string 8130 8131根据指定的列索引获取列名。 8132 8133**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8134 8135**参数:** 8136 8137| 参数名 | 类型 | 必填 | 说明 | 8138| ----------- | ------ | ---- | -------------------------- | 8139| columnIndex | number | 是 | 表示结果集中指定列的索引。 | 8140 8141**返回值:** 8142 8143| 类型 | 说明 | 8144| ------ | ------------------ | 8145| string | 返回指定列的名称。 | 8146 8147**错误码:** 8148 8149以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8150 8151| **错误码ID** | **错误信息** | 8152|-----------| ------------------------------------------------------------ | 8153| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8154| 14800000 | Inner error. | 8155| 14800011 | Failed to open the database because it is corrupted. | 8156| 14800013 | Resultset is empty or column index is out of bounds. | 8157| 14800014 | The RdbStore or ResultSet is already closed. | 8158| 14800019 | The SQL must be a query statement. | 8159| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8160| 14800022 | SQLite: Callback routine requested an abort. | 8161| 14800023 | SQLite: Access permission denied. | 8162| 14800024 | SQLite: The database file is locked. | 8163| 14800025 | SQLite: A table in the database is locked. | 8164| 14800026 | SQLite: The database is out of memory. | 8165| 14800027 | SQLite: Attempt to write a readonly database. | 8166| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8167| 14800029 | SQLite: The database is full. | 8168| 14800030 | SQLite: Unable to open the database file. | 8169| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8170| 14800032 | SQLite: Abort due to constraint violation. | 8171| 14800033 | SQLite: Data type mismatch. | 8172| 14800034 | SQLite: Library used incorrectly. | 8173 8174**示例:** 8175 8176```ts 8177if (resultSet != undefined) { 8178 const id = (resultSet as relationalStore.ResultSet).getColumnName(0); 8179 const name = (resultSet as relationalStore.ResultSet).getColumnName(1); 8180 const age = (resultSet as relationalStore.ResultSet).getColumnName(2); 8181} 8182``` 8183 8184### getColumnType<sup>18+</sup> 8185 8186getColumnType(columnIdentifier: number | string): Promise\<ColumnType> 8187 8188根据指定的列索引或列名称获取列数据类型,使用Promise异步回调。 8189 8190**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8191 8192**参数:** 8193 8194| 参数名 | 类型 | 必填 | 说明 | 8195| ---------------- | ---------------- | ---- | ------------------------------------------------------------ | 8196| columnIdentifier | number \| string | 是 | 表示结果集中指定列的索引或名称。索引必须是非负整数,最大不能超过属性columnNames的长度。列名必须是属性columnNames内的名称。 | 8197 8198**返回值:** 8199 8200| 类型 | 说明 | 8201| ------------------------------------ | ----------------------------------- | 8202| Promise<[ColumnType](#columntype18)> | Promise对象。返回指定列的数据类型。 | 8203 8204**错误码:** 8205 8206以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8207 8208| **错误码ID** | **错误信息** | 8209| ------------ | ------------------------------------------------------------ | 8210| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8211| 14800000 | Inner error. | 8212| 14800011 | Failed to open the database because it is corrupted. | 8213| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8214| 14800013 | Resultset is empty or column index is out of bounds. | 8215| 14800014 | The RdbStore or ResultSet is already closed. | 8216| 14800019 | The SQL must be a query statement. | 8217| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8218| 14800022 | SQLite: Callback routine requested an abort. | 8219| 14800023 | SQLite: Access permission denied. | 8220| 14800024 | SQLite: The database file is locked. | 8221| 14800025 | SQLite: A table in the database is locked. | 8222| 14800026 | SQLite: The database is out of memory. | 8223| 14800027 | SQLite: Attempt to write a readonly database. | 8224| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8225| 14800029 | SQLite: The database is full. | 8226| 14800030 | SQLite: Unable to open the database file. | 8227| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8228| 14800032 | SQLite: Abort due to constraint violation. | 8229| 14800033 | SQLite: Data type mismatch. | 8230| 14800034 | SQLite: Library used incorrectly. | 8231 8232**示例:** 8233 8234```ts 8235if (resultSet != undefined) { 8236 let idType = await (resultSet as relationalStore.ResultSet).getColumnType("ID") as relationalStore.ColumnType; 8237 let nameType = await (resultSet as relationalStore.ResultSet).getColumnType("NAME") as relationalStore.ColumnType; 8238 let ageType = await (resultSet as relationalStore.ResultSet).getColumnType("AGE") as relationalStore.ColumnType; 8239 let salaryType = await (resultSet as relationalStore.ResultSet).getColumnType("SALARY") as relationalStore.ColumnType; 8240 let codesType = await (resultSet as relationalStore.ResultSet).getColumnType("CODES") as relationalStore.ColumnType; 8241 let identityType = await (resultSet as relationalStore.ResultSet).getColumnType(5) as relationalStore.ColumnType; 8242 let assetDataType = await (resultSet as relationalStore.ResultSet).getColumnType(6) as relationalStore.ColumnType; 8243 let assetsDataType = await (resultSet as relationalStore.ResultSet).getColumnType(7) as relationalStore.ColumnType; 8244 let floatArrayType = await (resultSet as relationalStore.ResultSet).getColumnType(8) as relationalStore.ColumnType; 8245} 8246``` 8247 8248### getColumnTypeSync<sup>18+</sup> 8249 8250getColumnTypeSync(columnIdentifier: number | string): ColumnType 8251 8252根据指定的列索引或列名称获取列数据类型。 8253 8254**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8255 8256**参数:** 8257 8258| 参数名 | 类型 | 必填 | 说明 | 8259| ---------------- | ---------------- | ---- | ------------------------------------------------------------ | 8260| columnIdentifier | number \| string | 是 | 表示结果集中指定列的索引或名称。索引必须是非负整数,最大不能超过属性columnNames的长度。列名必须是属性columnNames内的名称。 | 8261 8262**返回值:** 8263 8264| 类型 | 说明 | 8265| --------------------------- | ---------------------- | 8266| [ColumnType](#columntype18) | 返回指定列的数据类型。 | 8267 8268**错误码:** 8269 8270以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8271 8272| **错误码ID** | **错误信息** | 8273| ------------ | ------------------------------------------------------------ | 8274| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8275| 14800000 | Inner error. | 8276| 14800011 | Failed to open the database because it is corrupted. | 8277| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8278| 14800013 | Resultset is empty or column index is out of bounds. | 8279| 14800014 | The RdbStore or ResultSet is already closed. | 8280| 14800019 | The SQL must be a query statement. | 8281| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8282| 14800022 | SQLite: Callback routine requested an abort. | 8283| 14800023 | SQLite: Access permission denied. | 8284| 14800024 | SQLite: The database file is locked. | 8285| 14800025 | SQLite: A table in the database is locked. | 8286| 14800026 | SQLite: The database is out of memory. | 8287| 14800027 | SQLite: Attempt to write a readonly database. | 8288| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8289| 14800029 | SQLite: The database is full. | 8290| 14800030 | SQLite: Unable to open the database file. | 8291| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8292| 14800032 | SQLite: Abort due to constraint violation. | 8293| 14800033 | SQLite: Data type mismatch. | 8294| 14800034 | SQLite: Library used incorrectly. | 8295 8296**示例:** 8297 8298```ts 8299if (resultSet != undefined) { 8300 let idType = (resultSet as relationalStore.ResultSet).getColumnTypeSync("ID") as relationalStore.ColumnType; 8301 let nameType = (resultSet as relationalStore.ResultSet).getColumnTypeSync("NAME") as relationalStore.ColumnType; 8302 let ageType = (resultSet as relationalStore.ResultSet).getColumnTypeSync("AGE") as relationalStore.ColumnType; 8303 let salaryType = (resultSet as relationalStore.ResultSet).getColumnTypeSync("SALARY") as relationalStore.ColumnType; 8304 let codesType = (resultSet as relationalStore.ResultSet).getColumnTypeSync("CODES") as relationalStore.ColumnType; 8305 let identityType = (resultSet as relationalStore.ResultSet).getColumnTypeSync(5) as relationalStore.ColumnType; 8306 let assetDataType = (resultSet as relationalStore.ResultSet).getColumnTypeSync(6) as relationalStore.ColumnType; 8307 let assetsDataType = (resultSet as relationalStore.ResultSet).getColumnTypeSync(7) as relationalStore.ColumnType; 8308 let floatArrayType = (resultSet as relationalStore.ResultSet).getColumnTypeSync(8) as relationalStore.ColumnType; 8309} 8310``` 8311 8312### goTo 8313 8314goTo(offset:number): boolean 8315 8316指定相对当前结果集指针位置的偏移量,以移动结果集的指针位置。 8317 8318**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8319 8320**参数:** 8321 8322| 参数名 | 类型 | 必填 | 说明 | 8323| ------ | ------ | ---- | ---------------------------- | 8324| offset | number | 是 | 表示相对当前结果集指针位置的偏移量,正值表示向后移动,负值表示向前移动。 | 8325 8326**返回值:** 8327 8328| 类型 | 说明 | 8329| ------- | --------------------------------------------- | 8330| boolean | 如果成功移动结果集,则为true;否则返回false。 | 8331 8332**错误码:** 8333 8334以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8335 8336| **错误码ID** | **错误信息** | 8337|-----------| ------------------------------------------------------------ | 8338| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8339| 14800000 | Inner error. | 8340| 14800011 | Failed to open the database because it is corrupted. | 8341| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8342| 14800014 | The RdbStore or ResultSet is already closed. | 8343| 14800019 | The SQL must be a query statement. | 8344| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8345| 14800022 | SQLite: Callback routine requested an abort. | 8346| 14800023 | SQLite: Access permission denied. | 8347| 14800024 | SQLite: The database file is locked. | 8348| 14800025 | SQLite: A table in the database is locked. | 8349| 14800026 | SQLite: The database is out of memory. | 8350| 14800027 | SQLite: Attempt to write a readonly database. | 8351| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8352| 14800029 | SQLite: The database is full. | 8353| 14800030 | SQLite: Unable to open the database file. | 8354| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8355| 14800032 | SQLite: Abort due to constraint violation. | 8356| 14800033 | SQLite: Data type mismatch. | 8357| 14800034 | SQLite: Library used incorrectly. | 8358 8359**示例:** 8360 8361```ts 8362if (resultSet != undefined) { 8363 (resultSet as relationalStore.ResultSet).goTo(1); 8364} 8365``` 8366 8367### goToRow 8368 8369goToRow(position: number): boolean 8370 8371转到结果集的指定行。 8372 8373**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8374 8375**参数:** 8376 8377| 参数名 | 类型 | 必填 | 说明 | 8378| -------- | ------ | ---- | ------------------------ | 8379| position | number | 是 | 表示要移动到的指定位置。 | 8380 8381**返回值:** 8382 8383| 类型 | 说明 | 8384| ------- | --------------------------------------------- | 8385| boolean | 如果成功移动结果集,则为true;否则返回false。 | 8386 8387**错误码:** 8388 8389以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8390 8391| **错误码ID** | **错误信息** | 8392|-----------| ------------------------------------------------------------ | 8393| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8394| 14800000 | Inner error. | 8395| 14800011 | Failed to open the database because it is corrupted. | 8396| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8397| 14800014 | The RdbStore or ResultSet is already closed. | 8398| 14800019 | The SQL must be a query statement. | 8399| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8400| 14800022 | SQLite: Callback routine requested an abort. | 8401| 14800023 | SQLite: Access permission denied. | 8402| 14800024 | SQLite: The database file is locked. | 8403| 14800025 | SQLite: A table in the database is locked. | 8404| 14800026 | SQLite: The database is out of memory. | 8405| 14800027 | SQLite: Attempt to write a readonly database. | 8406| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8407| 14800029 | SQLite: The database is full. | 8408| 14800030 | SQLite: Unable to open the database file. | 8409| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8410| 14800032 | SQLite: Abort due to constraint violation. | 8411| 14800033 | SQLite: Data type mismatch. | 8412| 14800034 | SQLite: Library used incorrectly. | 8413 8414**示例:** 8415 8416```ts 8417if (resultSet != undefined) { 8418 (resultSet as relationalStore.ResultSet).goToRow(5); 8419} 8420``` 8421 8422### goToFirstRow 8423 8424goToFirstRow(): boolean 8425 8426 8427转到结果集的第一行。 8428 8429**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8430 8431**返回值:** 8432 8433| 类型 | 说明 | 8434| ------- | --------------------------------------------- | 8435| boolean | 如果成功移动结果集,则为true;否则返回false。 | 8436 8437**错误码:** 8438 8439以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8440 8441| **错误码ID** | **错误信息** | 8442|-----------| ------------------------------------------------------------ | 8443| 14800000 | Inner error. | 8444| 14800011 | Failed to open the database because it is corrupted. | 8445| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8446| 14800014 | The RdbStore or ResultSet is already closed. | 8447| 14800019 | The SQL must be a query statement. | 8448| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8449| 14800022 | SQLite: Callback routine requested an abort. | 8450| 14800023 | SQLite: Access permission denied. | 8451| 14800024 | SQLite: The database file is locked. | 8452| 14800025 | SQLite: A table in the database is locked. | 8453| 14800026 | SQLite: The database is out of memory. | 8454| 14800027 | SQLite: Attempt to write a readonly database. | 8455| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8456| 14800029 | SQLite: The database is full. | 8457| 14800030 | SQLite: Unable to open the database file. | 8458| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8459| 14800032 | SQLite: Abort due to constraint violation. | 8460| 14800033 | SQLite: Data type mismatch. | 8461| 14800034 | SQLite: Library used incorrectly. | 8462 8463**示例:** 8464 8465```ts 8466if (resultSet != undefined) { 8467 (resultSet as relationalStore.ResultSet).goToFirstRow(); 8468} 8469``` 8470 8471### goToLastRow 8472 8473goToLastRow(): boolean 8474 8475转到结果集的最后一行。 8476 8477**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8478 8479**返回值:** 8480 8481| 类型 | 说明 | 8482| ------- | --------------------------------------------- | 8483| boolean | 如果成功移动结果集,则为true;否则返回false。 | 8484 8485**错误码:** 8486 8487以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8488 8489| **错误码ID** | **错误信息** | 8490|-----------| ------------------------------------------------------------ | 8491| 14800000 | Inner error. | 8492| 14800011 | Failed to open the database because it is corrupted. | 8493| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8494| 14800014 | The RdbStore or ResultSet is already closed. | 8495| 14800019 | The SQL must be a query statement. | 8496| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8497| 14800022 | SQLite: Callback routine requested an abort. | 8498| 14800023 | SQLite: Access permission denied. | 8499| 14800024 | SQLite: The database file is locked. | 8500| 14800025 | SQLite: A table in the database is locked. | 8501| 14800026 | SQLite: The database is out of memory. | 8502| 14800027 | SQLite: Attempt to write a readonly database. | 8503| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8504| 14800029 | SQLite: The database is full. | 8505| 14800030 | SQLite: Unable to open the database file. | 8506| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8507| 14800032 | SQLite: Abort due to constraint violation. | 8508| 14800033 | SQLite: Data type mismatch. | 8509| 14800034 | SQLite: Library used incorrectly. | 8510 8511**示例:** 8512 8513```ts 8514if (resultSet != undefined) { 8515 (resultSet as relationalStore.ResultSet).goToLastRow(); 8516} 8517``` 8518 8519### goToNextRow 8520 8521goToNextRow(): boolean 8522 8523转到结果集的下一行。 8524 8525**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8526 8527**返回值:** 8528 8529| 类型 | 说明 | 8530| ------- | --------------------------------------------- | 8531| boolean | 如果成功移动结果集,则为true;否则返回false。 | 8532 8533**错误码:** 8534 8535以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8536 8537| **错误码ID** | **错误信息** | 8538|-----------| ------------------------------------------------------------ | 8539| 14800000 | Inner error. | 8540| 14800011 | Failed to open the database because it is corrupted. | 8541| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8542| 14800014 | The RdbStore or ResultSet is already closed. | 8543| 14800019 | The SQL must be a query statement. | 8544| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8545| 14800022 | SQLite: Callback routine requested an abort. | 8546| 14800023 | SQLite: Access permission denied. | 8547| 14800024 | SQLite: The database file is locked. | 8548| 14800025 | SQLite: A table in the database is locked. | 8549| 14800026 | SQLite: The database is out of memory. | 8550| 14800027 | SQLite: Attempt to write a readonly database. | 8551| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8552| 14800029 | SQLite: The database is full. | 8553| 14800030 | SQLite: Unable to open the database file. | 8554| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8555| 14800032 | SQLite: Abort due to constraint violation. | 8556| 14800033 | SQLite: Data type mismatch. | 8557| 14800034 | SQLite: Library used incorrectly. | 8558 8559**示例:** 8560 8561```ts 8562if (resultSet != undefined) { 8563 (resultSet as relationalStore.ResultSet).goToNextRow(); 8564} 8565``` 8566 8567### goToPreviousRow 8568 8569goToPreviousRow(): boolean 8570 8571转到结果集的上一行。 8572 8573**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8574 8575**返回值:** 8576 8577| 类型 | 说明 | 8578| ------- | --------------------------------------------- | 8579| boolean | 如果成功移动结果集,则为true;否则返回false。 | 8580 8581**错误码:** 8582 8583以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8584 8585| **错误码ID** | **错误信息** | 8586|-----------| ------------------------------------------------------------ | 8587| 14800000 | Inner error. | 8588| 14800011 | Failed to open the database because it is corrupted. | 8589| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8590| 14800014 | The RdbStore or ResultSet is already closed. | 8591| 14800019 | The SQL must be a query statement. | 8592| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8593| 14800022 | SQLite: Callback routine requested an abort. | 8594| 14800023 | SQLite: Access permission denied. | 8595| 14800024 | SQLite: The database file is locked. | 8596| 14800025 | SQLite: A table in the database is locked. | 8597| 14800026 | SQLite: The database is out of memory. | 8598| 14800027 | SQLite: Attempt to write a readonly database. | 8599| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8600| 14800029 | SQLite: The database is full. | 8601| 14800030 | SQLite: Unable to open the database file. | 8602| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8603| 14800032 | SQLite: Abort due to constraint violation. | 8604| 14800033 | SQLite: Data type mismatch. | 8605| 14800034 | SQLite: Library used incorrectly. | 8606 8607**示例:** 8608 8609```ts 8610if (resultSet != undefined) { 8611 (resultSet as relationalStore.ResultSet).goToPreviousRow(); 8612} 8613``` 8614 8615### getValue<sup>12+</sup> 8616 8617getValue(columnIndex: number): ValueType 8618 8619获取当前行中指定列的值,如果值类型是ValueType中指定的任意类型,返回指定类型的值,否则返回14800000。 8620 8621**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8622 8623**参数:** 8624 8625| 参数名 | 类型 | 必填 | 说明 | 8626| ----------- | ------ | ---- | ----------------------- | 8627| columnIndex | number | 是 | 指定的列索引,从0开始。 | 8628 8629**返回值:** 8630 8631| 类型 | 说明 | 8632| ---------- | -------------------------------- | 8633| [ValueType](#valuetype) | 表示允许的数据字段类型。 | 8634 8635**错误码:** 8636 8637以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8638 8639| **错误码ID** | **错误信息** | 8640|-----------|---------| 8641| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8642| 14800000 | Inner error. | 8643| 14800011 | Failed to open the database because it is corrupted. | 8644| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8645| 14800013 | Resultset is empty or column index is out of bounds. | 8646| 14800014 | The RdbStore or ResultSet is already closed. | 8647| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8648| 14800022 | SQLite: Callback routine requested an abort. | 8649| 14800023 | SQLite: Access permission denied. | 8650| 14800024 | SQLite: The database file is locked. | 8651| 14800025 | SQLite: A table in the database is locked. | 8652| 14800026 | SQLite: The database is out of memory. | 8653| 14800027 | SQLite: Attempt to write a readonly database. | 8654| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8655| 14800029 | SQLite: The database is full. | 8656| 14800030 | SQLite: Unable to open the database file. | 8657| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8658| 14800032 | SQLite: Abort due to constraint violation. | 8659| 14800033 | SQLite: Data type mismatch. | 8660| 14800034 | SQLite: Library used incorrectly. | 8661 8662**示例:** 8663 8664```ts 8665if (resultSet != undefined) { 8666 const codes = (resultSet as relationalStore.ResultSet).getValue((resultSet as relationalStore.ResultSet).getColumnIndex("BIGINT_COLUMN")); 8667} 8668``` 8669 8670### getBlob 8671 8672getBlob(columnIndex: number): Uint8Array 8673 8674 8675以字节数组的形式获取当前行中指定列的值,如果当前列的数据类型为INTEGER、DOUBLE、TEXT、BLOB类型,会转成字节数组类型返回指定值,如果该列内容为空时,会返回空字节数组,其他类型则返回14800000。 8676 8677**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8678 8679**参数:** 8680 8681| 参数名 | 类型 | 必填 | 说明 | 8682| ----------- | ------ | ---- | ----------------------- | 8683| columnIndex | number | 是 | 指定的列索引,从0开始。 | 8684 8685**返回值:** 8686 8687| 类型 | 说明 | 8688| ---------- | -------------------------------- | 8689| Uint8Array | 以字节数组的形式返回指定列的值。 | 8690 8691**错误码:** 8692 8693以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8694 8695| **错误码ID** | **错误信息** | 8696|-----------| ------------------------------------------------------------ | 8697| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8698| 14800000 | Inner error. | 8699| 14800011 | Failed to open the database because it is corrupted. | 8700| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8701| 14800013 | Resultset is empty or column index is out of bounds. | 8702| 14800014 | The RdbStore or ResultSet is already closed. | 8703| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8704| 14800022 | SQLite: Callback routine requested an abort. | 8705| 14800023 | SQLite: Access permission denied. | 8706| 14800024 | SQLite: The database file is locked. | 8707| 14800025 | SQLite: A table in the database is locked. | 8708| 14800026 | SQLite: The database is out of memory. | 8709| 14800027 | SQLite: Attempt to write a readonly database. | 8710| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8711| 14800029 | SQLite: The database is full. | 8712| 14800030 | SQLite: Unable to open the database file. | 8713| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8714| 14800032 | SQLite: Abort due to constraint violation. | 8715| 14800033 | SQLite: Data type mismatch. | 8716| 14800034 | SQLite: Library used incorrectly. | 8717 8718**示例:** 8719 8720```ts 8721if (resultSet != undefined) { 8722 const codes = (resultSet as relationalStore.ResultSet).getBlob((resultSet as relationalStore.ResultSet).getColumnIndex("CODES")); 8723} 8724``` 8725 8726### getString 8727 8728getString(columnIndex: number): string 8729 8730以字符串形式获取当前行中指定列的值,如果当前列中的值为INTEGER、DOUBLE、TEXT、BLOB类型,会以字符串形式返回指定值,如果是当前列中的值为INTEGER,并且为空,则会返回空字符串"",其他类型则返回14800000。如果当前列中的值为DOUBLE类型,可能存在精度的丢失,建议使用[getDouble](#getdouble)接口获取。 8731 8732**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8733 8734**参数:** 8735 8736| 参数名 | 类型 | 必填 | 说明 | 8737| ----------- | ------ | ---- | ----------------------- | 8738| columnIndex | number | 是 | 指定的列索引,从0开始。 | 8739 8740**返回值:** 8741 8742| 类型 | 说明 | 8743| ------ | ---------------------------- | 8744| string | 以字符串形式返回指定列的值。 | 8745 8746**错误码:** 8747 8748以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8749 8750| **错误码ID** | **错误信息** | 8751|-----------| ------------------------------------------------------------ | 8752| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8753| 14800000 | Inner error. | 8754| 14800011 | Failed to open the database because it is corrupted. | 8755| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8756| 14800013 | Resultset is empty or column index is out of bounds. | 8757| 14800014 | The RdbStore or ResultSet is already closed. | 8758| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8759| 14800022 | SQLite: Callback routine requested an abort. | 8760| 14800023 | SQLite: Access permission denied. | 8761| 14800024 | SQLite: The database file is locked. | 8762| 14800025 | SQLite: A table in the database is locked. | 8763| 14800026 | SQLite: The database is out of memory. | 8764| 14800027 | SQLite: Attempt to write a readonly database. | 8765| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8766| 14800029 | SQLite: The database is full. | 8767| 14800030 | SQLite: Unable to open the database file. | 8768| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8769| 14800032 | SQLite: Abort due to constraint violation. | 8770| 14800033 | SQLite: Data type mismatch. | 8771| 14800034 | SQLite: Library used incorrectly. | 8772 8773**示例:** 8774 8775```ts 8776if (resultSet != undefined) { 8777 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 8778} 8779``` 8780 8781### getLong 8782 8783getLong(columnIndex: number): number 8784 8785以Long形式获取当前行中指定列的值,如果当前列的数据类型为INTEGER、DOUBLE、TEXT、BLOB类型,会转成Long类型返回指定值,如果该列内容为空时,会返回0,其他类型则返回14800000。 8786 8787**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8788 8789**参数:** 8790 8791| 参数名 | 类型 | 必填 | 说明 | 8792| ----------- | ------ | ---- | ----------------------- | 8793| columnIndex | number | 是 | 指定的列索引,从0开始。 | 8794 8795**返回值:** 8796 8797| 类型 | 说明 | 8798| ------ | ------------------------------------------------------------ | 8799| number | 以Long形式返回指定列的值。<br>该接口支持的数据范围是:Number.MIN_SAFE_INTEGER ~ Number.MAX_SAFE_INTEGER,若超出该范围,建议使用[getDouble](#getdouble)。 | 8800 8801**错误码:** 8802 8803以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8804 8805| **错误码ID** | **错误信息** | 8806|-----------| ------------------------------------------------------------ | 8807| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8808| 14800000 | Inner error. | 8809| 14800011 | Failed to open the database because it is corrupted. | 8810| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8811| 14800013 | Resultset is empty or column index is out of bounds. | 8812| 14800014 | The RdbStore or ResultSet is already closed. | 8813| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8814| 14800022 | SQLite: Callback routine requested an abort. | 8815| 14800023 | SQLite: Access permission denied. | 8816| 14800024 | SQLite: The database file is locked. | 8817| 14800025 | SQLite: A table in the database is locked. | 8818| 14800026 | SQLite: The database is out of memory. | 8819| 14800027 | SQLite: Attempt to write a readonly database. | 8820| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8821| 14800029 | SQLite: The database is full. | 8822| 14800030 | SQLite: Unable to open the database file. | 8823| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8824| 14800032 | SQLite: Abort due to constraint violation. | 8825| 14800033 | SQLite: Data type mismatch. | 8826| 14800034 | SQLite: Library used incorrectly. | 8827 8828**示例:** 8829 8830```ts 8831if (resultSet != undefined) { 8832 const age = (resultSet as relationalStore.ResultSet).getLong((resultSet as relationalStore.ResultSet).getColumnIndex("AGE")); 8833} 8834``` 8835 8836### getDouble 8837 8838getDouble(columnIndex: number): number 8839 8840以double形式获取当前行中指定列的值,如果当前列的数据类型为INTEGER、DOUBLE、TEXT、BLOB类型,会转成double类型返回指定值,如果该列内容为空时,会返回0.0,其他类型则返回14800000。 8841 8842**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8843 8844**参数:** 8845 8846| 参数名 | 类型 | 必填 | 说明 | 8847| ----------- | ------ | ---- | ----------------------- | 8848| columnIndex | number | 是 | 指定的列索引,从0开始。 | 8849 8850**返回值:** 8851 8852| 类型 | 说明 | 8853| ------ | ---------------------------- | 8854| number | 以double形式返回指定列的值。 | 8855 8856**错误码:** 8857 8858以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8859 8860| **错误码ID** | **错误信息** | 8861|-----------| ------------------------------------------------------------ | 8862| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8863| 14800000 | Inner error. | 8864| 14800011 | Failed to open the database because it is corrupted. | 8865| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8866| 14800013 | Resultset is empty or column index is out of bounds. | 8867| 14800014 | The RdbStore or ResultSet is already closed. | 8868| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8869| 14800022 | SQLite: Callback routine requested an abort. | 8870| 14800023 | SQLite: Access permission denied. | 8871| 14800024 | SQLite: The database file is locked. | 8872| 14800025 | SQLite: A table in the database is locked. | 8873| 14800026 | SQLite: The database is out of memory. | 8874| 14800027 | SQLite: Attempt to write a readonly database. | 8875| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8876| 14800029 | SQLite: The database is full. | 8877| 14800030 | SQLite: Unable to open the database file. | 8878| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8879| 14800032 | SQLite: Abort due to constraint violation. | 8880| 14800033 | SQLite: Data type mismatch. | 8881| 14800034 | SQLite: Library used incorrectly. | 8882 8883**示例:** 8884 8885```ts 8886if (resultSet != undefined) { 8887 const salary = (resultSet as relationalStore.ResultSet).getDouble((resultSet as relationalStore.ResultSet).getColumnIndex("SALARY")); 8888} 8889``` 8890 8891### getAsset<sup>10+</sup> 8892 8893getAsset(columnIndex: number): Asset 8894 8895以[Asset](#asset10)形式获取当前行中指定列的值,如果当前列的数据类型为Asset类型,会以Asset类型返回指定值,如果当前列中的值为null时,会返回null,其他类型则返回14800000。 8896 8897**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8898 8899**参数:** 8900 8901| 参数名 | 类型 | 必填 | 说明 | 8902| ----------- | ------ | --- | ------------ | 8903| columnIndex | number | 是 | 指定的列索引,从0开始。 | 8904 8905**返回值:** 8906 8907| 类型 | 说明 | 8908| --------------- | -------------------------- | 8909| [Asset](#asset10) | 以Asset形式返回指定列的值。 | 8910 8911**错误码:** 8912 8913以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8914 8915| **错误码ID** | **错误信息** | 8916|-----------| ------------------------------------------------------------ | 8917| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8918| 14800000 | Inner error. | 8919| 14800011 | Failed to open the database because it is corrupted. | 8920| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8921| 14800013 | Resultset is empty or column index is out of bounds. | 8922| 14800014 | The RdbStore or ResultSet is already closed. | 8923| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8924| 14800022 | SQLite: Callback routine requested an abort. | 8925| 14800023 | SQLite: Access permission denied. | 8926| 14800024 | SQLite: The database file is locked. | 8927| 14800025 | SQLite: A table in the database is locked. | 8928| 14800026 | SQLite: The database is out of memory. | 8929| 14800027 | SQLite: Attempt to write a readonly database. | 8930| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8931| 14800029 | SQLite: The database is full. | 8932| 14800030 | SQLite: Unable to open the database file. | 8933| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8934| 14800032 | SQLite: Abort due to constraint violation. | 8935| 14800033 | SQLite: Data type mismatch. | 8936| 14800034 | SQLite: Library used incorrectly. | 8937 8938**示例:** 8939 8940```ts 8941if (resultSet != undefined) { 8942 const doc = (resultSet as relationalStore.ResultSet).getAsset((resultSet as relationalStore.ResultSet).getColumnIndex("DOC")); 8943} 8944``` 8945 8946### getAssets<sup>10+</sup> 8947 8948getAssets(columnIndex: number): Assets 8949 8950以[Assets](#assets10)形式获取当前行中指定列的值,如果当前列的数据类型为Assets类型,会以Assets类型返回指定值,如果当前列中的值为null时,会返回null,其他类型则返回14800000。 8951 8952**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 8953 8954**参数:** 8955 8956| 参数名 | 类型 | 必填 | 说明 | 8957| ----------- | ------ | --- | ------------ | 8958| columnIndex | number | 是 | 指定的列索引,从0开始。 | 8959 8960**返回值:** 8961 8962| 类型 | 说明 | 8963| ---------------- | ---------------------------- | 8964| [Assets](#assets10)| 以Assets形式返回指定列的值。 | 8965 8966**错误码:** 8967 8968以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 8969 8970| **错误码ID** | **错误信息** | 8971|-----------| ------------------------------------------------------------ | 8972| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 8973| 14800000 | Inner error. | 8974| 14800011 | Failed to open the database because it is corrupted. | 8975| 14800012 | ResultSet is empty or pointer index is out of bounds. | 8976| 14800013 | Resultset is empty or column index is out of bounds. | 8977| 14800014 | The RdbStore or ResultSet is already closed. | 8978| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 8979| 14800022 | SQLite: Callback routine requested an abort. | 8980| 14800023 | SQLite: Access permission denied. | 8981| 14800024 | SQLite: The database file is locked. | 8982| 14800025 | SQLite: A table in the database is locked. | 8983| 14800026 | SQLite: The database is out of memory. | 8984| 14800027 | SQLite: Attempt to write a readonly database. | 8985| 14800028 | SQLite: Some kind of disk I/O error occurred. | 8986| 14800029 | SQLite: The database is full. | 8987| 14800030 | SQLite: Unable to open the database file. | 8988| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 8989| 14800032 | SQLite: Abort due to constraint violation. | 8990| 14800033 | SQLite: Data type mismatch. | 8991| 14800034 | SQLite: Library used incorrectly. | 8992 8993**示例:** 8994 8995```ts 8996if (resultSet != undefined) { 8997 const docs = (resultSet as relationalStore.ResultSet).getAssets((resultSet as relationalStore.ResultSet).getColumnIndex("DOCS")); 8998} 8999``` 9000 9001### getRow<sup>11+</sup> 9002 9003getRow(): ValuesBucket 9004 9005获取当前行。 9006 9007**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9008 9009**返回值:** 9010 9011| 类型 | 说明 | 9012| ---------------- | ---------------------------- | 9013| [ValuesBucket](#valuesbucket) | 返回指定行的值。 | 9014 9015**错误码:** 9016 9017以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9018 9019| **错误码ID** | **错误信息** | 9020|-----------| ------------------------------------------------------------ | 9021| 14800000 | Inner error. | 9022| 14800011 | Failed to open the database because it is corrupted. | 9023| 14800012 | ResultSet is empty or pointer index is out of bounds. | 9024| 14800013 | Resultset is empty or column index is out of bounds. | 9025| 14800014 | The RdbStore or ResultSet is already closed. | 9026| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 9027| 14800022 | SQLite: Callback routine requested an abort. | 9028| 14800023 | SQLite: Access permission denied. | 9029| 14800024 | SQLite: The database file is locked. | 9030| 14800025 | SQLite: A table in the database is locked. | 9031| 14800026 | SQLite: The database is out of memory. | 9032| 14800027 | SQLite: Attempt to write a readonly database. | 9033| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9034| 14800029 | SQLite: The database is full. | 9035| 14800030 | SQLite: Unable to open the database file. | 9036| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 9037| 14800032 | SQLite: Abort due to constraint violation. | 9038| 14800033 | SQLite: Data type mismatch. | 9039| 14800034 | SQLite: Library used incorrectly. | 9040 9041**示例:** 9042 9043```ts 9044if (resultSet != undefined) { 9045 const row = (resultSet as relationalStore.ResultSet).getRow(); 9046} 9047``` 9048 9049### getRows<sup>18+</sup> 9050 9051getRows(maxCount: number, position?: number): Promise<Array\<ValuesBucket>> 9052 9053从结果集中获取指定数量的数据,使用Promise异步回调。禁止与[ResultSet](#resultset)的其他接口并发调用,否则获取的数据可能非预期。 9054 9055**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9056 9057**参数:** 9058 9059| 参数名 | 类型 | 必填 | 说明 | 9060| ----------- | ------ | ---- | ----------------------- | 9061| maxCount | number | 是 | 正整数,指定要从结果集中获取数据的条数。不为正整数则参数非法,抛出错误码401。 | 9062| position | number | 否 | 非负整数,指定从结果集中获取数据的起始位置,不填则从结果集的当前行(默认首次获取数据时为当前结果集的第一行)开始获取数据。不为非负整数则参数非法,抛出错误码401。 | 9063 9064 9065**返回值:** 9066 9067| 类型 | 说明 | 9068| ---------------- | ---------------------------- | 9069| Promise<Array<[ValuesBucket](#valuesbucket)>> | 返回maxCount条数据,剩余数据不足maxCount条则返回剩余数据,返回空数组时代表已经遍历到结果集的末尾。 | 9070 9071**错误码:** 9072 9073以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9074 9075| **错误码ID** | **错误信息** | 9076|-----------| ------------------------------------------------------------ | 9077| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 9078| 14800000 | Inner error. | 9079| 14800011 | Failed to open the database because it is corrupted. | 9080| 14800012 | ResultSet is empty or pointer index is out of bounds. | 9081| 14800013 | Resultset is empty or column index is out of bounds. | 9082| 14800014 | The RdbStore or ResultSet is already closed. | 9083| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 9084| 14800022 | SQLite: Callback routine requested an abort. | 9085| 14800023 | SQLite: Access permission denied. | 9086| 14800024 | SQLite: The database file is locked. | 9087| 14800025 | SQLite: A table in the database is locked. | 9088| 14800026 | SQLite: The database is out of memory. | 9089| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9090| 14800029 | SQLite: The database is full. | 9091| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 9092| 14800032 | SQLite: Abort due to constraint violation. | 9093| 14800033 | SQLite: Data type mismatch. | 9094 9095**示例:** 9096 9097```ts 9098// 以查到100条数据为例 9099async function proccessRows(resultSet: relationalStore.ResultSet) { 9100 // 示例1:仅指定maxCount 9101 if (resultSet != undefined) { 9102 let rows: Array<relationalStore.ValuesBucket>; 9103 let maxCount: number = 50; 9104 // 从结果集的当前行(默认首次获取数据时为当前结果集的第一行,后续为上次获取数据结束位置的下一行)开始获取数据 9105 // getRows会自动移动结果集当前行到上次getRows获取结束位置的下一行,无需使用goToFirstRow、goToNextRow等接口移动 9106 while ((rows = await (resultSet as relationalStore.ResultSet).getRows(maxCount)).length != 0) { 9107 console.info(JSON.stringify(rows[0])); 9108 } 9109 } 9110 9111 // 示例2:指定maxCount和起始的position 9112 if (resultSet != undefined) { 9113 let rows: Array<relationalStore.ValuesBucket>; 9114 let maxCount: number = 50; 9115 let position: number = 50; 9116 while ((rows = await (resultSet as relationalStore.ResultSet).getRows(maxCount, position)).length != 0) { 9117 console.info(JSON.stringify(rows[0])); 9118 position += rows.length; 9119 } 9120 } 9121} 9122``` 9123 9124### getSendableRow<sup>12+</sup> 9125 9126getSendableRow(): sendableRelationalStore.ValuesBucket 9127 9128获取当前行数据的sendable形式,用于跨线程传递。 9129 9130**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9131 9132**返回值:** 9133 9134| 类型 | 说明 | 9135| ---------------------------------------------------------------------------------------------- | -------------------------------------------- | 9136| [sendableRelationalStore.ValuesBucket](./js-apis-data-sendableRelationalStore.md#valuesbucket) | 当前行数据的sendable形式,用于跨线程传递。 | 9137 9138**错误码:** 9139 9140以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9141 9142| **错误码ID** | **错误信息** | 9143| ------------ | --------------------------------------------- | 9144| 14800000 | Inner error. | 9145| 14800011 | Failed to open the database because it is corrupted. | 9146| 14800012 | ResultSet is empty or pointer index is out of bounds. | 9147| 14800013 | Resultset is empty or column index is out of bounds. | 9148| 14800014 | The RdbStore or ResultSet is already closed. | 9149| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 9150| 14800022 | SQLite: Callback routine requested an abort. | 9151| 14800023 | SQLite: Access permission denied. | 9152| 14800024 | SQLite: The database file is locked. | 9153| 14800025 | SQLite: A table in the database is locked. | 9154| 14800026 | SQLite: The database is out of memory. | 9155| 14800027 | SQLite: Attempt to write a readonly database. | 9156| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9157| 14800029 | SQLite: The database is full. | 9158| 14800030 | SQLite: Unable to open the database file. | 9159| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 9160| 14800032 | SQLite: Abort due to constraint violation. | 9161| 14800033 | SQLite: Data type mismatch. | 9162| 14800034 | SQLite: Library used incorrectly. | 9163 9164**示例:** 9165 9166<!--code_no_check--> 9167```ts 9168import { window } from '@kit.ArkUI'; 9169import { UIAbility } from '@kit.AbilityKit'; 9170import { relationalStore } from '@kit.ArkData'; 9171import { taskpool } from '@kit.ArkTS'; 9172import type ctx from '@ohos.app.ability.common'; 9173import { sendableRelationalStore } from '@kit.ArkData'; 9174 9175@Concurrent 9176async function getDataByName(name: string, context: ctx.UIAbilityContext) { 9177 const STORE_CONFIG: relationalStore.StoreConfig = { 9178 name: "RdbTest.db", 9179 securityLevel: relationalStore.SecurityLevel.S3 9180 }; 9181 const store = await relationalStore.getRdbStore(context, STORE_CONFIG); 9182 const predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 9183 predicates.equalTo("NAME", name); 9184 const resultSet = store.querySync(predicates); 9185 9186 if (resultSet.rowCount > 0) { 9187 resultSet.goToFirstRow(); 9188 const sendableValuesBucket = resultSet.getSendableRow(); 9189 return sendableValuesBucket; 9190 } else { 9191 return null; 9192 } 9193} 9194 9195export default class EntryAbility extends UIAbility { 9196 async onWindowStageCreate(windowStage: window.WindowStage) { 9197 const task = new taskpool.Task(getDataByName, 'Lisa', this.context); 9198 const sendableValuesBucket = await taskpool.execute(task) as sendableRelationalStore.ValuesBucket; 9199 9200 if (sendableValuesBucket) { 9201 const columnCount = sendableValuesBucket.size; 9202 const age = sendableValuesBucket.get('age'); 9203 const name = sendableValuesBucket.get('name'); 9204 console.info(`Query data in taskpool succeeded, name is "${name}", age is "${age}"`); 9205 } 9206 } 9207} 9208``` 9209 9210### isColumnNull 9211 9212isColumnNull(columnIndex: number): boolean 9213 9214检查当前行中指定列的值是否为null。 9215 9216**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9217 9218**参数:** 9219 9220| 参数名 | 类型 | 必填 | 说明 | 9221| ----------- | ------ | ---- | ----------------------- | 9222| columnIndex | number | 是 | 指定的列索引,从0开始。 | 9223 9224**返回值:** 9225 9226| 类型 | 说明 | 9227| ------- | --------------------------------------------------------- | 9228| boolean | 如果当前行中指定列的值为null,则返回true,否则返回false。 | 9229 9230**错误码:** 9231 9232以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9233 9234| **错误码ID** | **错误信息** | 9235|-----------| ------------------------------------------------------- | 9236| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 9237| 14800000 | Inner error. | 9238| 14800011 | Failed to open the database because it is corrupted. | 9239| 14800012 | ResultSet is empty or pointer index is out of bounds. | 9240| 14800013 | Resultset is empty or column index is out of bounds. | 9241| 14800014 | The RdbStore or ResultSet is already closed. | 9242| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 9243| 14800022 | SQLite: Callback routine requested an abort. | 9244| 14800023 | SQLite: Access permission denied. | 9245| 14800024 | SQLite: The database file is locked. | 9246| 14800025 | SQLite: A table in the database is locked. | 9247| 14800026 | SQLite: The database is out of memory. | 9248| 14800027 | SQLite: Attempt to write a readonly database. | 9249| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9250| 14800029 | SQLite: The database is full. | 9251| 14800030 | SQLite: Unable to open the database file. | 9252| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 9253| 14800032 | SQLite: Abort due to constraint violation. | 9254| 14800033 | SQLite: Data type mismatch. | 9255| 14800034 | SQLite: Library used incorrectly. | 9256 9257**示例:** 9258 9259```ts 9260if (resultSet != undefined) { 9261 const isColumnNull = (resultSet as relationalStore.ResultSet).isColumnNull((resultSet as relationalStore.ResultSet).getColumnIndex("CODES")); 9262} 9263``` 9264 9265### close 9266 9267close(): void 9268 9269关闭结果集,若不关闭可能会引起fd泄露和内存泄露。 9270 9271**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9272 9273**示例:** 9274 9275```ts 9276if (resultSet != undefined) { 9277 (resultSet as relationalStore.ResultSet).close(); 9278} 9279``` 9280 9281**错误码:** 9282 9283以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 9284 9285| **错误码ID** | **错误信息** | 9286|-----------| ------------------------------------------------------------ | 9287| 14800000 | Inner error. | 9288| 14800012 | ResultSet is empty or pointer index is out of bounds. | 9289 9290## Transaction<sup>14+</sup> 9291 9292提供以事务方式管理数据库的方法。事务对象是通过[createTransaction](#createtransaction14)接口创建的,不同事务对象之间的操作是隔离的,不同类型事务的区别见[TransactionType](#transactiontype14) 。 9293 9294当前关系型数据库同一时刻仅支持一个写事务,所以如果当前[RdbStore](#rdbstore)存在写事务未释放,创建IMMEDIATE或EXCLUSIVE事务会返回14800024错误码。如果是创建的DEFERRED事务,则可能在首次使用DEFERRED事务调用写操作时返回14800024错误码。通过IMMEDIATE或EXCLUSIVE创建写事务或者DEFERRED事务升级到写事务之后,[RdbStore](#rdbstore)的写操作也会返回14800024错误码。 9295 9296当事务并发量较高且写事务持续时间较长时,返回14800024错误码的次数可能会变多,开发者可以通过减少事务占用时长减少14800024出现的次数,也可以通过重试的方式处理14800024错误码。 9297 9298**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9299 9300**示例:** 9301 9302```ts 9303import { UIAbility } from '@kit.AbilityKit'; 9304import { BusinessError } from '@kit.BasicServicesKit'; 9305import { window } from '@kit.ArkUI'; 9306 9307let store: relationalStore.RdbStore | undefined = undefined; 9308 9309class EntryAbility extends UIAbility { 9310 async onWindowStageCreate(windowStage: window.WindowStage) { 9311 const STORE_CONFIG: relationalStore.StoreConfig = { 9312 name: "RdbTest.db", 9313 securityLevel: relationalStore.SecurityLevel.S3 9314 }; 9315 9316 await relationalStore.getRdbStore(this.context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => { 9317 store = rdbStore; 9318 console.info('Get RdbStore successfully.'); 9319 }).catch((err: BusinessError) => { 9320 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 9321 }); 9322 9323 if (store != undefined) { 9324 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 9325 console.info(`createTransaction success`); 9326 // 成功获取到事务对象后执行后续操作 9327 }).catch((err: BusinessError) => { 9328 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 9329 }); 9330 } 9331 } 9332} 9333``` 9334 9335### commit<sup>14+</sup> 9336 9337commit(): Promise<void> 9338 9339提交已执行的SQL语句。如果是使用异步接口执行sql语句,请确保异步接口执行完成之后再调用commit接口,否则可能会丢失SQL操作。调用commit接口之后,该Transaction对象及创建的ResultSet对象都将被关闭。 9340 9341**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9342 9343**返回值**: 9344 9345| 类型 | 说明 | 9346| ------------------- | ------------------------- | 9347| Promise<void> | 无返回结果的Promise对象。 | 9348 9349**错误码:** 9350 9351以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9352 9353| **错误码ID** | **错误信息** | 9354|-----------| ------------------------------------------------------------ | 9355| 14800000 | Inner error. | 9356| 14800011 | Failed to open the database because it is corrupted. | 9357| 14800014 | The RdbStore or ResultSet is already closed. | 9358| 14800023 | SQLite: Access permission denied. | 9359| 14800024 | SQLite: The database file is locked. | 9360| 14800026 | SQLite: The database is out of memory. | 9361| 14800027 | SQLite: Attempt to write a readonly database. | 9362| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9363| 14800029 | SQLite: The database is full. | 9364 9365**示例:** 9366 9367```ts 9368let value1 = "Lisa"; 9369let value2 = 18; 9370let value3 = 100.5; 9371let value4 = new Uint8Array([1, 2, 3]); 9372 9373if (store != undefined) { 9374 const valueBucket: relationalStore.ValuesBucket = { 9375 'NAME': value1, 9376 'AGE': value2, 9377 'SALARY': value3, 9378 'CODES': value4 9379 }; 9380 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 9381 transaction.execute("DELETE FROM TEST WHERE age = ? OR age = ?", ["18", "20"]).then(() => { 9382 transaction.commit(); 9383 }).catch((e: BusinessError) => { 9384 transaction.rollback(); 9385 console.error(`execute sql failed, code is ${e.code},message is ${e.message}`); 9386 }); 9387 }).catch((err: BusinessError) => { 9388 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 9389 }); 9390} 9391``` 9392 9393### rollback<sup>14+</sup> 9394 9395rollback(): Promise<void> 9396 9397回滚已经执行的SQL语句。调用rollback接口之后,该Transaction对象及创建的ResultSet对象都会被关闭。 9398 9399**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9400 9401**返回值**: 9402 9403| 类型 | 说明 | 9404| ------------------- | ------------------------- | 9405| Promise<void> | 无返回结果的Promise对象。 | 9406 9407**错误码:** 9408 9409以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9410 9411| **错误码ID** | **错误信息** | 9412|-----------| ------------------------------------------------------------ | 9413| 14800000 | Inner error. | 9414| 14800011 | Failed to open the database because it is corrupted. | 9415| 14800014 | The RdbStore or ResultSet is already closed. | 9416| 14800023 | SQLite: Access permission denied. | 9417| 14800024 | SQLite: The database file is locked. | 9418| 14800026 | SQLite: The database is out of memory. | 9419| 14800027 | SQLite: Attempt to write a readonly database. | 9420| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9421| 14800029 | SQLite: The database is full. | 9422 9423**示例:** 9424 9425```ts 9426if (store != undefined) { 9427 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 9428 transaction.execute("DELETE FROM TEST WHERE age = ? OR age = ?", ["18", "20"]).then(() => { 9429 transaction.commit(); 9430 }).catch((e: BusinessError) => { 9431 transaction.rollback(); 9432 console.error(`execute sql failed, code is ${e.code},message is ${e.message}`); 9433 }); 9434 }).catch((err: BusinessError) => { 9435 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 9436 }); 9437} 9438``` 9439 9440### insert<sup>14+</sup> 9441 9442insert(table: string, values: ValuesBucket, conflict?: ConflictResolution): Promise<number> 9443 9444向目标表中插入一行数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 9445 9446**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9447 9448**参数:** 9449 9450| 参数名 | 类型 | 必填 | 说明 | 9451| -------- | ------------------------------------------- | ---- | -------------------------- | 9452| table | string | 是 | 指定的目标表名。 | 9453| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 9454| conflict | [ConflictResolution](#conflictresolution10) | 否 | 指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 9455 9456**返回值**: 9457 9458| 类型 | 说明 | 9459| --------------------- | ------------------------------------------------- | 9460| Promise<number> | Promise对象。如果操作成功,返回行ID;否则返回-1。 | 9461 9462**错误码:** 9463 9464以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9465 9466| **错误码ID** | **错误信息** | 9467|-----------| ------------------------------------------------------------ | 9468| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 9469| 14800000 | Inner error. | 9470| 14800011 | Failed to open the database because it is corrupted. | 9471| 14800014 | The RdbStore or ResultSet is already closed. | 9472| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 9473| 14800023 | SQLite: Access permission denied. | 9474| 14800024 | SQLite: The database file is locked. | 9475| 14800025 | SQLite: A table in the database is locked. | 9476| 14800026 | SQLite: The database is out of memory. | 9477| 14800027 | SQLite: Attempt to write a readonly database. | 9478| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9479| 14800029 | SQLite: The database is full. | 9480| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 9481| 14800033 | SQLite: Data type mismatch. | 9482| 14800047 | The WAL file size exceeds the default limit. | 9483 9484**示例:** 9485 9486```ts 9487let value1 = "Lisa"; 9488let value2 = 18; 9489let value3 = 100.5; 9490let value4 = new Uint8Array([1, 2, 3, 4, 5]); 9491 9492// 以下三种方式可用 9493const valueBucket1: relationalStore.ValuesBucket = { 9494 'NAME': value1, 9495 'AGE': value2, 9496 'SALARY': value3, 9497 'CODES': value4 9498}; 9499const valueBucket2: relationalStore.ValuesBucket = { 9500 NAME: value1, 9501 AGE: value2, 9502 SALARY: value3, 9503 CODES: value4 9504}; 9505const valueBucket3: relationalStore.ValuesBucket = { 9506 "NAME": value1, 9507 "AGE": value2, 9508 "SALARY": value3, 9509 "CODES": value4 9510}; 9511 9512if (store != undefined) { 9513 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 9514 transaction.insert("EMPLOYEE", valueBucket1, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE).then((rowId: number) => { 9515 transaction.commit(); 9516 console.info(`Insert is successful, rowId = ${rowId}`); 9517 }).catch((e: BusinessError) => { 9518 transaction.rollback(); 9519 console.error(`Insert is failed, code is ${e.code},message is ${e.message}`); 9520 }); 9521 }).catch((err: BusinessError) => { 9522 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 9523 }); 9524} 9525``` 9526 9527### insertSync<sup>14+</sup> 9528 9529insertSync(table: string, values: ValuesBucket | sendableRelationalStore.ValuesBucket, conflict?: ConflictResolution): number 9530 9531向目标表中插入一行数据。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 9532 9533**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9534 9535**参数:** 9536 9537| 参数名 | 类型 | 必填 | 说明 | 9538| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 9539| table | string | 是 | 指定的目标表名。 | 9540| values | [ValuesBucket](#valuesbucket) \| [sendableRelationalStore.ValuesBucket](./js-apis-data-sendableRelationalStore.md#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 9541| conflict | [ConflictResolution](#conflictresolution10) | 否 | 指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 9542 9543**返回值**: 9544 9545| 类型 | 说明 | 9546| ------ | ------------------------------------ | 9547| number | 如果操作成功,返回行ID;否则返回-1。 | 9548 9549**错误码:** 9550 9551以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9552 9553| **错误码ID** | **错误信息** | 9554| ------------ | ------------------------------------------------------------ | 9555| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 9556| 14800000 | Inner error. | 9557| 14800011 | Failed to open the database because it is corrupted. | 9558| 14800014 | The RdbStore or ResultSet is already closed. | 9559| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 9560| 14800023 | SQLite: Access permission denied. | 9561| 14800024 | SQLite: The database file is locked. | 9562| 14800025 | SQLite: A table in the database is locked. | 9563| 14800026 | SQLite: The database is out of memory. | 9564| 14800027 | SQLite: Attempt to write a readonly database. | 9565| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9566| 14800029 | SQLite: The database is full. | 9567| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 9568| 14800033 | SQLite: Data type mismatch. | 9569| 14800047 | The WAL file size exceeds the default limit. | 9570 9571**示例:** 9572 9573```ts 9574let value1 = "Lisa"; 9575let value2 = 18; 9576let value3 = 100.5; 9577let value4 = new Uint8Array([1, 2, 3, 4, 5]); 9578 9579// 以下三种方式可用 9580const valueBucket1: relationalStore.ValuesBucket = { 9581 'NAME': value1, 9582 'AGE': value2, 9583 'SALARY': value3, 9584 'CODES': value4 9585}; 9586const valueBucket2: relationalStore.ValuesBucket = { 9587 NAME: value1, 9588 AGE: value2, 9589 SALARY: value3, 9590 CODES: value4 9591}; 9592const valueBucket3: relationalStore.ValuesBucket = { 9593 "NAME": value1, 9594 "AGE": value2, 9595 "SALARY": value3, 9596 "CODES": value4 9597}; 9598 9599if (store != undefined) { 9600 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 9601 try { 9602 let rowId: number = (transaction as relationalStore.Transaction).insertSync("EMPLOYEE", valueBucket1, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE); 9603 transaction.commit(); 9604 console.info(`Insert is successful, rowId = ${rowId}`); 9605 } catch (e) { 9606 transaction.rollback(); 9607 console.error(`Insert is failed, code is ${e.code},message is ${e.message}`); 9608 }; 9609 }).catch((err: BusinessError) => { 9610 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 9611 }); 9612} 9613``` 9614 9615### batchInsert<sup>14+</sup> 9616 9617batchInsert(table: string, values: Array<ValuesBucket>): Promise<number> 9618 9619向目标表中插入一组数据,使用Promise异步回调。 9620 9621**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9622 9623**参数:** 9624 9625| 参数名 | 类型 | 必填 | 说明 | 9626| ------ | ------------------------------------------ | ---- | ---------------------------- | 9627| table | string | 是 | 指定的目标表名。 | 9628| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。| 9629 9630**返回值**: 9631 9632| 类型 | 说明 | 9633| --------------------- | ----------------------------------------------------------- | 9634| Promise<number> | Promise对象。如果操作成功,返回插入的数据个数,否则返回-1。 | 9635 9636**错误码:** 9637 9638以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9639 9640| **错误码ID** | **错误信息** | 9641|-----------| ------------------------------------------------------------ | 9642| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 9643| 14800000 | Inner error. | 9644| 14800011 | Failed to open the database because it is corrupted. | 9645| 14800014 | The RdbStore or ResultSet is already closed. | 9646| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 9647| 14800023 | SQLite: Access permission denied. | 9648| 14800024 | SQLite: The database file is locked. | 9649| 14800025 | SQLite: A table in the database is locked. | 9650| 14800026 | SQLite: The database is out of memory. | 9651| 14800027 | SQLite: Attempt to write a readonly database. | 9652| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9653| 14800029 | SQLite: The database is full. | 9654| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 9655| 14800033 | SQLite: Data type mismatch. | 9656| 14800047 | The WAL file size exceeds the default limit. | 9657 9658**示例:** 9659 9660```ts 9661let value1 = "Lisa"; 9662let value2 = 18; 9663let value3 = 100.5; 9664let value4 = new Uint8Array([1, 2, 3, 4, 5]); 9665let value5 = "Jack"; 9666let value6 = 19; 9667let value7 = 101.5; 9668let value8 = new Uint8Array([6, 7, 8, 9, 10]); 9669let value9 = "Tom"; 9670let value10 = 20; 9671let value11 = 102.5; 9672let value12 = new Uint8Array([11, 12, 13, 14, 15]); 9673 9674const valueBucket1: relationalStore.ValuesBucket = { 9675 'NAME': value1, 9676 'AGE': value2, 9677 'SALARY': value3, 9678 'CODES': value4 9679}; 9680const valueBucket2: relationalStore.ValuesBucket = { 9681 'NAME': value5, 9682 'AGE': value6, 9683 'SALARY': value7, 9684 'CODES': value8 9685}; 9686const valueBucket3: relationalStore.ValuesBucket = { 9687 'NAME': value9, 9688 'AGE': value10, 9689 'SALARY': value11, 9690 'CODES': value12 9691}; 9692 9693let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 9694if (store != undefined) { 9695 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 9696 transaction.batchInsert("EMPLOYEE", valueBuckets).then((insertNum: number) => { 9697 transaction.commit(); 9698 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 9699 }).catch((e: BusinessError) => { 9700 transaction.rollback(); 9701 console.error(`batchInsert is failed, code is ${e.code},message is ${e.message}`); 9702 }); 9703 }).catch((err: BusinessError) => { 9704 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 9705 }); 9706} 9707``` 9708 9709### batchInsertSync<sup>14+</sup> 9710 9711batchInsertSync(table: string, values: Array<ValuesBucket>): number 9712 9713向目标表中插入一组数据。 9714 9715**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9716 9717**参数:** 9718 9719| 参数名 | 类型 | 必填 | 说明 | 9720| ------ | ------------------------------------------ | ---- | ---------------------------- | 9721| table | string | 是 | 指定的目标表名。 | 9722| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 9723 9724**返回值**: 9725 9726| 类型 | 说明 | 9727| ------ | ---------------------------------------------- | 9728| number | 如果操作成功,返回插入的数据个数,否则返回-1。 | 9729 9730**错误码:** 9731 9732以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9733 9734| **错误码ID** | **错误信息** | 9735| ------------ | ------------------------------------------------------------ | 9736| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 9737| 14800000 | Inner error. | 9738| 14800011 | Failed to open the database because it is corrupted. | 9739| 14800014 | The RdbStore or ResultSet is already closed. | 9740| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 9741| 14800023 | SQLite: Access permission denied. | 9742| 14800024 | SQLite: The database file is locked. | 9743| 14800025 | SQLite: A table in the database is locked. | 9744| 14800026 | SQLite: The database is out of memory. | 9745| 14800027 | SQLite: Attempt to write a readonly database. | 9746| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9747| 14800029 | SQLite: The database is full. | 9748| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 9749| 14800033 | SQLite: Data type mismatch. | 9750| 14800047 | The WAL file size exceeds the default limit. | 9751 9752**示例:** 9753 9754```ts 9755let value1 = "Lisa"; 9756let value2 = 18; 9757let value3 = 100.5; 9758let value4 = new Uint8Array([1, 2, 3, 4, 5]); 9759let value5 = "Jack"; 9760let value6 = 19; 9761let value7 = 101.5; 9762let value8 = new Uint8Array([6, 7, 8, 9, 10]); 9763let value9 = "Tom"; 9764let value10 = 20; 9765let value11 = 102.5; 9766let value12 = new Uint8Array([11, 12, 13, 14, 15]); 9767 9768const valueBucket1: relationalStore.ValuesBucket = { 9769 'NAME': value1, 9770 'AGE': value2, 9771 'SALARY': value3, 9772 'CODES': value4 9773}; 9774const valueBucket2: relationalStore.ValuesBucket = { 9775 'NAME': value5, 9776 'AGE': value6, 9777 'SALARY': value7, 9778 'CODES': value8 9779}; 9780const valueBucket3: relationalStore.ValuesBucket = { 9781 'NAME': value9, 9782 'AGE': value10, 9783 'SALARY': value11, 9784 'CODES': value12 9785}; 9786 9787let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 9788if (store != undefined) { 9789 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 9790 try { 9791 let insertNum: number = (transaction as relationalStore.Transaction).batchInsertSync("EMPLOYEE", valueBuckets); 9792 transaction.commit(); 9793 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 9794 } catch (e) { 9795 transaction.rollback(); 9796 console.error(`batchInsert is failed, code is ${e.code},message is ${e.message}`); 9797 }; 9798 }).catch((err: BusinessError) => { 9799 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 9800 }); 9801} 9802``` 9803 9804### batchInsertWithConflictResolution<sup>18+</sup> 9805 9806batchInsertWithConflictResolution(table: string, values: Array<ValuesBucket>, conflict: ConflictResolution): Promise<number> 9807 9808向目标表中插入一组数据,使用Promise异步回调。 9809 9810**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9811 9812**参数:** 9813 9814| 参数名 | 类型 | 必填 | 说明 | 9815| ------ | ------------------------------------------ | ---- | ---------------------------- | 9816| table | string | 是 | 指定的目标表名。 | 9817| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。| 9818| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。如果是ON_CONFLICT_ROLLBACK模式,当发生冲突时会回滚整个事务。 | 9819 9820**返回值**: 9821 9822| 类型 | 说明 | 9823| --------------------- | ----------------------------------------------------------- | 9824| Promise<number> | Promise对象。如果操作成功,返回插入的数据个数,否则返回-1。 | 9825 9826**错误码:** 9827 9828以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9829 9830| **错误码ID** | **错误信息** | 9831|-----------| ------------------------------------------------------------ | 9832| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 9833| 14800000 | Inner error. | 9834| 14800011 | Failed to open the database because it is corrupted. | 9835| 14800014 | The RdbStore or ResultSet is already closed. | 9836| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 9837| 14800022 | SQLite: Callback routine requested an abort. | 9838| 14800023 | SQLite: Access permission denied. | 9839| 14800024 | SQLite: The database file is locked. | 9840| 14800025 | SQLite: A table in the database is locked. | 9841| 14800026 | SQLite: The database is out of memory. | 9842| 14800027 | SQLite: Attempt to write a readonly database. | 9843| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9844| 14800029 | SQLite: The database is full. | 9845| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 9846| 14800032 | SQLite: Abort due to constraint violation. | 9847| 14800033 | SQLite: Data type mismatch. | 9848| 14800034 | SQLite: Library used incorrectly. | 9849| 14800047 | The WAL file size exceeds the default limit. | 9850 9851**示例:** 9852 9853```ts 9854let value1 = "Lisa"; 9855let value2 = 18; 9856let value3 = 100.5; 9857let value4 = new Uint8Array([1, 2, 3, 4, 5]); 9858let value5 = "Jack"; 9859let value6 = 19; 9860let value7 = 101.5; 9861let value8 = new Uint8Array([6, 7, 8, 9, 10]); 9862let value9 = "Tom"; 9863let value10 = 20; 9864let value11 = 102.5; 9865let value12 = new Uint8Array([11, 12, 13, 14, 15]); 9866 9867const valueBucket1: relationalStore.ValuesBucket = { 9868 'NAME': value1, 9869 'AGE': value2, 9870 'SALARY': value3, 9871 'CODES': value4 9872}; 9873const valueBucket2: relationalStore.ValuesBucket = { 9874 'NAME': value5, 9875 'AGE': value6, 9876 'SALARY': value7, 9877 'CODES': value8 9878}; 9879const valueBucket3: relationalStore.ValuesBucket = { 9880 'NAME': value9, 9881 'AGE': value10, 9882 'SALARY': value11, 9883 'CODES': value12 9884}; 9885 9886let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 9887if (store != undefined) { 9888 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 9889 transaction.batchInsertWithConflictResolution("EMPLOYEE", valueBuckets, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE).then((insertNum: number) => { 9890 transaction.commit(); 9891 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 9892 }).catch((e: BusinessError) => { 9893 transaction.rollback(); 9894 console.error(`batchInsert is failed, code is ${e.code},message is ${e.message}`); 9895 }); 9896 }).catch((err: BusinessError) => { 9897 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 9898 }); 9899} 9900``` 9901 9902### batchInsertWithConflictResolutionSync<sup>18+</sup> 9903 9904batchInsertWithConflictResolutionSync(table: string, values: Array<ValuesBucket>, conflict: ConflictResolution): number 9905 9906向目标表中插入一组数据。 9907 9908**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 9909 9910**参数:** 9911 9912| 参数名 | 类型 | 必填 | 说明 | 9913| ------ | ------------------------------------------ | ---- | ---------------------------- | 9914| table | string | 是 | 指定的目标表名。 | 9915| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 9916| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决模式。如果是ON_CONFLICT_ROLLBACK模式,当发生冲突时会回滚整个事务。 | 9917 9918**返回值**: 9919 9920| 类型 | 说明 | 9921| ------ | ---------------------------------------------- | 9922| number | 如果操作成功,返回插入的数据个数,否则返回-1。 | 9923 9924**错误码:** 9925 9926以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 9927 9928| **错误码ID** | **错误信息** | 9929| ------------ | ------------------------------------------------------------ | 9930| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 9931| 14800000 | Inner error. | 9932| 14800011 | Failed to open the database because it is corrupted. | 9933| 14800014 | The RdbStore or ResultSet is already closed. | 9934| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 9935| 14800022 | SQLite: Callback routine requested an abort. | 9936| 14800023 | SQLite: Access permission denied. | 9937| 14800024 | SQLite: The database file is locked. | 9938| 14800025 | SQLite: A table in the database is locked. | 9939| 14800026 | SQLite: The database is out of memory. | 9940| 14800027 | SQLite: Attempt to write a readonly database. | 9941| 14800028 | SQLite: Some kind of disk I/O error occurred. | 9942| 14800029 | SQLite: The database is full. | 9943| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 9944| 14800032 | SQLite: Abort due to constraint violation. | 9945| 14800033 | SQLite: Data type mismatch. | 9946| 14800034 | SQLite: Library used incorrectly. | 9947| 14800047 | The WAL file size exceeds the default limit. | 9948 9949**示例:** 9950 9951```ts 9952let value1 = "Lisa"; 9953let value2 = 18; 9954let value3 = 100.5; 9955let value4 = new Uint8Array([1, 2, 3, 4, 5]); 9956let value5 = "Jack"; 9957let value6 = 19; 9958let value7 = 101.5; 9959let value8 = new Uint8Array([6, 7, 8, 9, 10]); 9960let value9 = "Tom"; 9961let value10 = 20; 9962let value11 = 102.5; 9963let value12 = new Uint8Array([11, 12, 13, 14, 15]); 9964 9965const valueBucket1: relationalStore.ValuesBucket = { 9966 'NAME': value1, 9967 'AGE': value2, 9968 'SALARY': value3, 9969 'CODES': value4 9970}; 9971const valueBucket2: relationalStore.ValuesBucket = { 9972 'NAME': value5, 9973 'AGE': value6, 9974 'SALARY': value7, 9975 'CODES': value8 9976}; 9977const valueBucket3: relationalStore.ValuesBucket = { 9978 'NAME': value9, 9979 'AGE': value10, 9980 'SALARY': value11, 9981 'CODES': value12 9982}; 9983 9984let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 9985if (store != undefined) { 9986 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 9987 try { 9988 let insertNum: number = (transaction as relationalStore.Transaction).batchInsertWithConflictResolutionSync("EMPLOYEE", valueBuckets, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE); 9989 transaction.commit(); 9990 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 9991 } catch (e) { 9992 transaction.rollback(); 9993 console.error(`batchInsert is failed, code is ${e.code},message is ${e.message}`); 9994 }; 9995 }).catch((err: BusinessError) => { 9996 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 9997 }); 9998} 9999``` 10000 10001### update<sup>14+</sup> 10002 10003update(values: ValuesBucket, predicates: RdbPredicates, conflict?: ConflictResolution): Promise<number> 10004 10005根据RdbPredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 10006 10007**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 10008 10009**参数:** 10010 10011| 参数名 | 类型 | 必填 | 说明 | 10012| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 10013| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 10014| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 10015| conflict | [ConflictResolution](#conflictresolution10) | 否 | 指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 10016 10017**返回值**: 10018 10019| 类型 | 说明 | 10020| --------------------- | ----------------------------------------- | 10021| Promise<number> | 指定的Promise回调方法。返回受影响的行数。 | 10022 10023**错误码:** 10024 10025以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 10026 10027| **错误码ID** | **错误信息** | 10028|-----------| ------------------------------------------------------------ | 10029| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 10030| 14800000 | Inner error. | 10031| 14800011 | Failed to open the database because it is corrupted. | 10032| 14800014 | The RdbStore or ResultSet is already closed. | 10033| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 10034| 14800023 | SQLite: Access permission denied. | 10035| 14800024 | SQLite: The database file is locked. | 10036| 14800025 | SQLite: A table in the database is locked. | 10037| 14800026 | SQLite: The database is out of memory. | 10038| 14800027 | SQLite: Attempt to write a readonly database. | 10039| 14800028 | SQLite: Some kind of disk I/O error occurred. | 10040| 14800029 | SQLite: The database is full. | 10041| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 10042| 14800033 | SQLite: Data type mismatch. | 10043| 14800047 | The WAL file size exceeds the default limit. | 10044 10045**示例:** 10046 10047```ts 10048let value1 = "Rose"; 10049let value2 = 22; 10050let value3 = 200.5; 10051let value4 = new Uint8Array([1, 2, 3, 4, 5]); 10052 10053// 以下三种方式可用 10054const valueBucket1: relationalStore.ValuesBucket = { 10055 'NAME': value1, 10056 'AGE': value2, 10057 'SALARY': value3, 10058 'CODES': value4 10059}; 10060const valueBucket2: relationalStore.ValuesBucket = { 10061 NAME: value1, 10062 AGE: value2, 10063 SALARY: value3, 10064 CODES: value4 10065}; 10066const valueBucket3: relationalStore.ValuesBucket = { 10067 "NAME": value1, 10068 "AGE": value2, 10069 "SALARY": value3, 10070 "CODES": value4 10071}; 10072 10073let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 10074predicates.equalTo("NAME", "Lisa"); 10075 10076if (store != undefined) { 10077 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 10078 transaction.update(valueBucket1, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE).then(async (rows: Number) => { 10079 transaction.commit(); 10080 console.info(`Updated row count: ${rows}`); 10081 }).catch((e: BusinessError) => { 10082 transaction.rollback(); 10083 console.error(`Updated failed, code is ${e.code},message is ${e.message}`); 10084 }); 10085 }).catch((err: BusinessError) => { 10086 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 10087 }); 10088} 10089``` 10090 10091### updateSync<sup>14+</sup> 10092 10093updateSync(values: ValuesBucket, predicates: RdbPredicates, conflict?: ConflictResolution): number 10094 10095根据RdbPredicates的指定实例对象更新数据库中的数据。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 10096 10097**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 10098 10099**参数:** 10100 10101| 参数名 | 类型 | 必填 | 说明 | 10102| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 10103| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 10104| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 10105| conflict | [ConflictResolution](#conflictresolution10) | 否 | 指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 10106 10107**返回值**: 10108 10109| 类型 | 说明 | 10110| ------ | ------------------ | 10111| number | 返回受影响的行数。 | 10112 10113**错误码:** 10114 10115以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 10116 10117| **错误码ID** | **错误信息** | 10118| ------------ | ------------------------------------------------------------ | 10119| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 10120| 14800000 | Inner error. | 10121| 14800011 | Failed to open the database because it is corrupted. | 10122| 14800014 | The RdbStore or ResultSet is already closed. | 10123| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 10124| 14800023 | SQLite: Access permission denied. | 10125| 14800024 | SQLite: The database file is locked. | 10126| 14800025 | SQLite: A table in the database is locked. | 10127| 14800026 | SQLite: The database is out of memory. | 10128| 14800027 | SQLite: Attempt to write a readonly database. | 10129| 14800028 | SQLite: Some kind of disk I/O error occurred. | 10130| 14800029 | SQLite: The database is full. | 10131| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 10132| 14800033 | SQLite: Data type mismatch. | 10133| 14800047 | The WAL file size exceeds the default limit. | 10134 10135**示例:** 10136 10137```ts 10138let value1 = "Rose"; 10139let value2 = 22; 10140let value3 = 200.5; 10141let value4 = new Uint8Array([1, 2, 3, 4, 5]); 10142 10143// 以下三种方式可用 10144const valueBucket1: relationalStore.ValuesBucket = { 10145 'NAME': value1, 10146 'AGE': value2, 10147 'SALARY': value3, 10148 'CODES': value4 10149}; 10150const valueBucket2: relationalStore.ValuesBucket = { 10151 NAME: value1, 10152 AGE: value2, 10153 SALARY: value3, 10154 CODES: value4 10155}; 10156const valueBucket3: relationalStore.ValuesBucket = { 10157 "NAME": value1, 10158 "AGE": value2, 10159 "SALARY": value3, 10160 "CODES": value4 10161}; 10162 10163let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 10164predicates.equalTo("NAME", "Lisa"); 10165 10166if (store != undefined) { 10167 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 10168 try { 10169 let rows: Number = (transaction as relationalStore.Transaction).updateSync(valueBucket1, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE); 10170 transaction.commit(); 10171 console.info(`Updated row count: ${rows}`); 10172 } catch (e) { 10173 transaction.rollback(); 10174 console.error(`Updated failed, code is ${e.code},message is ${e.message}`); 10175 }; 10176 }).catch((err: BusinessError) => { 10177 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 10178 }); 10179} 10180``` 10181 10182### delete<sup>14+</sup> 10183 10184delete(predicates: RdbPredicates):Promise<number> 10185 10186根据RdbPredicates的指定实例对象从数据库中删除数据,使用Promise异步回调。 10187 10188**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 10189 10190**参数:** 10191 10192| 参数名 | 类型 | 必填 | 说明 | 10193| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 10194| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 10195 10196**返回值**: 10197 10198| 类型 | 说明 | 10199| --------------------- | ------------------------------- | 10200| Promise<number> | Promise对象。返回受影响的行数。 | 10201 10202**错误码:** 10203 10204以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 10205 10206| **错误码ID** | **错误信息** | 10207|-----------| ------------------------------------------------------------ | 10208| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 10209| 14800000 | Inner error. | 10210| 14800011 | Failed to open the database because it is corrupted. | 10211| 14800014 | The RdbStore or ResultSet is already closed. | 10212| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 10213| 14800023 | SQLite: Access permission denied. | 10214| 14800024 | SQLite: The database file is locked. | 10215| 14800025 | SQLite: A table in the database is locked. | 10216| 14800026 | SQLite: The database is out of memory. | 10217| 14800027 | SQLite: Attempt to write a readonly database. | 10218| 14800028 | SQLite: Some kind of disk I/O error occurred. | 10219| 14800029 | SQLite: The database is full. | 10220| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 10221| 14800033 | SQLite: Data type mismatch. | 10222| 14800047 | The WAL file size exceeds the default limit. | 10223 10224**示例:** 10225 10226```ts 10227let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 10228predicates.equalTo("NAME", "Lisa"); 10229 10230if (store != undefined) { 10231 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 10232 transaction.delete(predicates).then((rows: number) => { 10233 transaction.commit(); 10234 console.info(`Delete rows: ${rows}`); 10235 }).catch((e: BusinessError) => { 10236 transaction.rollback(); 10237 console.error(`Delete failed, code is ${e.code},message is ${e.message}`); 10238 }); 10239 }).catch((err: BusinessError) => { 10240 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 10241 }); 10242} 10243``` 10244 10245### deleteSync<sup>14+</sup> 10246 10247deleteSync(predicates: RdbPredicates): number 10248 10249根据RdbPredicates的指定实例对象从数据库中删除数据。 10250 10251**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 10252 10253**参数:** 10254 10255| 参数名 | 类型 | 必填 | 说明 | 10256| ---------- | ------------------------------- | ---- | --------------------------------------- | 10257| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 10258 10259**返回值**: 10260 10261| 类型 | 说明 | 10262| ------ | ------------------ | 10263| number | 返回受影响的行数。 | 10264 10265**错误码:** 10266 10267以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 10268 10269| **错误码ID** | **错误信息** | 10270| ------------ | ------------------------------------------------------------ | 10271| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 10272| 14800000 | Inner error. | 10273| 14800011 | Failed to open the database because it is corrupted. | 10274| 14800014 | The RdbStore or ResultSet is already closed. | 10275| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 10276| 14800023 | SQLite: Access permission denied. | 10277| 14800024 | SQLite: The database file is locked. | 10278| 14800025 | SQLite: A table in the database is locked. | 10279| 14800026 | SQLite: The database is out of memory. | 10280| 14800027 | SQLite: Attempt to write a readonly database. | 10281| 14800028 | SQLite: Some kind of disk I/O error occurred. | 10282| 14800029 | SQLite: The database is full. | 10283| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 10284| 14800033 | SQLite: Data type mismatch. | 10285| 14800047 | The WAL file size exceeds the default limit. | 10286 10287**示例:** 10288 10289```ts 10290let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 10291predicates.equalTo("NAME", "Lisa"); 10292 10293if (store != undefined) { 10294 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 10295 try { 10296 let rows: number = (transaction as relationalStore.Transaction).deleteSync(predicates); 10297 transaction.commit(); 10298 console.info(`Delete rows: ${rows}`); 10299 } catch (e) { 10300 transaction.rollback(); 10301 console.error(`Delete failed, code is ${e.code},message is ${e.message}`); 10302 }; 10303 }).catch((err: BusinessError) => { 10304 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 10305 }); 10306} 10307``` 10308 10309### query<sup>14+</sup> 10310 10311query(predicates: RdbPredicates, columns?: Array<string>): Promise<ResultSet> 10312 10313根据指定条件查询数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 10314 10315**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 10316 10317**参数:** 10318 10319| 参数名 | 类型 | 必填 | 说明 | 10320| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 10321| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 10322| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 10323 10324**错误码:** 10325 10326以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 10327 10328| **错误码ID** | **错误信息** | 10329|-----------| ------------------------------------------------------------ | 10330| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 10331| 14800000 | Inner error. | 10332| 14800011 | Failed to open the database because it is corrupted. | 10333| 14800014 | The RdbStore or ResultSet is already closed. | 10334| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 10335| 14800023 | SQLite: Access permission denied. | 10336| 14800024 | SQLite: The database file is locked. | 10337| 14800026 | SQLite: The database is out of memory. | 10338| 14800028 | SQLite: Some kind of disk I/O error occurred. | 10339| 14800047 | The WAL file size exceeds the default limit. | 10340 10341**返回值**: 10342 10343| 类型 | 说明 | 10344| ------------------------------------------------------- | -------------------------------------------------- | 10345| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 10346 10347**示例:** 10348 10349```ts 10350let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 10351predicates.equalTo("NAME", "Rose"); 10352 10353if (store != undefined) { 10354 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 10355 transaction.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then(async (resultSet: relationalStore.ResultSet) => { 10356 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 10357 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 10358 while (resultSet.goToNextRow()) { 10359 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 10360 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 10361 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 10362 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 10363 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 10364 } 10365 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 10366 resultSet.close(); 10367 transaction.commit(); 10368 }).catch((e: BusinessError) => { 10369 transaction.rollback(); 10370 console.error(`Query failed, code is ${e.code},message is ${e.message}`); 10371 }); 10372 }).catch((err: BusinessError) => { 10373 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 10374 }); 10375} 10376``` 10377 10378### querySync<sup>14+</sup> 10379 10380querySync(predicates: RdbPredicates, columns?: Array<string>): ResultSet 10381 10382根据指定条件查询数据库中的数据。对query同步接口获得的resultSet进行操作时,若逻辑复杂且循环次数过多,可能造成freeze问题,建议将此步骤放到[taskpool](../apis-arkts/js-apis-taskpool.md)线程中执行。 10383 10384**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 10385 10386**参数:** 10387 10388| 参数名 | 类型 | 必填 | 说明 | 10389| ---------- | ------------------------------- | ---- | ------------------------------------------------------------ | 10390| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 10391| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。默认值为空。 | 10392 10393**错误码:** 10394 10395以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 10396 10397| **错误码ID** | **错误信息** | 10398| ------------ | ------------------------------------------------------------ | 10399| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 10400| 14800000 | Inner error. | 10401| 14800011 | Failed to open the database because it is corrupted. | 10402| 14800014 | The RdbStore or ResultSet is already closed. | 10403| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 10404| 14800023 | SQLite: Access permission denied. | 10405| 14800024 | SQLite: The database file is locked. | 10406| 14800025 | SQLite: A table in the database is locked. | 10407| 14800026 | SQLite: The database is out of memory. | 10408| 14800028 | SQLite: Some kind of disk I/O error occurred. | 10409| 14800047 | The WAL file size exceeds the default limit. | 10410 10411**返回值**: 10412 10413| 类型 | 说明 | 10414| ----------------------- | ----------------------------------- | 10415| [ResultSet](#resultset) | 如果操作成功,则返回ResultSet对象。 | 10416 10417**示例:** 10418 10419```ts 10420let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 10421predicates.equalTo("NAME", "Rose"); 10422 10423if (store != undefined) { 10424 (store as relationalStore.RdbStore).createTransaction().then(async (transaction: relationalStore.Transaction) => { 10425 try { 10426 let resultSet: relationalStore.ResultSet = (transaction as relationalStore.Transaction).querySync(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 10427 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 10428 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 10429 while (resultSet.goToNextRow()) { 10430 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 10431 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 10432 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 10433 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 10434 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 10435 } 10436 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 10437 resultSet.close(); 10438 transaction.commit(); 10439 } catch (e) { 10440 transaction.rollback(); 10441 console.error(`Query failed, code is ${e.code},message is ${e.message}`); 10442 }; 10443 }).catch((err: BusinessError) => { 10444 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 10445 }); 10446} 10447``` 10448 10449### querySql<sup>14+</sup> 10450 10451querySql(sql: string, args?: Array<ValueType>): Promise<ResultSet> 10452 10453根据指定SQL语句查询数据库中的数据,SQL语句中的各种表达式和操作符之间的关系操作符号不超过1000个,使用Promise异步回调。 10454 10455**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 10456 10457**参数:** 10458 10459| 参数名 | 类型 | 必填 | 说明 | 10460| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 10461| sql | string | 是 | 指定要执行的SQL语句。 | 10462| args | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 10463 10464**返回值**: 10465 10466| 类型 | 说明 | 10467| ------------------------------------------------------- | -------------------------------------------------- | 10468| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 10469 10470**错误码:** 10471 10472以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 10473 10474| **错误码ID** | **错误信息** | 10475|-----------| ------------------------------------------------------------ | 10476| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 10477| 14800000 | Inner error. | 10478| 14800011 | Failed to open the database because it is corrupted. | 10479| 14800014 | The RdbStore or ResultSet is already closed. | 10480| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 10481| 14800023 | SQLite: Access permission denied. | 10482| 14800024 | SQLite: The database file is locked. | 10483| 14800025 | SQLite: A table in the database is locked. | 10484| 14800026 | SQLite: The database is out of memory. | 10485| 14800028 | SQLite: Some kind of disk I/O error occurred. | 10486| 14800047 | The WAL file size exceeds the default limit. | 10487 10488**示例:** 10489 10490```ts 10491if (store != undefined) { 10492 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 10493 transaction.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'").then(async (resultSet: relationalStore.ResultSet) => { 10494 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 10495 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 10496 while (resultSet.goToNextRow()) { 10497 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 10498 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 10499 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 10500 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 10501 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 10502 } 10503 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 10504 resultSet.close(); 10505 transaction.commit(); 10506 }).catch((e: BusinessError) => { 10507 transaction.rollback(); 10508 console.error(`Query failed, code is ${e.code},message is ${e.message}`); 10509 }); 10510 }).catch((err: BusinessError) => { 10511 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 10512 }); 10513} 10514``` 10515 10516### querySqlSync<sup>14+</sup> 10517 10518querySqlSync(sql: string, args?: Array<ValueType>): ResultSet 10519 10520根据指定SQL语句查询数据库中的数据,SQL语句中的各种表达式和操作符之间的关系操作符号不超过1000个。对query同步接口获得的resultSet进行操作时,若逻辑复杂且循环次数过多,可能造成freeze问题,建议将此步骤放到[taskpool](../apis-arkts/js-apis-taskpool.md)线程中执行。 10521 10522**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 10523 10524**参数:** 10525 10526| 参数名 | 类型 | 必填 | 说明 | 10527| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 10528| sql | string | 是 | 指定要执行的SQL语句。 | 10529| args | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。默认值为空。 | 10530 10531**返回值**: 10532 10533| 类型 | 说明 | 10534| ----------------------- | ----------------------------------- | 10535| [ResultSet](#resultset) | 如果操作成功,则返回ResultSet对象。 | 10536 10537**错误码:** 10538 10539以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 10540 10541| **错误码ID** | **错误信息** | 10542| ------------ | ------------------------------------------------------------ | 10543| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 10544| 14800000 | Inner error. | 10545| 14800011 | Failed to open the database because it is corrupted. | 10546| 14800014 | The RdbStore or ResultSet is already closed. | 10547| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 10548| 14800023 | SQLite: Access permission denied. | 10549| 14800024 | SQLite: The database file is locked. | 10550| 14800025 | SQLite: A table in the database is locked. | 10551| 14800026 | SQLite: The database is out of memory. | 10552| 14800028 | SQLite: Some kind of disk I/O error occurred. | 10553| 14800047 | The WAL file size exceeds the default limit. | 10554 10555**示例:** 10556 10557```ts 10558if (store != undefined) { 10559 (store as relationalStore.RdbStore).createTransaction().then(async (transaction: relationalStore.Transaction) => { 10560 try { 10561 let resultSet: relationalStore.ResultSet = (transaction as relationalStore.Transaction).querySqlSync("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'"); 10562 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 10563 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 10564 while (resultSet.goToNextRow()) { 10565 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 10566 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 10567 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 10568 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 10569 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 10570 } 10571 // 释放数据集的内存,若不释放可能会引起fd泄露与内存泄露 10572 resultSet.close(); 10573 transaction.commit(); 10574 } catch (e) { 10575 transaction.rollback(); 10576 console.error(`Query failed, code is ${e.code},message is ${e.message}`); 10577 }; 10578 }).catch((err: BusinessError) => { 10579 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 10580 }); 10581} 10582``` 10583 10584### execute<sup>14+</sup> 10585 10586execute(sql: string, args?: Array<ValueType>): Promise<ValueType> 10587 10588执行包含指定参数的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,返回值类型为ValueType,使用Promise异步回调。 10589 10590该接口支持执行增删改操作,支持执行PRAGMA语法的sql,支持对表的操作(建表、删表、修改表),返回结果类型由执行具体sql的结果决定。 10591 10592此接口不支持执行查询、附加数据库和事务操作,查询可以使用[querySql](#querysql14)、[query](#query14)接口代替、附加数据库可以使用[attach](#attach12)接口代替。 10593 10594不支持分号分隔的多条语句。 10595 10596**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 10597 10598**参数:** 10599 10600| 参数名 | 类型 | 必填 | 说明 | 10601| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 10602| sql | string | 是 | 指定要执行的SQL语句。 | 10603| args | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 10604 10605**返回值**: 10606 10607| 类型 | 说明 | 10608| ------------------- | ------------------------- | 10609| Promise<[ValueType](#valuetype)> | Promise对象,返回sql执行后的结果。 | 10610 10611**错误码:** 10612 10613以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 10614 10615| **错误码ID** | **错误信息** | 10616|-----------| ------------------------------------------------------------ | 10617| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 10618| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 10619| 14800000 | Inner error. | 10620| 14800011 | Failed to open the database because it is corrupted. | 10621| 14800014 | The RdbStore or ResultSet is already closed. | 10622| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 10623| 14800023 | SQLite: Access permission denied. | 10624| 14800024 | SQLite: The database file is locked. | 10625| 14800025 | SQLite: A table in the database is locked. | 10626| 14800026 | SQLite: The database is out of memory. | 10627| 14800027 | SQLite: Attempt to write a readonly database. | 10628| 14800028 | SQLite: Some kind of disk I/O error occurred. | 10629| 14800029 | SQLite: The database is full. | 10630| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 10631| 14800033 | SQLite: Data type mismatch. | 10632| 14800047 | The WAL file size exceeds the default limit. | 10633 10634**示例:** 10635 10636```ts 10637// 删除表中所有数据 10638if (store != undefined) { 10639 const SQL_DELETE_TABLE = 'DELETE FROM test'; 10640 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 10641 transaction.execute(SQL_DELETE_TABLE).then((data) => { 10642 transaction.commit(); 10643 console.info(`delete result: ${data}`); 10644 }).catch((e: BusinessError) => { 10645 transaction.rollback(); 10646 console.error(`delete failed, code is ${e.code}, message is ${e.message}`); 10647 }); 10648 }).catch((err: BusinessError) => { 10649 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 10650 }); 10651} 10652``` 10653 10654### executeSync<sup>14+</sup> 10655 10656executeSync(sql: string, args?: Array<ValueType>): ValueType 10657 10658执行包含指定参数的SQL语句,语句中的各种表达式和操作符之间的关系操作符号不超过1000个,返回值类型为ValueType。 10659 10660该接口支持执行增删改操作,支持执行PRAGMA语法的sql,支持对表的操作(建表、删表、修改表),返回结果类型由执行具体sql的结果决定。 10661 10662此接口不支持执行查询、附加数据库和事务操作,查询可以使用[querySql](#querysql14)、[query](#query14)接口代替、附加数据库可以使用[attach](#attach12)接口代替。 10663 10664不支持分号分隔的多条语句。 10665 10666**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 10667 10668**参数:** 10669 10670| 参数名 | 类型 | 必填 | 说明 | 10671| ------ | ------------------------------------ | ---- | ------------------------------------------------------------ | 10672| sql | string | 是 | 指定要执行的SQL语句。 | 10673| args | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。该参数不填,或者填null或undefined,都认为是sql参数语句完整。默认值为空。 | 10674 10675**返回值**: 10676 10677| 类型 | 说明 | 10678| ----------------------- | ------------------- | 10679| [ValueType](#valuetype) | 返回sql执行后的结果。 | 10680 10681**错误码:** 10682 10683以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 10684 10685| **错误码ID** | **错误信息** | 10686| ------------ | ------------------------------------------------------------ | 10687| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 10688| 801 | Capability not supported the sql(attach,begin,commit,rollback etc.). | 10689| 14800000 | Inner error. | 10690| 14800011 | Failed to open the database because it is corrupted. | 10691| 14800014 | The RdbStore or ResultSet is already closed. | 10692| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 10693| 14800023 | SQLite: Access permission denied. | 10694| 14800024 | SQLite: The database file is locked. | 10695| 14800025 | SQLite: A table in the database is locked. | 10696| 14800026 | SQLite: The database is out of memory. | 10697| 14800027 | SQLite: Attempt to write a readonly database. | 10698| 14800028 | SQLite: Some kind of disk I/O error occurred. | 10699| 14800029 | SQLite: The database is full. | 10700| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 10701| 14800033 | SQLite: Data type mismatch. | 10702| 14800047 | The WAL file size exceeds the default limit. | 10703 10704**示例:** 10705 10706```ts 10707// 删除表中所有数据 10708if (store != undefined) { 10709 (store as relationalStore.RdbStore).createTransaction().then((transaction: relationalStore.Transaction) => { 10710 const SQL_DELETE_TABLE = 'DELETE FROM test'; 10711 try { 10712 let data = (transaction as relationalStore.Transaction).executeSync(SQL_DELETE_TABLE); 10713 transaction.commit(); 10714 console.info(`delete result: ${data}`); 10715 } catch (e) { 10716 transaction.rollback(); 10717 console.error(`delete failed, code is ${e.code}, message is ${e.message}`); 10718 }; 10719 }).catch((err: BusinessError) => { 10720 console.error(`createTransaction failed, code is ${err.code},message is ${err.message}`); 10721 }); 10722} 10723```