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