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语句来满足复杂的场景需要。不支持Worker线程。 10ArkTS侧支持的基本数据类型:number、string、二进制类型数据、boolean。为保证插入并读取数据成功,建议一条数据不要超过2M。超出该大小,插入成功,读取失败。 11 12该模块提供以下关系型数据库相关的常用功能: 13 14- [RdbPredicates](arkts-apis-data-relationalStore-RdbPredicates.md):数据库中用来代表数据实体的性质、特征或者数据实体之间关系的词项,主要用来定义数据库的操作条件。 15- [RdbStore](arkts-apis-data-relationalStore-RdbStore.md):提供管理关系数据库(RDB)方法的接口。 16- [ResultSet](arkts-apis-data-relationalStore-ResultSet.md):提供用户调用关系型数据库查询接口之后返回的结果集合。 17 18> **说明:** 19> 20> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 21> 22> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.data.relationalStore (关系型数据库)](arkts-apis-data-relationalStore.md)。 23 24## 导入模块 25 26```ts 27import { relationalStore } from '@kit.ArkData'; 28``` 29 30## StoreConfig 31 32管理数据库配置。 33 34**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 35 36| 名称 | 类型 | 必填 | 说明 | 37| ------------- | ------------- | ---- | --------------------------------------------------------- | 38| isSearchable<sup>11+</sup> | boolean | 否 | 指定数据库是否支持搜索,true表示支持搜索,false表示不支持搜索,默认不支持搜索。<br/>**系统接口:** 此接口为系统接口。<br/>从API version 11开始,支持此可选参数。<br/> | 39| haMode<sup>12+</sup> | [HAMode](#hamode12) | 否 | 指定关系型数据库存储的高可用性模式,SINGLE表示将数据写入单个关系型数据库存储,MAIN_REPLICA表示将数据写入主关系型数据库存储和副本关系型数据库存储,但不支持加密场景和attach场景。MAIN_REPLICA会导致数据库写入性能的劣化,默认为SINGLE。<br/>**系统接口:** 此接口为系统接口。<br/>从API version 12开始,支持此可选参数。<br/> | 40 41## HAMode<sup>12+</sup> 42 43描述关系型数据库存储的高可用性模式的枚举。 44 45**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 46 47| 名称 | 值 | 说明 | 48| ------------------------------- | --- | -------------- | 49| SINGLE | 0 | 表示将数据写入单个关系型数据库存储。 | 50| MAIN_REPLICA | 1 | 表示将数据写入主关系型数据库存储和副本关系型数据库存储,不支持加密场景和attach场景。 | 51 52## Reference<sup>11+</sup> 53 54记录表之间通过表字段指定的关联关系。其中表a关联到表b,称a为b关联的子表,b为a关联的父表。 55 56**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 57 58**系统接口:** 此接口为系统接口。 59 60| 名称 | 类型 | 必填 | 说明 | 61| ---------- | ------ | ---- | ---------------------------------------- | 62| sourceTable | string | 是 | 关联的子表名称。 | 63| targetTable | string | 是 | 关联的父表名称。 | 64| refFields | Record<string, string> | 是 | 表示关联表的关联字段。键值数据中键为子表字段,值为父表字段。 | 65 66## DistributedConfig<sup>10+</sup> 67 68记录表的分布式配置信息。 69 70**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 71 72| 名称 | 类型 | 必填 | 说明 | 73| -------- | ------- | ---- | ------------------------------------------------------------ | 74| references<sup>11+</sup> | Array<[Reference](#reference11)> | 否 | 设置表之间的关联关系,可以设置多个字段的关联,子表和父表关联字段的值必须相同。默认数据库表之间无关联关系。<br/>**系统接口:** 此接口为系统接口。<br/>从API version 11开始,支持此可选参数。| 75 76## RdbStore 77 78提供管理关系型数据库(RDB)的接口。 79 80在使用以下API前,请先通过[getRdbStore](arkts-apis-data-relationalStore-f.md#relationalstoregetrdbstore-1)方法获取RdbStore实例,并使用该实例调用对应接口方法。 81 82在此基础上,建议优先使用[execute](arkts-apis-data-relationalStore-RdbStore.md#execute12)方法完成数据库表结构和初始数据的初始化,以确保相关接口调用的前置条件已满足。 83 84### update 85 86update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<number>):void 87 88根据DataSharePredicates的指定实例对象更新数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 89 90**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 91 92**模型约束:** 此接口仅可在Stage模型下可用。 93 94**系统接口:** 此接口为系统接口。 95 96**参数:** 97 98| 参数名 | 类型 | 必填 | 说明 | 99| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 100| table | string | 是 | 指定的目标表名。 | 101| values | [ValuesBucket](arkts-apis-data-relationalStore-t.md#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 102| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的更新条件。 | 103| callback | AsyncCallback<number> | 是 | 指定的callback回调方法。返回受影响的行数。 | 104 105**错误码:** 106 107以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 108 109| **错误码ID** | **错误信息** | 110|-----------| ------------------------------------------------------------ | 111| 202 | Permission verification failed, application which is not a system application uses system API. | 112| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 113| 14800000 | Inner error. | 114| 14800011 | Failed to open the database because it is corrupted. | 115| 14800014 | The RdbStore or ResultSet is already closed. | 116| 14800015 | The database does not respond. | 117| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 118| 14800022 | SQLite: Callback routine requested an abort. | 119| 14800023 | SQLite: Access permission denied. | 120| 14800024 | SQLite: The database file is locked. | 121| 14800025 | SQLite: A table in the database is locked. | 122| 14800026 | SQLite: The database is out of memory. | 123| 14800027 | SQLite: Attempt to write a readonly database. | 124| 14800028 | SQLite: Some kind of disk I/O error occurred. | 125| 14800029 | SQLite: The database is full. | 126| 14800030 | SQLite: Unable to open the database file. | 127| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 128| 14800032 | SQLite: Abort due to constraint violation. | 129| 14800033 | SQLite: Data type mismatch. | 130| 14800034 | SQLite: Library used incorrectly. | 131| 14800047 | The WAL file size exceeds the default limit. | 132 133**示例:** 134 135```ts 136import { dataSharePredicates } from '@kit.ArkData'; 137import { ValuesBucket } from '@kit.ArkData'; 138 139let value1 = "Rose"; 140let value2 = 22; 141let value3 = 200.5; 142let value4 = new Uint8Array([1, 2, 3, 4, 5]); 143 144// 以下三种方式可用 145const valueBucket1: ValuesBucket = { 146 'NAME': value1, 147 'AGE': value2, 148 'SALARY': value3, 149 'CODES': value4, 150}; 151const valueBucket2: ValuesBucket = { 152 NAME: value1, 153 AGE: value2, 154 SALARY: value3, 155 CODES: value4, 156}; 157const valueBucket3: ValuesBucket = { 158 "NAME": value1, 159 "AGE": value2, 160 "SALARY": value3, 161 "CODES": value4, 162}; 163 164let predicates = new dataSharePredicates.DataSharePredicates(); 165predicates.equalTo("NAME", "Lisa"); 166if (store != undefined) { 167 (store as relationalStore.RdbStore).update("EMPLOYEE", valueBucket1, predicates, (err, rows) => { 168 if (err) { 169 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 170 return; 171 } 172 console.info(`Updated row count: ${rows}`); 173 }); 174} 175``` 176 177### update 178 179update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates):Promise<number> 180 181根据DataSharePredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 182 183**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 184 185**模型约束:** 此接口仅可在Stage模型下可用。 186 187**系统接口:** 此接口为系统接口。 188 189**参数:** 190 191| 参数名 | 类型 | 必填 | 说明 | 192| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 193| table | string | 是 | 指定的目标表名。 | 194| values | [ValuesBucket](arkts-apis-data-relationalStore-t.md#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 195| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的更新条件。 | 196 197**返回值**: 198 199| 类型 | 说明 | 200| --------------------- | ----------------------------------------- | 201| Promise<number> | 指定的Promise回调方法。返回受影响的行数。 | 202 203**错误码:** 204 205以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 206 207| **错误码ID** | **错误信息** | 208|-----------| ------------------------------------------------------------ | 209| 202 | Permission verification failed, application which is not a system application uses system API. | 210| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 211| 14800000 | Inner error. | 212| 14800011 | Failed to open the database because it is corrupted. | 213| 14800014 | The RdbStore or ResultSet is already closed. | 214| 14800015 | The database does not respond. | 215| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 216| 14800022 | SQLite: Callback routine requested an abort. | 217| 14800023 | SQLite: Access permission denied. | 218| 14800024 | SQLite: The database file is locked. | 219| 14800025 | SQLite: A table in the database is locked. | 220| 14800026 | SQLite: The database is out of memory. | 221| 14800027 | SQLite: Attempt to write a readonly database. | 222| 14800028 | SQLite: Some kind of disk I/O error occurred. | 223| 14800029 | SQLite: The database is full. | 224| 14800030 | SQLite: Unable to open the database file. | 225| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 226| 14800032 | SQLite: Abort due to constraint violation. | 227| 14800033 | SQLite: Data type mismatch. | 228| 14800034 | SQLite: Library used incorrectly. | 229| 14800047 | The WAL file size exceeds the default limit. | 230 231**示例:** 232 233```ts 234import { dataSharePredicates } from '@kit.ArkData'; 235import { ValuesBucket } from '@kit.ArkData'; 236import { BusinessError } from '@kit.BasicServicesKit'; 237 238let value1 = "Rose"; 239let value2 = 22; 240let value3 = 200.5; 241let value4 = new Uint8Array([1, 2, 3, 4, 5]); 242 243// 以下三种方式可用 244const valueBucket1: ValuesBucket = { 245 'NAME': value1, 246 'AGE': value2, 247 'SALARY': value3, 248 'CODES': value4, 249}; 250const valueBucket2: ValuesBucket = { 251 NAME: value1, 252 AGE: value2, 253 SALARY: value3, 254 CODES: value4, 255}; 256const valueBucket3: ValuesBucket = { 257 "NAME": value1, 258 "AGE": value2, 259 "SALARY": value3, 260 "CODES": value4, 261}; 262 263let predicates = new dataSharePredicates.DataSharePredicates(); 264predicates.equalTo("NAME", "Lisa"); 265if (store != undefined) { 266 (store as relationalStore.RdbStore).update("EMPLOYEE", valueBucket1, predicates).then(async (rows: number) => { 267 console.info(`Updated row count: ${rows}`); 268 }).catch((err: BusinessError) => { 269 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 270 }); 271} 272``` 273 274### delete 275 276delete(table: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<number>):void 277 278根据DataSharePredicates的指定实例对象从数据库中删除数据,使用callback异步回调。 279 280**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 281 282**模型约束:** 此接口仅可在Stage模型下可用。 283 284**系统接口:** 此接口为系统接口。 285 286**参数:** 287 288| 参数名 | 类型 | 必填 | 说明 | 289| ---------- | ------------------------------------------------------------ | ---- | --------------------------------------------- | 290| table | string | 是 | 指定的目标表名,不能为空字符串。 | 291| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的删除条件。 | 292| callback | AsyncCallback<number> | 是 | 指定callback回调函数。返回受影响的行数量。 | 293 294**错误码:** 295 296以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 297 298| **错误码ID** | **错误信息** | 299|-----------| ------------------------------------------------------------ | 300| 202 | Permission verification failed, application which is not a system application uses system API. | 301| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 302| 14800000 | Inner error. | 303| 14800011 | Failed to open the database because it is corrupted. | 304| 14800014 | The RdbStore or ResultSet is already closed. | 305| 14800015 | The database does not respond. | 306| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 307| 14800022 | SQLite: Callback routine requested an abort. | 308| 14800023 | SQLite: Access permission denied. | 309| 14800024 | SQLite: The database file is locked. | 310| 14800025 | SQLite: A table in the database is locked. | 311| 14800026 | SQLite: The database is out of memory. | 312| 14800027 | SQLite: Attempt to write a readonly database. | 313| 14800028 | SQLite: Some kind of disk I/O error occurred. | 314| 14800029 | SQLite: The database is full. | 315| 14800030 | SQLite: Unable to open the database file. | 316| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 317| 14800032 | SQLite: Abort due to constraint violation. | 318| 14800033 | SQLite: Data type mismatch. | 319| 14800034 | SQLite: Library used incorrectly. | 320| 14800047 | The WAL file size exceeds the default limit. | 321 322**示例:** 323 324```ts 325import { dataSharePredicates } from '@kit.ArkData'; 326 327let predicates = new dataSharePredicates.DataSharePredicates(); 328predicates.equalTo("NAME", "Lisa"); 329if (store != undefined) { 330 (store as relationalStore.RdbStore).delete("EMPLOYEE", predicates, (err, rows) => { 331 if (err) { 332 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 333 return; 334 } 335 console.info(`Delete rows: ${rows}`); 336 }); 337} 338``` 339 340### delete 341 342delete(table: string, predicates: dataSharePredicates.DataSharePredicates):Promise<number> 343 344根据DataSharePredicates的指定实例对象从数据库中删除数据,使用Promise异步回调。 345 346**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 347 348**模型约束:** 此接口仅可在Stage模型下可用。 349 350**系统接口:** 此接口为系统接口。 351 352**参数:** 353 354| 参数名 | 类型 | 必填 | 说明 | 355| ---------- | ------------------------------------------------------------ | ---- | --------------------------------------------- | 356| table | string | 是 | 指定的目标表名。 | 357| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的删除条件。 | 358 359**返回值**: 360 361| 类型 | 说明 | 362| --------------------- | ------------------------------- | 363| Promise<number> | Promise对象。返回受影响的行数。 | 364 365**错误码:** 366 367以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 368 369| **错误码ID** | **错误信息** | 370|-----------| --------------------- | 371| 202 | Permission verification failed, application which is not a system application uses system API. | 372| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 373| 14800000 | Inner error. | 374| 14800011 | Failed to open the database because it is corrupted. | 375| 14800014 | The RdbStore or ResultSet is already closed. | 376| 14800015 | The database does not respond. | 377| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 378| 14800022 | SQLite: Callback routine requested an abort. | 379| 14800023 | SQLite: Access permission denied. | 380| 14800024 | SQLite: The database file is locked. | 381| 14800025 | SQLite: A table in the database is locked. | 382| 14800026 | SQLite: The database is out of memory. | 383| 14800027 | SQLite: Attempt to write a readonly database. | 384| 14800028 | SQLite: Some kind of disk I/O error occurred. | 385| 14800029 | SQLite: The database is full. | 386| 14800030 | SQLite: Unable to open the database file. | 387| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 388| 14800032 | SQLite: Abort due to constraint violation. | 389| 14800033 | SQLite: Data type mismatch. | 390| 14800034 | SQLite: Library used incorrectly. | 391| 14800047 | The WAL file size exceeds the default limit. | 392 393**示例:** 394 395```ts 396import { dataSharePredicates } from '@kit.ArkData'; 397import { BusinessError } from '@kit.BasicServicesKit'; 398 399let predicates = new dataSharePredicates.DataSharePredicates(); 400predicates.equalTo("NAME", "Lisa"); 401if (store != undefined) { 402 (store as relationalStore.RdbStore).delete("EMPLOYEE", predicates).then((rows: number) => { 403 console.info(`Delete rows: ${rows}`); 404 }).catch((err: BusinessError) => { 405 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 406 }); 407} 408``` 409 410### query<sup>10+</sup> 411 412query(table: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<ResultSet>):void 413 414根据指定条件查询数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 415 416**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 417 418**模型约束:** 此接口仅可在Stage模型下可用。 419 420**系统接口:** 此接口为系统接口。 421 422**参数:** 423 424| 参数名 | 类型 | 必填 | 说明 | 425| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 426| table | string | 是 | 指定的目标表名。 | 427| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的查询条件。 | 428| callback | AsyncCallback<[ResultSet](arkts-apis-data-relationalStore-ResultSet.md)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 429 430**错误码:** 431 432以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 433 434| **错误码ID** | **错误信息** | 435|-----------| ------------------ | 436| 202 | Permission verification failed, application which is not a system application uses system API. | 437| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 438| 14800000 | Inner error. | 439| 14800014 | The RdbStore or ResultSet is already closed. | 440| 14800015 | The database does not respond. | 441 442**示例:** 443 444```ts 445import { dataSharePredicates } from '@kit.ArkData'; 446 447let predicates = new dataSharePredicates.DataSharePredicates(); 448predicates.equalTo("NAME", "Rose"); 449if (store != undefined) { 450 (store as relationalStore.RdbStore).query("EMPLOYEE", predicates, (err, resultSet) => { 451 if (err) { 452 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 453 return; 454 } 455 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 456 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 457 while (resultSet.goToNextRow()) { 458 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 459 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 460 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 461 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 462 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 463 } 464 // 释放数据集的内存 465 resultSet.close(); 466 }); 467} 468``` 469 470### query 471 472query(table: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void 473 474根据指定条件查询数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 475 476**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 477 478**模型约束:** 此接口仅可在Stage模型下可用。 479 480**系统接口:** 此接口为系统接口。 481 482**参数:** 483 484| 参数名 | 类型 | 必填 | 说明 | 485| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 486| table | string | 是 | 指定的目标表名。 | 487| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的查询条件。 | 488| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 489| callback | AsyncCallback<[ResultSet](arkts-apis-data-relationalStore-ResultSet.md)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 490 491**错误码:** 492 493以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 494 495| **错误码ID** | **错误信息** | 496|-----------| --------------- | 497| 202 | Permission verification failed, application which is not a system application uses system API. | 498| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 499| 14800000 | Inner error. | 500| 14800014 | The RdbStore or ResultSet is already closed. | 501| 14800015 | The database does not respond. | 502 503**示例:** 504 505```ts 506import { dataSharePredicates } from '@kit.ArkData'; 507 508let predicates = new dataSharePredicates.DataSharePredicates(); 509predicates.equalTo("NAME", "Rose"); 510if (store != undefined) { 511 (store as relationalStore.RdbStore).query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], (err, resultSet) => { 512 if (err) { 513 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 514 return; 515 } 516 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 517 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 518 while (resultSet.goToNextRow()) { 519 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 520 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 521 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 522 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 523 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 524 } 525 // 释放数据集的内存 526 resultSet.close(); 527 }); 528} 529``` 530 531### query 532 533query(table: string, predicates: dataSharePredicates.DataSharePredicates, columns?: Array<string>):Promise<ResultSet> 534 535根据指定条件查询数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 536 537**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 538 539**模型约束:** 此接口仅可在Stage模型下可用。 540 541**系统接口:** 此接口为系统接口。 542 543**参数:** 544 545| 参数名 | 类型 | 必填 | 说明 | 546| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------ | 547| table | string | 是 | 指定的目标表名。 | 548| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的查询条件。 | 549| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 550 551**返回值**: 552 553| 类型 | 说明 | 554| ------------------------------------------------------- | -------------------------------------------------- | 555| Promise<[ResultSet](arkts-apis-data-relationalStore-ResultSet.md)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 556 557**错误码:** 558 559以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 560 561| **错误码ID** | **错误信息** | 562|-----------| ----------- | 563| 202 | Permission verification failed, application which is not a system application uses system API. | 564| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 565| 14800000 | Inner error. | 566| 14800014 | The RdbStore or ResultSet is already closed. | 567| 14800015 | The database does not respond. | 568 569**示例:** 570 571```ts 572import { dataSharePredicates } from '@kit.ArkData'; 573import { BusinessError } from '@kit.BasicServicesKit'; 574 575let predicates = new dataSharePredicates.DataSharePredicates(); 576predicates.equalTo("NAME", "Rose"); 577if (store != undefined) { 578 (store as relationalStore.RdbStore).query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((resultSet: relationalStore.ResultSet) => { 579 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 580 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 581 while (resultSet.goToNextRow()) { 582 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 583 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 584 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 585 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 586 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 587 } 588 // 释放数据集的内存 589 resultSet.close(); 590 }).catch((err: BusinessError) => { 591 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 592 }); 593} 594``` 595 596### cloudSync<sup>11+</sup> 597 598cloudSync(mode: SyncMode, predicates: RdbPredicates, progress: Callback<ProgressDetails>, callback: AsyncCallback<void>): void 599 600手动执行按条件进行端云同步,使用callback异步回调。使用该接口需要实现云同步功能。 601 602> **说明:** 603> 604> 从API version 18开始,手动执行端云同步时,设置谓词条件时新增支持指定资产下载能力。此时,同步模式需要设置为`relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST`。 605> 606> 谓词中支持使用主键(必填)和资产(可选)作为同步条件:选择资产作为同步条件时,谓词仅支持[equalTo](arkts-apis-data-relationalStore-RdbPredicates.md#equalto);指定资产的数量较多时(最多支持指定50个资产),建议谓词中仅使用主键作为同步条件。 607 608**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 609 610**系统接口:** 此接口为系统接口。 611 612**参数:** 613 614| 参数名 | 类型 | 必填 | 说明 | 615|-------------|--------------------------------| ---- |-------------------------------| 616| mode | [SyncMode](arkts-apis-data-relationalStore-e.md#syncmode) | 是 | 表示数据库的同步模式。 | 617| predicates | [RdbPredicates](arkts-apis-data-relationalStore-RdbPredicates.md) | 是 | 表示同步数据的谓词条件。 | 618| progress | Callback<[ProgressDetails](arkts-apis-data-relationalStore-i.md#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 619| callback | AsyncCallback<void> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。 | 620 621**错误码:** 622 623以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 624 625| **错误码ID** | **错误信息** | 626|-----------|--------------| 627| 202 | if permission verification failed, application which is not a system application uses system API. | 628| 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.| 629| 801 | Capability not supported. | 630| 14800014 | The RdbStore or ResultSet is already closed. | 631 632**示例1:手动同步,同步模式为云端同步到本地设备** 633 634```ts 635let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 636predicates.in("id", ["id1", "id2"]); 637 638if (store != undefined) { 639 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, predicates, (progressDetail: relationalStore.ProgressDetails) => { 640 console.info(`progress: ${progressDetail.schedule}`); 641 }, (err) => { 642 if (err) { 643 console.error(`cloudSync failed, code is ${err.code}, message is ${err.message}`); 644 return; 645 } 646 console.info('Cloud sync succeeded'); 647 }); 648}; 649``` 650**示例2:指定资产下载** 651```ts 652let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 653let asset: relationalStore.Asset = { 654 name: "name", 655 uri: "uri", 656 path: "path", 657 createTime: new Date().getTime().toString(), 658 modifyTime: new Date().getTime().toString(), 659 size: "1024" 660}; 661// 谓词条件中指定主键和资产,asset为数据库的资产列 662predicates.beginWrap().equalTo("id", "id1").and().equalTo("asset", asset).endWrap(); 663 664if (store != undefined) { 665 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, predicates, (progressDetail: relationalStore.ProgressDetails) => { 666 console.info(`progress: ${progressDetail.schedule}`); 667 }, (err) => { 668 if (err) { 669 console.error(`cloud sync failed, code is ${err.code}, message is ${err.message}`); 670 return; 671 } 672 console.info('cloud sync succeeded'); 673 }); 674}; 675``` 676 677### cloudSync<sup>11+</sup> 678 679cloudSync(mode: SyncMode, predicates: RdbPredicates, progress: Callback<ProgressDetails>): Promise<void> 680 681手动执行按条件进行端云同步,使用Promise异步处理。使用该接口需要实现云同步功能。 682 683> **说明:** 684> 685> 从API version 18开始,手动执行端云同步时,设置谓词条件时新增支持指定资产下载能力。此时,同步模式需要设置为`relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST`。 686> 687> 谓词中支持使用主键(必填)和资产(可选)作为同步条件:选择资产作为同步条件时,谓词仅支持[equalTo](arkts-apis-data-relationalStore-RdbPredicates.md#equalto);指定资产的数量较多时(最多支持指定50个资产),建议谓词中仅使用主键作为同步条件。 688 689**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 690 691**系统接口:** 此接口为系统接口。 692 693**参数:** 694 695| 参数名 | 类型 | 必填 | 说明 | 696|------------|---------------------------------| ---- |---------------------| 697| mode | [SyncMode](arkts-apis-data-relationalStore-e.md#syncmode) | 是 | 表示数据库的同步模式。 | 698| predicates | [RdbPredicates](arkts-apis-data-relationalStore-RdbPredicates.md) | 是 | 表示同步数据的谓词条件。 | 699| progress | Callback<[ProgressDetails](arkts-apis-data-relationalStore-i.md#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 700 701**返回值**: 702 703| 类型 | 说明 | 704| ------------------- | --------------------------------------- | 705| Promise<void> | Promise对象,用于向调用者发送同步结果。 | 706 707**错误码:** 708 709以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 710 711| **错误码ID** | **错误信息** | 712|-----------|---------------------------| 713| 202 | if permission verification failed, application which is not a system application uses system API. | 714| 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. | 715| 801 | Capability not supported. | 716| 14800014 | The RdbStore or ResultSet is already closed. | 717 718**示例1:手动同步,同步模式为云端同步到本地设备** 719 720```ts 721import { BusinessError } from '@kit.BasicServicesKit'; 722 723let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 724predicates.in("id", ["id1", "id2"]); 725 726if (store != undefined) { 727 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, predicates, (progressDetail: relationalStore.ProgressDetails) => { 728 console.info(`progress: ${progressDetail.schedule}`); 729 }).then(() => { 730 console.info('cloud sync succeeded'); 731 }).catch((err: BusinessError) => { 732 console.error(`cloud sync failed, code is ${err.code}, message is ${err.message}`); 733 }); 734}; 735``` 736**示例2:指定资产下载** 737```ts 738import { BusinessError } from '@kit.BasicServicesKit'; 739 740let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 741let asset: relationalStore.Asset = { 742 name: "name", 743 uri: "uri", 744 path: "path", 745 createTime: new Date().getTime().toString(), 746 modifyTime: new Date().getTime().toString(), 747 size: "1024" 748}; 749// 谓词条件中指定主键和资产,asset为数据库的资产列 750predicates.beginWrap().equalTo("id", "id1").and().equalTo("asset", asset).endWrap(); 751 752if (store != undefined) { 753 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, predicates, (progressDetail: relationalStore.ProgressDetails) => { 754 console.info(`progress: ${progressDetail.schedule}`); 755 }).then(() => { 756 console.info('Cloud sync succeeded'); 757 }).catch((err: BusinessError) => { 758 console.error(`cloudSync failed, code is ${err.code},message is ${err.message}}`); 759 }); 760}; 761``` 762 763### querySharingResource<sup>11+</sup> 764 765querySharingResource(predicates: RdbPredicates, columns?: Array<string>): Promise<ResultSet> 766 767根据谓词条件匹配的数据记录查找对应记录的共享资源标识,返回查找的结果集。如果指定了列字段,则返回结果集中同时包含对应列的字段值,使用Promise异步回调。 768 769**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 770 771**系统接口:** 此接口为系统接口。 772 773**参数:** 774 775| 参数名 | 类型 | 必填 | 说明 | 776| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 777| predicates | [RdbPredicates](arkts-apis-data-relationalStore-RdbPredicates.md) | 是 | 表示查询的谓词条件。 | 778| columns | Array<string> | 否 | 表示要查找的列字段名。此参数不填时,返回的结果集中只包含共享资源标识字段。 | 779 780**返回值:** 781 782| 参数名 | 说明 | 783| -------- | ------------------------------------------------- | 784| Promise<[ResultSet](arkts-apis-data-relationalStore-ResultSet.md)> | Promise对象,返回查询的结果集。 | 785 786**错误码:** 787 788以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 789 790| **错误码ID** | **错误信息** | 791|-----------|-------------| 792| 401 | Parameter error. Possible causes: 1. Need 1 - 3 parameter(s)! 2. The RdbStore must be not nullptr. 3. The predicates must be an RdbPredicates. 4. The columns must be a string array. | 793| 801 | Capability not supported. | 794| 14800000 | Inner error. | 795| 14800011 | Failed to open the database because it is corrupted. | 796| 14800014 | The RdbStore or ResultSet is already closed. | 797| 14800015 | The database does not respond. | 798| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 799| 14800022 | SQLite: Callback routine requested an abort. | 800| 14800023 | SQLite: Access permission denied. | 801| 14800024 | SQLite: The database file is locked. | 802| 14800025 | SQLite: A table in the database is locked. | 803| 14800026 | SQLite: The database is out of memory. | 804| 14800027 | SQLite: Attempt to write a readonly database. | 805| 14800028 | SQLite: Some kind of disk I/O error occurred. | 806| 14800029 | SQLite: The database is full. | 807| 14800030 | SQLite: Unable to open the database file. | 808| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 809| 14800032 | SQLite: Abort due to constraint violation. | 810| 14800033 | SQLite: Data type mismatch. | 811| 14800034 | SQLite: Library used incorrectly. | 812 813**示例:** 814 815```ts 816import { BusinessError } from '@kit.BasicServicesKit'; 817 818let sharingResource: string; 819let predicates = new relationalStore.RdbPredicates('test_table'); 820predicates.equalTo('data', 'data_test'); 821if (store != undefined) { 822 (store as relationalStore.RdbStore).querySharingResource(predicates, ['uuid', 'data']).then((resultSet) => { 823 if (!resultSet.goToFirstRow()) { 824 console.error(`resultSet error`); 825 return; 826 } 827 const res = resultSet.getString(resultSet.getColumnIndex(relationalStore.Field.SHARING_RESOURCE_FIELD)); 828 console.info(`sharing resource: ${res}`); 829 sharingResource = res; 830 }).catch((err: BusinessError) => { 831 console.error(`query sharing resource failed, code is ${err.code},message is ${err.message}`); 832 }); 833} 834``` 835 836### querySharingResource<sup>11+</sup> 837 838querySharingResource(predicates: RdbPredicates, callback: AsyncCallback<ResultSet>): void 839 840根据谓词条件匹配的数据记录查找对应记录的共享资源,返回查找的结果集,使用callback异步回调。 841 842**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 843 844**系统接口:** 此接口为系统接口。 845 846**参数:** 847 848| 参数名 | 类型 | 必填 | 说明 | 849| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 850| predicates | [RdbPredicates](arkts-apis-data-relationalStore-RdbPredicates.md) | 是 | 表示查询的谓词条件。 | 851| callback | AsyncCallback<[ResultSet](arkts-apis-data-relationalStore-ResultSet.md)> | 是 | 回调函数。返回查询的结果集。 | 852 853**错误码:** 854 855以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 856 857| **错误码ID** | **错误信息** | 858|-----------|------| 859| 401 | Parameter error. Possible causes: 1. Need 1 - 3 parameter(s)! 2. The RdbStore must be not nullptr. 3. The predicates must be an RdbPredicates. 4. The columns must be a string array. | 860| 801 | Capability not supported. | 861| 14800000 | Inner error. | 862| 14800011 | Failed to open the database because it is corrupted. | 863| 14800014 | The RdbStore or ResultSet is already closed. | 864| 14800015 | The database does not respond. | 865| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 866| 14800022 | SQLite: Callback routine requested an abort. | 867| 14800023 | SQLite: Access permission denied. | 868| 14800024 | SQLite: The database file is locked. | 869| 14800025 | SQLite: A table in the database is locked. | 870| 14800026 | SQLite: The database is out of memory. | 871| 14800027 | SQLite: Attempt to write a readonly database. | 872| 14800028 | SQLite: Some kind of disk I/O error occurred. | 873| 14800029 | SQLite: The database is full. | 874| 14800030 | SQLite: Unable to open the database file. | 875| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 876| 14800032 | SQLite: Abort due to constraint violation. | 877| 14800033 | SQLite: Data type mismatch. | 878| 14800034 | SQLite: Library used incorrectly. | 879 880 881**示例:** 882 883```ts 884let sharingResource: string; 885let predicates = new relationalStore.RdbPredicates('test_table'); 886predicates.equalTo('data', 'data_test'); 887if (store != undefined) { 888 (store as relationalStore.RdbStore).querySharingResource(predicates, (err, resultSet) => { 889 if (err) { 890 console.error(`sharing resource failed, code is ${err.code},message is ${err.message}`); 891 return; 892 } 893 if (!resultSet.goToFirstRow()) { 894 console.error(`resultSet error`); 895 return; 896 } 897 const res = resultSet.getString(resultSet.getColumnIndex(relationalStore.Field.SHARING_RESOURCE_FIELD)); 898 console.info(`sharing resource: ${res}`); 899 sharingResource = res; 900 }); 901} 902``` 903 904### querySharingResource<sup>11+</sup> 905 906querySharingResource(predicates: RdbPredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>): void 907 908根据谓词条件匹配的数据记录查找对应记录的共享资源,返回查找到的共享资源的结果集,同时在结果集中返回谓词条件匹配的指定列名的字段值,使用callback异步回调。 909 910**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 911 912**系统接口:** 此接口为系统接口。 913 914**参数:** 915 916| 参数名 | 类型 | 必填 | 说明 | 917| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 918| predicates | [RdbPredicates](arkts-apis-data-relationalStore-RdbPredicates.md) | 是 | 表示查询的谓词条件。 | 919| columns | Array<string> | 是 | 表示要查找的列字段名。 | 920| callback | AsyncCallback<[ResultSet](arkts-apis-data-relationalStore-ResultSet.md)> | 是 | 回调函数。返回查询的结果集。 | 921 922**错误码:** 923 924以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 925 926| **错误码ID** | **错误信息** | 927|-----------|--------------| 928| 401 | Parameter error. Possible causes: 1. Need 1 - 3 parameter(s)! 2. The RdbStore must be not nullptr. 3. The predicates must be an RdbPredicates. 4. The columns must be a string array. | 929| 801 | Capability not supported. | 930| 14800000 | Inner error. | 931| 14800011 | Failed to open the database because it is corrupted. | 932| 14800014 | The RdbStore or ResultSet is already closed. | 933| 14800015 | The database does not respond. | 934| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 935| 14800022 | SQLite: Callback routine requested an abort. | 936| 14800023 | SQLite: Access permission denied. | 937| 14800024 | SQLite: The database file is locked. | 938| 14800025 | SQLite: A table in the database is locked. | 939| 14800026 | SQLite: The database is out of memory. | 940| 14800027 | SQLite: Attempt to write a readonly database. | 941| 14800028 | SQLite: Some kind of disk I/O error occurred. | 942| 14800029 | SQLite: The database is full. | 943| 14800030 | SQLite: Unable to open the database file. | 944| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 945| 14800032 | SQLite: Abort due to constraint violation. | 946| 14800033 | SQLite: Data type mismatch. | 947| 14800034 | SQLite: Library used incorrectly. | 948 949 950**示例:** 951 952```ts 953let sharingResource: string; 954let predicates = new relationalStore.RdbPredicates('test_table'); 955predicates.equalTo('data', 'data_test'); 956if (store != undefined) { 957 (store as relationalStore.RdbStore).querySharingResource(predicates, ['uuid', 'data'], (err, resultSet) => { 958 if (err) { 959 console.error(`sharing resource failed, code is ${err.code},message is ${err.message}`); 960 return; 961 } 962 if (!resultSet.goToFirstRow()) { 963 console.error(`resultSet error`); 964 return; 965 } 966 const res = resultSet.getString(resultSet.getColumnIndex(relationalStore.Field.SHARING_RESOURCE_FIELD)); 967 console.info(`sharing resource: ${res}`); 968 sharingResource = res; 969 }); 970} 971``` 972 973 974### lockCloudContainer<sup>12+</sup> 975 976lockCloudContainer(): Promise<number> 977 978手动对应用云端数据库加锁,使用Promise异步处理。 979 980> **说明:** 981> 982> 若手动加锁成功,则其他同账户设备的同应用禁止同步到云端。使用该接口需要实现云同步功能。 983 984**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 985 986**系统接口:** 此接口为系统接口。 987 988**返回值**: 989 990| 类型 | 说明 | 991| ------------------- | ---------------------------------------| 992| Promise<number> | Promise对象,如果加锁成功,返回锁的有效时长;如果加锁失败,返回0,单位:ms。 | 993 994**错误码:** 995 996以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 997 998| **错误码ID** | **错误信息** | 999|-----------|---------------------------| 1000| 202 | Permission verification failed, application which is not a system application uses system API. | 1001 1002**示例:** 1003 1004```ts 1005import { BusinessError } from '@kit.BasicServicesKit'; 1006 1007if (store != undefined) { 1008 (store as relationalStore.RdbStore).lockCloudContainer().then((time: number) => { 1009 console.info('lockCloudContainer succeeded time:' + time); 1010 }).catch((err: BusinessError) => { 1011 console.error(`lockCloudContainer failed, code is ${err.code},message is ${err.message}`); 1012 }); 1013} 1014``` 1015 1016### unlockCloudContainer<sup>12+</sup> 1017 1018unlockCloudContainer(): Promise<void> 1019 1020手动对应用云端数据库解锁,使用Promise异步处理。使用该接口需要实现云同步功能。 1021 1022**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1023 1024**系统接口:** 此接口为系统接口。 1025 1026**返回值**: 1027 1028| 类型 | 说明 | 1029| ------------------- | --------------------------------------- | 1030| Promise<void> | 无返回结果的Promise对象。 | 1031 1032**错误码:** 1033 1034以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1035 1036| **错误码ID** | **错误信息** | 1037|-----------|---------------------------| 1038| 202 | Permission verification failed, application which is not a system application uses system API. | 1039 1040**示例:** 1041 1042```ts 1043import { BusinessError } from '@kit.BasicServicesKit'; 1044 1045if (store != undefined) { 1046 (store as relationalStore.RdbStore).unlockCloudContainer().then(() => { 1047 console.info('unlockCloudContainer succeeded'); 1048 }).catch((err: BusinessError) => { 1049 console.error(`unlockCloudContainer failed, code is ${err.code},message is ${err.message}`); 1050 }); 1051} 1052``` 1053 1054### restore<sup>12+</sup> 1055 1056restore(): Promise<void> 1057 1058从副本关系型数据库文件恢复数据库,使用Promise异步回调。此接口仅供[HAMode](#hamode12)为MAIN_REPLICA时使用,且不支持在事务中使用。 1059 1060**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1061 1062**系统接口:** 此接口为系统接口。 1063 1064**返回值**: 1065 1066| 类型 | 说明 | 1067| ------------------- | ------------------------- | 1068| Promise<void> | 无返回结果的Promise对象。 | 1069 1070**错误码:** 1071 1072以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 1073 1074| **错误码ID** | **错误信息** | 1075|-----------| ------------------------------------------------------------ | 1076| 202 | Permission verification failed, application which is not a system application uses system API. | 1077| 14800000 | Inner error. | 1078| 14800010 | Failed to open or delete the database by an invalid database path. | 1079| 14800011 | Failed to open the database because it is corrupted. | 1080| 14800014 | The RdbStore or ResultSet is already closed. | 1081| 14800015 | The database does not respond. | 1082| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 1083| 14800022 | SQLite: Callback routine requested an abort. | 1084| 14800023 | SQLite: Access permission denied. | 1085| 14800024 | SQLite: The database file is locked. | 1086| 14800025 | SQLite: A table in the database is locked. | 1087| 14800026 | SQLite: The database is out of memory. | 1088| 14800027 | SQLite: Attempt to write a readonly database. | 1089| 14800028 | SQLite: Some kind of disk I/O error occurred. | 1090| 14800029 | SQLite: The database is full. | 1091| 14800030 | SQLite: Unable to open the database file. | 1092| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 1093| 14800032 | SQLite: Abort due to constraint violation. | 1094| 14800033 | SQLite: Data type mismatch. | 1095| 14800034 | SQLite: Library used incorrectly. | 1096 1097**示例:** 1098 1099```ts 1100import { BusinessError } from '@kit.BasicServicesKit'; 1101 1102if (store != undefined) { 1103 let promiseRestore = (store as relationalStore.RdbStore).restore(); 1104 promiseRestore.then(() => { 1105 console.info('Succeeded in restoring.'); 1106 }).catch((err: BusinessError) => { 1107 console.error(`Failed to restore, code is ${err.code},message is ${err.message}`); 1108 }); 1109} 1110``` 1111 1112## ResultSet 1113 1114提供通过查询数据库生成的数据库结果集的访问方法。结果集是指用户调用关系型数据库查询接口之后返回的结果集合,提供了多种灵活的数据访问方式,以便用户获取各项数据。 1115 1116### getFloat32Array<sup>12+</sup> 1117 1118getFloat32Array(columnIndex: number): Float32Array 1119 1120以浮点数组的形式获取当前行中指定列的值,仅可在[向量数据库](#storeconfig)下可用。 1121 1122**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1123 1124**参数:** 1125 1126| 参数名 | 类型 | 必填 | 说明 | 1127| ----------- | ------ | ---- | ----------------------- | 1128| columnIndex | number | 是 | 指定的列索引,从0开始。 | 1129 1130**返回值:** 1131 1132| 类型 | 说明 | 1133| ---------- | -------------------------------- | 1134| Float32Array | 以浮点数组的形式返回指定列的值。 | 1135 1136**错误码:** 1137 1138以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 1139 1140| **错误码ID** | **错误信息** | 1141|-----------| ------------ | 1142| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1143| 801 | The capability is not supported because the database is not a vector DB. | 1144| 14800011 | Failed to open the database because it is corrupted. | 1145| 14800013 | ResultSet is empty or column index is out of bounds. | 1146| 14800014 | The RdbStore or ResultSet is already closed. | 1147| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 1148| 14800022 | SQLite: Callback routine requested an abort. | 1149| 14800023 | SQLite: Access permission denied. | 1150| 14800024 | SQLite: The database file is locked. | 1151| 14800025 | SQLite: A table in the database is locked. | 1152| 14800026 | SQLite: The database is out of memory. | 1153| 14800027 | SQLite: Attempt to write a readonly database. | 1154| 14800028 | SQLite: Some kind of disk I/O error occurred. | 1155| 14800029 | SQLite: The database is full. | 1156| 14800030 | SQLite: Unable to open the database file. | 1157| 14800031 | SQLite: TEXT or BLOB exceeds size limit. | 1158| 14800032 | SQLite: Abort due to constraint violation. | 1159| 14800033 | SQLite: Data type mismatch. | 1160| 14800034 | SQLite: Library used incorrectly. | 1161 1162**示例:** 1163 1164```ts 1165let resultSet: relationalStore.ResultSet | undefined; 1166if (resultSet != undefined) { 1167 const id = (resultSet as relationalStore.ResultSet).getFloat32Array(0); 1168} 1169```