1# Functions 2<!--Kit: ArkData--> 3<!--Subsystem: DistributedDataManager--> 4<!--Owner: @baijidong--> 5<!--Designer: @widecode; @htt1997--> 6<!--Tester: @yippo; @logic42--> 7<!--Adviser: @ge-yafang--> 8 9> **说明:** 10> 11> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12 13## 导入模块 14 15```ts 16import { relationalStore } from '@kit.ArkData'; 17``` 18 19## relationalStore.getRdbStore 20 21getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback<RdbStore>): void 22 23创建或打开已有的关系型数据库,开发者可以根据自己的需求配置config参数,然后通过RdbStore调用相关接口执行数据操作。使用callback异步回调。 24 25对应沙箱路径下无数据库文件时,将创建数据库文件,文件创建位置详见[StoreConfig](arkts-apis-data-relationalStore-i.md#storeconfig)。对应路径下已有数据库文件时,将打开已有数据库文件。 26 27开发者在创建数据库时,应谨慎配置是否进行数据库加密的参数[encrypt](arkts-apis-data-relationalStore-i.md#storeconfig),数据库创建后,禁止对该参数进行修改。 28 29| 当前开库的加密类型 | 本设备上创建该数据库时的加密类型 | 结果 | 30| ------- | -------------------------------- | ---- | 31| 非加密 | 加密 | 将数据库以加密方式打开。 | 32| 加密 | 非加密 | 将数据库以非加密方式打开。 | 33 34getRdbStore目前不支持多线程并发操作。 35 36**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 37 38**参数:** 39 40| 参数名 | 类型 | 必填 | 说明 | 41| -------- | ---------------------------------------------- | ---- | ------------------------------------------------------------ | 42| context | Context | 是 | 应用的上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 43| config | [StoreConfig](arkts-apis-data-relationalStore-i.md#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 44| callback | AsyncCallback<[RdbStore](arkts-apis-data-relationalStore-RdbStore.md)> | 是 | 指定callback回调函数,返回RdbStore对象。 | 45 46**错误码:** 47 48以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 49 50| **错误码ID** | **错误信息** | 51|-----------|---------| 52| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 53| 14800000 | Inner error. | 54| 14800010 | Failed to open or delete the database by an invalid database path. | 55| 14800011 | Failed to open the database because it is corrupted. | 56| 14801001 | The operation is supported in the stage model only. | 57| 14801002 | Invalid data group ID. | 58| 14800017 | StoreConfig is changed. | 59| 14800020 | The secret key is corrupted or lost. | 60| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 61| 14800022 | SQLite: Callback routine requested an abort. | 62| 14800023 | SQLite: Access permission denied. | 63| 14800027 | SQLite: Attempt to write a readonly database. | 64| 14800028 | SQLite: Some kind of disk I/O error occurred. | 65| 14800029 | SQLite: The database is full. | 66| 14800030 | SQLite: Unable to open the database file. | 67 68**示例:** 69 70FA模型示例: 71 72<!--code_no_check_fa--> 73```js 74import { featureAbility } from '@kit.AbilityKit'; 75import { BusinessError } from '@kit.BasicServicesKit'; 76 77let store: relationalStore.RdbStore | undefined = undefined; 78let context = featureAbility.getContext(); 79 80const STORE_CONFIG: relationalStore.StoreConfig = { 81 name: "RdbTest.db", 82 securityLevel: relationalStore.SecurityLevel.S3 83}; 84 85relationalStore.getRdbStore(context, STORE_CONFIG, async (err: BusinessError, rdbStore: relationalStore.RdbStore) => { 86 if (err) { 87 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 88 return; 89 } 90 console.info('Get RdbStore successfully.'); 91 store = rdbStore; 92 // 成功获取到 rdbStore 后执行后续操作 93}); 94``` 95 96Stage模型示例: 97 98```ts 99import { UIAbility } from '@kit.AbilityKit'; 100import { window } from '@kit.ArkUI'; 101import { BusinessError } from '@kit.BasicServicesKit'; 102 103let store: relationalStore.RdbStore | undefined = undefined; 104 105class EntryAbility extends UIAbility { 106 onWindowStageCreate(windowStage: window.WindowStage) { 107 const STORE_CONFIG: relationalStore.StoreConfig = { 108 name: "RdbTest.db", 109 securityLevel: relationalStore.SecurityLevel.S3 110 }; 111 112 relationalStore.getRdbStore(this.context, STORE_CONFIG, async (err: BusinessError, rdbStore: relationalStore.RdbStore) => { 113 if (err) { 114 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 115 return; 116 } 117 console.info('Get RdbStore successfully.'); 118 store = rdbStore; 119 // 成功获取到 rdbStore 后执行后续操作 120 }); 121 } 122} 123``` 124 125## relationalStore.getRdbStore 126 127getRdbStore(context: Context, config: StoreConfig): Promise<RdbStore> 128 129创建或打开已有的关系型数据库,开发者可以根据自己的需求配置config参数,然后通过RdbStore调用相关接口执行数据操作。使用Promise异步回调。 130 131对应沙箱路径下无数据库文件时,将创建数据库文件,文件创建位置详见[StoreConfig](arkts-apis-data-relationalStore-i.md#storeconfig)。对应路径下已有数据库文件时,将打开已有数据库文件。 132 133开发者在创建数据库时,应谨慎配置是否进行数据库加密的参数[encrypt](arkts-apis-data-relationalStore-i.md#storeconfig),数据库创建后,禁止对该参数进行修改。 134 135| 当前开库的加密类型 | 本设备上创建该数据库时的加密类型 | 结果 | 136| ------- | -------------------------------- | ---- | 137| 非加密 | 加密 | 将数据库以加密方式打开。 | 138| 加密 | 非加密 | 将数据库以非加密方式打开。 | 139 140getRdbStore目前不支持多线程并发操作。 141 142**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 143 144**参数:** 145 146| 参数名 | 类型 | 必填 | 说明 | 147| ------- | -------------------------------- | ---- | ------------------------------------------------------------ | 148| context | Context | 是 | 应用的上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 149| config | [StoreConfig](arkts-apis-data-relationalStore-i.md#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 150 151**返回值**: 152 153| 类型 | 说明 | 154| ----------------------------------------- | --------------------------------- | 155| Promise<[RdbStore](arkts-apis-data-relationalStore-RdbStore.md)> | Promise对象。返回RdbStore对象。 | 156 157**错误码:** 158 159以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。其中,14800011错误码处理可参考[数据库备份与恢复](../../database/data-backup-and-restore.md)。 160 161| **错误码ID** | **错误信息** | 162|-----------| ------------------------------------------------------------ | 163| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 164| 14800000 | Inner error. | 165| 14800010 | Failed to open or delete the database by an invalid database path. | 166| 14800011 | Failed to open the database because it is corrupted. | 167| 14801001 | The operation is supported in the stage model only. | 168| 14801002 | Invalid data group ID. | 169| 14800017 | StoreConfig is changed. | 170| 14800020 | The secret key is corrupted or lost. | 171| 14800021 | SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist. | 172| 14800022 | SQLite: Callback routine requested an abort. | 173| 14800023 | SQLite: Access permission denied. | 174| 14800027 | SQLite: Attempt to write a readonly database. | 175| 14800028 | SQLite: Some kind of disk I/O error occurred. | 176| 14800029 | SQLite: The database is full. | 177| 14800030 | SQLite: Unable to open the database file. | 178 179**示例:** 180 181FA模型示例: 182 183<!--code_no_check_fa--> 184```js 185import { featureAbility } from '@kit.AbilityKit'; 186import { BusinessError } from '@kit.BasicServicesKit'; 187 188let store: relationalStore.RdbStore | undefined = undefined; 189let context = featureAbility.getContext(); 190 191const STORE_CONFIG: relationalStore.StoreConfig = { 192 name: "RdbTest.db", 193 securityLevel: relationalStore.SecurityLevel.S3 194}; 195 196relationalStore.getRdbStore(context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => { 197 store = rdbStore; 198 console.info('Get RdbStore successfully.'); 199}).catch((err: BusinessError) => { 200 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 201}); 202``` 203 204Stage模型示例: 205 206```ts 207import { UIAbility } from '@kit.AbilityKit'; 208import { window } from '@kit.ArkUI'; 209import { BusinessError } from '@kit.BasicServicesKit'; 210 211let store: relationalStore.RdbStore | undefined = undefined; 212 213class EntryAbility extends UIAbility { 214 onWindowStageCreate(windowStage: window.WindowStage) { 215 const STORE_CONFIG: relationalStore.StoreConfig = { 216 name: "RdbTest.db", 217 securityLevel: relationalStore.SecurityLevel.S3 218 }; 219 220 relationalStore.getRdbStore(this.context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => { 221 store = rdbStore; 222 console.info('Get RdbStore successfully.'); 223 }).catch((err: BusinessError) => { 224 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 225 }); 226 } 227} 228``` 229 230## relationalStore.deleteRdbStore 231 232deleteRdbStore(context: Context, name: string, callback: AsyncCallback<void>): void 233 234删除数据库文件,使用callback异步回调。 235 236删除成功后,建议将数据库对象置为null。建立数据库时,若在[StoreConfig](arkts-apis-data-relationalStore-i.md#storeconfig)中配置了自定义路径,则调用此接口进行删库无效,必须使用 [deleteRdbStore](#relationalstoredeleterdbstore10) 接口进行删库。 237 238当使用向量数据库时,在调用deleteRdbStore接口前,应当确保向量数据库已打开的RdbStore和ResultSet均已成功关闭。 239 240**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 241 242**参数:** 243 244| 参数名 | 类型 | 必填 | 说明 | 245| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 246| context | Context | 是 | 应用的上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 247| name | string | 是 | 数据库名称。 | 248| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 249 250**错误码:** 251 252以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 253 254| **错误码ID** | **错误信息** | 255|-----------|---------------------------------------| 256| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 257| 14800000 | Inner error. | 258| 14800010 | Failed to open or delete the database by an invalid database path. | 259 260**示例:** 261 262FA模型示例: 263 264<!--code_no_check_fa--> 265```js 266import { featureAbility } from '@kit.AbilityKit'; 267import { BusinessError } from '@kit.BasicServicesKit'; 268 269let context = featureAbility.getContext(); 270 271relationalStore.deleteRdbStore(context, "RdbTest.db", (err: BusinessError) => { 272 if (err) { 273 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 274 return; 275 } 276 // 数据库删除成功后,已初始化的RdbStore实例将无法继续使用。 277 // 及时将相关变量置空以释放资源。 278 console.info('Delete RdbStore successfully.'); 279}); 280``` 281 282Stage模型示例: 283 284```ts 285import { UIAbility } from '@kit.AbilityKit'; 286import { window } from '@kit.ArkUI'; 287import { BusinessError } from '@kit.BasicServicesKit'; 288 289class EntryAbility extends UIAbility { 290 onWindowStageCreate(windowStage: window.WindowStage) { 291 relationalStore.deleteRdbStore(this.context, "RdbTest.db", (err: BusinessError) => { 292 if (err) { 293 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 294 return; 295 } 296 // 数据库删除成功后,已初始化的RdbStore实例将无法继续使用。 297 // 及时将相关变量置空以释放资源。 298 console.info('Delete RdbStore successfully.'); 299 }); 300 } 301} 302``` 303 304## relationalStore.deleteRdbStore 305 306deleteRdbStore(context: Context, name: string): Promise<void> 307 308使用指定的数据库文件配置删除数据库,使用Promise异步回调。 309 310删除成功后,建议将数据库对象置为null。建立数据库时,若在[StoreConfig](arkts-apis-data-relationalStore-i.md#storeconfig)中配置了自定义路径,则调用此接口进行删库无效,必须使用 [deleteRdbStore](#relationalstoredeleterdbstore10-1) 接口进行删库。 311 312当使用向量数据库时,在调用deleteRdbStore接口前,应当确保向量数据库已打开的RdbStore和ResultSet均已成功关闭。 313 314**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 315 316**参数** 317 318| 参数名 | 类型 | 必填 | 说明 | 319| ------- | ------- | ---- | ------------------------------------------------------------ | 320| context | Context | 是 | 应用的上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 321| name | string | 是 | 数据库名称。 | 322 323**返回值**: 324 325| 类型 | 说明 | 326| ------------------- | ------------------------- | 327| Promise<void> | 无返回结果的Promise对象。 | 328 329**错误码:** 330 331以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 332 333| **错误码ID** | **错误信息** | 334|-----------|----------------------------------------------------------------------------------| 335| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 336| 14800000 | Inner error. | 337| 14800010 | Failed to open or delete the database by an invalid database path. | 338 339**示例:** 340 341FA模型示例: 342 343<!--code_no_check_fa--> 344```js 345import { featureAbility } from '@kit.AbilityKit'; 346import { BusinessError } from '@kit.BasicServicesKit'; 347 348let context = featureAbility.getContext(); 349 350relationalStore.deleteRdbStore(context, "RdbTest.db").then(() => { 351 // 数据库删除成功后,已初始化的RdbStore实例将无法继续使用。 352 // 及时将相关变量置空以释放资源。 353 console.info('Delete RdbStore successfully.'); 354}).catch((err: BusinessError) => { 355 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 356}); 357``` 358 359Stage模型示例: 360 361```ts 362import { UIAbility } from '@kit.AbilityKit'; 363import { window } from '@kit.ArkUI'; 364import { BusinessError } from '@kit.BasicServicesKit'; 365 366class EntryAbility extends UIAbility { 367 onWindowStageCreate(windowStage: window.WindowStage) { 368 relationalStore.deleteRdbStore(this.context, "RdbTest.db").then(() => { 369 // 数据库删除成功后,已初始化的RdbStore实例将无法继续使用。 370 // 及时将相关变量置空以释放资源。 371 console.info('Delete RdbStore successfully.'); 372 }).catch((err: BusinessError) => { 373 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 374 }); 375 } 376} 377``` 378 379## relationalStore.deleteRdbStore<sup>10+</sup> 380 381deleteRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback\<void>): void 382 383使用指定的数据库文件配置删除数据库,使用callback异步回调。 384 385删除成功后,建议将数据库对象置为null。若数据库文件处于公共沙箱目录下,则删除数据库时必须使用该接口,当存在多个进程操作同一个数据库的情况,建议向其他进程发送数据库删除通知使其感知并处理。建立数据库时,若在[StoreConfig](arkts-apis-data-relationalStore-i.md#storeconfig)中配置了自定义路径,则必须调用此接口进行删库。 386 387当使用向量数据库时,在调用deleteRdbStore接口前,应当确保向量数据库已打开的RdbStore和ResultSet均已成功关闭。 388 389**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 390 391**参数:** 392 393| 参数名 | 类型 | 必填 | 说明 | 394| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 395| context | Context | 是 | 应用的上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 396| config | [StoreConfig](arkts-apis-data-relationalStore-i.md#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 397| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 398 399**错误码:** 400 401以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[关系型数据库错误码](errorcode-data-rdb.md)。 402 403| **错误码ID** | **错误信息** | 404|-----------|----------| 405| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 406| 14800000 | Inner error. | 407| 14800010 | Failed to open or delete the database by an invalid database path. | 408| 14801001 | The operation is supported in the stage model only. | 409| 14801002 | Invalid data group ID. | 410 411**示例:** 412 413FA模型示例: 414 415<!--code_no_check_fa--> 416```js 417import { featureAbility } from '@kit.AbilityKit'; 418import { BusinessError } from '@kit.BasicServicesKit'; 419 420let context = featureAbility.getContext(); 421 422const STORE_CONFIG: relationalStore.StoreConfig = { 423 name: "RdbTest.db", 424 securityLevel: relationalStore.SecurityLevel.S3 425}; 426 427relationalStore.deleteRdbStore(context, STORE_CONFIG, (err: BusinessError) => { 428 if (err) { 429 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 430 return; 431 } 432 // 数据库删除成功后,已初始化的RdbStore实例将无法继续使用。 433 // 及时将相关变量置空以释放资源。 434 console.info('Delete RdbStore successfully.'); 435}); 436``` 437 438Stage模型示例: 439 440```ts 441import { UIAbility } from '@kit.AbilityKit'; 442import { window } from '@kit.ArkUI'; 443import { BusinessError } from '@kit.BasicServicesKit'; 444 445class EntryAbility extends UIAbility { 446 onWindowStageCreate(windowStage: window.WindowStage) { 447 const STORE_CONFIG: relationalStore.StoreConfig = { 448 name: "RdbTest.db", 449 securityLevel: relationalStore.SecurityLevel.S3 450 }; 451 relationalStore.deleteRdbStore(this.context, STORE_CONFIG, (err: BusinessError) => { 452 if (err) { 453 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 454 return; 455 } 456 // 数据库删除成功后,已初始化的RdbStore实例将无法继续使用。 457 // 及时将相关变量置空以释放资源。 458 console.info('Delete RdbStore successfully.'); 459 }); 460 } 461} 462``` 463 464## relationalStore.deleteRdbStore<sup>10+</sup> 465 466deleteRdbStore(context: Context, config: StoreConfig): Promise\<void> 467 468使用指定的数据库文件配置删除数据库,使用Promise异步回调。 469 470删除成功后,建议将数据库对象置为null。若数据库文件处于公共沙箱目录下,则删除数据库时必须使用该接口,当存在多个进程操作同一个数据库的情况,建议向其他进程发送数据库删除通知使其感知并处理。建立数据库时,若在[StoreConfig](arkts-apis-data-relationalStore-i.md#storeconfig)中配置了自定义路径,则必须调用此接口进行删库。 471 472当使用向量数据库时,在调用deleteRdbStore接口前,应当确保向量数据库已打开的RdbStore和ResultSet均已成功关闭。 473 474**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 475 476**参数** 477 478| 参数名 | 类型 | 必填 | 说明 | 479| ------- | --------------------------- | ---- | ------------------------------------------------------------ | 480| context | Context | 是 | 应用的上下文。<br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 | 481| config | [StoreConfig](arkts-apis-data-relationalStore-i.md#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 482 483**返回值**: 484 485| 类型 | 说明 | 486| ------------------- | ------------------------- | 487| Promise<void> | 无返回结果的Promise对象。 | 488 489**错误码:** 490 491以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 492 493| **错误码ID** | **错误信息** | 494|-----------|---------------------| 495| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 496| 801 | Capability not supported. | 497| 14800000 | Inner error. | 498| 14800010 | Failed to open or delete the database by an invalid database path. | 499| 14801001 | The operation is supported in the stage model only. | 500| 14801002 | Invalid data group ID. | 501 502**示例:** 503 504FA模型示例: 505 506<!--code_no_check_fa--> 507```js 508import { featureAbility } from "@kit.AbilityKit"; 509import { BusinessError } from '@kit.BasicServicesKit'; 510 511let context = featureAbility.getContext(); 512 513const STORE_CONFIG: relationalStore.StoreConfig = { 514 name: "RdbTest.db", 515 securityLevel: relationalStore.SecurityLevel.S3 516}; 517 518relationalStore.deleteRdbStore(context, STORE_CONFIG).then(() => { 519 // 数据库删除成功后,已初始化的RdbStore实例将无法继续使用。 520 // 及时将相关变量置空以释放资源。 521 console.info('Delete RdbStore successfully.'); 522}).catch((err: BusinessError) => { 523 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 524}); 525``` 526 527Stage模型示例: 528 529```ts 530import { UIAbility } from '@kit.AbilityKit'; 531import { window } from '@kit.ArkUI'; 532import { BusinessError } from '@kit.BasicServicesKit'; 533 534class EntryAbility extends UIAbility { 535 onWindowStageCreate(windowStage: window.WindowStage) { 536 const STORE_CONFIG: relationalStore.StoreConfig = { 537 name: "RdbTest.db", 538 securityLevel: relationalStore.SecurityLevel.S3 539 }; 540 relationalStore.deleteRdbStore(this.context, STORE_CONFIG).then(() => { 541 // 数据库删除成功后,已初始化的RdbStore实例将无法继续使用。 542 // 及时将相关变量置空以释放资源。 543 console.info('Delete RdbStore successfully.'); 544 }).catch((err: BusinessError) => { 545 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 546 }); 547 } 548} 549``` 550## relationalStore.isVectorSupported<sup>18+</sup> 551 552isVectorSupported(): boolean 553 554判断系统是否提供向量数据库能力。 555 556**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 557 558**返回值**: 559 560| 类型 | 说明 | 561| ------- | ------------------------------------------------- | 562| boolean | 系统具备向量数据库能力时返回true,否则返回false。 | 563 564**示例:** 565 566```ts 567import { contextConstant, UIAbility } from '@kit.AbilityKit'; 568import { window } from '@kit.ArkUI'; 569import { BusinessError } from '@kit.BasicServicesKit'; 570import { relationalStore } from '@kit.ArkData'; 571 572let store: relationalStore.RdbStore | undefined = undefined; 573export default class EntryAbility extends UIAbility { 574 async onWindowStageCreate(windowStage: window.WindowStage) { 575 let supported = relationalStore.isVectorSupported(); 576 if (supported) { 577 // 支持向量数据库 578 console.info("Vector database supported on current platform."); 579 const STORE_CONFIG: relationalStore.StoreConfig = { 580 name: "VectorTest.db", 581 securityLevel: relationalStore.SecurityLevel.S3, 582 vector: true 583 }; 584 try { 585 const context = this.context.getApplicationContext().createAreaModeContext(contextConstant.AreaMode.EL3); 586 const rdbStore = await relationalStore.getRdbStore(context, STORE_CONFIG); 587 console.info('Get RdbStore successfully.'); 588 store = rdbStore; 589 // 成功获取到 rdbStore 后执行后续操作 590 } catch (error) { 591 const err = error as BusinessError; 592 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 593 } 594 } else { 595 console.info("Vector database not supported on current platform."); 596 } 597 } 598} 599``` 600 601## relationalStore.isTokenizerSupported<sup>18+</sup> 602 603isTokenizerSupported(tokenizer: Tokenizer): boolean 604 605判断当前平台是否支持传入的分词器,此为同步接口。 606 607如果当前平台支持传入的分词器时,此接口返回值为true;反之,返回值为false。 608 609**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 610 611**参数:** 612 613| 参数名 | 类型 | 必填 | 说明 | 614| ------- | --------------------- | ---- | ------------------------------------------------------------ | 615| tokenizer | [Tokenizer](arkts-apis-data-relationalStore-e.md#tokenizer17) | 是 | 需要被判断是否支持的分词器。 | 616 617**返回值:** 618 619| 类型 | 说明 | 620| ------------------- | ------------------------- | 621| boolean | true表示当前平台支持当前传入的分词器,false表示当前平台不支持当前传入的分词器。 | 622 623**错误码:** 624 625以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 626 627| **错误码ID** | **错误信息** | 628|-----------|---------------------| 629| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 630 631 632**示例:** 633 634```ts 635let customType = relationalStore.Tokenizer.CUSTOM_TOKENIZER; 636let customTypeSupported = relationalStore.isTokenizerSupported(customType); 637console.info("custom tokenizer supported on current platform: " + customTypeSupported); 638``` 639 640## relationalStore.getInsertSqlInfo<sup>20+</sup> 641 642getInsertSqlInfo(table: string, values: ValuesBucket, conflict?: ConflictResolution): SqlInfo 643 644获取用于插入数据的SQL语句,此为同步接口。 645 646**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 647 648**参数:** 649 650| 参数名 | 类型 | 必填 | 说明 | 651| ------- | --------------------- | ---- | ------------------------------------------------------------ | 652| table | string | 是 | 要写入数据的数据库表名。 | 653| values | [ValuesBucket](arkts-apis-data-relationalStore-t.md#valuesbucket) | 是 | 要写入数据库中数据的字段信息以及对应的值信息。 | 654| conflict | [ConflictResolution](arkts-apis-data-relationalStore-e.md#conflictresolution10) | 否 |指定冲突解决模式。默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。 | 655 656**返回值:** 657 658| 类型 | 说明 | 659| ------------------- | ------------------------- | 660| [SqlInfo](arkts-apis-data-relationalStore-i.md#sqlinfo20) | SqlInfo对象,其中sql为返回的sql语句,args为执行SQL中的参数信息。 | 661 662**错误码:** 663 664以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 665 666| **错误码ID** | **错误信息** | 667|-----------|---------------------| 668| 14800001 | Invalid arguments. Possible causes: 1. Parameter is out of valid range. | 669 670 671**示例:** 672 673```ts 674const bucket: relationalStore.ValuesBucket = { 675 name: "Logitech", 676 age: 18, 677 sex: "man", 678 desc: "asserter" 679}; 680const sqlInfo: relationalStore.SqlInfo = relationalStore.getInsertSqlInfo( 681 "USER", 682 bucket, 683 relationalStore.ConflictResolution.ON_CONFLICT_NONE 684); 685``` 686 687## relationalStore.getUpdateSqlInfo<sup>20+</sup> 688 689getUpdateSqlInfo(predicates: RdbPredicates, values: ValuesBucket, conflict?: ConflictResolution): SqlInfo 690 691获取用于更新数据的SQL语句,此为同步接口。 692 693**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 694 695**参数:** 696 697| 参数名 | 类型 | 必填 | 说明 | 698| ------- | --------------------- | ---- | ------------------------------------------------------------ | 699| predicates | [RdbPredicates](arkts-apis-data-relationalStore-RdbPredicates.md) | 是 | 与指定字段匹配的谓词。 | 700| values | [ValuesBucket](arkts-apis-data-relationalStore-t.md#valuesbucket) | 是 | 要写入数据库中数据的字段信息以及对应的值信息。 | 701| conflict | [ConflictResolution](arkts-apis-data-relationalStore-e.md#conflictresolution10) | 否 | 指定冲突解决模式。 默认值是relationalStore.ConflictResolution.ON_CONFLICT_NONE。| 702 703**返回值:** 704 705| 类型 | 说明 | 706| ------------------- | ------------------------- | 707| [SqlInfo](arkts-apis-data-relationalStore-i.md#sqlinfo20) | SqlInfo对象,其中sql为返回的sql语句,args为执行SQL中的参数信息。 | 708 709**错误码:** 710 711以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 712 713| **错误码ID** | **错误信息** | 714|-----------|---------------------| 715| 14800001 | Invalid arguments. Possible causes: 1. Parameter is out of valid range. | 716 717 718**示例:** 719 720```ts 721const bucket: relationalStore.ValuesBucket = { 722 name: "Logitech", 723 age: 18, 724 sex: "man", 725 desc: "asserter" 726}; 727const predicates = new relationalStore.RdbPredicates("users"); 728const sqlInfo: relationalStore.SqlInfo = relationalStore.getUpdateSqlInfo( 729 predicates, 730 bucket, 731 relationalStore.ConflictResolution.ON_CONFLICT_NONE 732); 733``` 734 735## relationalStore.getDeleteSqlInfo<sup>20+</sup> 736 737getDeleteSqlInfo(predicates: RdbPredicates): SqlInfo 738 739获取用于删除数据的SQL语句,此为同步接口。 740 741**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 742 743**参数:** 744 745| 参数名 | 类型 | 必填 | 说明 | 746| ------- | --------------------- | ---- | ------------------------------------------------------------ | 747| predicates | [RdbPredicates](arkts-apis-data-relationalStore-RdbPredicates.md) | 是 | 与指定字段匹配的谓词。 | 748 749**返回值:** 750 751| 类型 | 说明 | 752| ------------------- | ------------------------- | 753| [SqlInfo](arkts-apis-data-relationalStore-i.md#sqlinfo20) | SqlInfo对象,其中sql为返回的sql语句,args为执行SQL中的参数信息。 | 754 755**错误码:** 756 757以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 758 759| **错误码ID** | **错误信息** | 760|-----------|---------------------| 761| 14800001 | Invalid arguments. Possible causes: 1. Parameter is out of valid range. | 762 763 764**示例:** 765 766```ts 767const predicates = new relationalStore.RdbPredicates("users"); 768predicates.equalTo("tableName", "a"); 769predicates.notEqualTo("age", 18); 770const sqlInfo: relationalStore.SqlInfo = relationalStore.getDeleteSqlInfo(predicates); 771``` 772 773## relationalStore.getQuerySqlInfo<sup>20+</sup> 774 775getQuerySqlInfo(predicates: RdbPredicates, columns?: Array\<string>): SqlInfo 776 777获取用于查询数据的SQL语句,此为同步接口。 778 779**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 780 781**参数:** 782 783| 参数名 | 类型 | 必填 | 说明 | 784| ------- | --------------------- | ---- | ------------------------------------------------------------ | 785| predicates | [RdbPredicates](arkts-apis-data-relationalStore-RdbPredicates.md) | 是 | 与指定字段匹配的谓词。 | 786| columns | Array\<string> | 否 | 要查询的列;如果不指定此参数,则查询所有列。 | 787 788**返回值:** 789 790| 类型 | 说明 | 791| ------------------- | ------------------------- | 792| [SqlInfo](arkts-apis-data-relationalStore-i.md#sqlinfo20) | SqlInfo对象,其中sql为返回的sql语句,args为执行SQL中的参数信息。 | 793 794**错误码:** 795 796以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 797 798| **错误码ID** | **错误信息** | 799|-----------|---------------------| 800| 14800001 | Invalid arguments. Possible causes: 1. Parameter is out of valid range. | 801 802 803**示例:** 804 805```ts 806const predicates = new relationalStore.RdbPredicates("users"); 807predicates.notEqualTo("age", 18); 808predicates.equalTo("name", "zhangsan"); 809const sqlInfo: relationalStore.SqlInfo = relationalStore.getQuerySqlInfo(predicates); 810``` 811