1# @ohos.data.relationalStore (关系型数据库) 2 3关系型数据库(Relational Database,RDB)是一种基于关系模型来管理数据的数据库。关系型数据库基于SQLite组件提供了一套完整的对本地数据库进行管理的机制,对外提供了一系列的增、删、改、查等接口,也可以直接运行用户输入的SQL语句来满足复杂的场景需要。不支持Worker线程。 4ArkTS侧支持的基本数据类型:number、string、二进制类型数据、boolean。为保证插入并读取数据成功,建议一条数据不要超过2M。超出该大小,插入成功,读取失败。 5 6该模块提供以下关系型数据库相关的常用功能: 7 8- [RdbPredicates](#rdbpredicates): 数据库中用来代表数据实体的性质、特征或者数据实体之间关系的词项,主要用来定义数据库的操作条件。 9- [RdbStore](#rdbstore):提供管理关系数据库(RDB)方法的接口。 10- [ResultSet](#resultset):提供用户调用关系型数据库查询接口之后返回的结果集合。 11 12> **说明:** 13> 14> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15 16## 导入模块 17 18```ts 19import relationalStore from '@ohos.data.relationalStore'; 20``` 21 22## relationalStore.getRdbStore 23 24getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback<RdbStore>): void 25 26获得一个相关的RdbStore,操作关系型数据库,用户可以根据自己的需求配置RdbStore的参数,然后通过RdbStore调用相关接口可以执行相关的数据操作,使用callback异步回调。 27 28**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 29 30**参数:** 31 32| 参数名 | 类型 | 必填 | 说明 | 33| -------- | ---------------------------------------------- | ---- | ------------------------------------------------------------ | 34| 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)。 | 35| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 36| callback | AsyncCallback<[RdbStore](#rdbstore)> | 是 | 指定callback回调函数,返回RdbStore对象。 | 37 38**错误码:** 39 40以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 41 42| **错误码ID** | **错误信息** | 43| ------------ | ----------------------------------------------------------- | 44| 14800010 | Failed to open or delete database by invalid database path. | 45| 14800011 | Failed to open database by database corrupted. | 46| 14800000 | Inner error. | 47| 14801001 | Only supported in stage mode. | 48| 14801002 | The data group id is not valid. | 49 50**示例:** 51 52FA模型示例: 53 54```js 55import featureAbility from '@ohos.ability.featureAbility'; 56import { BusinessError } from "@ohos.base"; 57 58let store: relationalStore.RdbStore | undefined = undefined; 59let context = getContext(this); 60 61const STORE_CONFIG: relationalStore.StoreConfig = { 62 name: "RdbTest.db", 63 securityLevel: relationalStore.SecurityLevel.S1 64}; 65 66relationalStore.getRdbStore(context, STORE_CONFIG, (err: BusinessError, rdbStore: relationalStore.RdbStore) => { 67 store = rdbStore; 68 if (err) { 69 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 70 return; 71 } 72 console.info('Get RdbStore successfully.'); 73}) 74``` 75 76Stage模型示例: 77 78```ts 79import UIAbility from '@ohos.app.ability.UIAbility'; 80import window from '@ohos.window'; 81import { BusinessError } from "@ohos.base"; 82 83let store: relationalStore.RdbStore | undefined = undefined; 84 85class EntryAbility extends UIAbility { 86 onWindowStageCreate(windowStage: window.WindowStage) { 87 const STORE_CONFIG: relationalStore.StoreConfig = { 88 name: "RdbTest.db", 89 securityLevel: relationalStore.SecurityLevel.S1 90 }; 91 92 relationalStore.getRdbStore(this.context, STORE_CONFIG, (err: BusinessError, rdbStore: relationalStore.RdbStore) => { 93 store = rdbStore; 94 if (err) { 95 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 96 return; 97 } 98 console.info('Get RdbStore successfully.'); 99 }) 100 } 101} 102``` 103 104## relationalStore.getRdbStore 105 106getRdbStore(context: Context, config: StoreConfig): Promise<RdbStore> 107 108获得一个相关的RdbStore,操作关系型数据库,用户可以根据自己的需求配置RdbStore的参数,然后通过RdbStore调用相关接口可以执行相关的数据操作,使用Promise异步回调。 109 110**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 111 112**参数:** 113 114| 参数名 | 类型 | 必填 | 说明 | 115| ------- | -------------------------------- | ---- | ------------------------------------------------------------ | 116| 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)。 | 117| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 118 119**返回值**: 120 121| 类型 | 说明 | 122| ----------------------------------------- | --------------------------------- | 123| Promise<[RdbStore](#rdbstore)> | Promise对象。返回RdbStore对象。 | 124 125**错误码:** 126 127以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 128 129| **错误码ID** | **错误信息** | 130| ------------ | ----------------------------------------------------------- | 131| 14800010 | Failed to open or delete database by invalid database path. | 132| 14800011 | Failed to open database by database corrupted. | 133| 14800000 | Inner error. | 134| 14801001 | Only supported in stage mode. | 135| 14801002 | The data group id is not valid. | 136 137**示例:** 138 139FA模型示例: 140 141```js 142import featureAbility from '@ohos.ability.featureAbility'; 143import { BusinessError } from "@ohos.base"; 144 145let store: relationalStore.RdbStore | undefined = undefined; 146let context = getContext(this); 147 148const STORE_CONFIG: relationalStore.StoreConfig = { 149 name: "RdbTest.db", 150 securityLevel: relationalStore.SecurityLevel.S1 151}; 152 153relationalStore.getRdbStore(context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => { 154 store = rdbStore; 155 console.info('Get RdbStore successfully.') 156}).catch((err: BusinessError) => { 157 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 158}) 159``` 160 161Stage模型示例: 162 163```ts 164import UIAbility from '@ohos.app.ability.UIAbility'; 165import window from '@ohos.window'; 166import { BusinessError } from "@ohos.base"; 167 168let store: relationalStore.RdbStore | undefined = undefined; 169 170class EntryAbility extends UIAbility { 171 onWindowStageCreate(windowStage: window.WindowStage) { 172 const STORE_CONFIG: relationalStore.StoreConfig = { 173 name: "RdbTest.db", 174 securityLevel: relationalStore.SecurityLevel.S1 175 }; 176 177 relationalStore.getRdbStore(this.context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => { 178 store = rdbStore; 179 console.info('Get RdbStore successfully.') 180 }).catch((err: BusinessError) => { 181 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 182 }) 183 } 184} 185``` 186 187## relationalStore.deleteRdbStore 188 189deleteRdbStore(context: Context, name: string, callback: AsyncCallback<void>): void 190 191删除数据库文件,使用callback异步回调。 192 193删除成功后,建议将数据库对象置为null。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则调用此接口进行删库无效,必须使用 [deleteRdbStore<sup>10+</sup>](#relationalstoredeleterdbstore10) 接口进行删库。 194 195**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 196 197**参数:** 198 199| 参数名 | 类型 | 必填 | 说明 | 200| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 201| 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)。 | 202| name | string | 是 | 数据库名称。 | 203| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 204 205**错误码:** 206 207以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 208 209| **错误码ID** | **错误信息** | 210| ------------ | ----------------------------------------------------------- | 211| 14800010 | Failed to open or delete database by invalid database path. | 212| 14800000 | Inner error. | 213 214**示例:** 215 216FA模型示例: 217 218```js 219import featureAbility from '@ohos.ability.featureAbility'; 220import { BusinessError } from "@ohos.base"; 221 222let store: relationalStore.RdbStore | undefined = undefined; 223let context = getContext(this); 224 225relationalStore.deleteRdbStore(context, "RdbTest.db", (err: BusinessError) => { 226 if (err) { 227 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 228 return; 229 } 230 store = undefined; 231 console.info('Delete RdbStore successfully.'); 232}) 233``` 234 235Stage模型示例: 236 237```ts 238import UIAbility from '@ohos.app.ability.UIAbility'; 239import window from '@ohos.window'; 240import { BusinessError } from "@ohos.base"; 241 242let store: relationalStore.RdbStore | undefined = undefined; 243 244class EntryAbility extends UIAbility { 245 onWindowStageCreate(windowStage: window.WindowStage){ 246 relationalStore.deleteRdbStore(this.context, "RdbTest.db", (err: BusinessError) => { 247 if (err) { 248 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 249 return; 250 } 251 store = undefined; 252 console.info('Delete RdbStore successfully.'); 253 }) 254 } 255} 256``` 257 258## relationalStore.deleteRdbStore 259 260deleteRdbStore(context: Context, name: string): Promise<void> 261 262使用指定的数据库文件配置删除数据库,使用Promise异步回调。 263 264删除成功后,建议将数据库对象置为null。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则调用此接口进行删库无效,必须使用 [deleteRdbStore<sup>10+</sup>](#relationalstoredeleterdbstore10-1) 接口进行删库。 265 266**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 267 268**参数** 269 270| 参数名 | 类型 | 必填 | 说明 | 271| ------- | ------- | ---- | ------------------------------------------------------------ | 272| 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)。 | 273| name | string | 是 | 数据库名称。 | 274 275**返回值**: 276 277| 类型 | 说明 | 278| ------------------- | ------------------------- | 279| Promise<void> | 无返回结果的Promise对象。 | 280 281**错误码:** 282 283以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 284 285| **错误码ID** | **错误信息** | 286| ------------ | ----------------------------------------------------------- | 287| 14800010 | Failed to open or delete database by invalid database path. | 288| 14800000 | Inner error. | 289 290**示例:** 291 292FA模型示例: 293 294```js 295import featureAbility from '@ohos.ability.featureAbility'; 296import { BusinessError } from "@ohos.base"; 297 298let store: relationalStore.RdbStore | undefined = undefined; 299let context = getContext(this); 300 301relationalStore.deleteRdbStore(context, "RdbTest.db").then(()=>{ 302 store = undefined; 303 console.info('Delete RdbStore successfully.'); 304}).catch((err: BusinessError) => { 305 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 306}) 307``` 308 309Stage模型示例: 310 311```ts 312import UIAbility from '@ohos.app.ability.UIAbility'; 313import window from '@ohos.window'; 314import { BusinessError } from "@ohos.base"; 315 316let store: relationalStore.RdbStore | undefined = undefined; 317 318class EntryAbility extends UIAbility { 319 onWindowStageCreate(windowStage: window.WindowStage){ 320 relationalStore.deleteRdbStore(this.context, "RdbTest.db").then(()=>{ 321 store = undefined; 322 console.info('Delete RdbStore successfully.'); 323 }).catch((err: BusinessError) => { 324 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 325 }) 326 } 327} 328``` 329 330## relationalStore.deleteRdbStore<sup>10+</sup> 331 332deleteRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback\<void>): void 333 334使用指定的数据库文件配置删除数据库,使用callback异步回调。 335 336删除成功后,建议将数据库对象置为null。若数据库文件处于公共沙箱目录下,则删除数据库时必须使用该接口,当存在多个进程操作同一个数据库的情况,建议向其他进程发送数据库删除通知使其感知并处理。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则必须调用此接口进行删库。 337 338**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 339 340**参数:** 341 342| 参数名 | 类型 | 必填 | 说明 | 343| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 344| 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)。 | 345| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 346| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 347 348**错误码:** 349 350以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 351 352| **错误码ID** | **错误信息** | 353| ------------ | ----------------------------------------------------------- | 354| 14800010 | Failed to open or delete database by invalid database path. | 355| 14800000 | Inner error. | 356| 14801001 | Only supported in stage mode. | 357| 14801002 | The data group id is not valid. | 358 359**示例:** 360 361FA模型示例: 362 363```js 364import featureAbility from '@ohos.ability.featureAbility'; 365import { BusinessError } from "@ohos.base"; 366 367let store: relationalStore.RdbStore | undefined = undefined; 368let context = getContext(this); 369 370const STORE_CONFIG: relationalStore.StoreConfig = { 371 name: "RdbTest.db", 372 securityLevel: relationalStore.SecurityLevel.S1 373}; 374 375relationalStore.deleteRdbStore(context, STORE_CONFIG, (err: BusinessError) => { 376 if (err) { 377 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 378 return; 379 } 380 store = undefined; 381 console.info('Delete RdbStore successfully.'); 382}) 383``` 384 385Stage模型示例: 386 387```ts 388import UIAbility from '@ohos.app.ability.UIAbility'; 389import window from '@ohos.window'; 390import { BusinessError } from "@ohos.base"; 391 392let store: relationalStore.RdbStore | undefined = undefined; 393 394class EntryAbility extends UIAbility { 395 onWindowStageCreate(windowStage: window.WindowStage){ 396 const STORE_CONFIG: relationalStore.StoreConfig = { 397 name: "RdbTest.db", 398 securityLevel: relationalStore.SecurityLevel.S1 399 }; 400 relationalStore.deleteRdbStore(this.context, STORE_CONFIG, (err: BusinessError) => { 401 if (err) { 402 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 403 return; 404 } 405 store = undefined; 406 console.info('Delete RdbStore successfully.'); 407 }) 408 } 409} 410 411``` 412 413## relationalStore.deleteRdbStore<sup>10+</sup> 414 415deleteRdbStore(context: Context, config: StoreConfig): Promise\<void> 416 417使用指定的数据库文件配置删除数据库,使用Promise异步回调。 418 419删除成功后,建议将数据库对象置为null。若数据库文件处于公共沙箱目录下,则删除数据库时必须使用该接口,当存在多个进程操作同一个数据库的情况,建议向其他进程发送数据库删除通知使其感知并处理。建立数据库时,若在[StoreConfig](#storeconfig)中配置了自定义路径,则必须调用此接口进行删库。 420 421**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 422 423**参数** 424 425| 参数名 | 类型 | 必填 | 说明 | 426| ------- | --------------------------- | ---- | ------------------------------------------------------------ | 427| 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)。 | 428| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 429 430**返回值**: 431 432| 类型 | 说明 | 433| ------------------- | ------------------------- | 434| Promise<void> | 无返回结果的Promise对象。 | 435 436**错误码:** 437 438以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 439 440| **错误码ID** | **错误信息** | 441| ------------ | ----------------------------------------------------------- | 442| 14800010 | Failed to open or delete database by invalid database path. | 443| 14800000 | Inner error. | 444| 14801001 | Only supported in stage mode. | 445| 14801002 | The data group id is not valid. | 446 447**示例:** 448 449FA模型示例: 450 451```js 452import featureAbility from '@ohos.ability.featureAbility'; 453import { BusinessError } from "@ohos.base"; 454 455let store: relationalStore.RdbStore | undefined = undefined; 456let context = getContext(this); 457 458const STORE_CONFIG: relationalStore.StoreConfig = { 459 name: "RdbTest.db", 460 securityLevel: relationalStore.SecurityLevel.S1 461}; 462 463relationalStore.deleteRdbStore(context, STORE_CONFIG).then(()=>{ 464 store = undefined; 465 console.info('Delete RdbStore successfully.'); 466}).catch((err: BusinessError) => { 467 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 468}) 469``` 470 471Stage模型示例: 472 473```ts 474import UIAbility from '@ohos.app.ability.UIAbility'; 475import window from '@ohos.window'; 476import { BusinessError } from "@ohos.base"; 477 478let store: relationalStore.RdbStore | undefined = undefined; 479 480class EntryAbility extends UIAbility { 481 onWindowStageCreate(windowStage: window.WindowStage){ 482 const STORE_CONFIG: relationalStore.StoreConfig = { 483 name: "RdbTest.db", 484 securityLevel: relationalStore.SecurityLevel.S1 485 }; 486 relationalStore.deleteRdbStore(this.context, STORE_CONFIG).then(()=>{ 487 store = undefined; 488 console.info('Delete RdbStore successfully.'); 489 }).catch((err: BusinessError) => { 490 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 491 }) 492 } 493} 494``` 495 496## StoreConfig 497 498管理关系数据库配置。 499 500| 名称 | 类型 | 必填 | 说明 | 501| ------------- | ------------- | ---- | --------------------------------------------------------- | 502| name | string | 是 | 数据库文件名,也是数据库唯一标识符。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 503| securityLevel | [SecurityLevel](#securitylevel) | 是 | 设置数据库安全级别。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core| 504| encrypt | boolean | 否 | 指定数据库是否加密,默认不加密。<br/> true:加密。<br/> false:非加密。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 505| dataGroupId<sup>10+</sup> | string | 否 | 应用组ID,需要向应用市场获取。<br/>**模型约束:** 此属性仅在Stage模型下可用。<br/>从API version 10开始,支持此可选参数。指定在此dataGroupId对应的沙箱路径下创建RdbStore实例,当此参数不填时,默认在本应用沙箱目录下创建RdbStore实例。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 506| customDir<sup>11+</sup> | string | 否 | 数据库自定义路径。<br/>**使用约束:** 数据库路径大小限制为128字节,如果超过该大小会开库失败,返回错误。<br/>从API version 11开始,支持此可选参数。数据库将在如下的目录结构中被创建:context.databaseDir + "/rdb/" + customDir,其中context.databaseDir是应用沙箱对应的路径,"/rdb/"表示创建的是关系型数据库,customDir表示自定义的路径。当此参数不填时,默认在本应用沙箱目录下创建RdbStore实例。<br/>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 507| autoCleanDirtyData<sup>11+<sup> | boolean | 否 | 指定是否自动清理云端删除后同步到本地的数据,true表示自动清理,false表示手动清理,默认自动清理。<br/>对于端云协同的数据库,当云端删除的数据同步到设备端时,可通过该参数设置设备端是否自动清理。手动清理可以通过[cleanDirtyData<sup>11+</sup>](#cleandirtydata11)接口清理。<br/>从API version 11开始,支持此可选参数。<br/>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 508 509## SecurityLevel 510 511数据库的安全级别枚举。请使用枚举名称而非枚举值。 512 513> **说明:** 514> 515> 若需要进行同步操作,数据库安全等级应不高于对端设备安全等级,具体可见[跨设备同步访问控制机制](../../database/sync-app-data-across-devices-overview.md#跨设备同步访问控制机制)。 516 517**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 518 519| 名称 | 值 | 说明 | 520| ---- | ---- | ------------------------------------------------------------ | 521| S1 | 1 | 表示数据库的安全级别为低级别,当数据泄露时会产生较低影响。例如,包含壁纸等系统数据的数据库。 | 522| S2 | 2 | 表示数据库的安全级别为中级别,当数据泄露时会产生较大影响。例如,包含录音、视频等用户生成数据或通话记录等信息的数据库。 | 523| S3 | 3 | 表示数据库的安全级别为高级别,当数据泄露时会产生重大影响。例如,包含用户运动、健康、位置等信息的数据库。 | 524| S4 | 4 | 表示数据库的安全级别为关键级别,当数据泄露时会产生严重影响。例如,包含认证凭据、财务数据等信息的数据库。 | 525 526## AssetStatus<sup>10+</sup> 527 528描述资产附件的状态枚举。请使用枚举名称而非枚举值。 529 530**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 531 532| 名称 | 值 | 说明 | 533| ------------------------------- | --- | -------------- | 534| ASSET_NORMAL | 1 | 表示资产状态正常。 | 535| ASSET_INSERT | 2 | 表示资产需要插入到云端。 | 536| ASSET_UPDATE | 3 | 表示资产需要更新到云端。 | 537| ASSET_DELETE | 4 | 表示资产需要在云端删除。 | 538| ASSET_ABNORMAL | 5 | 表示资产状态异常。 | 539| ASSET_DOWNLOADING | 6 | 表示资产正在下载到本地设备。 | 540 541## Asset<sup>10+</sup> 542 543记录资产附件(文件、图片、视频等类型文件)的相关信息。资产类型的相关接口暂不支持Datashare。 544 545**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 546 547| 名称 | 类型 | 必填 | 说明 | 548| ----------- | --------------------------- | --- | ------------ | 549| name | string | 是 | 资产的名称。 | 550| uri | string | 是 | 资产的uri,在系统里的绝对路径。 | 551| path | string | 是 | 资产在应用沙箱里的路径。 | 552| createTime | string | 是 | 资产被创建出来的时间。 | 553| modifyTime | string | 是 | 资产最后一次被修改的时间。 | 554| size | string | 是 | 资产占用空间的大小。 | 555| status | [AssetStatus](#assetstatus10) | 否 | 资产的状态,默认值为ASSET_NORMAL。 | 556 557## Assets<sup>10+</sup> 558 559表示[Asset](#asset10)类型的数组。 560 561**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 562 563| 类型 | 说明 | 564| ------- | -------------------- | 565| [Asset](#asset10)[] | 表示Asset类型的数组。 | 566 567## ValueType 568 569用于表示允许的数据字段类型,接口参数具体类型根据其功能而定。 570 571**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 572 573| 类型 | 说明 | 574| ------- | -------------------- | 575| null<sup>10+</sup> | 表示值类型为空。 | 576| number | 表示值类型为数字。 | 577| string | 表示值类型为字符串。 | 578| boolean | 表示值类型为布尔值。 | 579| Uint8Array<sup>10+</sup> | 表示值类型为Uint8类型的数组。 | 580| Asset<sup>10+</sup> | 表示值类型为附件[Asset](#asset10)。 | 581| Assets<sup>10+</sup> | 表示值类型为附件数组[Assets](#assets10)。 | 582 583## ValuesBucket 584 585用于存储键值对的类型。该类型不是多线程安全的,如果应用中存在多线程同时操作该类派生出的实例,注意加锁保护。 586 587**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 588 589| 键类型 | 值类型 | 590| ------ | ----------------------- | 591| number | 主键的类型可以是number。 | 592| string | 主键的类型可以是string。 | 593 594## PRIKeyType<sup>10+</sup> 595 596用于表示数据库表某一行主键的数据类型。 597 598**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 599 600| 类型 | 说明 | 601| ---------------- | ---------------------------------- | 602| number | 主键的类型可以是number。 | 603| string | 主键的类型可以是string。 | 604 605## UTCTime<sup>10+</sup> 606 607用于表示UTC类型时间的数据类型。 608 609**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 610 611| 类型 | 说明 | 612| ---- | --------------- | 613| Date | UTC类型的时间。 | 614 615## ModifyTime<sup>10+</sup> 616 617用于存储数据库表的主键和修改时间的数据类型。 618 619**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 620 621| 类型 | 说明 | 622| ------------------------------------------------------- | ------------------------------------------------------------ | 623| Map<[PRIKeyType](#prikeytype10), [UTCTime](#utctime10)> | 键表示是数据库表某一行的主键,值表示该行的最后修改时间,用UTC格式表示。 | 624 625## SyncMode 626 627指数据库同步模式。请使用枚举名称而非枚举值。 628 629| 名称 | 值 | 说明 | 630| -------------- | ---- | ---------------------------------- | 631| SYNC_MODE_PUSH | 0 | 表示数据从本地设备推送到远程设备。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 632| SYNC_MODE_PULL | 1 | 表示数据从远程设备拉至本地设备。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 633| SYNC_MODE_TIME_FIRST<sup>10+</sup> | 4 | 表示数据从修改时间较近的一端同步到修改时间较远的一端。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 634| SYNC_MODE_NATIVE_FIRST<sup>10+</sup> | 5 | 表示数据从本地设备同步到云端。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 635| SYNC_MODE_CLOUD_FIRST<sup>10+</sup> | 6 | 表示数据从云端同步到本地设备。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 636 637## Origin<sup>11+</sup> 638 639表示数据来源。请使用枚举名称而非枚举值。 640 641**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 642 643| 名称 | 值 | 说明 | 644| -------------- | ---- | ---------------------------------- | 645| LOCAL | 0 | 表示本地数据。 | 646| CLOUD | 1 | 表示云端同步的数据。 | 647| REMOTE | 2 | 表示端端同步的数据。 | 648 649## Field<sup>11+</sup> 650 651用于谓词查询条件的特殊字段。请使用枚举名称而非枚举值。 652 653**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 654 655| 名称 | 值 | 说明 | 656| -------------- | ---- | ---------------------------------- | 657| CURSOR_FIELD | '#_cursor' | 用于cursor查找的字段名。| 658| ORIGIN_FIELD | '#_origin' | 用于cursor查找时指定数据来源的字段名。 | 659| DELETED_FLAG_FIELD | '#_deleted_flag' | 用于cursor查找的结果集返回时填充的字段,表示云端删除的数据同步到本地后数据是否清理。<br>返回的结果集中,该字段对应的value为false表示数据未清理,true表示数据已清理。| 660| OWNER_FIELD | '#_cloud_owner' | 用于共享表中查找owner时返回的结果集中填充的字段,表示当前共享记录的共享发起者。| 661| PRIVILEGE_FIELD | '#_cloud_privilege' | 用于共享表中查找共享数据权限时返回的结果集中填充的字段,表示当前共享记录的允许的操作权限。| 662| SHARING_RESOURCE_FIELD | '#_sharing_resource_field' | 用于数据共享时查找共享数据的共享资源时返回的结果集中填充的字段,表示共享数据的共享资源标识。| 663 664## SubscribeType 665 666描述订阅类型。请使用枚举名称而非枚举值。 667 668**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 669 670| 名称 | 值 | 说明 | 671| --------------------- | ---- | ------------------ | 672| SUBSCRIBE_TYPE_REMOTE | 0 | 订阅远程数据更改。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 673| SUBSCRIBE_TYPE_CLOUD<sup>10+</sup> | 1 | 订阅云端数据更改。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 674| SUBSCRIBE_TYPE_CLOUD_DETAILS<sup>10+</sup> | 2 | 订阅云端数据更改详情。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 675 676## ChangeType<sup>10+</sup> 677 678描述数据变更类型的枚举。请使用枚举名称而非枚举值。 679 680**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 681 682**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 683 684| 名称 | 值 | 说明 | 685| -------------------------- | --- | -------------------------- | 686| DATA_CHANGE | 0 | 表示是数据发生变更。 | 687| ASSET_CHANGE | 1 | 表示是资产附件发生了变更。 | 688 689## ChangeInfo<sup>10+</sup> 690 691记录端云同步过程详情。 692 693**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 694 695| 名称 | 类型 | 必填 | 说明 | 696| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 697| table | string | 是 | 表示发生变化的表的名称。 | 698| type | [ChangeType](#changetype10) | 是 | 表示发生变化的数据的类型,数据或者资产附件发生变化。 | 699| inserted | Array\<string\> \| Array\<number\> | 是 | 记录插入数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示插入数据的行号。 | 700| updated | Array\<string\> \| Array\<number\> | 是 | 记录更新数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示更新数据的行号。 | 701| deleted | Array\<string\> \| Array\<number\> | 是 | 记录删除数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示删除数据的行号。 | 702 703## DistributedType<sup>10+</sup> 704 705描述表的分布式类型的枚举。请使用枚举名称而非枚举值。 706 707**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 708 709| 名称 | 值 | 说明 | 710| ------------------ | --- | -------------------------------------------------------------------------------------------------- | 711| DISTRIBUTED_DEVICE | 0 | 表示在不同设备之间分布式的数据库表。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core | 712| DISTRIBUTED_CLOUD | 1 | 表示在设备和云端之间分布式的数据库表。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client | 713 714## DistributedConfig<sup>10+</sup> 715 716记录表的分布式配置信息。 717 718**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 719 720| 名称 | 类型 | 必填 | 说明 | 721| -------- | ------- | ---- | ------------------------------------------------------------ | 722| autoSync | boolean | 是 | 该值为true时,表示该表支持自动同步和手动同步;该值为false时,表示该表只支持手动同步,不支持自动同步。 | 723 724## ConflictResolution<sup>10+</sup> 725 726插入和修改接口的冲突解决方式。请使用枚举名称而非枚举值。 727 728**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 729 730| 名称 | 值 | 说明 | 731| -------------------- | ---- | ------------------------------------------------------------ | 732| ON_CONFLICT_NONE | 0 | 表示当冲突发生时,不做任何处理。 | 733| ON_CONFLICT_ROLLBACK | 1 | 表示当冲突发生时,中止SQL语句并回滚当前事务。 | 734| ON_CONFLICT_ABORT | 2 | 表示当冲突发生时,中止当前SQL语句,并撤销当前 SQL 语句所做的任何更改,但是由同一事务中先前的 SQL 语句引起的更改被保留并且事务保持活动状态。 | 735| ON_CONFLICT_FAIL | 3 | 表示当冲突发生时,中止当前 SQL 语句。但它不会撤销失败的 SQL 语句的先前更改,也不会结束事务。 | 736| ON_CONFLICT_IGNORE | 4 | 表示当冲突发生时,跳过包含违反约束的行并继续处理 SQL 语句的后续行。 | 737| ON_CONFLICT_REPLACE | 5 | 表示当冲突发生时,在插入或更新当前行之前删除导致约束违例的预先存在的行,并且命令会继续正常执行。 | 738 739## Progress<sup>10+</sup> 740 741描述端云同步过程的枚举。请使用枚举名称而非枚举值。 742 743**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 744 745| 名称 | 值 | 说明 | 746| ---------------- | ---- | ------------------------ | 747| SYNC_BEGIN | 0 | 表示端云同步过程开始。 | 748| SYNC_IN_PROGRESS | 1 | 表示正在端云同步过程中。 | 749| SYNC_FINISH | 2 | 表示端云同步过程已完成。 | 750 751## Statistic<sup>10+</sup> 752 753描述数据库表的端云同步过程的统计信息。 754 755**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 756 757| 名称 | 类型 | 必填 | 说明 | 758| ---------- | ------ | ---- | ---------------------------------------- | 759| total | number | 是 | 表示数据库表中需要端云同步的总行数。 | 760| successful | number | 是 | 表示数据库表中端云同步成功的行数。 | 761| failed | number | 是 | 表示数据库表中端云同步失败的行数。 | 762| remained | number | 是 | 表示数据库表中端云同步剩余未执行的行数。 | 763 764## TableDetails<sup>10+</sup> 765 766描述数据库表执行端云同步任务上传和下载的统计信息。 767 768**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 769 770| 名称 | 类型 | 必填 | 说明 | 771| -------- | ------------------------- | ---- | ------------------------------------------ | 772| upload | [Statistic](#statistic10) | 是 | 表示数据库表中端云同步上传过程的统计信息。 | 773| download | [Statistic](#statistic10) | 是 | 表示数据库表中端云同步下载过程的统计信息。 | 774 775## ProgressCode<sup>10+</sup> 776 777表示端云同步过程的状态。请使用枚举名称而非枚举值。 778 779**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 780 781| 名称 | 值 | 说明 | 782| --------------------- | ---- | ------------------------------------------------------------ | 783| SUCCESS | 0 | 表示端云同步过程成功。 | 784| UNKNOWN_ERROR | 1 | 表示端云同步过程遇到未知错误。 | 785| NETWORK_ERROR | 2 | 表示端云同步过程遇到网络错误。 | 786| CLOUD_DISABLED | 3 | 表示云端不可用。 | 787| LOCKED_BY_OTHERS | 4 | 表示有其他设备正在端云同步,本设备无法进行端云同步。<br>请确保无其他设备占用云端资源后,再使用本设备进行端云同步任务。 | 788| RECORD_LIMIT_EXCEEDED | 5 | 表示本次端云同步需要同步的条目或大小超出最大值。由云端配置最大值。 | 789| NO_SPACE_FOR_ASSET | 6 | 表示云空间剩余空间小于待同步的资产大小。 | 790 791## ProgressDetails<sup>10+</sup> 792 793描述数据库整体执行端云同步任务上传和下载的统计信息。 794 795**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 796 797| 名称 | 类型 | 必填 | 说明 | 798| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 799| schedule | [Progress](#progress10) | 是 | 表示端云同步过程。 | 800| code | [ProgressCode](#progresscode10) | 是 | 表示端云同步过程的状态。 | 801| details | Record<string, [TableDetails](#tabledetails10)> | 是 | 表示端云同步各表的统计信息。<br>键表示表名,值表示该表的端云同步过程统计信息。 | 802 803## RdbPredicates 804 805表示关系型数据库(RDB)的谓词。该类确定RDB中条件表达式的值是true还是false。该类型不是多线程安全的,如果应用中存在多线程同时操作该类派生出的实例,注意加锁保护。 806 807### constructor 808 809constructor(name: string) 810 811构造函数。 812 813**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 814 815**参数:** 816 817| 参数名 | 类型 | 必填 | 说明 | 818| ------ | ------ | ---- | ------------ | 819| name | string | 是 | 数据库表名。 | 820 821**示例:** 822 823```ts 824let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 825``` 826 827### inDevices 828 829inDevices(devices: Array<string>): RdbPredicates 830 831同步分布式数据库时连接到组网内指定的远程设备。 832 833> **说明:** 834> 835> 其中devices通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 836数据库同步时调用Sync接口,需要在入参谓词中调用inDevices接口选择设备。如果不调用inDevices接口即默认连接组网内所有的设备。 837 838**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 839 840**参数:** 841 842| 参数名 | 类型 | 必填 | 说明 | 843| ------- | ------------------- | ---- | -------------------------- | 844| devices | Array<string> | 是 | 指定的组网内的远程设备ID。 | 845 846**返回值**: 847 848| 类型 | 说明 | 849| ------------------------------------ | -------------------------- | 850| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 851 852**示例:** 853 854```ts 855import deviceManager from '@ohos.distributedDeviceManager'; 856 857let dmInstance: deviceManager.DeviceManager; 858let deviceIds: Array<string> = []; 859 860try { 861 dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); 862 let devices: Array<deviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 863 for (let i = 0; i < devices.length; i++) { 864 deviceIds[i] = devices[i].networkId!; 865 } 866} catch (err) { 867 let code = (err as BusinessError).code; 868 let message = (err as BusinessError).message 869 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 870} 871 872let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 873predicates.inDevices(deviceIds); 874``` 875 876### inAllDevices 877 878inAllDevices(): RdbPredicates 879 880 881同步分布式数据库时连接到组网内所有的远程设备。 882 883 884**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 885 886**返回值**: 887 888| 类型 | 说明 | 889| ------------------------------------ | -------------------------- | 890| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 891 892**示例:** 893 894```ts 895let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 896predicates.inAllDevices(); 897``` 898 899### equalTo 900 901equalTo(field: string, value: ValueType): RdbPredicates 902 903 904配置谓词以匹配数据表的field列中值为value的字段。 905 906**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 907 908**参数:** 909 910| 参数名 | 类型 | 必填 | 说明 | 911| ------ | ----------------------- | ---- | ---------------------- | 912| field | string | 是 | 数据库表中的列名。 | 913| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 914 915**返回值**: 916 917| 类型 | 说明 | 918| ------------------------------------ | -------------------------- | 919| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 920 921**示例:** 922 923```ts 924// 匹配数据表的"NAME"列中值为"Lisa"的字段 925let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 926predicates.equalTo("NAME", "Lisa"); 927``` 928 929 930### notEqualTo 931 932notEqualTo(field: string, value: ValueType): RdbPredicates 933 934 935配置谓词以匹配数据表的field列中值不为value的字段。 936 937**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 938 939**参数:** 940 941| 参数名 | 类型 | 必填 | 说明 | 942| ------ | ----------------------- | ---- | ---------------------- | 943| field | string | 是 | 数据库表中的列名。 | 944| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 945 946**返回值**: 947 948| 类型 | 说明 | 949| ------------------------------------ | -------------------------- | 950| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 951 952**示例:** 953 954```ts 955// 匹配数据表的"NAME"列中值不为"Lisa"的字段 956let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 957predicates.notEqualTo("NAME", "Lisa"); 958``` 959 960 961### beginWrap 962 963beginWrap(): RdbPredicates 964 965 966向谓词添加左括号。 967 968**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 969 970**返回值**: 971 972| 类型 | 说明 | 973| ------------------------------------ | ------------------------- | 974| [RdbPredicates](#rdbpredicates) | 返回带有左括号的Rdb谓词。 | 975 976**示例:** 977 978```ts 979let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 980predicates.equalTo("NAME", "Lisa") 981 .beginWrap() 982 .equalTo("AGE", 18) 983 .or() 984 .equalTo("SALARY", 200.5) 985 .endWrap() 986``` 987 988### endWrap 989 990endWrap(): RdbPredicates 991 992向谓词添加右括号。 993 994**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 995 996**返回值**: 997 998| 类型 | 说明 | 999| ------------------------------------ | ------------------------- | 1000| [RdbPredicates](#rdbpredicates) | 返回带有右括号的Rdb谓词。 | 1001 1002**示例:** 1003 1004```ts 1005let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1006predicates.equalTo("NAME", "Lisa") 1007 .beginWrap() 1008 .equalTo("AGE", 18) 1009 .or() 1010 .equalTo("SALARY", 200.5) 1011 .endWrap() 1012``` 1013 1014### or 1015 1016or(): RdbPredicates 1017 1018将或条件添加到谓词中。 1019 1020**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1021 1022**返回值**: 1023 1024| 类型 | 说明 | 1025| ------------------------------------ | ------------------------- | 1026| [RdbPredicates](#rdbpredicates) | 返回带有或条件的Rdb谓词。 | 1027 1028**示例:** 1029 1030```ts 1031// 匹配数据表的"NAME"列中值为"Lisa"或"Rose"的字段 1032let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1033predicates.equalTo("NAME", "Lisa") 1034 .or() 1035 .equalTo("NAME", "Rose") 1036``` 1037 1038### and 1039 1040and(): RdbPredicates 1041 1042向谓词添加和条件。 1043 1044**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1045 1046**返回值**: 1047 1048| 类型 | 说明 | 1049| ------------------------------------ | ------------------------- | 1050| [RdbPredicates](#rdbpredicates) | 返回带有和条件的Rdb谓词。 | 1051 1052**示例:** 1053 1054```ts 1055// 匹配数据表的"NAME"列中值为"Lisa"且"SALARY"列中值为"200.5"的字段 1056let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1057predicates.equalTo("NAME", "Lisa") 1058 .and() 1059 .equalTo("SALARY", 200.5) 1060``` 1061 1062### contains 1063 1064contains(field: string, value: string): RdbPredicates 1065 1066配置谓词以匹配数据表的field列中包含value的字段。 1067 1068**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1069 1070**参数:** 1071 1072| 参数名 | 类型 | 必填 | 说明 | 1073| ------ | ------ | ---- | ---------------------- | 1074| field | string | 是 | 数据库表中的列名。 | 1075| value | string | 是 | 指示要与谓词匹配的值。 | 1076 1077**返回值**: 1078 1079| 类型 | 说明 | 1080| ------------------------------------ | -------------------------- | 1081| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1082 1083**示例:** 1084 1085```ts 1086// 匹配数据表的"NAME"列中包含"os"的字段,如"Rose" 1087let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1088predicates.contains("NAME", "os"); 1089``` 1090 1091### beginsWith 1092 1093beginsWith(field: string, value: string): RdbPredicates 1094 1095配置谓词以匹配数据表的field列中以value开头的字段。 1096 1097**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1098 1099**参数:** 1100 1101| 参数名 | 类型 | 必填 | 说明 | 1102| ------ | ------ | ---- | ---------------------- | 1103| field | string | 是 | 数据库表中的列名。 | 1104| value | string | 是 | 指示要与谓词匹配的值。 | 1105 1106**返回值**: 1107 1108| 类型 | 说明 | 1109| ------------------------------------ | -------------------------- | 1110| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1111 1112**示例:** 1113 1114```ts 1115// 匹配数据表的"NAME"列中以"Li"开头的字段,如"Lisa" 1116let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1117predicates.beginsWith("NAME", "Li"); 1118``` 1119 1120### endsWith 1121 1122endsWith(field: string, value: string): RdbPredicates 1123 1124配置谓词以匹配数据表的field列中以value结尾的字段。 1125 1126**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1127 1128**参数:** 1129 1130| 参数名 | 类型 | 必填 | 说明 | 1131| ------ | ------ | ---- | ---------------------- | 1132| field | string | 是 | 数据库表中的列名。 | 1133| value | string | 是 | 指示要与谓词匹配的值。 | 1134 1135**返回值**: 1136 1137| 类型 | 说明 | 1138| ------------------------------------ | -------------------------- | 1139| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1140 1141**示例:** 1142 1143```ts 1144// 匹配数据表的"NAME"列中以"se"结尾的字段,如"Rose" 1145let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1146predicates.endsWith("NAME", "se"); 1147``` 1148 1149### isNull 1150 1151isNull(field: string): RdbPredicates 1152 1153配置谓词以匹配数据表的field列中值为null的字段。 1154 1155**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1156 1157**参数:** 1158 1159| 参数名 | 类型 | 必填 | 说明 | 1160| ------ | ------ | ---- | ------------------ | 1161| field | string | 是 | 数据库表中的列名。 | 1162 1163**返回值**: 1164 1165| 类型 | 说明 | 1166| ------------------------------------ | -------------------------- | 1167| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1168 1169**示例**: 1170 1171```ts 1172let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1173predicates.isNull("NAME"); 1174``` 1175 1176### isNotNull 1177 1178isNotNull(field: string): RdbPredicates 1179 1180配置谓词以匹配数据表的field列中值不为null的字段。 1181 1182**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1183 1184**参数:** 1185 1186| 参数名 | 类型 | 必填 | 说明 | 1187| ------ | ------ | ---- | ------------------ | 1188| field | string | 是 | 数据库表中的列名。 | 1189 1190**返回值**: 1191 1192| 类型 | 说明 | 1193| ------------------------------------ | -------------------------- | 1194| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1195 1196**示例:** 1197 1198```ts 1199let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1200predicates.isNotNull("NAME"); 1201``` 1202 1203### like 1204 1205like(field: string, value: string): RdbPredicates 1206 1207配置谓词以匹配数据表的field列中值类似于value的字段。 1208 1209**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1210 1211**参数:** 1212 1213| 参数名 | 类型 | 必填 | 说明 | 1214| ------ | ------ | ---- | ---------------------- | 1215| field | string | 是 | 数据库表中的列名。 | 1216| value | string | 是 | 指示要与谓词匹配的值。 | 1217 1218**返回值**: 1219 1220| 类型 | 说明 | 1221| ------------------------------------ | -------------------------- | 1222| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1223 1224**示例:** 1225 1226```ts 1227// 匹配数据表的"NAME"列中值类似于"os"的字段,如"Rose" 1228let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1229predicates.like("NAME", "%os%"); 1230``` 1231 1232### glob 1233 1234glob(field: string, value: string): RdbPredicates 1235 1236配置谓词匹配数据字段为string的指定字段。 1237 1238**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1239 1240**参数:** 1241 1242| 参数名 | 类型 | 必填 | 说明 | 1243| ------ | ------ | ---- | ------------------------------------------------------------ | 1244| field | string | 是 | 数据库表中的列名。 | 1245| value | string | 是 | 指示要与谓词匹配的值。<br>支持通配符,*表示0个、1个或多个数字或字符,?表示1个数字或字符。 | 1246 1247**返回值**: 1248 1249| 类型 | 说明 | 1250| ------------------------------------ | -------------------------- | 1251| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1252 1253**示例:** 1254 1255```ts 1256// 匹配数据表的"NAME"列中类型为string且值为"?h*g"的字段 1257let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1258predicates.glob("NAME", "?h*g"); 1259``` 1260 1261### between 1262 1263between(field: string, low: ValueType, high: ValueType): RdbPredicates 1264 1265配置谓词以匹配数据表的field列中值在给定范围内的字段(包含范围边界)。 1266 1267**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1268 1269**参数:** 1270 1271| 参数名 | 类型 | 必填 | 说明 | 1272| ------ | ----------------------- | ---- | -------------------------- | 1273| field | string | 是 | 数据库表中的列名。 | 1274| low | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最小值。 | 1275| high | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最大值。 | 1276 1277**返回值**: 1278 1279| 类型 | 说明 | 1280| ------------------------------------ | -------------------------- | 1281| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1282 1283**示例:** 1284 1285```ts 1286// 匹配数据表的"AGE"列中大于等于10且小于等于50的值 1287let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1288predicates.between("AGE", 10, 50); 1289``` 1290 1291### notBetween 1292 1293notBetween(field: string, low: ValueType, high: ValueType): RdbPredicates 1294 1295配置谓词以匹配数据表的field列中值超出给定范围的字段(不包含范围边界)。 1296 1297**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1298 1299**参数:** 1300 1301| 参数名 | 类型 | 必填 | 说明 | 1302| ------ | ----------------------- | ---- | -------------------------- | 1303| field | string | 是 | 数据库表中的列名。 | 1304| low | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最小值。 | 1305| high | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的最大值。 | 1306 1307**返回值**: 1308 1309| 类型 | 说明 | 1310| ------------------------------------ | -------------------------- | 1311| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1312 1313**示例:** 1314 1315```ts 1316// 匹配数据表的"AGE"列中小于10或大于50的值 1317let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1318predicates.notBetween("AGE", 10, 50); 1319``` 1320 1321### greaterThan 1322 1323greaterThan(field: string, value: ValueType): RdbPredicates 1324 1325配置谓词以匹配数据表的field列中值大于value的字段。 1326 1327**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1328 1329**参数:** 1330 1331| 参数名 | 类型 | 必填 | 说明 | 1332| ------ | ----------------------- | ---- | ---------------------- | 1333| field | string | 是 | 数据库表中的列名。 | 1334| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1335 1336**返回值**: 1337 1338| 类型 | 说明 | 1339| ------------------------------------ | -------------------------- | 1340| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1341 1342**示例:** 1343 1344```ts 1345// 匹配数据表的"AGE"列中大于18的值 1346let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1347predicates.greaterThan("AGE", 18); 1348``` 1349 1350### lessThan 1351 1352lessThan(field: string, value: ValueType): RdbPredicates 1353 1354配置谓词以匹配数据表的field列中值小于value的字段。 1355 1356**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1357 1358**参数:** 1359 1360| 参数名 | 类型 | 必填 | 说明 | 1361| ------ | ----------------------- | ---- | ---------------------- | 1362| field | string | 是 | 数据库表中的列名。 | 1363| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1364 1365**返回值**: 1366 1367| 类型 | 说明 | 1368| ------------------------------------ | -------------------------- | 1369| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1370 1371**示例:** 1372 1373```ts 1374// 匹配数据表的"AGE"列中小于20的值 1375let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1376predicates.lessThan("AGE", 20); 1377``` 1378 1379### greaterThanOrEqualTo 1380 1381greaterThanOrEqualTo(field: string, value: ValueType): RdbPredicates 1382 1383配置谓词以匹配数据表的field列中值大于或者等于value的字段。 1384 1385**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1386 1387**参数:** 1388 1389| 参数名 | 类型 | 必填 | 说明 | 1390| ------ | ----------------------- | ---- | ---------------------- | 1391| field | string | 是 | 数据库表中的列名。 | 1392| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1393 1394**返回值**: 1395 1396| 类型 | 说明 | 1397| ------------------------------------ | -------------------------- | 1398| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1399 1400**示例:** 1401 1402```ts 1403// 匹配数据表的"AGE"列中大于等于18的值 1404let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1405predicates.greaterThanOrEqualTo("AGE", 18); 1406``` 1407 1408### lessThanOrEqualTo 1409 1410lessThanOrEqualTo(field: string, value: ValueType): RdbPredicates 1411 1412配置谓词以匹配数据表的field列中值小于或者等于value的字段。 1413 1414**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1415 1416**参数:** 1417 1418| 参数名 | 类型 | 必填 | 说明 | 1419| ------ | ----------------------- | ---- | ---------------------- | 1420| field | string | 是 | 数据库表中的列名。 | 1421| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 1422 1423**返回值**: 1424 1425| 类型 | 说明 | 1426| ------------------------------------ | -------------------------- | 1427| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1428 1429**示例:** 1430 1431```ts 1432// 匹配数据表的"AGE"列中小于等于20的值 1433let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1434predicates.lessThanOrEqualTo("AGE", 20); 1435``` 1436 1437### orderByAsc 1438 1439orderByAsc(field: string): RdbPredicates 1440 1441配置谓词以匹配数据表的field列中值按升序排序的列。 1442 1443**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1444 1445**参数:** 1446 1447| 参数名 | 类型 | 必填 | 说明 | 1448| ------ | ------ | ---- | ------------------ | 1449| field | string | 是 | 数据库表中的列名。 | 1450 1451**返回值**: 1452 1453| 类型 | 说明 | 1454| ------------------------------------ | -------------------------- | 1455| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1456 1457**示例:** 1458 1459```ts 1460let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1461predicates.orderByAsc("NAME"); 1462``` 1463 1464### orderByDesc 1465 1466orderByDesc(field: string): RdbPredicates 1467 1468配置谓词以匹配数据表的field列中值按降序排序的列。 1469 1470**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1471 1472**参数:** 1473 1474| 参数名 | 类型 | 必填 | 说明 | 1475| ------ | ------ | ---- | ------------------ | 1476| field | string | 是 | 数据库表中的列名。 | 1477 1478**返回值**: 1479 1480| 类型 | 说明 | 1481| ------------------------------------ | -------------------------- | 1482| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1483 1484**示例:** 1485 1486```ts 1487let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1488predicates.orderByDesc("AGE"); 1489``` 1490 1491### distinct 1492 1493distinct(): RdbPredicates 1494 1495配置谓词以过滤重复记录并仅保留其中一个。 1496 1497**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1498 1499**返回值**: 1500 1501| 类型 | 说明 | 1502| ------------------------------------ | ------------------------------ | 1503| [RdbPredicates](#rdbpredicates) | 返回可用于过滤重复记录的谓词。 | 1504 1505**示例:** 1506 1507```ts 1508let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1509predicates.equalTo("NAME", "Rose").distinct(); 1510``` 1511 1512### limitAs 1513 1514limitAs(value: number): RdbPredicates 1515 1516设置最大数据记录数的谓词。 1517 1518**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1519 1520**参数:** 1521 1522| 参数名 | 类型 | 必填 | 说明 | 1523| ------ | ------ | ---- | ---------------- | 1524| value | number | 是 | 最大数据记录数。 | 1525 1526**返回值**: 1527 1528| 类型 | 说明 | 1529| ------------------------------------ | ------------------------------------ | 1530| [RdbPredicates](#rdbpredicates) | 返回可用于设置最大数据记录数的谓词。 | 1531 1532**示例:** 1533 1534```ts 1535let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1536predicates.equalTo("NAME", "Rose").limitAs(3); 1537``` 1538 1539### offsetAs 1540 1541offsetAs(rowOffset: number): RdbPredicates 1542 1543配置谓词以指定返回结果的起始位置。 1544 1545**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1546 1547**参数:** 1548 1549| 参数名 | 类型 | 必填 | 说明 | 1550| --------- | ------ | ---- | ---------------------------------- | 1551| rowOffset | number | 是 | 返回结果的起始位置,取值为正整数。 | 1552 1553**返回值**: 1554 1555| 类型 | 说明 | 1556| ------------------------------------ | ------------------------------------ | 1557| [RdbPredicates](#rdbpredicates) | 返回具有指定返回结果起始位置的谓词。 | 1558 1559**示例:** 1560 1561```ts 1562let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1563predicates.equalTo("NAME", "Rose").offsetAs(3); 1564``` 1565 1566### groupBy 1567 1568groupBy(fields: Array<string>): RdbPredicates 1569 1570配置谓词按指定列分组查询结果。 1571 1572**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1573 1574**参数:** 1575 1576| 参数名 | 类型 | 必填 | 说明 | 1577| ------ | ------------------- | ---- | -------------------- | 1578| fields | Array<string> | 是 | 指定分组依赖的列名。 | 1579 1580**返回值**: 1581 1582| 类型 | 说明 | 1583| ------------------------------------ | ---------------------- | 1584| [RdbPredicates](#rdbpredicates) | 返回分组查询列的谓词。 | 1585 1586**示例:** 1587 1588```ts 1589let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1590predicates.groupBy(["AGE", "NAME"]); 1591``` 1592 1593### indexedBy 1594 1595indexedBy(field: string): RdbPredicates 1596 1597配置谓词以指定索引列。 1598 1599**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1600 1601**参数:** 1602 1603| 参数名 | 类型 | 必填 | 说明 | 1604| ------ | ------ | ---- | -------------- | 1605| field | string | 是 | 索引列的名称。 | 1606 1607**返回值**: 1608 1609 1610| 类型 | 说明 | 1611| ------------------------------------ | ------------------------------------- | 1612| [RdbPredicates](#rdbpredicates) | 返回具有指定索引列的RdbPredicates。 | 1613 1614**示例:** 1615 1616```ts 1617let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1618predicates.indexedBy("SALARY"); 1619``` 1620 1621### in 1622 1623in(field: string, value: Array<ValueType>): RdbPredicates 1624 1625配置谓词以匹配数据表的field列中值在给定范围内的字段。 1626 1627**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1628 1629**参数:** 1630 1631| 参数名 | 类型 | 必填 | 说明 | 1632| ------ | ------------------------------------ | ---- | --------------------------------------- | 1633| field | string | 是 | 数据库表中的列名。 | 1634| value | Array<[ValueType](#valuetype)> | 是 | 以ValueType型数组形式指定的要匹配的值。 | 1635 1636**返回值**: 1637 1638| 类型 | 说明 | 1639| ------------------------------------ | -------------------------- | 1640| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1641 1642**示例:** 1643 1644```ts 1645// 匹配数据表的"AGE"列中在[18,20]中的值 1646let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1647predicates.in("AGE", [18, 20]); 1648``` 1649 1650### notIn 1651 1652notIn(field: string, value: Array<ValueType>): RdbPredicates 1653 1654将谓词配置为匹配数据字段为ValueType且值超出给定范围的指定字段。 1655 1656**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1657 1658**参数:** 1659 1660| 参数名 | 类型 | 必填 | 说明 | 1661| ------ | ------------------------------------ | ---- | ------------------------------------- | 1662| field | string | 是 | 数据库表中的列名。 | 1663| value | Array<[ValueType](#valuetype)> | 是 | 以ValueType数组形式指定的要匹配的值。 | 1664 1665**返回值**: 1666 1667| 类型 | 说明 | 1668| ------------------------------------ | -------------------------- | 1669| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1670 1671**示例:** 1672 1673```ts 1674// 匹配数据表的"NAME"列中不在["Lisa", "Rose"]中的值 1675let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1676predicates.notIn("NAME", ["Lisa", "Rose"]); 1677``` 1678 1679## RdbStore 1680 1681提供管理关系数据库(RDB)方法的接口。 1682 1683在使用以下相关接口前,请使用[executeSql](#executesql)接口初始化数据库表结构和相关数据。 1684 1685### 属性<sup>10+</sup> 1686 1687**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1688 1689| 名称 | 类型 | 必填 | 说明 | 1690| ------------ | ----------- | ---- | -------------------------------- | 1691| version<sup>10+</sup> | number | 是 | 设置和获取数据库版本,值为大于0的正整数。 | 1692 1693**示例:** 1694 1695```ts 1696// 设置数据库版本 1697if(store != undefined) { 1698 (store as relationalStore.RdbStore).version = 3; 1699 // 获取数据库版本 1700 console.info(`RdbStore version is ${store.version}`); 1701} 1702``` 1703 1704### insert 1705 1706insert(table: string, values: ValuesBucket, callback: AsyncCallback<number>):void 1707 1708向目标表中插入一行数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 1709 1710**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1711 1712**参数:** 1713 1714| 参数名 | 类型 | 必填 | 说明 | 1715| -------- | ----------------------------- | ---- | ---------------------------------------------------------- | 1716| table | string | 是 | 指定的目标表名。 | 1717| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 1718| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回行ID;否则返回-1。 | 1719 1720**错误码:** 1721 1722以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 1723 1724| **错误码ID** | **错误信息** | 1725| ------------ | -------------------------------------------- | 1726| 14800047 | The WAL file size exceeds the default limit. | 1727| 14800000 | Inner error. | 1728 1729**示例:** 1730 1731```ts 1732import { ValuesBucket } from '@ohos.data.ValuesBucket'; 1733 1734let value1 = "Lisa"; 1735let value2 = 18; 1736let value3 = 100.5; 1737let value4 = new Uint8Array([1, 2, 3, 4, 5]); 1738 1739// 以下三种方式可用 1740const valueBucket1: ValuesBucket = { 1741 'NAME': value1, 1742 'AGE': value2, 1743 'SALARY': value3, 1744 'CODES': value4, 1745}; 1746const valueBucket2: ValuesBucket = { 1747 NAME: value1, 1748 AGE: value2, 1749 SALARY: value3, 1750 CODES: value4, 1751}; 1752const valueBucket3: ValuesBucket = { 1753 "NAME": value1, 1754 "AGE": value2, 1755 "SALARY": value3, 1756 "CODES": value4, 1757}; 1758 1759if(store != undefined) { 1760 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1, (err: BusinessError, rowId: number) => { 1761 if (err) { 1762 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 1763 return; 1764 } 1765 console.info(`Insert is successful, rowId = ${rowId}`); 1766 }) 1767} 1768``` 1769 1770### insert<sup>10+</sup> 1771 1772insert(table: string, values: ValuesBucket, conflict: ConflictResolution, callback: AsyncCallback<number>):void 1773 1774向目标表中插入一行数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 1775 1776**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1777 1778**参数:** 1779 1780| 参数名 | 类型 | 必填 | 说明 | 1781| -------- | ------------------------------------------- | ---- | ---------------------------------------------------------- | 1782| table | string | 是 | 指定的目标表名。 | 1783| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 1784| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决方式。 | 1785| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回行ID;否则返回-1。 | 1786 1787**错误码:** 1788 1789以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 1790 1791| **错误码ID** | **错误信息** | 1792| ------------ | -------------------------------------------- | 1793| 14800047 | The WAL file size exceeds the default limit. | 1794| 14800000 | Inner error. | 1795 1796**示例:** 1797 1798```ts 1799import { ValuesBucket } from '@ohos.data.ValuesBucket'; 1800 1801let value1 = "Lisa"; 1802let value2 = 18; 1803let value3 = 100.5; 1804let value4 = new Uint8Array([1, 2, 3, 4, 5]); 1805 1806// 以下三种方式可用 1807const valueBucket1: ValuesBucket = { 1808 'NAME': value1, 1809 'AGE': value2, 1810 'SALARY': value3, 1811 'CODES': value4, 1812}; 1813const valueBucket2: ValuesBucket = { 1814 NAME: value1, 1815 AGE: value2, 1816 SALARY: value3, 1817 CODES: value4, 1818}; 1819const valueBucket3: ValuesBucket = { 1820 "NAME": value1, 1821 "AGE": value2, 1822 "SALARY": value3, 1823 "CODES": value4, 1824}; 1825 1826if(store != undefined) { 1827 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE, 1828 (err: BusinessError, rowId: number) => { 1829 if (err) { 1830 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 1831 return; 1832 } 1833 console.info(`Insert is successful, rowId = ${rowId}`); 1834 }) 1835} 1836``` 1837 1838### insert 1839 1840insert(table: string, values: ValuesBucket):Promise<number> 1841 1842向目标表中插入一行数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 1843 1844**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1845 1846**参数:** 1847 1848| 参数名 | 类型 | 必填 | 说明 | 1849| ------ | ----------------------------- | ---- | -------------------------- | 1850| table | string | 是 | 指定的目标表名。 | 1851| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 1852 1853**返回值**: 1854 1855| 类型 | 说明 | 1856| --------------------- | ------------------------------------------------- | 1857| Promise<number> | Promise对象。如果操作成功,返回行ID;否则返回-1。 | 1858 1859**错误码:** 1860 1861以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 1862 1863| **错误码ID** | **错误信息** | 1864| ------------ | -------------------------------------------- | 1865| 14800047 | The WAL file size exceeds the default limit. | 1866| 14800000 | Inner error. | 1867 1868**示例:** 1869 1870```ts 1871import { ValuesBucket } from '@ohos.data.ValuesBucket'; 1872import { BusinessError } from "@ohos.base"; 1873 1874let value1 = "Lisa"; 1875let value2 = 18; 1876let value3 = 100.5; 1877let value4 = new Uint8Array([1, 2, 3, 4, 5]); 1878 1879// 以下三种方式可用 1880const valueBucket1: ValuesBucket = { 1881 'NAME': value1, 1882 'AGE': value2, 1883 'SALARY': value3, 1884 'CODES': value4, 1885}; 1886const valueBucket2: ValuesBucket = { 1887 NAME: value1, 1888 AGE: value2, 1889 SALARY: value3, 1890 CODES: value4, 1891}; 1892const valueBucket3: ValuesBucket = { 1893 "NAME": value1, 1894 "AGE": value2, 1895 "SALARY": value3, 1896 "CODES": value4, 1897}; 1898 1899if(store != undefined) { 1900 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1).then((rowId: number) => { 1901 console.info(`Insert is successful, rowId = ${rowId}`); 1902 }).catch((err: BusinessError) => { 1903 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 1904 }) 1905} 1906``` 1907 1908### insert<sup>10+</sup> 1909 1910insert(table: string, values: ValuesBucket, conflict: ConflictResolution):Promise<number> 1911 1912向目标表中插入一行数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 1913 1914**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1915 1916**参数:** 1917 1918| 参数名 | 类型 | 必填 | 说明 | 1919| -------- | ------------------------------------------- | ---- | -------------------------- | 1920| table | string | 是 | 指定的目标表名。 | 1921| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 1922| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决方式。 | 1923 1924**返回值**: 1925 1926| 类型 | 说明 | 1927| --------------------- | ------------------------------------------------- | 1928| Promise<number> | Promise对象。如果操作成功,返回行ID;否则返回-1。 | 1929 1930**错误码:** 1931 1932以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 1933 1934| **错误码ID** | **错误信息** | 1935| ------------ | -------------------------------------------- | 1936| 14800047 | The WAL file size exceeds the default limit. | 1937| 14800000 | Inner error. | 1938 1939**示例:** 1940 1941```ts 1942import { ValuesBucket } from '@ohos.data.ValuesBucket'; 1943import { BusinessError } from "@ohos.base"; 1944 1945let value1 = "Lisa"; 1946let value2 = 18; 1947let value3 = 100.5; 1948let value4 = new Uint8Array([1, 2, 3, 4, 5]); 1949 1950// 以下三种方式可用 1951const valueBucket1: ValuesBucket = { 1952 'NAME': value1, 1953 'AGE': value2, 1954 'SALARY': value3, 1955 'CODES': value4, 1956}; 1957const valueBucket2: ValuesBucket = { 1958 NAME: value1, 1959 AGE: value2, 1960 SALARY: value3, 1961 CODES: value4, 1962}; 1963const valueBucket3: ValuesBucket = { 1964 "NAME": value1, 1965 "AGE": value2, 1966 "SALARY": value3, 1967 "CODES": value4, 1968}; 1969 1970if(store != undefined) { 1971 (store as relationalStore.RdbStore).insert("EMPLOYEE", valueBucket1, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE).then((rowId: number) => { 1972 console.info(`Insert is successful, rowId = ${rowId}`); 1973 }).catch((err: BusinessError) => { 1974 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 1975 }) 1976} 1977``` 1978 1979### batchInsert 1980 1981batchInsert(table: string, values: Array<ValuesBucket>, callback: AsyncCallback<number>):void 1982 1983向目标表中插入一组数据,使用callback异步回调。 1984 1985**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1986 1987**参数:** 1988 1989| 参数名 | 类型 | 必填 | 说明 | 1990| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 1991| table | string | 是 | 指定的目标表名。 | 1992| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 1993| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回插入的数据个数,否则返回-1。 | 1994 1995**错误码:** 1996 1997以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 1998 1999| **错误码ID** | **错误信息** | 2000| ------------ | -------------------------------------------- | 2001| 14800047 | The WAL file size exceeds the default limit. | 2002| 14800000 | Inner error. | 2003 2004**示例:** 2005 2006```ts 2007import { ValuesBucket } from '@ohos.data.ValuesBucket'; 2008 2009let value1 = "Lisa"; 2010let value2 = 18; 2011let value3 = 100.5; 2012let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2013let value5 = "Jack"; 2014let value6 = 19; 2015let value7 = 101.5; 2016let value8 = new Uint8Array([6, 7, 8, 9, 10]); 2017let value9 = "Tom"; 2018let value10 = 20; 2019let value11 = 102.5; 2020let value12 = new Uint8Array([11, 12, 13, 14, 15]); 2021 2022const valueBucket1: ValuesBucket = { 2023 'NAME': value1, 2024 'AGE': value2, 2025 'SALARY': value3, 2026 'CODES': value4, 2027}; 2028const valueBucket2: ValuesBucket = { 2029 'NAME': value5, 2030 'AGE': value6, 2031 'SALARY': value7, 2032 'CODES': value8, 2033}; 2034const valueBucket3: ValuesBucket = { 2035 'NAME': value9, 2036 'AGE': value10, 2037 'SALARY': value11, 2038 'CODES': value12, 2039}; 2040 2041let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 2042if(store != undefined) { 2043 (store as relationalStore.RdbStore).batchInsert("EMPLOYEE", valueBuckets, (err, insertNum) => { 2044 if (err) { 2045 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 2046 return; 2047 } 2048 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 2049 }) 2050} 2051``` 2052 2053### batchInsert 2054 2055batchInsert(table: string, values: Array<ValuesBucket>):Promise<number> 2056 2057向目标表中插入一组数据,使用Promise异步回调。 2058 2059**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2060 2061**参数:** 2062 2063| 参数名 | 类型 | 必填 | 说明 | 2064| ------ | ------------------------------------------ | ---- | ---------------------------- | 2065| table | string | 是 | 指定的目标表名。 | 2066| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 2067 2068**返回值**: 2069 2070| 类型 | 说明 | 2071| --------------------- | ----------------------------------------------------------- | 2072| Promise<number> | Promise对象。如果操作成功,返回插入的数据个数,否则返回-1。 | 2073 2074**错误码:** 2075 2076以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2077 2078| **错误码ID** | **错误信息** | 2079| ------------ | -------------------------------------------- | 2080| 14800047 | The WAL file size exceeds the default limit. | 2081| 14800000 | Inner error. | 2082 2083**示例:** 2084 2085```ts 2086import { ValuesBucket } from '@ohos.data.ValuesBucket'; 2087import { BusinessError } from "@ohos.base"; 2088 2089let value1 = "Lisa"; 2090let value2 = 18; 2091let value3 = 100.5; 2092let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2093let value5 = "Jack"; 2094let value6 = 19; 2095let value7 = 101.5; 2096let value8 = new Uint8Array([6, 7, 8, 9, 10]); 2097let value9 = "Tom"; 2098let value10 = 20; 2099let value11 = 102.5; 2100let value12 = new Uint8Array([11, 12, 13, 14, 15]); 2101 2102const valueBucket1: ValuesBucket = { 2103 'NAME': value1, 2104 'AGE': value2, 2105 'SALARY': value3, 2106 'CODES': value4, 2107}; 2108const valueBucket2: ValuesBucket = { 2109 'NAME': value5, 2110 'AGE': value6, 2111 'SALARY': value7, 2112 'CODES': value8, 2113}; 2114const valueBucket3: ValuesBucket = { 2115 'NAME': value9, 2116 'AGE': value10, 2117 'SALARY': value11, 2118 'CODES': value12, 2119}; 2120 2121let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 2122if(store != undefined) { 2123 (store as relationalStore.RdbStore).batchInsert("EMPLOYEE", valueBuckets).then((insertNum: number) => { 2124 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 2125 }).catch((err: BusinessError) => { 2126 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 2127 }) 2128} 2129``` 2130 2131### update 2132 2133update(values: ValuesBucket, predicates: RdbPredicates, callback: AsyncCallback<number>):void 2134 2135根据RdbPredicates的指定实例对象更新数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2136 2137**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2138 2139**参数:** 2140 2141| 参数名 | 类型 | 必填 | 说明 | 2142| ---------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 2143| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 2144| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 2145| callback | AsyncCallback<number> | 是 | 指定的callback回调方法。返回受影响的行数。 | 2146 2147**错误码:** 2148 2149以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2150 2151| **错误码ID** | **错误信息** | 2152| ------------ | -------------------------------------------- | 2153| 14800047 | The WAL file size exceeds the default limit. | 2154| 14800000 | Inner error. | 2155 2156**示例:** 2157 2158```ts 2159import { ValuesBucket } from '@ohos.data.ValuesBucket'; 2160 2161let value1 = "Rose"; 2162let value2 = 22; 2163let value3 = 200.5; 2164let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2165 2166// 以下三种方式可用 2167const valueBucket1: ValuesBucket = { 2168 'NAME': value1, 2169 'AGE': value2, 2170 'SALARY': value3, 2171 'CODES': value4, 2172}; 2173const valueBucket2: ValuesBucket = { 2174 NAME: value1, 2175 AGE: value2, 2176 SALARY: value3, 2177 CODES: value4, 2178}; 2179const valueBucket3: ValuesBucket = { 2180 "NAME": value1, 2181 "AGE": value2, 2182 "SALARY": value3, 2183 "CODES": value4, 2184}; 2185 2186let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2187predicates.equalTo("NAME", "Lisa"); 2188if(store != undefined) { 2189 (store as relationalStore.RdbStore).update(valueBucket1, predicates,(err, rows) => { 2190 if (err) { 2191 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 2192 return; 2193 } 2194 console.info(`Updated row count: ${rows}`); 2195 }) 2196} 2197``` 2198 2199### update<sup>10+</sup> 2200 2201update(values: ValuesBucket, predicates: RdbPredicates, conflict: ConflictResolution, callback: AsyncCallback<number>):void 2202 2203根据RdbPredicates的指定实例对象更新数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2204 2205**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2206 2207**参数:** 2208 2209| 参数名 | 类型 | 必填 | 说明 | 2210| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 2211| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 2212| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 2213| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决方式。 | 2214| callback | AsyncCallback<number> | 是 | 指定的callback回调方法。返回受影响的行数。 | 2215 2216**错误码:** 2217 2218以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2219 2220| **错误码ID** | **错误信息** | 2221| ------------ | -------------------------------------------- | 2222| 14800047 | The WAL file size exceeds the default limit. | 2223| 14800000 | Inner error. | 2224 2225**示例:** 2226 2227```ts 2228import { ValuesBucket } from '@ohos.data.ValuesBucket'; 2229 2230let value1 = "Rose"; 2231let value2 = 22; 2232let value3 = 200.5; 2233let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2234 2235// 以下三种方式可用 2236const valueBucket1: ValuesBucket = { 2237 'NAME': value1, 2238 'AGE': value2, 2239 'SALARY': value3, 2240 'CODES': value4, 2241}; 2242const valueBucket2: ValuesBucket = { 2243 NAME: value1, 2244 AGE: value2, 2245 SALARY: value3, 2246 CODES: value4, 2247}; 2248const valueBucket3: ValuesBucket = { 2249 "NAME": value1, 2250 "AGE": value2, 2251 "SALARY": value3, 2252 "CODES": value4, 2253}; 2254 2255let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2256predicates.equalTo("NAME", "Lisa"); 2257if(store != undefined) { 2258 (store as relationalStore.RdbStore).update(valueBucket1, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE, (err, rows) => { 2259 if (err) { 2260 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 2261 return; 2262 } 2263 console.info(`Updated row count: ${rows}`); 2264 }) 2265} 2266``` 2267 2268### update 2269 2270update(values: ValuesBucket, predicates: RdbPredicates):Promise<number> 2271 2272根据RdbPredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2273 2274**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2275 2276**参数:** 2277 2278| 参数名 | 类型 | 必填 | 说明 | 2279| ------------ | ------------------------------------ | ---- | ------------------------------------------------------------ | 2280| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 2281| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 2282 2283**返回值**: 2284 2285| 类型 | 说明 | 2286| --------------------- | ----------------------------------------- | 2287| Promise<number> | 指定的Promise回调方法。返回受影响的行数。 | 2288 2289**错误码:** 2290 2291以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2292 2293| **错误码ID** | **错误信息** | 2294| ------------ | -------------------------------------------- | 2295| 14800047 | The WAL file size exceeds the default limit. | 2296| 14800000 | Inner error. | 2297 2298**示例:** 2299 2300```ts 2301import { ValuesBucket } from '@ohos.data.ValuesBucket'; 2302import { BusinessError } from "@ohos.base"; 2303 2304let value1 = "Rose"; 2305let value2 = 22; 2306let value3 = 200.5; 2307let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2308 2309// 以下三种方式可用 2310const valueBucket1: ValuesBucket = { 2311 'NAME': value1, 2312 'AGE': value2, 2313 'SALARY': value3, 2314 'CODES': value4, 2315}; 2316const valueBucket2: ValuesBucket = { 2317 NAME: value1, 2318 AGE: value2, 2319 SALARY: value3, 2320 CODES: value4, 2321}; 2322const valueBucket3: ValuesBucket = { 2323 "NAME": value1, 2324 "AGE": value2, 2325 "SALARY": value3, 2326 "CODES": value4, 2327}; 2328 2329let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2330predicates.equalTo("NAME", "Lisa"); 2331if(store != undefined) { 2332 (store as relationalStore.RdbStore).update(valueBucket1, predicates).then(async (rows: Number) => { 2333 console.info(`Updated row count: ${rows}`); 2334 }).catch((err: BusinessError) => { 2335 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 2336 }) 2337} 2338``` 2339 2340### update<sup>10+</sup> 2341 2342update(values: ValuesBucket, predicates: RdbPredicates, conflict: ConflictResolution):Promise<number> 2343 2344根据RdbPredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2345 2346**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2347 2348**参数:** 2349 2350| 参数名 | 类型 | 必填 | 说明 | 2351| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 2352| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 2353| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 2354| conflict | [ConflictResolution](#conflictresolution10) | 是 | 指定冲突解决方式。 | 2355 2356**返回值**: 2357 2358| 类型 | 说明 | 2359| --------------------- | ----------------------------------------- | 2360| Promise<number> | 指定的Promise回调方法。返回受影响的行数。 | 2361 2362**错误码:** 2363 2364以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2365 2366| **错误码ID** | **错误信息** | 2367| ------------ | -------------------------------------------- | 2368| 14800047 | The WAL file size exceeds the default limit. | 2369| 14800000 | Inner error. | 2370 2371**示例:** 2372 2373```ts 2374import { ValuesBucket } from '@ohos.data.ValuesBucket'; 2375import { BusinessError } from "@ohos.base"; 2376 2377let value1 = "Rose"; 2378let value2 = 22; 2379let value3 = 200.5; 2380let value4 = new Uint8Array([1, 2, 3, 4, 5]); 2381 2382// 以下三种方式可用 2383const valueBucket1: ValuesBucket = { 2384 'NAME': value1, 2385 'AGE': value2, 2386 'SALARY': value3, 2387 'CODES': value4, 2388}; 2389const valueBucket2: ValuesBucket = { 2390 NAME: value1, 2391 AGE: value2, 2392 SALARY: value3, 2393 CODES: value4, 2394}; 2395const valueBucket3: ValuesBucket = { 2396 "NAME": value1, 2397 "AGE": value2, 2398 "SALARY": value3, 2399 "CODES": value4, 2400}; 2401 2402let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2403predicates.equalTo("NAME", "Lisa"); 2404if(store != undefined) { 2405 (store as relationalStore.RdbStore).update(valueBucket1, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE).then(async (rows: Number) => { 2406 console.info(`Updated row count: ${rows}`); 2407 }).catch((err: BusinessError) => { 2408 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 2409 }) 2410} 2411``` 2412 2413### delete 2414 2415delete(predicates: RdbPredicates, callback: AsyncCallback<number>):void 2416 2417根据RdbPredicates的指定实例对象从数据库中删除数据,使用callback异步回调。 2418 2419**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2420 2421**参数:** 2422 2423| 参数名 | 类型 | 必填 | 说明 | 2424| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 2425| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 2426| callback | AsyncCallback<number> | 是 | 指定callback回调函数。返回受影响的行数。 | 2427 2428**错误码:** 2429 2430以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2431 2432| **错误码ID** | **错误信息** | 2433| ------------ | -------------------------------------------- | 2434| 14800047 | The WAL file size exceeds the default limit. | 2435| 14800000 | Inner error. | 2436 2437**示例:** 2438 2439```ts 2440let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2441predicates.equalTo("NAME", "Lisa"); 2442if(store != undefined) { 2443 (store as relationalStore.RdbStore).delete(predicates, (err, rows) => { 2444 if (err) { 2445 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 2446 return; 2447 } 2448 console.info(`Delete rows: ${rows}`); 2449 }) 2450} 2451``` 2452 2453### delete 2454 2455delete(predicates: RdbPredicates):Promise<number> 2456 2457根据RdbPredicates的指定实例对象从数据库中删除数据,使用Promise异步回调。 2458 2459**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2460 2461**参数:** 2462 2463| 参数名 | 类型 | 必填 | 说明 | 2464| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 2465| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 2466 2467**返回值**: 2468 2469| 类型 | 说明 | 2470| --------------------- | ------------------------------- | 2471| Promise<number> | Promise对象。返回受影响的行数。 | 2472 2473**错误码:** 2474 2475以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2476 2477| **错误码ID** | **错误信息** | 2478| ------------ | -------------------------------------------- | 2479| 14800047 | The WAL file size exceeds the default limit. | 2480| 14800000 | Inner error. | 2481 2482**示例:** 2483 2484```ts 2485import { BusinessError } from "@ohos.base"; 2486 2487let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2488predicates.equalTo("NAME", "Lisa"); 2489if(store != undefined) { 2490 (store as relationalStore.RdbStore).delete(predicates).then((rows: Number) => { 2491 console.info(`Delete rows: ${rows}`); 2492 }).catch((err: BusinessError) => { 2493 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 2494 }) 2495} 2496``` 2497 2498### query<sup>10+</sup> 2499 2500query(predicates: RdbPredicates, callback: AsyncCallback<ResultSet>):void 2501 2502根据指定条件查询数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2503 2504**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2505 2506**参数:** 2507 2508| 参数名 | 类型 | 必填 | 说明 | 2509| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 2510| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 2511| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 2512 2513**错误码:** 2514 2515以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2516 2517| **错误码ID** | **错误信息** | 2518| ------------ | ---------------------------- | 2519| 14800000 | Inner error. | 2520 2521**示例:** 2522 2523```ts 2524let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2525predicates.equalTo("NAME", "Rose"); 2526if(store != undefined) { 2527 (store as relationalStore.RdbStore).query(predicates, (err, resultSet) => { 2528 if (err) { 2529 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 2530 return; 2531 } 2532 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 2533 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 2534 while (resultSet.goToNextRow()) { 2535 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 2536 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 2537 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 2538 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 2539 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 2540 } 2541 // 释放数据集的内存 2542 resultSet.close(); 2543 }) 2544} 2545``` 2546 2547### query 2548 2549query(predicates: RdbPredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void 2550 2551根据指定条件查询数据库中的数据,使用callback异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2552 2553**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2554 2555**参数:** 2556 2557| 参数名 | 类型 | 必填 | 说明 | 2558| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 2559| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 2560| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 2561| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 2562 2563**错误码:** 2564 2565以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2566 2567| **错误码ID** | **错误信息** | 2568| ------------ | ---------------------------- | 2569| 14800000 | Inner error. | 2570 2571**示例:** 2572 2573```ts 2574let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2575predicates.equalTo("NAME", "Rose"); 2576if(store != undefined) { 2577 (store as relationalStore.RdbStore).query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], (err, resultSet) => { 2578 if (err) { 2579 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 2580 return; 2581 } 2582 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 2583 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 2584 while (resultSet.goToNextRow()) { 2585 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 2586 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 2587 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 2588 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 2589 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 2590 } 2591 // 释放数据集的内存 2592 resultSet.close(); 2593 }) 2594} 2595``` 2596 2597### query 2598 2599query(predicates: RdbPredicates, columns?: Array<string>):Promise<ResultSet> 2600 2601根据指定条件查询数据库中的数据,使用Promise异步回调。由于共享内存大小限制为2Mb,因此单条数据的大小需小于2Mb,否则会查询失败。 2602 2603**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2604 2605**参数:** 2606 2607| 参数名 | 类型 | 必填 | 说明 | 2608| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 2609| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 2610| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 2611 2612**错误码:** 2613 2614以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2615 2616| **错误码ID** | **错误信息** | 2617| ------------ | ---------------------------- | 2618| 14800000 | Inner error. | 2619 2620**返回值**: 2621 2622| 类型 | 说明 | 2623| ------------------------------------------------------- | -------------------------------------------------- | 2624| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 2625 2626**示例:** 2627 2628```ts 2629import { BusinessError } from "@ohos.base"; 2630 2631let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2632predicates.equalTo("NAME", "Rose"); 2633if(store != undefined) { 2634 (store as relationalStore.RdbStore).query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((resultSet: relationalStore.ResultSet) => { 2635 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 2636 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 2637 while (resultSet.goToNextRow()) { 2638 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 2639 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 2640 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 2641 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 2642 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 2643 } 2644 // 释放数据集的内存 2645 resultSet.close(); 2646 }).catch((err: BusinessError) => { 2647 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 2648 }) 2649} 2650``` 2651 2652### remoteQuery 2653 2654remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array<string> , callback: AsyncCallback<ResultSet>): void 2655 2656根据指定条件查询远程设备数据库中的数据。使用callback异步回调。 2657 2658> **说明:** 2659> 2660> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 2661 2662**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2663 2664**参数:** 2665 2666| 参数名 | 类型 | 必填 | 说明 | 2667| ---------- | -------------------------------------------- | ---- | --------------------------------------------------------- | 2668| device | string | 是 | 指定的远程设备ID。 | 2669| table | string | 是 | 指定的目标表名。 | 2670| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象,指定查询的条件。 | 2671| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 2672| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 2673 2674**错误码:** 2675 2676以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2677 2678| **错误码ID** | **错误信息** | 2679| ------------ | ---------------------------- | 2680| 14800000 | Inner error. | 2681 2682**示例:** 2683 2684```ts 2685import deviceManager from '@ohos.distributedDeviceManager'; 2686import { BusinessError } from "@ohos.base"; 2687 2688let dmInstance: deviceManager.DeviceManager; 2689let deviceId: string | undefined = undefined; 2690 2691try { 2692 dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); 2693 let devices = dmInstance.getAvailableDeviceListSync(); 2694 if(deviceId != undefined) { 2695 deviceId = devices[0].networkId; 2696 } 2697} catch (err) { 2698 let code = (err as BusinessError).code; 2699 let message = (err as BusinessError).message; 2700 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 2701} 2702 2703let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 2704predicates.greaterThan("id", 0); 2705if(store != undefined && deviceId != undefined) { 2706 (store as relationalStore.RdbStore).remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((resultSet: relationalStore.ResultSet) => { 2707 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 2708 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 2709 while (resultSet.goToNextRow()) { 2710 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 2711 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 2712 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 2713 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 2714 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 2715 } 2716 // 释放数据集的内存 2717 resultSet.close(); 2718 }).catch((err: BusinessError) => { 2719 console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`); 2720 }) 2721} 2722``` 2723 2724### remoteQuery 2725 2726remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array<string>): Promise<ResultSet> 2727 2728根据指定条件查询远程设备数据库中的数据。使用Promise异步回调。 2729 2730> **说明:** 2731> 2732> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 2733 2734**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2735 2736**参数:** 2737 2738| 参数名 | 类型 | 必填 | 说明 | 2739| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 2740| device | string | 是 | 指定的远程设备ID。 | 2741| table | string | 是 | 指定的目标表名。 | 2742| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象,指定查询的条件。 | 2743| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 2744 2745**返回值**: 2746 2747| 类型 | 说明 | 2748| ------------------------------------------------------------ | -------------------------------------------------- | 2749| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 2750 2751**错误码:** 2752 2753以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2754 2755| **错误码ID** | **错误信息** | 2756| ------------ | ---------------------------- | 2757| 14800000 | Inner error. | 2758 2759**示例:** 2760 2761```ts 2762import deviceManager from '@ohos.distributedDeviceManager'; 2763import { BusinessError } from "@ohos.base"; 2764 2765let dmInstance: deviceManager.DeviceManager; 2766let deviceId: string | undefined = undefined; 2767 2768try { 2769 dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); 2770 let devices: Array<deviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 2771 if(devices != undefined) { 2772 deviceId = devices[0].networkId; 2773 } 2774} catch (err) { 2775 let code = (err as BusinessError).code; 2776 let message = (err as BusinessError).message; 2777 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 2778} 2779 2780let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 2781predicates.greaterThan("id", 0); 2782if(store != undefined && deviceId != undefined) { 2783 (store as relationalStore.RdbStore).remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((resultSet: relationalStore.ResultSet) => { 2784 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 2785 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 2786 while (resultSet.goToNextRow()) { 2787 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 2788 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 2789 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 2790 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 2791 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 2792 } 2793 // 释放数据集的内存 2794 resultSet.close(); 2795 }).catch((err: BusinessError) => { 2796 console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`); 2797 }) 2798} 2799``` 2800 2801### querySql<sup>10+</sup> 2802 2803querySql(sql: string, callback: AsyncCallback<ResultSet>):void 2804 2805根据指定SQL语句查询数据库中的数据,使用callback异步回调。 2806 2807**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2808 2809**参数:** 2810 2811| 参数名 | 类型 | 必填 | 说明 | 2812| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 2813| sql | string | 是 | 指定要执行的SQL语句。 | 2814| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 2815 2816**错误码:** 2817 2818以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2819 2820| **错误码ID** | **错误信息** | 2821| ------------ | ---------------------------- | 2822| 14800000 | Inner error. | 2823 2824**示例:** 2825 2826```ts 2827if(store != undefined) { 2828 (store as relationalStore.RdbStore).querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'", (err, resultSet) => { 2829 if (err) { 2830 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 2831 return; 2832 } 2833 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 2834 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 2835 while (resultSet.goToNextRow()) { 2836 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 2837 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 2838 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 2839 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 2840 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 2841 } 2842 // 释放数据集的内存 2843 resultSet.close(); 2844 }) 2845} 2846``` 2847 2848### querySql 2849 2850querySql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<ResultSet>):void 2851 2852根据指定SQL语句查询数据库中的数据,使用callback异步回调。 2853 2854**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2855 2856**参数:** 2857 2858| 参数名 | 类型 | 必填 | 说明 | 2859| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 2860| sql | string | 是 | 指定要执行的SQL语句。 | 2861| bindArgs | Array<[ValueType](#valuetype)> | 是 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数需为空数组。 | 2862| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 2863 2864**错误码:** 2865 2866以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2867 2868| **错误码ID** | **错误信息** | 2869| ------------ | ---------------------------- | 2870| 14800000 | Inner error. | 2871 2872**示例:** 2873 2874```ts 2875if(store != undefined) { 2876 (store as relationalStore.RdbStore).querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], (err, resultSet) => { 2877 if (err) { 2878 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 2879 return; 2880 } 2881 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 2882 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 2883 while (resultSet.goToNextRow()) { 2884 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 2885 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 2886 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 2887 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 2888 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 2889 } 2890 // 释放数据集的内存 2891 resultSet.close(); 2892 }) 2893} 2894``` 2895 2896### querySql 2897 2898querySql(sql: string, bindArgs?: Array<ValueType>):Promise<ResultSet> 2899 2900根据指定SQL语句查询数据库中的数据,使用Promise异步回调。 2901 2902**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2903 2904**参数:** 2905 2906| 参数名 | 类型 | 必填 | 说明 | 2907| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 2908| sql | string | 是 | 指定要执行的SQL语句。 | 2909| bindArgs | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 2910 2911**返回值**: 2912 2913| 类型 | 说明 | 2914| ------------------------------------------------------- | -------------------------------------------------- | 2915| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 2916 2917**错误码:** 2918 2919以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2920 2921| **错误码ID** | **错误信息** | 2922| ------------ | ---------------------------- | 2923| 14800000 | Inner error. | 2924 2925**示例:** 2926 2927```ts 2928import { BusinessError } from "@ohos.base"; 2929 2930if(store != undefined) { 2931 (store as relationalStore.RdbStore).querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'").then((resultSet: relationalStore.ResultSet) => { 2932 console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 2933 // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 2934 while (resultSet.goToNextRow()) { 2935 const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 2936 const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 2937 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 2938 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 2939 console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`); 2940 } 2941 // 释放数据集的内存 2942 resultSet.close(); 2943 }).catch((err: BusinessError) => { 2944 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 2945 }) 2946} 2947``` 2948 2949### executeSql<sup>10+</sup> 2950 2951executeSql(sql: string, callback: AsyncCallback<void>):void 2952 2953执行包含指定参数但不返回值的SQL语句,使用callback异步回调。 2954 2955**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2956 2957**参数:** 2958 2959| 参数名 | 类型 | 必填 | 说明 | 2960| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 2961| sql | string | 是 | 指定要执行的SQL语句。 | 2962| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 2963 2964**错误码:** 2965 2966以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 2967 2968| **错误码ID** | **错误信息** | 2969| ------------ | -------------------------------------------- | 2970| 14800047 | The WAL file size exceeds the default limit. | 2971| 14800000 | Inner error. | 2972 2973**示例:** 2974 2975```ts 2976const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = 'zhangsan'" 2977if(store != undefined) { 2978 (store as relationalStore.RdbStore).executeSql(SQL_DELETE_TABLE, (err) => { 2979 if (err) { 2980 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 2981 return; 2982 } 2983 console.info('Delete table done.'); 2984 }) 2985} 2986``` 2987 2988### executeSql 2989 2990executeSql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<void>):void 2991 2992执行包含指定参数但不返回值的SQL语句,使用callback异步回调。 2993 2994**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2995 2996**参数:** 2997 2998| 参数名 | 类型 | 必填 | 说明 | 2999| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 3000| sql | string | 是 | 指定要执行的SQL语句。 | 3001| bindArgs | Array<[ValueType](#valuetype)> | 是 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数需为空数组。 | 3002| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 3003 3004**错误码:** 3005 3006以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3007 3008| **错误码ID** | **错误信息** | 3009| ------------ | -------------------------------------------- | 3010| 14800047 | The WAL file size exceeds the default limit. | 3011| 14800000 | Inner error. | 3012 3013**示例:** 3014 3015```ts 3016const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = ?" 3017if(store != undefined) { 3018 (store as relationalStore.RdbStore).executeSql(SQL_DELETE_TABLE, ['zhangsan'], (err) => { 3019 if (err) { 3020 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 3021 return; 3022 } 3023 console.info('Delete table done.'); 3024 }) 3025} 3026``` 3027 3028### executeSql 3029 3030executeSql(sql: string, bindArgs?: Array<ValueType>):Promise<void> 3031 3032执行包含指定参数但不返回值的SQL语句,使用Promise异步回调。 3033 3034**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3035 3036**参数:** 3037 3038| 参数名 | 类型 | 必填 | 说明 | 3039| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 3040| sql | string | 是 | 指定要执行的SQL语句。 | 3041| bindArgs | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 3042 3043**返回值**: 3044 3045| 类型 | 说明 | 3046| ------------------- | ------------------------- | 3047| Promise<void> | 无返回结果的Promise对象。 | 3048 3049**错误码:** 3050 3051以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3052 3053| **错误码ID** | **错误信息** | 3054| ------------ | -------------------------------------------- | 3055| 14800047 | The WAL file size exceeds the default limit. | 3056| 14800000 | Inner error. | 3057 3058**示例:** 3059 3060```ts 3061import { BusinessError } from "@ohos.base"; 3062 3063const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = 'zhangsan'" 3064if(store != undefined) { 3065 (store as relationalStore.RdbStore).executeSql(SQL_DELETE_TABLE).then(() => { 3066 console.info('Delete table done.'); 3067 }).catch((err: BusinessError) => { 3068 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 3069 }) 3070} 3071``` 3072 3073### getModifyTime<sup>10+</sup> 3074 3075getModifyTime(table: string, columnName: string, primaryKeys: PRIKeyType[], callback: AsyncCallback<ModifyTime>): void 3076 3077获取数据库表中数据的最后修改时间,使用callback异步回调。 3078 3079**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3080 3081**参数:** 3082 3083| 参数名 | 类型 | 必填 | 说明 | 3084| ----------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ | 3085| table | string | 是 | 指定要查询的数据库表的表名。 | 3086| columnName | string | 是 | 指定要查询的数据库表的列名。 | 3087| primaryKeys | [PRIKeyType](#prikeytype10)[] | 是 | 指定要查询的行的主键。<br>如果数据库表无主键,参数columnName需传入"rowid",此时primaryKeys为要查询的数据库表的行号。<br>如果数据库表无主键,参数columnName传入不为"rowid",返回对应的错误码。 | 3088| callback | AsyncCallback<[ModifyTime](#modifytime10)> | 是 | 指定callback回调函数。如果操作成功,则返回ModifyTime对象,表示数据的最后修改时间。 | 3089 3090**错误码:** 3091 3092以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3093 3094| **错误码ID** | **错误信息** | 3095| ------------ | ------------ | 3096| 14800000 | Inner error. | 3097 3098**示例:** 3099 3100```ts 3101let PRIKey = [1, 4, 2, 3]; 3102if(store != undefined) { 3103 (store as relationalStore.RdbStore).getModifyTime("EMPLOYEE", "NAME", PRIKey, (err, modifyTime: relationalStore.ModifyTime) => { 3104 if (err) { 3105 console.error(`getModifyTime failed, code is ${err.code},message is ${err.message}`); 3106 return; 3107 } 3108 let size = modifyTime.size; 3109 }); 3110} 3111``` 3112 3113### getModifyTime<sup>10+</sup> 3114 3115getModifyTime(table: string, columnName: string, primaryKeys: PRIKeyType[]): Promise<ModifyTime> 3116 3117获取数据库表中数据的最后修改时间,使用Promise异步回调。 3118 3119**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3120 3121**参数:** 3122 3123| 参数名 | 类型 | 必填 | 说明 | 3124| ----------- | ----------------------------- | ---- | ------------------------------------------------------------ | 3125| table | string | 是 | 指定要查询的数据库表的表名。 | 3126| columnName | string | 是 | 指定要查询的数据库表的列名。 | 3127| primaryKeys | [PRIKeyType](#prikeytype10)[] | 是 | 指定要查询的行的主键。<br>如果数据库表无主键,参数columnName需传入"rowid",此时primaryKeys为要查询的数据库表的行号。<br>如果数据库表无主键,参数columnName传入不为"rowid",返回对应的错误码。 | 3128 3129**返回值**: 3130 3131| 类型 | 说明 | 3132| ------------------------------------------ | --------------------------------------------------------- | 3133| Promise<[ModifyTime](#modifytime10)> | 返回ModifyTime类型的Promise对象,表示数据最后的修改时间。 | 3134 3135**错误码:** 3136 3137以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3138 3139| **错误码ID** | **错误信息** | 3140| ------------ | ------------ | 3141| 14800000 | Inner error. | 3142 3143**示例:** 3144 3145```ts 3146import { BusinessError } from "@ohos.base"; 3147 3148let PRIKey = [1, 2, 3]; 3149if(store != undefined) { 3150 (store as relationalStore.RdbStore).getModifyTime("EMPLOYEE", "NAME", PRIKey) 3151 .then((modifyTime: relationalStore.ModifyTime) => { 3152 let size = modifyTime.size; 3153 }) 3154 .catch((err: BusinessError) => { 3155 console.error(`getModifyTime failed, code is ${err.code},message is ${err.message}`); 3156 }); 3157} 3158``` 3159 3160### beginTransaction 3161 3162beginTransaction():void 3163 3164在开始执行SQL语句之前,开始事务。 3165此接口不支持在多进程或多线程中使用。 3166 3167**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3168 3169**错误码:** 3170 3171以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3172 3173| **错误码ID** | **错误信息** | 3174| ------------ | -------------------------------------------- | 3175| 14800047 | The WAL file size exceeds the default limit. | 3176| 14800000 | Inner error. | 3177 3178**示例:** 3179 3180```ts 3181import featureAbility from '@ohos.ability.featureAbility' 3182import { ValuesBucket } from '@ohos.data.ValuesBucket'; 3183 3184let value1 = "Lisa"; 3185let value2 = 18; 3186let value3 = 100.5; 3187let value4 = new Uint8Array([1, 2, 3]); 3188 3189store.beginTransaction(); 3190const valueBucket: ValuesBucket = { 3191 'NAME': value1, 3192 'AGE': value2, 3193 'SALARY': value3, 3194 'CODES': value4, 3195}; 3196store.insert("test", valueBucket); 3197store.commit(); 3198``` 3199 3200### commit 3201 3202commit():void 3203 3204提交已执行的SQL语句。 3205此接口不支持在多进程或多线程中使用。 3206 3207**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3208 3209**示例:** 3210 3211```ts 3212import { ValuesBucket } from '@ohos.data.ValuesBucket'; 3213 3214let value1 = "Lisa"; 3215let value2 = 18; 3216let value3 = 100.5; 3217let value4 = new Uint8Array([1, 2, 3]); 3218 3219store.beginTransaction(); 3220const valueBucket: ValuesBucket = { 3221 'NAME': value1, 3222 'AGE': value2, 3223 'SALARY': value3, 3224 'CODES': value4, 3225}; 3226store.insert("test", valueBucket); 3227store.commit(); 3228``` 3229 3230### rollBack 3231 3232rollBack():void 3233 3234回滚已经执行的SQL语句。 3235此接口不支持在多进程或多线程中使用。 3236 3237**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3238 3239**示例:** 3240 3241```ts 3242import { ValuesBucket } from '@ohos.data.ValuesBucket'; 3243 3244let value1 = "Lisa"; 3245let value2 = 18; 3246let value3 = 100.5; 3247let value4 = new Uint8Array([1, 2, 3]); 3248 3249try { 3250 store.beginTransaction() 3251 const valueBucket: ValuesBucket = { 3252 'NAME': value1, 3253 'AGE': value2, 3254 'SALARY': value3, 3255 'CODES': value4, 3256 }; 3257 store.insert("test", valueBucket); 3258 store.commit(); 3259} catch (err) { 3260 let code = (err as BusinessError).code; 3261 let message = (err as BusinessError).message 3262 console.error(`Transaction failed, code is ${code},message is ${message}`); 3263 store.rollBack(); 3264} 3265``` 3266 3267### backup 3268 3269backup(destName:string, callback: AsyncCallback<void>):void 3270 3271以指定名称备份数据库,使用callback异步回调。 3272 3273**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3274 3275**参数:** 3276 3277| 参数名 | 类型 | 必填 | 说明 | 3278| -------- | ------------------------- | ---- | ------------------------ | 3279| destName | string | 是 | 指定数据库的备份文件名。 | 3280| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 3281 3282**错误码:** 3283 3284以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3285 3286| **错误码ID** | **错误信息** | 3287| ------------ | ---------------------------- | 3288| 14800000 | Inner error. | 3289 3290**示例:** 3291 3292```ts 3293if(store != undefined) { 3294 (store as relationalStore.RdbStore).backup("dbBackup.db", (err) => { 3295 if (err) { 3296 console.error(`Backup failed, code is ${err.code},message is ${err.message}`); 3297 return; 3298 } 3299 console.info('Backup success.'); 3300 }) 3301} 3302``` 3303 3304### backup 3305 3306backup(destName:string): Promise<void> 3307 3308以指定名称备份数据库,使用Promise异步回调。 3309 3310**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3311 3312**参数:** 3313 3314| 参数名 | 类型 | 必填 | 说明 | 3315| -------- | ------ | ---- | ------------------------ | 3316| destName | string | 是 | 指定数据库的备份文件名。 | 3317 3318**返回值**: 3319 3320| 类型 | 说明 | 3321| ------------------- | ------------------------- | 3322| Promise<void> | 无返回结果的Promise对象。 | 3323 3324**错误码:** 3325 3326以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3327 3328| **错误码ID** | **错误信息** | 3329| ------------ | ---------------------------- | 3330| 14800000 | Inner error. | 3331 3332**示例:** 3333 3334```ts 3335import { BusinessError } from "@ohos.base"; 3336 3337if(store != undefined) { 3338 let promiseBackup = (store as relationalStore.RdbStore).backup("dbBackup.db"); 3339 promiseBackup.then(() => { 3340 console.info('Backup success.'); 3341 }).catch((err: BusinessError) => { 3342 console.error(`Backup failed, code is ${err.code},message is ${err.message}`); 3343 }) 3344} 3345``` 3346 3347### restore 3348 3349restore(srcName:string, callback: AsyncCallback<void>):void 3350 3351从指定的数据库备份文件恢复数据库,使用callback异步回调。 3352 3353**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3354 3355**参数:** 3356 3357| 参数名 | 类型 | 必填 | 说明 | 3358| -------- | ------------------------- | ---- | ------------------------ | 3359| srcName | string | 是 | 指定数据库的备份文件名。 | 3360| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 3361 3362**错误码:** 3363 3364以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3365 3366| **错误码ID** | **错误信息** | 3367| ------------ | ---------------------------- | 3368| 14800000 | Inner error. | 3369 3370**示例:** 3371 3372```ts 3373if(store != undefined) { 3374 (store as relationalStore.RdbStore).restore("dbBackup.db", (err) => { 3375 if (err) { 3376 console.error(`Restore failed, code is ${err.code},message is ${err.message}`); 3377 return; 3378 } 3379 console.info('Restore success.'); 3380 }) 3381} 3382``` 3383 3384### restore 3385 3386restore(srcName:string): Promise<void> 3387 3388从指定的数据库备份文件恢复数据库,使用Promise异步回调。 3389 3390**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3391 3392**参数:** 3393 3394| 参数名 | 类型 | 必填 | 说明 | 3395| ------- | ------ | ---- | ------------------------ | 3396| srcName | string | 是 | 指定数据库的备份文件名。 | 3397 3398**返回值**: 3399 3400| 类型 | 说明 | 3401| ------------------- | ------------------------- | 3402| Promise<void> | 无返回结果的Promise对象。 | 3403 3404**错误码:** 3405 3406以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3407 3408| **错误码ID** | **错误信息** | 3409| ------------ | ---------------------------- | 3410| 14800000 | Inner error. | 3411 3412**示例:** 3413 3414```ts 3415import { BusinessError } from "@ohos.base"; 3416 3417if(store != undefined) { 3418 let promiseRestore = (store as relationalStore.RdbStore).restore("dbBackup.db"); 3419 promiseRestore.then(() => { 3420 console.info('Restore success.'); 3421 }).catch((err: BusinessError) => { 3422 console.error(`Restore failed, code is ${err.code},message is ${err.message}`); 3423 }) 3424} 3425``` 3426 3427### setDistributedTables 3428 3429setDistributedTables(tables: Array<string>, callback: AsyncCallback<void>): void 3430 3431设置分布式数据库表,使用callback异步回调。 3432 3433**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3434 3435**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3436 3437**参数:** 3438 3439| 参数名 | 类型 | 必填 | 说明 | 3440| -------- | ------------------------- | ---- | ---------------------- | 3441| tables | Array<string> | 是 | 要设置的分布式数据库表表名。 | 3442| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 3443 3444**错误码:** 3445 3446以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3447 3448| **错误码ID** | **错误信息** | 3449| ------------ | ---------------------------- | 3450| 14800000 | Inner error. | 3451 3452**示例:** 3453 3454```ts 3455if(store != undefined) { 3456 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], (err) => { 3457 if (err) { 3458 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 3459 return; 3460 } 3461 console.info('SetDistributedTables successfully.'); 3462 }) 3463} 3464``` 3465 3466### setDistributedTables 3467 3468 setDistributedTables(tables: Array<string>): Promise<void> 3469 3470设置分布式数据库表,使用Promise异步回调。 3471 3472**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3473 3474**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3475 3476**参数:** 3477 3478| 参数名 | 类型 | 必填 | 说明 | 3479| ------ | ------------------------ | ---- | ------------------------ | 3480| tables | ArrayArray<string> | 是 | 要设置的分布式数据库表表名。 | 3481 3482**返回值**: 3483 3484| 类型 | 说明 | 3485| ------------------- | ------------------------- | 3486| Promise<void> | 无返回结果的Promise对象。 | 3487 3488**错误码:** 3489 3490以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3491 3492| **错误码ID** | **错误信息** | 3493| ------------ | ------------ | 3494| 14800000 | Inner error. | 3495 3496**示例:** 3497 3498```ts 3499import { BusinessError } from "@ohos.base"; 3500 3501if(store != undefined) { 3502 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"]).then(() => { 3503 console.info('SetDistributedTables successfully.'); 3504 }).catch((err: BusinessError) => { 3505 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 3506 }) 3507} 3508``` 3509 3510### setDistributedTables<sup>10+</sup> 3511 3512setDistributedTables(tables: Array<string>, type: DistributedType, callback: AsyncCallback<void>): void 3513 3514设置分布式数据库表,使用callback异步回调。 3515 3516**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3517 3518**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3519 3520**参数:** 3521 3522| 参数名 | 类型 | 必填 | 说明 | 3523| -------- | ------------------------------------- | ---- | ---------------------------- | 3524| tables | Array<string> | 是 | 要设置的分布式数据库表表名。 | 3525| type | [DistributedType](#distributedtype10) | 是 | 表的分布式类型。 | 3526| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 3527 3528**错误码:** 3529 3530以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3531 3532| **错误码ID** | **错误信息** | 3533| ------------ | ------------ | 3534| 14800000 | Inner error. | 3535| 14800051 |The type of the distributed table does not match.| 3536 3537**示例:** 3538 3539```ts 3540if(store != undefined) { 3541 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, (err) => { 3542 if (err) { 3543 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 3544 return; 3545 } 3546 console.info('SetDistributedTables successfully.'); 3547 }) 3548} 3549``` 3550 3551### 3552 3553### setDistributedTables<sup>10+</sup> 3554 3555setDistributedTables(tables: Array<string>, type: DistributedType, config: DistributedConfig, callback: AsyncCallback<void>): void 3556 3557设置分布式数据库表,使用callback异步回调。 3558 3559**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3560 3561**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3562 3563**参数:** 3564 3565| 参数名 | 类型 | 必填 | 说明 | 3566| -------- | ----------------------------------- | --- | --------------- | 3567| tables | Array<string> | 是 | 要设置的分布式数据库表表名。 | 3568| type | [DistributedType](#distributedtype10) | 是 | 表的分布式类型。 | 3569| config | [DistributedConfig](#distributedconfig10) | 是 | 表的分布式配置信息。 | 3570| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 3571 3572**错误码:** 3573 3574以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3575 3576| **错误码ID** | **错误信息** | 3577| ------------ | ------------------------------------------------- | 3578| 14800000 | Inner error. | 3579| 14800051 | The type of the distributed table does not match. | 3580 3581**示例:** 3582 3583```ts 3584if(store != undefined) { 3585 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, { 3586 autoSync: true 3587 }, (err) => { 3588 if (err) { 3589 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 3590 return; 3591 } 3592 console.info('SetDistributedTables successfully.'); 3593 }) 3594} 3595``` 3596 3597### setDistributedTables<sup>10+</sup> 3598 3599 setDistributedTables(tables: Array<string>, type?: DistributedType, config?: DistributedConfig): Promise<void> 3600 3601设置分布式数据库表,使用Promise异步回调。 3602 3603**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3604 3605**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3606 3607**参数:** 3608 3609| 参数名 | 类型 | 必填 | 说明 | 3610| ------ | ----------------------------------------- | ---- | ------------------------------------------------------------ | 3611| tables | Array<string> | 是 | 要设置的分布式数据库表表名。 | 3612| type | [DistributedType](#distributedtype10) | 否 | 表的分布式类型。默认值是relationalStore.DistributedType.DISTRIBUTED_DEVICE。 | 3613| config | [DistributedConfig](#distributedconfig10) | 否 | 表的分布式配置信息。不传入时默认autoSync为false,即只支持手动同步。 | 3614 3615**返回值**: 3616 3617| 类型 | 说明 | 3618| ------------------- | ------------------------- | 3619| Promise<void> | 无返回结果的Promise对象。 | 3620 3621**错误码:** 3622 3623以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3624 3625| **错误码ID** | **错误信息** | 3626| ------------ | ------------------------------------------------- | 3627| 14800000 | Inner error. | 3628| 14800051 | The type of the distributed table does not match. | 3629 3630**示例:** 3631 3632```ts 3633import { BusinessError } from "@ohos.base"; 3634 3635if(store != undefined) { 3636 (store as relationalStore.RdbStore).setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, { 3637 autoSync: true 3638 }).then(() => { 3639 console.info('SetDistributedTables successfully.'); 3640 }).catch((err: BusinessError) => { 3641 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 3642 }) 3643} 3644``` 3645 3646### obtainDistributedTableName 3647 3648obtainDistributedTableName(device: string, table: string, callback: AsyncCallback<string>): void 3649 3650根据远程设备的本地表名获取指定远程设备的分布式表名。在查询远程设备数据库时,需要使用分布式表名, 使用callback异步回调。 3651 3652> **说明:** 3653> 3654> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 3655 3656**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3657 3658**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3659 3660**参数:** 3661 3662| 参数名 | 类型 | 必填 | 说明 | 3663| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 3664| device | string | 是 | 远程设备ID 。 | 3665| table | string | 是 | 远程设备的本地表名。 | 3666| callback | AsyncCallback<string> | 是 | 指定的callback回调函数。如果操作成功,返回远程设备的分布式表名。 | 3667 3668**错误码:** 3669 3670以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3671 3672| **错误码ID** | **错误信息** | 3673| ------------ | ---------------------------- | 3674| 14800000 | Inner error. | 3675 3676**示例:** 3677 3678```ts 3679import deviceManager from '@ohos.distributedDeviceManager'; 3680 3681let dmInstance: deviceManager.DeviceManager; 3682let deviceId: string | undefined = undefined; 3683 3684try { 3685 dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); 3686 let devices = dmInstance.getAvailableDeviceListSync(); 3687 deviceId = devices[0].networkId; 3688} catch (err) { 3689 let code = (err as BusinessError).code; 3690 let message = (err as BusinessError).message 3691 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 3692} 3693 3694if(store != undefined && deviceId != undefined) { 3695 (store as relationalStore.RdbStore).obtainDistributedTableName(deviceId, "EMPLOYEE", (err, tableName) => { 3696 if (err) { 3697 console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`); 3698 return; 3699 } 3700 console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`); 3701 }) 3702} 3703``` 3704 3705### obtainDistributedTableName 3706 3707 obtainDistributedTableName(device: string, table: string): Promise<string> 3708 3709根据远程设备的本地表名获取指定远程设备的分布式表名。在查询远程设备数据库时,需要使用分布式表名,使用Promise异步回调。 3710 3711> **说明:** 3712> 3713> 其中device通过调用[deviceManager.getAvailableDeviceListSync](../apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync)方法得到。 3714 3715**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3716 3717**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3718 3719**参数:** 3720 3721| 参数名 | 类型 | 必填 | 说明 | 3722| ------ | ------ | ---- | -------------------- | 3723| device | string | 是 | 远程设备ID。 | 3724| table | string | 是 | 远程设备的本地表名。 | 3725 3726**返回值**: 3727 3728| 类型 | 说明 | 3729| --------------------- | ----------------------------------------------------- | 3730| Promise<string> | Promise对象。如果操作成功,返回远程设备的分布式表名。 | 3731 3732**错误码:** 3733 3734以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3735 3736| **错误码ID** | **错误信息** | 3737| ------------ | ---------------------------- | 3738| 14800000 | Inner error. | 3739 3740**示例:** 3741 3742```ts 3743import deviceManager from '@ohos.distributedDeviceManager'; 3744import { BusinessError } from "@ohos.base"; 3745 3746let dmInstance: deviceManager.DeviceManager; 3747let deviceId: string | undefined = undefined; 3748 3749try { 3750 dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); 3751 let devices = dmInstance.getAvailableDeviceListSync(); 3752 deviceId = devices[0].networkId; 3753} catch (err) { 3754 let code = (err as BusinessError).code; 3755 let message = (err as BusinessError).message 3756 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 3757} 3758 3759if(store != undefined && deviceId != undefined) { 3760 (store as relationalStore.RdbStore).obtainDistributedTableName(deviceId, "EMPLOYEE").then((tableName: string) => { 3761 console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`); 3762 }).catch((err: BusinessError) => { 3763 console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`); 3764 }) 3765} 3766``` 3767 3768### sync 3769 3770sync(mode: SyncMode, predicates: RdbPredicates, callback: AsyncCallback<Array<[string, number]>>): void 3771 3772在设备之间同步数据, 使用callback异步回调。 3773 3774**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3775 3776**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3777 3778**参数:** 3779 3780| 参数名 | 类型 | 必填 | 说明 | 3781| ---------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ | 3782| mode | [SyncMode](#syncmode) | 是 | 指同步模式。该值可以是relationalStore.SyncMode.SYNC_MODE_PUSH、relationalStore.SyncMode.SYNC_MODE_PULL。 | 3783| predicates | [RdbPredicates](#rdbpredicates) | 是 | 约束同步数据和设备。 | 3784| callback | AsyncCallback<Array<[string, number]>> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 | 3785 3786**错误码:** 3787 3788以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3789 3790| **错误码ID** | **错误信息** | 3791| ------------ | ---------------------------- | 3792| 14800000 | Inner error. | 3793 3794**示例:** 3795 3796```ts 3797import deviceManager from '@ohos.distributedDeviceManager'; 3798 3799let dmInstance: deviceManager.DeviceManager; 3800let deviceIds: Array<string> = []; 3801 3802try { 3803 dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); 3804 let devices: Array<deviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 3805 for (let i = 0; i < devices.length; i++) { 3806 deviceIds[i] = devices[i].networkId!; 3807 } 3808} catch (err) { 3809 let code = (err as BusinessError).code; 3810 let message = (err as BusinessError).message 3811 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 3812} 3813 3814let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 3815predicates.inDevices(deviceIds); 3816if(store != undefined) { 3817 (store as relationalStore.RdbStore).sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates, (err, result) => { 3818 if (err) { 3819 console.error(`Sync failed, code is ${err.code},message is ${err.message}`); 3820 return; 3821 } 3822 console.info('Sync done.'); 3823 for (let i = 0; i < result.length; i++) { 3824 console.info(`device= ${result[i][0]}, status= ${result[i][1]}`); 3825 } 3826 }) 3827} 3828``` 3829 3830### sync 3831 3832 sync(mode: SyncMode, predicates: RdbPredicates): Promise<Array<[string, number]>> 3833 3834在设备之间同步数据,使用Promise异步回调。 3835 3836**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3837 3838**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3839 3840**参数:** 3841 3842| 参数名 | 类型 | 必填 | 说明 | 3843| ---------- | ------------------------------------ | ---- | ------------------------------ | 3844| mode | [SyncMode](#syncmode) | 是 | 指同步模式。该值可以是relationalStore.SyncMode.SYNC_MODE_PUSH、relationalStore.SyncMode.SYNC_MODE_PULL。 | 3845| predicates | [RdbPredicates](#rdbpredicates) | 是 | 约束同步数据和设备。 | 3846 3847**返回值**: 3848 3849| 类型 | 说明 | 3850| -------------------------------------------- | ------------------------------------------------------------ | 3851| Promise<Array<[string, number]>> | Promise对象,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 | 3852 3853**错误码:** 3854 3855以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 3856 3857| **错误码ID** | **错误信息** | 3858| ------------ | ---------------------------- | 3859| 14800000 | Inner error. | 3860 3861**示例:** 3862 3863```ts 3864import deviceManager from '@ohos.distributedDeviceManager'; 3865import { BusinessError } from "@ohos.base"; 3866 3867let dmInstance: deviceManager.DeviceManager; 3868let deviceIds: Array<string> = []; 3869 3870try { 3871 dmInstance = deviceManager.createDeviceManager("com.example.appdatamgrverify"); 3872 let devices: Array<deviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 3873 for (let i = 0; i < devices.length; i++) { 3874 deviceIds[i] = devices[i].networkId!; 3875 } 3876} catch (err) { 3877 let code = (err as BusinessError).code; 3878 let message = (err as BusinessError).message 3879 console.error("createDeviceManager errCode:" + code + ",errMessage:" + message); 3880} 3881 3882let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 3883predicates.inDevices(deviceIds); 3884if(store != undefined) { 3885 (store as relationalStore.RdbStore).sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates).then((result: Object[][]) => { 3886 console.info('Sync done.'); 3887 for (let i = 0; i < result.length; i++) { 3888 console.info(`device= ${result[i][0]}, status= ${result[i][1]}`); 3889 } 3890 }).catch((err: BusinessError) => { 3891 console.error(`Sync failed, code is ${err.code},message is ${err.message}`); 3892 }) 3893} 3894``` 3895 3896### cloudSync<sup>10+</sup> 3897 3898cloudSync(mode: SyncMode, progress: Callback<ProgressDetails>, callback: AsyncCallback<void>): void 3899 3900手动执行对所有分布式表的端云同步,使用callback异步回调。使用该接口需要实现云服务功能。 3901 3902**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3903 3904**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 3905 3906**参数:** 3907 3908| 参数名 | 类型 | 必填 | 说明 | 3909| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 3910| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 3911| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 3912| callback | AsyncCallback<void> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。 | 3913 3914**示例:** 3915 3916```ts 3917if(store != undefined) { 3918 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, (progressDetails) => { 3919 console.info(`Progess: ${progressDetails}`); 3920 }, (err) => { 3921 if (err) { 3922 console.error(`Cloud sync failed, code is ${err.code},message is ${err.message}`); 3923 return; 3924 } 3925 console.info('Cloud sync succeeded'); 3926 }); 3927} 3928``` 3929 3930### cloudSync<sup>10+</sup> 3931 3932cloudSync(mode: SyncMode, progress: Callback<ProgressDetails>): Promise<void> 3933 3934手动执行对所有分布式表的端云同步,使用Promise异步回调。使用该接口需要实现云服务功能。 3935 3936**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3937 3938**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 3939 3940**参数:** 3941 3942| 参数名 | 类型 | 必填 | 说明 | 3943| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 3944| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 3945| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 3946 3947**返回值**: 3948 3949| 类型 | 说明 | 3950| ------------------- | --------------------------------------- | 3951| Promise<void> | Promise对象,用于向调用者发送同步结果。 | 3952 3953**示例:** 3954 3955```ts 3956import { BusinessError } from "@ohos.base"; 3957 3958if(store != undefined) { 3959 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, (progressDetail: relationalStore.ProgressDetails) => { 3960 console.info(`progress: ${progressDetail}`); 3961 }).then(() => { 3962 console.info('Cloud sync succeeded'); 3963 }).catch((err: BusinessError) => { 3964 console.error(`cloudSync failed, code is ${err.code},message is ${err.message}`); 3965 }); 3966} 3967``` 3968 3969### cloudSync<sup>10+</sup> 3970 3971cloudSync(mode: SyncMode, tables: string[], progress: Callback<ProgressDetails>, callback: AsyncCallback<void>): void 3972 3973手动执行对指定表的端云同步,使用callback异步回调。使用该接口需要实现云服务功能。 3974 3975**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 3976 3977**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 3978 3979**参数:** 3980 3981| 参数名 | 类型 | 必填 | 说明 | 3982| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 3983| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 3984| tables | string[] | 是 | 指定同步的表名。 | 3985| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 3986| callback | AsyncCallback<void> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。 | 3987 3988**示例:** 3989 3990```ts 3991const tables = ["table1", "table2"]; 3992 3993if(store != undefined) { 3994 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, tables, (progressDetail: relationalStore.ProgressDetails) => { 3995 console.info(`Progess: ${progressDetail}`); 3996 }, (err) => { 3997 if (err) { 3998 console.error(`Cloud sync failed, code is ${err.code},message is ${err.message}`); 3999 return; 4000 } 4001 console.info('Cloud sync succeeded'); 4002 }); 4003}; 4004``` 4005 4006### cloudSync<sup>10+</sup> 4007 4008cloudSync(mode: SyncMode, tables: string[], progress: Callback<ProgressDetails>): Promise<void> 4009 4010手动执行对指定表的端云同步,使用Promise异步回调。使用该接口需要实现云服务功能。 4011 4012**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 4013 4014**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 4015 4016**参数:** 4017 4018| 参数名 | 类型 | 必填 | 说明 | 4019| -------- | ----------------------------------------------------- | ---- | -------------------------------------- | 4020| mode | [SyncMode](#syncmode) | 是 | 表示数据库的同步模式。 | 4021| tables | string[] | 是 | 指定同步的表名。 | 4022| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 用来处理数据库同步详细信息的回调函数。 | 4023 4024**返回值**: 4025 4026| 类型 | 说明 | 4027| ------------------- | --------------------------------------- | 4028| Promise<void> | Promise对象,用于向调用者发送同步结果。 | 4029 4030**示例:** 4031 4032```ts 4033import { BusinessError } from "@ohos.base"; 4034 4035const tables = ["table1", "table2"]; 4036 4037if(store != undefined) { 4038 (store as relationalStore.RdbStore).cloudSync(relationalStore.SyncMode.SYNC_MODE_CLOUD_FIRST, (progressDetail: relationalStore.ProgressDetails) => { 4039 console.info(`progress: ${progressDetail}`); 4040 }).then(() => { 4041 console.info('Cloud sync succeeded'); 4042 }).catch((err: BusinessError) => { 4043 console.error(`cloudSync failed, code is ${err.code},message is ${err.message}`); 4044 }); 4045}; 4046``` 4047 4048### on('dataChange') 4049 4050on(event: 'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void 4051 4052注册数据库的数据变更的事件监听。当分布式数据库中的数据发生更改时,将调用回调。 4053 4054**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4055 4056**参数:** 4057 4058| 参数名 | 类型 | 必填 | 说明 | 4059| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4060| event | string | 是 | 取值为'dataChange',表示数据更改。 | 4061| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 4062| observer | Callback<Array<string>> | 是 | 指分布式数据库中数据更改事件的观察者。Array<string>为数据库中的数据发生改变的对端设备ID。 | 4063 4064**示例:** 4065 4066```ts 4067import deviceManager from '@ohos.distributedHardware.deviceManager'; 4068 4069let devices: string | undefined = undefined; 4070 4071try { 4072 if (store != undefined) { 4073 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, (storeObserver) => { 4074 if (devices != undefined) { 4075 for (let i = 0; i < devices.length; i++) { 4076 console.info(`device= ${devices[i]} data changed`); 4077 } 4078 } 4079 }) 4080 } 4081} catch (err) { 4082 let code = (err as BusinessError).code; 4083 let message = (err as BusinessError).message 4084 console.error(`Register observer failed, code is ${code},message is ${message}`); 4085} 4086``` 4087 4088### on('dataChange')<sup>10+</sup> 4089 4090on(event: 'dataChange', type: SubscribeType, observer: Callback<Array<string>>\| Callback<Array<ChangeInfo>>): void 4091 4092注册数据库的数据变更的事件监听。当分布式数据库中的数据发生更改时,将调用回调。 4093 4094**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4095 4096**参数:** 4097 4098| 参数名 | 类型 | 必填 | 说明 | 4099| -------- | ----------------------------------- | ---- | ------------------------------------------- | 4100| event | string | 是 | 取值为'dataChange',表示数据更改。 | 4101| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 4102| observer | Callback<Array<string>> \| Callback<Array<[ChangeInfo](#changeinfo10)>> | 是 | 回调函数。<br>当type为SUBSCRIBE_TYPE_REMOTE,observer类型需为Callback<Array<string>>,其中Array<string>为数据库中的数据发生改变的对端设备ID。<br> 当type为SUBSCRIBE_TYPE_CLOUD,observer类型需为Callback<Array<string>>,其中Array<string>为数据库中的数据发生改变的云端帐号。 <br> 当type为SUBSCRIBE_TYPE_CLOUD_DETAILS,observer类型需为Callback<Array<ChangeInfo>>,其中Array<ChangeInfo>为数据库端云同步过程的详情。 | 4103 4104**示例:** 4105 4106```ts 4107import deviceManager from '@ohos.distributedHardware.deviceManager'; 4108 4109let devices: string | undefined = undefined; 4110 4111try { 4112 if(store != undefined) { 4113 (store as relationalStore.RdbStore).on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver => { 4114 if (devices != undefined) { 4115 for (let i = 0; i < devices.length; i++) { 4116 console.info(`device= ${devices[i]} data changed`); 4117 } 4118 } 4119 }); 4120 } 4121} catch (err) { 4122 let code = (err as BusinessError).code; 4123 let message = (err as BusinessError).message 4124 console.error(`Register observer failed, code is ${code},message is ${message}`); 4125} 4126``` 4127 4128### on<sup>10+</sup> 4129 4130on(event: string, interProcess: boolean, observer: Callback\<void>): void 4131 4132注册数据库的进程内或者进程间事件监听。当调用[emit](#emit10)接口时,将调用回调。 4133 4134**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4135 4136**参数:** 4137 4138| 参数名 | 类型 | 必填 | 说明 | 4139| ------------ | --------------- | ---- | ------------------------------------------------------------ | 4140| event | string | 是 | 订阅事件名称。 | 4141| interProcess | boolean | 是 | 指定是进程间还是本进程订阅。<br/> true:进程间。<br/> false:本进程。 | 4142| observer | Callback\<void> | 是 | 回调函数。 | 4143 4144**错误码:** 4145 4146以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4147 4148| **错误码ID** | **错误信息** | 4149| ------------ | -------------------------------------- | 4150| 14800000 | Inner error. | 4151| 14800050 | Failed to obtain subscription service. | 4152 4153**示例:** 4154 4155```ts 4156try { 4157 if(store != undefined) { 4158 (store as relationalStore.RdbStore).on('storeObserver', false, (storeObserver) => { 4159 console.info(`storeObserver`); 4160 }); 4161 } 4162} catch (err) { 4163 let code = (err as BusinessError).code; 4164 let message = (err as BusinessError).message 4165 console.error(`Register observer failed, code is ${code},message is ${message}`); 4166} 4167``` 4168 4169### on('autoSyncProgress')<sup>11+</sup> 4170 4171on(event: 'autoSyncProgress', progress: Callback<ProgressDetails>): void 4172 4173在已打开端云同步,并且网络状态正常的条件下,注册自动同步进度通知,自动同步进行时调用回调。 4174 4175**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4176 4177**参数:** 4178 4179| 参数名 | 类型 | 必填 | 说明 | 4180| ------------ |---------------------------------| ---- |-----------------------------------| 4181| event | string | 是 | 取值为'autoSyncProgress',表示自动同步进度通知。 | 4182| progress | Callback<[ProgressDetails](#progressdetails10)> | 是 | 回调函数。 | 4183 4184**示例:** 4185 4186```ts 4187import {BusinessError} from "@ohos.base"; 4188 4189try { 4190 if(store != undefined) { 4191 (store as relationalStore.RdbStore).on('autoSyncProgress', (progressDetail: relationalStore.ProgressDetails) => { 4192 console.info(`progress: ${progressDetail}`); 4193 }); 4194 } 4195} catch (err) { 4196 let code = (err as BusinessError).code; 4197 let message = (err as BusinessError).message 4198 console.error(`Register observer failed, code is ${code},message is ${message}`); 4199} 4200``` 4201 4202### off('dataChange') 4203 4204off(event:'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void 4205 4206取消数据变更的事件监听。 4207 4208**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4209 4210**参数:** 4211 4212| 参数名 | 类型 | 必填 | 说明 | 4213| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 4214| event | string | 是 | 取值为'dataChange',表示数据更改。 | 4215| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 4216| observer | Callback<Array<string>> | 是 | 指已注册的数据更改观察者。Array<string>为数据库中的数据发生改变的对端设备ID。 | 4217 4218**示例:** 4219 4220```ts 4221let devices: string | undefined = undefined; 4222 4223try { 4224 if(store != undefined) { 4225 (store as relationalStore.RdbStore).off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, (storeObserver) => { 4226 if (devices != undefined){ 4227 for (let i = 0; i < devices.length; i++) { 4228 console.info(`device= ${devices[i]} data changed`); 4229 } 4230 } 4231 }); 4232 } 4233} catch (err) { 4234 let code = (err as BusinessError).code; 4235 let message = (err as BusinessError).message 4236 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 4237} 4238``` 4239 4240### off('dataChange')<sup>10+</sup> 4241 4242off(event:'dataChange', type: SubscribeType, observer?: Callback<Array<string>>\| Callback<Array<ChangeInfo>>): void 4243 4244取消数据变更的事件监听。 4245 4246**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4247 4248**参数:** 4249 4250| 参数名 | 类型 | 必填 | 说明 | 4251| -------- | ---------------------------------- | ---- | ------------------------------------------ | 4252| event | string | 是 | 取值为'dataChange',表示数据更改。 | 4253| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 4254| observer | Callback<Array<string>>\| Callback<Array<[ChangeInfo](#changeinfo10)>> | 否 | 回调函数。<br/>当type为SUBSCRIBE_TYPE_REMOTE,observer类型需为Callback<Array<string>>,其中Array<string>为数据库中的数据发生改变的对端设备ID。<br/> 当type为SUBSCRIBE_TYPE_CLOUD,observer类型需为Callback<Array<string>>,其中Array<string>为数据库中的数据发生改变的云端帐号。 <br/> 当type为SUBSCRIBE_TYPE_CLOUD_DETAILS,observer类型需为Callback<Array<ChangeInfo>>,其中Array<ChangeInfo>为数据库端云同步过程的详情。<br> 当observer没有传入时,表示取消当前type类型下所有数据变更的事件监听。 | 4255 4256**示例:** 4257 4258```ts 4259import deviceManager from '@ohos.distributedHardware.deviceManager'; 4260 4261let devices: string | undefined = undefined; 4262 4263try { 4264 if(store != undefined) { 4265 (store as relationalStore.RdbStore).off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, (storeObserver) => { 4266 if (devices != undefined) { 4267 for (let i = 0; i < devices.length; i++) { 4268 console.info(`device= ${devices[i]} data changed`); 4269 } 4270 } 4271 }); 4272 } 4273} catch (err) { 4274 let code = (err as BusinessError).code; 4275 let message = (err as BusinessError).message 4276 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 4277} 4278``` 4279 4280### off<sup>10+</sup> 4281 4282off(event: string, interProcess: boolean, observer?: Callback\<void>): void 4283 4284取消数据变更的事件监听。 4285 4286**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4287 4288**参数:** 4289 4290| 参数名 | 类型 | 必填 | 说明 | 4291| ------------ | --------------- | ---- | ------------------------------------------------------------ | 4292| event | string | 是 | 取消订阅事件名称。 | 4293| interProcess | boolean | 是 | 指定是进程间还是本进程取消订阅。<br/> true:进程间。<br/> false:本进程。 | 4294| observer | Callback\<void> | 否 | 该参数存在,则取消指定Callback监听回调,否则取消该event事件的所有监听回调。 | 4295 4296**错误码:** 4297 4298以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4299 4300| **错误码ID** | **错误信息** | 4301| ------------ | -------------------------------------- | 4302| 14800000 | Inner error. | 4303| 14800050 | Failed to obtain subscription service. | 4304 4305**示例:** 4306 4307```ts 4308try { 4309 if(store != undefined) { 4310 (store as relationalStore.RdbStore).off('storeObserver', false, (storeObserver) => { 4311 console.info(`storeObserver`); 4312 }); 4313 } 4314} catch (err) { 4315 let code = (err as BusinessError).code; 4316 let message = (err as BusinessError).message 4317 console.error(`Unregister observer failed, code is ${code},message is ${message}`); 4318} 4319``` 4320 4321### off('autoSyncProgress')<sup>11+</sup> 4322 4323off(event: 'autoSyncProgress', progress?: Callback<ProgressDetails>): void 4324 4325取消订阅自动同步进度的通知。 4326 4327**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4328 4329**参数:** 4330 4331| 参数名 | 类型 | 必填 | 说明 | 4332| ------------ |---------------------------------| ---- |------------------------------------------------------------------| 4333| event | string | 是 | 取值为'autoSyncProgress',表示自动同步进度通知。 | 4334| observer | Callback<[ProgressDetails](#progressdetails10)> | 否 | 指已注册的自动同步进度观察者。该参数存在,则取消订阅指定回调,该参数为null或undefined或不存在,则取消订阅所有回调。 | 4335 4336**示例:** 4337 4338```ts 4339import {BusinessError} from "@ohos.base"; 4340 4341try { 4342 if(store != undefined) { 4343 (store as relationalStore.RdbStore).off('autoSyncProgress', (progressDetail: relationalStore.ProgressDetails) => { 4344 console.info(`progress: ${progressDetail}`); 4345 }); 4346 } 4347} catch (err) { 4348 let code = (err as BusinessError).code; 4349 let message = (err as BusinessError).message; 4350 console.error(`Unregister failed, code is ${code},message is ${message}`); 4351} 4352``` 4353 4354### emit<sup>10+</sup> 4355 4356emit(event: string): void 4357 4358通知通过[on](#on10)注册的进程间或者进程内监听事件。 4359 4360**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4361 4362**参数:** 4363 4364| 参数名 | 类型 | 必填 | 说明 | 4365| ------ | ------ | ---- | -------------------- | 4366| event | string | 是 | 通知订阅事件的名称。 | 4367 4368**错误码:** 4369 4370以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4371 4372| **错误码ID** | **错误信息** | 4373| ------------ | -------------------------------------- | 4374| 14800000 | Inner error. | 4375| 14800050 | Failed to obtain subscription service. | 4376 4377**示例:** 4378 4379```ts 4380if(store != undefined) { 4381 (store as relationalStore.RdbStore).emit('storeObserver'); 4382} 4383``` 4384 4385### cleanDirtyData<sup>11+</sup> 4386 4387cleanDirtyData(table: string, cursor: number, callback: AsyncCallback<void>): void 4388 4389清理云端删除的数据同步到本地后,未自动清理的,且数据的游标(cursor)小于指定游标的数据。 4390 4391**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 4392 4393**参数:** 4394 4395| 参数名 | 类型 | 必填 | 说明 | 4396| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 4397| table | string | 是 | 表示当前数据库的表的名称。 | 4398| cursor | number | 是 | 整数类型,表示数据游标,小于此游标的脏数据将被清理。 | 4399| callback | AsyncCallback<void> | 是 | 指定的callback回调函数。 | 4400 4401**错误码:** 4402 4403以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4404 4405| **错误码ID** | **错误信息** | 4406| ------------ | -------------------------------------- | 4407| 14800000 | Inner error. | 4408 4409**示例:** 4410 4411```ts 4412if(store != undefined) { 4413 (store as relationalStore.RdbStore).cleanDirtyData('test_table', 100, (err) => { 4414 if (err) { 4415 console.error(`clean dirty data failed, code is ${err.code},message is ${err.message}`); 4416 return; 4417 } 4418 console.info('clean dirty data succeeded'); 4419 }) 4420} 4421``` 4422 4423### cleanDirtyData<sup>11+</sup> 4424 4425cleanDirtyData(table: string, callback: AsyncCallback<void>): void 4426 4427清理云端删除的数据同步到本地后,未自动清理的所有数据。 4428 4429**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 4430 4431**参数:** 4432 4433| 参数名 | 类型 | 必填 | 说明 | 4434| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 4435| table | string | 是 | 表示当前数据库的表的名称。 | 4436| callback | AsyncCallback<void> | 是 | 指定的callback回调函数。 | 4437 4438**错误码:** 4439 4440以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4441 4442| **错误码ID** | **错误信息** | 4443| ------------ | -------------------------------------- | 4444| 14800000 | Inner error. | 4445 4446**示例:** 4447 4448```ts 4449if(store != undefined) { 4450 (store as relationalStore.RdbStore).cleanDirtyData('test_table', (err) => { 4451 if (err) { 4452 console.error(`clean dirty data failed, code is ${err.code},message is ${err.message}`); 4453 return; 4454 } 4455 console.info('clean dirty data succeeded'); 4456 }) 4457} 4458``` 4459 4460### cleanDirtyData<sup>11+</sup> 4461 4462cleanDirtyData(table: string, cursor?: number): Promise<void> 4463 4464清理云端删除的数据同步到本地后,未自动清理的,且数据的游标(cursor)小于指定游标的数据。若无cursor参数,将全部清理。 4465 4466**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client 4467 4468**参数:** 4469 4470| 参数名 | 类型 | 必填 | 说明 | 4471| -------- | ----------------------------------------------------- | ---- | -------------------------------------------------- | 4472| table | string | 是 | 表示当前数据库的表的名称。 | 4473| cursor | number | 否 | 整数类型,表示数据游标,小于此游标的脏数据将被清理。当此参数不填时,清理当前表的所有脏数据。 | 4474 4475**返回值:** 4476| 参数名 | 说明 | 4477| -------- | ------------------------------------------------- | 4478| Promise\<void> | 无返回结果的Promise对象。 | 4479 4480**错误码:** 4481 4482以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4483 4484| **错误码ID** | **错误信息** | 4485| ------------ | -------------------------------------- | 4486| 14800000 | Inner error. | 4487 4488**示例:** 4489 4490```ts 4491import { BusinessError } from "@ohos.base"; 4492 4493if(store != undefined) { 4494 (store as relationalStore.RdbStore).cleanDirtyData('test_table', 100).then(() => { 4495 console.info('clean dirty data succeeded'); 4496 }).catch ((err: BusinessError) => { 4497 console.error(`clean dirty data failed, code is ${err.code},message is ${err.message}`); 4498 }) 4499} 4500``` 4501 4502## ResultSet 4503 4504提供通过查询数据库生成的数据库结果集的访问方法。结果集是指用户调用关系型数据库查询接口之后返回的结果集合,提供了多种灵活的数据访问方式,以便用户获取各项数据。 4505 4506### 使用说明 4507 4508首先需要获取resultSet对象。 4509 4510**示例:** 4511 4512```ts 4513let resultSet: relationalStore.ResultSet | undefined = undefined; 4514let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 4515predicates.equalTo("AGE", 18); 4516if(store != undefined) { 4517 (store as relationalStore.RdbStore).query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]).then((result: relationalStore.ResultSet) => { 4518 resultSet = result; 4519 console.info(`resultSet columnNames: ${resultSet.columnNames}`); 4520 console.info(`resultSet columnCount: ${resultSet.columnCount}`); 4521 }); 4522} 4523``` 4524 4525### 属性 4526 4527**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4528 4529| 名称 | 类型 | 必填 | 说明 | 4530| ------------ | ------------------- | ---- | -------------------------------- | 4531| columnNames | Array<string> | 是 | 获取结果集中所有列的名称。 | 4532| columnCount | number | 是 | 获取结果集中的列数。 | 4533| rowCount | number | 是 | 获取结果集中的行数。 | 4534| rowIndex | number | 是 | 获取结果集当前行的索引。 | 4535| isAtFirstRow | boolean | 是 | 检查结果集是否位于第一行。 | 4536| isAtLastRow | boolean | 是 | 检查结果集是否位于最后一行。 | 4537| isEnded | boolean | 是 | 检查结果集是否位于最后一行之后。 | 4538| isStarted | boolean | 是 | 检查指针是否移动过。 | 4539| isClosed | boolean | 是 | 检查当前结果集是否关闭。 | 4540 4541### getColumnIndex 4542 4543getColumnIndex(columnName: string): number 4544 4545根据指定的列名获取列索引。 4546 4547**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4548 4549**参数:** 4550 4551| 参数名 | 类型 | 必填 | 说明 | 4552| ---------- | ------ | ---- | -------------------------- | 4553| columnName | string | 是 | 表示结果集中指定列的名称。 | 4554 4555**返回值:** 4556 4557| 类型 | 说明 | 4558| ------ | ------------------ | 4559| number | 返回指定列的索引。 | 4560 4561**错误码:** 4562 4563以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4564 4565| **错误码ID** | **错误信息** | 4566| ------------ | ------------------------------------------------------------ | 4567| 14800013 | The column value is null or the column type is incompatible. | 4568 4569**示例:** 4570 4571```ts 4572if(resultSet != undefined) { 4573 const id = (resultSet as relationalStore.ResultSet).getLong((resultSet as relationalStore.ResultSet).getColumnIndex("ID")); 4574 const name = (resultSet as relationalStore.ResultSet).getString((resultSet as relationalStore.ResultSet).getColumnIndex("NAME")); 4575 const age = (resultSet as relationalStore.ResultSet).getLong((resultSet as relationalStore.ResultSet).getColumnIndex("AGE")); 4576 const salary = (resultSet as relationalStore.ResultSet).getDouble((resultSet as relationalStore.ResultSet).getColumnIndex("SALARY")); 4577} 4578``` 4579 4580### getColumnName 4581 4582getColumnName(columnIndex: number): string 4583 4584根据指定的列索引获取列名。 4585 4586**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4587 4588**参数:** 4589 4590| 参数名 | 类型 | 必填 | 说明 | 4591| ----------- | ------ | ---- | -------------------------- | 4592| columnIndex | number | 是 | 表示结果集中指定列的索引。 | 4593 4594**返回值:** 4595 4596| 类型 | 说明 | 4597| ------ | ------------------ | 4598| string | 返回指定列的名称。 | 4599 4600**错误码:** 4601 4602以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4603 4604| **错误码ID** | **错误信息** | 4605| ------------ | ------------------------------------------------------------ | 4606| 14800013 | The column value is null or the column type is incompatible. | 4607 4608**示例:** 4609 4610```ts 4611if(resultSet != undefined) { 4612 const id = (resultSet as relationalStore.ResultSet).getColumnName(0); 4613 const name = (resultSet as relationalStore.ResultSet).getColumnName(1); 4614 const age = (resultSet as relationalStore.ResultSet).getColumnName(2); 4615} 4616``` 4617 4618### goTo 4619 4620goTo(offset:number): boolean 4621 4622向前或向后转至结果集的指定行,相对于其当前位置偏移。 4623 4624**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4625 4626**参数:** 4627 4628| 参数名 | 类型 | 必填 | 说明 | 4629| ------ | ------ | ---- | ---------------------------- | 4630| offset | number | 是 | 表示相对于当前位置的偏移量。 | 4631 4632**返回值:** 4633 4634| 类型 | 说明 | 4635| ------- | --------------------------------------------- | 4636| boolean | 如果成功移动结果集,则为true;否则返回false。 | 4637 4638**错误码:** 4639 4640以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4641 4642| **错误码ID** | **错误信息** | 4643| ------------ | ------------------------------------------------------------ | 4644| 14800012 | The result set is empty or the specified location is invalid. | 4645 4646**示例:** 4647 4648```ts 4649if(resultSet != undefined) { 4650 (resultSet as relationalStore.ResultSet).goTo(1); 4651} 4652``` 4653 4654### goToRow 4655 4656goToRow(position: number): boolean 4657 4658转到结果集的指定行。 4659 4660**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4661 4662**参数:** 4663 4664| 参数名 | 类型 | 必填 | 说明 | 4665| -------- | ------ | ---- | ------------------------ | 4666| position | number | 是 | 表示要移动到的指定位置。 | 4667 4668**返回值:** 4669 4670| 类型 | 说明 | 4671| ------- | --------------------------------------------- | 4672| boolean | 如果成功移动结果集,则为true;否则返回false。 | 4673 4674**错误码:** 4675 4676以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4677 4678| **错误码ID** | **错误信息** | 4679| ------------ | ------------------------------------------------------------ | 4680| 14800012 | The result set is empty or the specified location is invalid. | 4681 4682**示例:** 4683 4684```ts 4685if(resultSet != undefined) { 4686 (resultSet as relationalStore.ResultSet).goToRow(5); 4687} 4688``` 4689 4690### goToFirstRow 4691 4692goToFirstRow(): boolean 4693 4694 4695转到结果集的第一行。 4696 4697**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4698 4699**返回值:** 4700 4701| 类型 | 说明 | 4702| ------- | --------------------------------------------- | 4703| boolean | 如果成功移动结果集,则为true;否则返回false。 | 4704 4705**错误码:** 4706 4707以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4708 4709| **错误码ID** | **错误信息** | 4710| ------------ | ------------------------------------------------------------ | 4711| 14800012 | The result set is empty or the specified location is invalid. | 4712 4713**示例:** 4714 4715```ts 4716if(resultSet != undefined) { 4717 (resultSet as relationalStore.ResultSet).goToFirstRow(); 4718} 4719``` 4720 4721### goToLastRow 4722 4723goToLastRow(): boolean 4724 4725转到结果集的最后一行。 4726 4727**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4728 4729**返回值:** 4730 4731| 类型 | 说明 | 4732| ------- | --------------------------------------------- | 4733| boolean | 如果成功移动结果集,则为true;否则返回false。 | 4734 4735**错误码:** 4736 4737以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4738 4739| **错误码ID** | **错误信息** | 4740| ------------ | ------------------------------------------------------------ | 4741| 14800012 | The result set is empty or the specified location is invalid. | 4742 4743**示例:** 4744 4745```ts 4746if(resultSet != undefined) { 4747 (resultSet as relationalStore.ResultSet).goToLastRow(); 4748} 4749``` 4750 4751### goToNextRow 4752 4753goToNextRow(): boolean 4754 4755转到结果集的下一行。 4756 4757**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4758 4759**返回值:** 4760 4761| 类型 | 说明 | 4762| ------- | --------------------------------------------- | 4763| boolean | 如果成功移动结果集,则为true;否则返回false。 | 4764 4765**错误码:** 4766 4767以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4768 4769| **错误码ID** | **错误信息** | 4770| ------------ | ------------------------------------------------------------ | 4771| 14800012 | The result set is empty or the specified location is invalid. | 4772 4773**示例:** 4774 4775```ts 4776if(resultSet != undefined) { 4777 (resultSet as relationalStore.ResultSet).goToNextRow(); 4778} 4779``` 4780 4781### goToPreviousRow 4782 4783goToPreviousRow(): boolean 4784 4785转到结果集的上一行。 4786 4787**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4788 4789**返回值:** 4790 4791| 类型 | 说明 | 4792| ------- | --------------------------------------------- | 4793| boolean | 如果成功移动结果集,则为true;否则返回false。 | 4794 4795**错误码:** 4796 4797以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4798 4799| **错误码ID** | **错误信息** | 4800| ------------ | ------------------------------------------------------------ | 4801| 14800012 | The result set is empty or the specified location is invalid. | 4802 4803**示例:** 4804 4805```ts 4806if(resultSet != undefined) { 4807 (resultSet as relationalStore.ResultSet).goToPreviousRow(); 4808} 4809``` 4810 4811### getBlob 4812 4813getBlob(columnIndex: number): Uint8Array 4814 4815以字节数组的形式获取当前行中指定列的值。 4816 4817**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4818 4819**参数:** 4820 4821| 参数名 | 类型 | 必填 | 说明 | 4822| ----------- | ------ | ---- | ----------------------- | 4823| columnIndex | number | 是 | 指定的列索引,从0开始。 | 4824 4825**返回值:** 4826 4827| 类型 | 说明 | 4828| ---------- | -------------------------------- | 4829| Uint8Array | 以字节数组的形式返回指定列的值。 | 4830 4831**错误码:** 4832 4833以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4834 4835| **错误码ID** | **错误信息** | 4836| ------------ | ------------------------------------------------------------ | 4837| 14800013 | The column value is null or the column type is incompatible. | 4838 4839**示例:** 4840 4841```ts 4842if(resultSet != undefined) { 4843 const codes = (resultSet as relationalStore.ResultSet).getBlob((resultSet as relationalStore.ResultSet).getColumnIndex("CODES")); 4844} 4845``` 4846 4847### getString 4848 4849getString(columnIndex: number): string 4850 4851以字符串形式获取当前行中指定列的值。 4852 4853**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4854 4855**参数:** 4856 4857| 参数名 | 类型 | 必填 | 说明 | 4858| ----------- | ------ | ---- | ----------------------- | 4859| columnIndex | number | 是 | 指定的列索引,从0开始。 | 4860 4861**返回值:** 4862 4863| 类型 | 说明 | 4864| ------ | ---------------------------- | 4865| string | 以字符串形式返回指定列的值。 | 4866 4867**错误码:** 4868 4869以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4870 4871| **错误码ID** | **错误信息** | 4872| ------------ | ------------------------------------------------------------ | 4873| 14800013 | The column value is null or the column type is incompatible. | 4874 4875**示例:** 4876 4877```ts 4878if(resultSet != undefined) { 4879 const name = (resultSet as relationalStore.ResultSet).getString((resultSet as relationalStore.ResultSet).getColumnIndex("NAME")); 4880} 4881``` 4882 4883### getLong 4884 4885getLong(columnIndex: number): number 4886 4887以Long形式获取当前行中指定列的值。 4888 4889**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4890 4891**参数:** 4892 4893| 参数名 | 类型 | 必填 | 说明 | 4894| ----------- | ------ | ---- | ----------------------- | 4895| columnIndex | number | 是 | 指定的列索引,从0开始。 | 4896 4897**返回值:** 4898 4899| 类型 | 说明 | 4900| ------ | ------------------------------------------------------------ | 4901| number | 以Long形式返回指定列的值。<br>该接口支持的数据范围是:Number.MIN_SAFE_INTEGER ~ Number.MAX_SAFE_INTEGER,若超出该范围,建议使用[getDouble](#getdouble)。 | 4902 4903**错误码:** 4904 4905以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4906 4907| **错误码ID** | **错误信息** | 4908| ------------ | ------------------------------------------------------------ | 4909| 14800013 | The column value is null or the column type is incompatible. | 4910 4911**示例:** 4912 4913```ts 4914if(resultSet != undefined) { 4915 const age = (resultSet as relationalStore.ResultSet).getLong((resultSet as relationalStore.ResultSet).getColumnIndex("AGE")); 4916 } 4917``` 4918 4919### getDouble 4920 4921getDouble(columnIndex: number): number 4922 4923以double形式获取当前行中指定列的值。 4924 4925**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4926 4927**参数:** 4928 4929| 参数名 | 类型 | 必填 | 说明 | 4930| ----------- | ------ | ---- | ----------------------- | 4931| columnIndex | number | 是 | 指定的列索引,从0开始。 | 4932 4933**返回值:** 4934 4935| 类型 | 说明 | 4936| ------ | ---------------------------- | 4937| number | 以double形式返回指定列的值。 | 4938 4939**错误码:** 4940 4941以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4942 4943| **错误码ID** | **错误信息** | 4944| ------------ | ------------------------------------------------------------ | 4945| 14800013 | The column value is null or the column type is incompatible. | 4946 4947**示例:** 4948 4949```ts 4950if(resultSet != undefined) { 4951 const salary = (resultSet as relationalStore.ResultSet).getDouble((resultSet as relationalStore.ResultSet).getColumnIndex("SALARY")); 4952} 4953``` 4954 4955### getAsset<sup>10+</sup> 4956 4957getAsset(columnIndex: number): Asset 4958 4959以[Asset](#asset10)形式获取当前行中指定列的值。 4960 4961**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4962 4963**参数:** 4964 4965| 参数名 | 类型 | 必填 | 说明 | 4966| ----------- | ------ | --- | ------------ | 4967| columnIndex | number | 是 | 指定的列索引,从0开始。 | 4968 4969**返回值:** 4970 4971| 类型 | 说明 | 4972| --------------- | -------------------------- | 4973| [Asset](#asset10) | 以Asset形式返回指定列的值。 | 4974 4975**错误码:** 4976 4977以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 4978 4979| **错误码ID** | **错误信息** | 4980| --------- | ------------------------------------------------------------ | 4981| 14800013 | The column value is null or the column type is incompatible. | 4982 4983**示例:** 4984 4985```ts 4986if(resultSet != undefined) { 4987 const doc = (resultSet as relationalStore.ResultSet).getAsset((resultSet as relationalStore.ResultSet).getColumnIndex("DOC")); 4988} 4989``` 4990 4991### getAssets<sup>10+</sup> 4992 4993getAssets(columnIndex: number): Assets 4994 4995以[Assets](#assets10)形式获取当前行中指定列的值。 4996 4997**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 4998 4999**参数:** 5000 5001| 参数名 | 类型 | 必填 | 说明 | 5002| ----------- | ------ | --- | ------------ | 5003| columnIndex | number | 是 | 指定的列索引,从0开始。 | 5004 5005**返回值:** 5006 5007| 类型 | 说明 | 5008| ---------------- | ---------------------------- | 5009| [Assets](#assets10)| 以Assets形式返回指定列的值。 | 5010 5011**错误码:** 5012 5013以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 5014 5015| **错误码ID** | **错误信息** | 5016| ------------ | ------------------------------------------------------------ | 5017| 14800013 | The column value is null or the column type is incompatible. | 5018 5019**示例:** 5020 5021```ts 5022if(resultSet != undefined) { 5023 const docs = (resultSet as relationalStore.ResultSet).getAssets((resultSet as relationalStore.ResultSet).getColumnIndex("DOCS")); 5024} 5025``` 5026 5027### getRow<sup>11+</sup> 5028 5029getRow(): ValuesBucket 5030 5031获取当前行。 5032 5033**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5034 5035**返回值:** 5036 5037| 类型 | 说明 | 5038| ---------------- | ---------------------------- | 5039| [ValuesBucket](#valuesbucket) | 返回指定行的值。 | 5040 5041**错误码:** 5042 5043以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 5044 5045| **错误码ID** | **错误信息** | 5046| ------------ | ---------------------------- | 5047| 14800000 | Inner error. | 5048 5049**示例:** 5050 5051```ts 5052if(resultSet != undefined) { 5053 const row = (resultSet as relationalStore.ResultSet).getRow(); 5054} 5055``` 5056 5057### isColumnNull 5058 5059isColumnNull(columnIndex: number): boolean 5060 5061检查当前行中指定列的值是否为null。 5062 5063**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5064 5065**参数:** 5066 5067| 参数名 | 类型 | 必填 | 说明 | 5068| ----------- | ------ | ---- | ----------------------- | 5069| columnIndex | number | 是 | 指定的列索引,从0开始。 | 5070 5071**返回值:** 5072 5073| 类型 | 说明 | 5074| ------- | --------------------------------------------------------- | 5075| boolean | 如果当前行中指定列的值为null,则返回true,否则返回false。 | 5076 5077**错误码:** 5078 5079以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 5080 5081| **错误码ID** | **错误信息** | 5082| ------------ | ------------------------------------------------------------ | 5083| 14800013 | The column value is null or the column type is incompatible. | 5084 5085**示例:** 5086 5087```ts 5088if(resultSet != undefined) { 5089 const isColumnNull = (resultSet as relationalStore.ResultSet).isColumnNull((resultSet as relationalStore.ResultSet).getColumnIndex("CODES")); 5090} 5091``` 5092 5093### close 5094 5095close(): void 5096 5097关闭结果集。 5098 5099**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 5100 5101**示例:** 5102 5103```ts 5104if(resultSet != undefined) { 5105 (resultSet as relationalStore.ResultSet).close(); 5106} 5107``` 5108 5109**错误码:** 5110 5111以下错误码的详细介绍请参见[关系型数据库错误码](errorcode-data-rdb.md)。 5112 5113| **错误码ID** | **错误信息** | 5114| ------------ | ------------------------------------------------------------ | 5115| 14800012 | The result set is empty or the specified location is invalid. | 5116