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