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```js 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 open database, database corrupted. | 45| 14800000 | The inner error is occurred. | 46 47**示例:** 48 49FA模型示例: 50 51```js 52 53import featureAbility from '@ohos.ability.featureAbility' 54 55var store; 56 57// 获取context 58let context = featureAbility.getContext(); 59 60const STORE_CONFIG = { 61 name: "RdbTest.db", 62 securityLevel: relationalStore.SecurityLevel.S1 63}; 64 65relationalStore.getRdbStore(context, STORE_CONFIG, function (err, 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' 79 80class EntryAbility extends UIAbility { 81 onWindowStageCreate(windowStage) { 82 var store; 83 const STORE_CONFIG = { 84 name: "RdbTest.db", 85 securityLevel: relationalStore.SecurityLevel.S1 86 }; 87 88 relationalStore.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) { 89 store = rdbStore; 90 if (err) { 91 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 92 return; 93 } 94 console.info(`Get RdbStore successfully.`); 95 }) 96 } 97} 98``` 99 100## relationalStore.getRdbStore 101 102getRdbStore(context: Context, config: StoreConfig): Promise<RdbStore> 103 104获得一个相关的RdbStore,操作关系型数据库,用户可以根据自己的需求配置RdbStore的参数,然后通过RdbStore调用相关接口可以执行相关的数据操作,使用Promise异步回调。 105 106**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 107 108**参数:** 109 110| 参数名 | 类型 | 必填 | 说明 | 111| ------- | -------------------------------- | ---- | ------------------------------------------------------------ | 112| context | Context | 是 | 应用的上下文。 <br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](js-apis-inner-application-uiAbilityContext.md)。 | 113| config | [StoreConfig](#storeconfig) | 是 | 与此RDB存储相关的数据库配置。 | 114 115**返回值**: 116 117| 类型 | 说明 | 118| ----------------------------------------- | --------------------------------- | 119| Promise<[RdbStore](#rdbstore)> | Promise对象。返回RdbStore对象。 | 120 121**错误码:** 122 123以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 124 125| **错误码ID** | **错误信息** | 126| ------------ | ------------------------------------------------- | 127| 14800010 | Failed to open or delete database by invalid database path. | 128| 14800011 | Failed open database, database corrupted. | 129| 14800000 | The inner error is occurred. | 130 131**示例:** 132 133FA模型示例: 134 135```js 136import featureAbility from '@ohos.ability.featureAbility' 137 138var store; 139 140// 获取context 141let context = featureAbility.getContext(); 142 143const STORE_CONFIG = { 144 name: "RdbTest.db", 145 securityLevel: relationalStore.SecurityLevel.S1 146}; 147 148let promise = relationalStore.getRdbStore(context, STORE_CONFIG); 149promise.then(async (rdbStore) => { 150 store = rdbStore; 151 console.info(`Get RdbStore successfully.`); 152}).catch((err) => { 153 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 154}) 155``` 156 157Stage模型示例: 158 159```ts 160import UIAbility from '@ohos.app.ability.UIAbility' 161 162class EntryAbility extends UIAbility { 163 onWindowStageCreate(windowStage) { 164 var store; 165 const STORE_CONFIG = { 166 name: "RdbTest.db", 167 securityLevel: relationalStore.SecurityLevel.S1 168 }; 169 170 let promise = relationalStore.getRdbStore(this.context, STORE_CONFIG); 171 promise.then(async (rdbStore) => { 172 store = rdbStore; 173 console.info(`Get RdbStore successfully.`) 174 }).catch((err) => { 175 console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`); 176 }) 177 } 178} 179``` 180 181## relationalStore.deleteRdbStore 182 183deleteRdbStore(context: Context, name: string, callback: AsyncCallback<void>): void 184 185删除数据库,使用callback异步回调。 186 187**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 188 189**参数:** 190 191| 参数名 | 类型 | 必填 | 说明 | 192| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 193| context | Context | 是 | 应用的上下文。 <br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](js-apis-inner-application-uiAbilityContext.md)。 | 194| name | string | 是 | 数据库名称。 | 195| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 196 197**错误码:** 198 199以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 200 201| **错误码ID** | **错误信息** | 202| ------------ | ------------------------------------------------- | 203| 14800010 | Failed to open or delete database by invalid database path. | 204| 14800000 | The inner error is occurred. | 205 206**示例:** 207 208FA模型示例: 209 210```js 211import featureAbility from '@ohos.ability.featureAbility' 212 213// 获取context 214let context = featureAbility.getContext() 215 216relationalStore.deleteRdbStore(context, "RdbTest.db", function (err) { 217 if (err) { 218 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 219 return; 220 } 221 console.info(`Delete RdbStore successfully.`); 222}) 223``` 224 225Stage模型示例: 226 227```ts 228import UIAbility from '@ohos.app.ability.UIAbility' 229 230class EntryAbility extends UIAbility { 231 onWindowStageCreate(windowStage){ 232 relationalStore.deleteRdbStore(this.context, "RdbTest.db", function (err) { 233 if (err) { 234 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 235 return; 236 } 237 console.info(`Delete RdbStore successfully.`); 238 }) 239 } 240} 241``` 242 243## relationalStore.deleteRdbStore 244 245deleteRdbStore(context: Context, name: string): Promise<void> 246 247使用指定的数据库文件配置删除数据库,使用Promise异步回调。 248 249**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 250 251**参数** 252 253| 参数名 | 类型 | 必填 | 说明 | 254| ------- | ------- | ---- | ------------------------------------------------------------ | 255| context | Context | 是 | 应用的上下文。 <br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](js-apis-inner-application-uiAbilityContext.md)。 | 256| name | string | 是 | 数据库名称。 | 257 258**返回值**: 259 260| 类型 | 说明 | 261| ------------------- | ------------------------- | 262| Promise<void> | 无返回结果的Promise对象。 | 263 264**错误码:** 265 266以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 267 268| **错误码ID** | **错误信息** | 269| ------------ | ------------------------------------------------- | 270| 14800010 | Failed to open or delete database by invalid database path. | 271| 14800000 | The inner error is occurred. | 272 273**示例:** 274 275FA模型示例: 276 277```js 278import featureAbility from '@ohos.ability.featureAbility' 279 280// 获取context 281let context = featureAbility.getContext(); 282 283let promise = relationalStore.deleteRdbStore(context, "RdbTest.db"); 284promise.then(()=>{ 285 console.info(`Delete RdbStore successfully.`); 286}).catch((err) => { 287 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 288}) 289``` 290 291Stage模型示例: 292 293```ts 294import UIAbility from '@ohos.app.ability.UIAbility' 295 296class EntryAbility extends UIAbility { 297 onWindowStageCreate(windowStage){ 298 let promise = relationalStore.deleteRdbStore(this.context, "RdbTest.db"); 299 promise.then(()=>{ 300 console.info(`Delete RdbStore successfully.`); 301 }).catch((err) => { 302 console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`); 303 }) 304 } 305} 306``` 307 308## StoreConfig 309 310管理关系数据库配置。 311 312**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 313 314| 名称 | 类型 | 必填 | 说明 | 315| ------------- | ------------- | ---- | --------------------------------------------------------- | 316| name | string | 是 | 数据库文件名。 | 317| securityLevel | [SecurityLevel](#securitylevel) | 是 | 设置数据库安全级别 | 318| encrypt | boolean | 否 | 指定数据库是否加密,默认不加密。<br/> true:加密。<br/> false:非加密。 | 319 320## SecurityLevel 321 322数据库的安全级别枚举。 323 324> **说明:** 325> 326> 若需要进行同步操作,数据库安全等级应不高于对端设备安全等级,具体可见[跨设备同步访问控制机制](../../database/sync-app-data-across-devices-overview.md#跨设备同步访问控制机制)。 327 328**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 329 330| 名称 | 值 | 说明 | 331| ---- | ---- | ------------------------------------------------------------ | 332| S1 | 1 | 表示数据库的安全级别为低级别,当数据泄露时会产生较低影响。例如,包含壁纸等系统数据的数据库。 | 333| S2 | 2 | 表示数据库的安全级别为中级别,当数据泄露时会产生较大影响。例如,包含录音、视频等用户生成数据或通话记录等信息的数据库。 | 334| S3 | 3 | 表示数据库的安全级别为高级别,当数据泄露时会产生重大影响。例如,包含用户运动、健康、位置等信息的数据库。 | 335| S4 | 4 | 表示数据库的安全级别为关键级别,当数据泄露时会产生严重影响。例如,包含认证凭据、财务数据等信息的数据库。 | 336 337## ValueType 338 339用于表示允许的数据字段类型。 340 341**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 342 343| 类型 | 说明 | 344| ------- | -------------------- | 345| number | 表示值类型为数字。 | 346| string | 表示值类型为字符。 | 347| boolean | 表示值类型为布尔值。 | 348 349## ValuesBucket 350 351用于存储键值对的类型。该类型不是多线程安全的,如果应用中存在多线程同时操作该类派生出的实例,注意加锁保护。 352 353**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 354 355| 键类型 | 值类型 | 356| ------ | ----------------------------------------------------------- | 357| string | [ValueType](#valuetype)\| Uint8Array \| null | 358 359## SyncMode 360 361指数据库同步模式。 362 363**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 364 365| 名称 | 值 | 说明 | 366| -------------- | ---- | ---------------------------------- | 367| SYNC_MODE_PUSH | 0 | 表示数据从本地设备推送到远程设备。 | 368| SYNC_MODE_PULL | 1 | 表示数据从远程设备拉至本地设备。 | 369 370## SubscribeType 371 372描述订阅类型。 373 374**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 375 376**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 377 378| 名称 | 值 | 说明 | 379| --------------------- | ---- | ------------------ | 380| SUBSCRIBE_TYPE_REMOTE | 0 | 订阅远程数据更改。 | 381 382## RdbPredicates 383 384表示关系型数据库(RDB)的谓词。该类确定RDB中条件表达式的值是true还是false。该类型不是多线程安全的,如果应用中存在多线程同时操作该类派生出的实例,注意加锁保护。 385 386### constructor 387 388constructor(name: string) 389 390构造函数。 391 392**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 393 394**参数:** 395 396| 参数名 | 类型 | 必填 | 说明 | 397| ------ | ------ | ---- | ------------ | 398| name | string | 是 | 数据库表名。 | 399 400**示例:** 401 402```js 403let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 404``` 405 406### inDevices 407 408inDevices(devices: Array<string>): RdbPredicates 409 410同步分布式数据库时连接到组网内指定的远程设备。 411 412> **说明:** 413> 414> 其中devices通过调用[deviceManager.getTrustedDeviceListSync](js-apis-device-manager.md#gettrusteddevicelistsync)方法得到。deviceManager模块的接口均为系统接口,仅系统应用可用。 415 416**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 417 418**参数:** 419 420| 参数名 | 类型 | 必填 | 说明 | 421| ------- | ------------------- | ---- | -------------------------- | 422| devices | Array<string> | 是 | 指定的组网内的远程设备ID。 | 423 424**返回值**: 425 426| 类型 | 说明 | 427| ------------------------------------ | -------------------------- | 428| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 429 430**示例:** 431 432```js 433import deviceManager from '@ohos.distributedHardware.deviceManager'; 434let dmInstance = null; 435let deviceIds = []; 436 437deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { 438 if (err) { 439 console.log("create device manager failed, err=" + err); 440 return; 441 } 442 dmInstance = manager; 443 let devices = dmInstance.getTrustedDeviceListSync(); 444 for (var i = 0; i < devices.length; i++) { 445 deviceIds[i] = devices[i].deviceId; 446 } 447}) 448 449let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 450predicates.inDevices(deviceIds); 451``` 452 453### inAllDevices 454 455inAllDevices(): RdbPredicates 456 457 458同步分布式数据库时连接到组网内所有的远程设备。 459 460**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 461 462**返回值**: 463 464| 类型 | 说明 | 465| ------------------------------------ | -------------------------- | 466| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 467 468**示例:** 469 470```js 471let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 472predicates.inAllDevices(); 473``` 474 475### equalTo 476 477equalTo(field: string, value: ValueType): RdbPredicates 478 479 480配置谓词以匹配数据字段为ValueType且值等于指定值的字段。 481 482**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 483 484**参数:** 485 486| 参数名 | 类型 | 必填 | 说明 | 487| ------ | ----------------------- | ---- | ---------------------- | 488| field | string | 是 | 数据库表中的列名。 | 489| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 490 491**返回值**: 492 493| 类型 | 说明 | 494| ------------------------------------ | -------------------------- | 495| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 496 497**示例:** 498 499```js 500let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 501predicates.equalTo("NAME", "lisi"); 502``` 503 504 505### notEqualTo 506 507notEqualTo(field: string, value: ValueType): RdbPredicates 508 509 510配置谓词以匹配数据字段为ValueType且值不等于指定值的字段。 511 512**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 513 514**参数:** 515 516| 参数名 | 类型 | 必填 | 说明 | 517| ------ | ----------------------- | ---- | ---------------------- | 518| field | string | 是 | 数据库表中的列名。 | 519| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 520 521**返回值**: 522 523| 类型 | 说明 | 524| ------------------------------------ | -------------------------- | 525| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 526 527**示例:** 528 529```js 530let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 531predicates.notEqualTo("NAME", "lisi"); 532``` 533 534 535### beginWrap 536 537beginWrap(): RdbPredicates 538 539 540向谓词添加左括号。 541 542**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 543 544**返回值**: 545 546| 类型 | 说明 | 547| ------------------------------------ | ------------------------- | 548| [RdbPredicates](#rdbpredicates) | 返回带有左括号的Rdb谓词。 | 549 550**示例:** 551 552```js 553let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 554predicates.equalTo("NAME", "lisi") 555 .beginWrap() 556 .equalTo("AGE", 18) 557 .or() 558 .equalTo("SALARY", 200.5) 559 .endWrap() 560``` 561 562### endWrap 563 564endWrap(): RdbPredicates 565 566向谓词添加右括号。 567 568**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 569 570**返回值**: 571 572| 类型 | 说明 | 573| ------------------------------------ | ------------------------- | 574| [RdbPredicates](#rdbpredicates) | 返回带有右括号的Rdb谓词。 | 575 576**示例:** 577 578```js 579let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 580predicates.equalTo("NAME", "lisi") 581 .beginWrap() 582 .equalTo("AGE", 18) 583 .or() 584 .equalTo("SALARY", 200.5) 585 .endWrap() 586``` 587 588### or 589 590or(): RdbPredicates 591 592将或条件添加到谓词中。 593 594**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 595 596**返回值**: 597 598| 类型 | 说明 | 599| ------------------------------------ | ------------------------- | 600| [RdbPredicates](#rdbpredicates) | 返回带有或条件的Rdb谓词。 | 601 602**示例:** 603 604```js 605let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 606predicates.equalTo("NAME", "Lisa") 607 .or() 608 .equalTo("NAME", "Rose") 609``` 610 611### and 612 613and(): RdbPredicates 614 615向谓词添加和条件。 616 617**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 618 619**返回值**: 620 621| 类型 | 说明 | 622| ------------------------------------ | ------------------------- | 623| [RdbPredicates](#rdbpredicates) | 返回带有和条件的Rdb谓词。 | 624 625**示例:** 626 627```js 628let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 629predicates.equalTo("NAME", "Lisa") 630 .and() 631 .equalTo("SALARY", 200.5) 632``` 633 634### contains 635 636contains(field: string, value: string): RdbPredicates 637 638配置谓词以匹配数据字段为string且value包含指定值的字段。 639 640**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 641 642**参数:** 643 644| 参数名 | 类型 | 必填 | 说明 | 645| ------ | ------ | ---- | ---------------------- | 646| field | string | 是 | 数据库表中的列名。 | 647| value | string | 是 | 指示要与谓词匹配的值。 | 648 649**返回值**: 650 651| 类型 | 说明 | 652| ------------------------------------ | -------------------------- | 653| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 654 655**示例:** 656 657```js 658let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 659predicates.contains("NAME", "os"); 660``` 661 662### beginsWith 663 664beginsWith(field: string, value: string): RdbPredicates 665 666配置谓词以匹配数据字段为string且值以指定字符串开头的字段。 667 668**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 669 670**参数:** 671 672| 参数名 | 类型 | 必填 | 说明 | 673| ------ | ------ | ---- | ---------------------- | 674| field | string | 是 | 数据库表中的列名。 | 675| value | string | 是 | 指示要与谓词匹配的值。 | 676 677**返回值**: 678 679| 类型 | 说明 | 680| ------------------------------------ | -------------------------- | 681| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 682 683**示例:** 684 685```js 686let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 687predicates.beginsWith("NAME", "os"); 688``` 689 690### endsWith 691 692endsWith(field: string, value: string): RdbPredicates 693 694配置谓词以匹配数据字段为string且值以指定字符串结尾的字段。 695 696**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 697 698**参数:** 699 700| 参数名 | 类型 | 必填 | 说明 | 701| ------ | ------ | ---- | ---------------------- | 702| field | string | 是 | 数据库表中的列名。 | 703| value | string | 是 | 指示要与谓词匹配的值。 | 704 705**返回值**: 706 707| 类型 | 说明 | 708| ------------------------------------ | -------------------------- | 709| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 710 711**示例:** 712 713```js 714let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 715predicates.endsWith("NAME", "se"); 716``` 717 718### isNull 719 720isNull(field: string): RdbPredicates 721 722配置谓词以匹配值为null的字段。 723 724**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 725 726**参数:** 727 728| 参数名 | 类型 | 必填 | 说明 | 729| ------ | ------ | ---- | ------------------ | 730| field | string | 是 | 数据库表中的列名。 | 731 732**返回值**: 733 734| 类型 | 说明 | 735| ------------------------------------ | -------------------------- | 736| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 737 738**示例**: 739 740```js 741let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 742predicates.isNull("NAME"); 743``` 744 745### isNotNull 746 747isNotNull(field: string): RdbPredicates 748 749配置谓词以匹配值不为null的指定字段。 750 751**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 752 753**参数:** 754 755| 参数名 | 类型 | 必填 | 说明 | 756| ------ | ------ | ---- | ------------------ | 757| field | string | 是 | 数据库表中的列名。 | 758 759**返回值**: 760 761| 类型 | 说明 | 762| ------------------------------------ | -------------------------- | 763| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 764 765**示例:** 766 767```js 768let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 769predicates.isNotNull("NAME"); 770``` 771 772### like 773 774like(field: string, value: string): RdbPredicates 775 776配置谓词以匹配数据字段为string且值类似于指定字符串的字段。 777 778**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 779 780**参数:** 781 782| 参数名 | 类型 | 必填 | 说明 | 783| ------ | ------ | ---- | ---------------------- | 784| field | string | 是 | 数据库表中的列名。 | 785| value | string | 是 | 指示要与谓词匹配的值。 | 786 787**返回值**: 788 789| 类型 | 说明 | 790| ------------------------------------ | -------------------------- | 791| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 792 793**示例:** 794 795```js 796let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 797predicates.like("NAME", "%os%"); 798``` 799 800### glob 801 802glob(field: string, value: string): RdbPredicates 803 804配置RdbPredicates匹配数据字段为string的指定字段。 805 806**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 807 808**参数:** 809 810| 参数名 | 类型 | 必填 | 说明 | 811| ------ | ------ | ---- | ------------------------------------------------------------ | 812| field | string | 是 | 数据库表中的列名。 | 813| value | string | 是 | 指示要与谓词匹配的值。<br>支持通配符,*表示0个、1个或多个数字或字符,?表示1个数字或字符。 | 814 815**返回值**: 816 817| 类型 | 说明 | 818| ------------------------------------ | -------------------------- | 819| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 820 821**示例:** 822 823```js 824let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 825predicates.glob("NAME", "?h*g"); 826``` 827 828### between 829 830between(field: string, low: ValueType, high: ValueType): RdbPredicates 831 832将谓词配置为匹配数据字段为ValueType且value在给定范围内的指定字段。 833 834**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 835 836**参数:** 837 838| 参数名 | 类型 | 必填 | 说明 | 839| ------ | ----------------------- | ---- | -------------------------- | 840| field | string | 是 | 数据库表中的列名。 | 841| low | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最小值。 | 842| high | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的最大值。 | 843 844**返回值**: 845 846| 类型 | 说明 | 847| ------------------------------------ | -------------------------- | 848| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 849 850**示例:** 851 852```js 853let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 854predicates.between("AGE", 10, 50); 855``` 856 857### notBetween 858 859notBetween(field: string, low: ValueType, high: ValueType): RdbPredicates 860 861配置RdbPredicates以匹配数据字段为ValueType且value超出给定范围的指定字段。 862 863**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 864 865**参数:** 866 867| 参数名 | 类型 | 必填 | 说明 | 868| ------ | ----------------------- | ---- | -------------------------- | 869| field | string | 是 | 数据库表中的列名。 | 870| low | [ValueType](#valuetype) | 是 | 指示与谓词匹配的最小值。 | 871| high | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的最大值。 | 872 873**返回值**: 874 875| 类型 | 说明 | 876| ------------------------------------ | -------------------------- | 877| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 878 879**示例:** 880 881```js 882let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 883predicates.notBetween("AGE", 10, 50); 884``` 885 886### greaterThan 887 888greaterThan(field: string, value: ValueType): RdbPredicates 889 890配置谓词以匹配数据字段为ValueType且值大于指定值的字段。 891 892**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 893 894**参数:** 895 896| 参数名 | 类型 | 必填 | 说明 | 897| ------ | ----------------------- | ---- | ---------------------- | 898| field | string | 是 | 数据库表中的列名。 | 899| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 900 901**返回值**: 902 903| 类型 | 说明 | 904| ------------------------------------ | -------------------------- | 905| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 906 907**示例:** 908 909```js 910let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 911predicates.greaterThan("AGE", 18); 912``` 913 914### lessThan 915 916lessThan(field: string, value: ValueType): RdbPredicates 917 918配置谓词以匹配数据字段为valueType且value小于指定值的字段。 919 920**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 921 922**参数:** 923 924| 参数名 | 类型 | 必填 | 说明 | 925| ------ | ----------------------- | ---- | ---------------------- | 926| field | string | 是 | 数据库表中的列名。 | 927| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 928 929**返回值**: 930 931| 类型 | 说明 | 932| ------------------------------------ | -------------------------- | 933| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 934 935**示例:** 936 937```js 938let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 939predicates.lessThan("AGE", 20); 940``` 941 942### greaterThanOrEqualTo 943 944greaterThanOrEqualTo(field: string, value: ValueType): RdbPredicates 945 946配置谓词以匹配数据字段为ValueType且value大于或等于指定值的字段。 947 948**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 949 950**参数:** 951 952| 参数名 | 类型 | 必填 | 说明 | 953| ------ | ----------------------- | ---- | ---------------------- | 954| field | string | 是 | 数据库表中的列名。 | 955| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 956 957**返回值**: 958 959| 类型 | 说明 | 960| ------------------------------------ | -------------------------- | 961| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 962 963**示例:** 964 965```js 966let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 967predicates.greaterThanOrEqualTo("AGE", 18); 968``` 969 970### lessThanOrEqualTo 971 972lessThanOrEqualTo(field: string, value: ValueType): RdbPredicates 973 974配置谓词以匹配数据字段为ValueType且value小于或等于指定值的字段。 975 976**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 977 978**参数:** 979 980| 参数名 | 类型 | 必填 | 说明 | 981| ------ | ----------------------- | ---- | ---------------------- | 982| field | string | 是 | 数据库表中的列名。 | 983| value | [ValueType](#valuetype) | 是 | 指示要与谓词匹配的值。 | 984 985**返回值**: 986 987| 类型 | 说明 | 988| ------------------------------------ | -------------------------- | 989| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 990 991**示例:** 992 993```js 994let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 995predicates.lessThanOrEqualTo("AGE", 20); 996``` 997 998### orderByAsc 999 1000orderByAsc(field: string): RdbPredicates 1001 1002配置谓词以匹配其值按升序排序的列。 1003 1004**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1005 1006**参数:** 1007 1008| 参数名 | 类型 | 必填 | 说明 | 1009| ------ | ------ | ---- | ------------------ | 1010| field | string | 是 | 数据库表中的列名。 | 1011 1012**返回值**: 1013 1014| 类型 | 说明 | 1015| ------------------------------------ | -------------------------- | 1016| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1017 1018**示例:** 1019 1020```js 1021let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1022predicates.orderByAsc("NAME"); 1023``` 1024 1025### orderByDesc 1026 1027orderByDesc(field: string): RdbPredicates 1028 1029配置谓词以匹配其值按降序排序的列。 1030 1031**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1032 1033**参数:** 1034 1035| 参数名 | 类型 | 必填 | 说明 | 1036| ------ | ------ | ---- | ------------------ | 1037| field | string | 是 | 数据库表中的列名。 | 1038 1039**返回值**: 1040 1041| 类型 | 说明 | 1042| ------------------------------------ | -------------------------- | 1043| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1044 1045**示例:** 1046 1047```js 1048let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1049predicates.orderByDesc("AGE"); 1050``` 1051 1052### distinct 1053 1054distinct(): RdbPredicates 1055 1056配置谓词以过滤重复记录并仅保留其中一个。 1057 1058**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1059 1060**返回值**: 1061 1062| 类型 | 说明 | 1063| ------------------------------------ | ------------------------------ | 1064| [RdbPredicates](#rdbpredicates) | 返回可用于过滤重复记录的谓词。 | 1065 1066**示例:** 1067 1068```js 1069let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1070predicates.equalTo("NAME", "Rose").distinct(); 1071``` 1072 1073### limitAs 1074 1075limitAs(value: number): RdbPredicates 1076 1077设置最大数据记录数的谓词。 1078 1079**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1080 1081**参数:** 1082 1083| 参数名 | 类型 | 必填 | 说明 | 1084| ------ | ------ | ---- | ---------------- | 1085| value | number | 是 | 最大数据记录数。 | 1086 1087**返回值**: 1088 1089| 类型 | 说明 | 1090| ------------------------------------ | ------------------------------------ | 1091| [RdbPredicates](#rdbpredicates) | 返回可用于设置最大数据记录数的谓词。 | 1092 1093**示例:** 1094 1095```js 1096let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1097predicates.equalTo("NAME", "Rose").limitAs(3); 1098``` 1099 1100### offsetAs 1101 1102offsetAs(rowOffset: number): RdbPredicates 1103 1104配置RdbPredicates以指定返回结果的起始位置。 1105 1106**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1107 1108**参数:** 1109 1110| 参数名 | 类型 | 必填 | 说明 | 1111| --------- | ------ | ---- | ---------------------------------- | 1112| rowOffset | number | 是 | 返回结果的起始位置,取值为正整数。 | 1113 1114**返回值**: 1115 1116| 类型 | 说明 | 1117| ------------------------------------ | ------------------------------------ | 1118| [RdbPredicates](#rdbpredicates) | 返回具有指定返回结果起始位置的谓词。 | 1119 1120**示例:** 1121 1122```js 1123let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1124predicates.equalTo("NAME", "Rose").offsetAs(3); 1125``` 1126 1127### groupBy 1128 1129groupBy(fields: Array<string>): RdbPredicates 1130 1131配置RdbPredicates按指定列分组查询结果。 1132 1133**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1134 1135**参数:** 1136 1137| 参数名 | 类型 | 必填 | 说明 | 1138| ------ | ------------------- | ---- | -------------------- | 1139| fields | Array<string> | 是 | 指定分组依赖的列名。 | 1140 1141**返回值**: 1142 1143| 类型 | 说明 | 1144| ------------------------------------ | ---------------------- | 1145| [RdbPredicates](#rdbpredicates) | 返回分组查询列的谓词。 | 1146 1147**示例:** 1148 1149```js 1150let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1151predicates.groupBy(["AGE", "NAME"]); 1152``` 1153 1154### indexedBy 1155 1156indexedBy(field: string): RdbPredicates 1157 1158配置RdbPredicates以指定索引列。 1159 1160**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1161 1162**参数:** 1163 1164| 参数名 | 类型 | 必填 | 说明 | 1165| ------ | ------ | ---- | -------------- | 1166| field | string | 是 | 索引列的名称。 | 1167 1168**返回值**: 1169 1170 1171| 类型 | 说明 | 1172| ------------------------------------ | ------------------------------------- | 1173| [RdbPredicates](#rdbpredicates) | 返回具有指定索引列的RdbPredicates。 | 1174 1175**示例:** 1176 1177```js 1178let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1179predicates.indexedBy("SALARY_INDEX"); 1180``` 1181 1182### in 1183 1184in(field: string, value: Array<ValueType>): RdbPredicates 1185 1186配置RdbPredicates以匹配数据字段为ValueType数组且值在给定范围内的指定字段。 1187 1188**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1189 1190**参数:** 1191 1192| 参数名 | 类型 | 必填 | 说明 | 1193| ------ | ------------------------------------ | ---- | --------------------------------------- | 1194| field | string | 是 | 数据库表中的列名。 | 1195| value | Array<[ValueType](#valuetype)> | 是 | 以ValueType型数组形式指定的要匹配的值。 | 1196 1197**返回值**: 1198 1199| 类型 | 说明 | 1200| ------------------------------------ | -------------------------- | 1201| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1202 1203**示例:** 1204 1205```js 1206let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1207predicates.in("AGE", [18, 20]); 1208``` 1209 1210### notIn 1211 1212notIn(field: string, value: Array<ValueType>): RdbPredicates 1213 1214将RdbPredicates配置为匹配数据字段为ValueType且值超出给定范围的指定字段。 1215 1216**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1217 1218**参数:** 1219 1220| 参数名 | 类型 | 必填 | 说明 | 1221| ------ | ------------------------------------ | ---- | ------------------------------------- | 1222| field | string | 是 | 数据库表中的列名。 | 1223| value | Array<[ValueType](#valuetype)> | 是 | 以ValueType数组形式指定的要匹配的值。 | 1224 1225**返回值**: 1226 1227| 类型 | 说明 | 1228| ------------------------------------ | -------------------------- | 1229| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 | 1230 1231**示例:** 1232 1233```js 1234let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1235predicates.notIn("NAME", ["Lisa", "Rose"]); 1236``` 1237 1238## RdbStore 1239 1240提供管理关系数据库(RDB)方法的接口。 1241 1242在使用以下相关接口前,请使用[executeSql](#executesql)接口初始化数据库表结构和相关数据。 1243 1244### insert 1245 1246insert(table: string, values: ValuesBucket, callback: AsyncCallback<number>):void 1247 1248向目标表中插入一行数据,使用callback异步回调。 1249 1250**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1251 1252**参数:** 1253 1254| 参数名 | 类型 | 必填 | 说明 | 1255| -------- | ----------------------------- | ---- | ---------------------------------------------------------- | 1256| table | string | 是 | 指定的目标表名。 | 1257| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 1258| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回行ID;否则返回-1。 | 1259 1260**示例:** 1261 1262```js 1263const valueBucket = { 1264 "NAME": "Lisa", 1265 "AGE": 18, 1266 "SALARY": 100.5, 1267 "CODES": new Uint8Array([1, 2, 3, 4, 5]), 1268}; 1269store.insert("EMPLOYEE", valueBucket, function (err, rowId) { 1270 if (err) { 1271 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 1272 return; 1273 } 1274 console.info(`Insert is successful, rowId = ${rowId}`); 1275}) 1276``` 1277 1278 1279### insert 1280 1281insert(table: string, values: ValuesBucket):Promise<number> 1282 1283向目标表中插入一行数据,使用Promise异步回调。 1284 1285**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1286 1287**参数:** 1288 1289| 参数名 | 类型 | 必填 | 说明 | 1290| ------ | ----------------------------- | ---- | -------------------------- | 1291| table | string | 是 | 指定的目标表名。 | 1292| values | [ValuesBucket](#valuesbucket) | 是 | 表示要插入到表中的数据行。 | 1293 1294**返回值**: 1295 1296| 类型 | 说明 | 1297| --------------------- | ------------------------------------------------- | 1298| Promise<number> | Promise对象。如果操作成功,返回行ID;否则返回-1。 | 1299 1300**错误码:** 1301 1302以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1303 1304| **错误码ID** | **错误信息** | 1305| ------------ | -------------------------------------------- | 1306| 14800000 | The inner error is occurred. | 1307 1308**示例:** 1309 1310```js 1311const valueBucket = { 1312 "NAME": "Lisa", 1313 "AGE": 18, 1314 "SALARY": 100.5, 1315 "CODES": new Uint8Array([1, 2, 3, 4, 5]), 1316}; 1317let promise = store.insert("EMPLOYEE", valueBucket); 1318promise.then((rowId) => { 1319 console.info(`Insert is successful, rowId = ${rowId}`); 1320}).catch((err) => { 1321 console.error(`Insert is failed, code is ${err.code},message is ${err.message}`); 1322}) 1323``` 1324 1325 1326### batchInsert 1327 1328batchInsert(table: string, values: Array<ValuesBucket>, callback: AsyncCallback<number>):void 1329 1330向目标表中插入一组数据,使用callback异步回调。 1331 1332**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1333 1334**参数:** 1335 1336| 参数名 | 类型 | 必填 | 说明 | 1337| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ | 1338| table | string | 是 | 指定的目标表名。 | 1339| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 1340| callback | AsyncCallback<number> | 是 | 指定callback回调函数。如果操作成功,返回插入的数据个数,否则返回-1。 | 1341 1342**错误码:** 1343 1344以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1345 1346| **错误码ID** | **错误信息** | 1347| ------------ | -------------------------------------------- | 1348| 14800000 | The inner error is occurred. | 1349 1350**示例:** 1351 1352```js 1353const valueBucket1 = { 1354 "NAME": "Lisa", 1355 "AGE": 18, 1356 "SALARY": 100.5, 1357 "CODES": new Uint8Array([1, 2, 3, 4, 5]) 1358}; 1359const valueBucket2 = { 1360 "NAME": "Jack", 1361 "AGE": 19, 1362 "SALARY": 101.5, 1363 "CODES": new Uint8Array([6, 7, 8, 9, 10]) 1364}; 1365const valueBucket3 = { 1366 "NAME": "Tom", 1367 "AGE": 20, 1368 "SALARY": 102.5, 1369 "CODES": new Uint8Array([11, 12, 13, 14, 15]) 1370}; 1371 1372let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 1373store.batchInsert("EMPLOYEE", valueBuckets, function(err, insertNum) { 1374 if (err) { 1375 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 1376 return; 1377 } 1378 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 1379}) 1380``` 1381 1382### batchInsert 1383 1384batchInsert(table: string, values: Array<ValuesBucket>):Promise<number> 1385 1386向目标表中插入一组数据,使用Promise异步回调。 1387 1388**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1389 1390**参数:** 1391 1392| 参数名 | 类型 | 必填 | 说明 | 1393| ------ | ------------------------------------------ | ---- | ---------------------------- | 1394| table | string | 是 | 指定的目标表名。 | 1395| values | Array<[ValuesBucket](#valuesbucket)> | 是 | 表示要插入到表中的一组数据。 | 1396 1397**返回值**: 1398 1399| 类型 | 说明 | 1400| --------------------- | ----------------------------------------------------------- | 1401| Promise<number> | Promise对象。如果操作成功,返回插入的数据个数,否则返回-1。 | 1402 1403**错误码:** 1404 1405以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1406 1407| **错误码ID** | **错误信息** | 1408| ------------ | -------------------------------------------- | 1409| 14800000 | The inner error is occurred. | 1410 1411**示例:** 1412 1413```js 1414const valueBucket1 = { 1415 "NAME": "Lisa", 1416 "AGE": 18, 1417 "SALARY": 100.5, 1418 "CODES": new Uint8Array([1, 2, 3, 4, 5]) 1419}; 1420const valueBucket2 = { 1421 "NAME": "Jack", 1422 "AGE": 19, 1423 "SALARY": 101.5, 1424 "CODES": new Uint8Array([6, 7, 8, 9, 10]) 1425}; 1426const valueBucket3 = { 1427 "NAME": "Tom", 1428 "AGE": 20, 1429 "SALARY": 102.5, 1430 "CODES": new Uint8Array([11, 12, 13, 14, 15]) 1431}; 1432 1433let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3); 1434let promise = store.batchInsert("EMPLOYEE", valueBuckets); 1435promise.then((insertNum) => { 1436 console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`); 1437}).catch((err) => { 1438 console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`); 1439}) 1440``` 1441 1442### update 1443 1444update(values: ValuesBucket, predicates: RdbPredicates, callback: AsyncCallback<number>):void 1445 1446根据RdbPredicates的指定实例对象更新数据库中的数据,使用callback异步回调。 1447 1448**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1449 1450**参数:** 1451 1452| 参数名 | 类型 | 必填 | 说明 | 1453| ---------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 1454| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 1455| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 1456| callback | AsyncCallback<number> | 是 | 指定的callback回调方法。返回受影响的行数。 | 1457 1458 1459**错误码:** 1460 1461以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1462 1463| **错误码ID** | **错误信息** | 1464| ------------ | -------------------------------------------- | 1465| 14800000 | The inner error is occurred. | 1466 1467**示例:** 1468 1469```js 1470const valueBucket = { 1471 "NAME": "Rose", 1472 "AGE": 22, 1473 "SALARY": 200.5, 1474 "CODES": new Uint8Array([1, 2, 3, 4, 5]), 1475}; 1476let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1477predicates.equalTo("NAME", "Lisa"); 1478store.update(valueBucket, predicates, function (err, rows) { 1479 if (err) { 1480 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 1481 return; 1482 } 1483 console.info(`Updated row count: ${rows}`); 1484}) 1485``` 1486 1487 1488### update 1489 1490update(values: ValuesBucket, predicates: RdbPredicates):Promise<number> 1491 1492根据RdbPredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。 1493 1494**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1495 1496**参数:** 1497 1498| 参数名 | 类型 | 必填 | 说明 | 1499| ------------ | ------------------------------------ | ---- | ------------------------------------------------------------ | 1500| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 1501| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的更新条件。 | 1502 1503**返回值**: 1504 1505| 类型 | 说明 | 1506| --------------------- | ----------------------------------------- | 1507| Promise<number> | 指定的Promise回调方法。返回受影响的行数。 | 1508 1509**错误码:** 1510 1511以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1512 1513| **错误码ID** | **错误信息** | 1514| ------------ | -------------------------------------------- | 1515| 14800000 | The inner error is occurred. | 1516 1517**示例:** 1518 1519```js 1520const valueBucket = { 1521 "NAME": "Rose", 1522 "AGE": 22, 1523 "SALARY": 200.5, 1524 "CODES": new Uint8Array([1, 2, 3, 4, 5]), 1525}; 1526let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1527predicates.equalTo("NAME", "Lisa"); 1528let promise = store.update(valueBucket, predicates); 1529promise.then(async (rows) => { 1530 console.info(`Updated row count: ${rows}`); 1531}).catch((err) => { 1532 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 1533}) 1534``` 1535 1536 1537### update 1538 1539update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<number>):void 1540 1541根据DataSharePredicates的指定实例对象更新数据库中的数据,使用callback异步回调。 1542 1543**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1544 1545**模型约束:** 此接口仅可在Stage模型下使用。 1546 1547**系统接口:** 此接口为系统接口。 1548 1549**参数:** 1550 1551| 参数名 | 类型 | 必填 | 说明 | 1552| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1553| table | string | 是 | 指定的目标表名。 | 1554| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 1555| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的更新条件。 | 1556| callback | AsyncCallback<number> | 是 | 指定的callback回调方法。返回受影响的行数。 | 1557 1558**错误码:** 1559 1560以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1561 1562| **错误码ID** | **错误信息** | 1563| ------------ | -------------------------------------------- | 1564| 14800000 | The inner error is occurred. | 1565 1566**示例:** 1567 1568```js 1569import dataSharePredicates from '@ohos.data.dataSharePredicates' 1570const valueBucket = { 1571 "NAME": "Rose", 1572 "AGE": 22, 1573 "SALARY": 200.5, 1574 "CODES": new Uint8Array([1, 2, 3, 4, 5]), 1575}; 1576let predicates = new dataSharePredicates.DataSharePredicates(); 1577predicates.equalTo("NAME", "Lisa"); 1578store.update("EMPLOYEE", valueBucket, predicates, function (err, rows) { 1579 if (err) { 1580 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 1581 return; 1582 } 1583 console.info(`Updated row count: ${rows}`); 1584}) 1585``` 1586 1587### update 1588 1589update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates):Promise<number> 1590 1591根据DataSharePredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。 1592 1593**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1594 1595**模型约束:** 此接口仅可在Stage模型下使用。 1596 1597**系统接口:** 此接口为系统接口。 1598 1599**参数:** 1600 1601| 参数名 | 类型 | 必填 | 说明 | 1602| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1603| table | string | 是 | 指定的目标表名。 | 1604| values | [ValuesBucket](#valuesbucket) | 是 | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 | 1605| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的更新条件。 | 1606 1607**返回值**: 1608 1609| 类型 | 说明 | 1610| --------------------- | ----------------------------------------- | 1611| Promise<number> | 指定的Promise回调方法。返回受影响的行数。 | 1612 1613**错误码:** 1614 1615以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1616 1617| **错误码ID** | **错误信息** | 1618| ------------ | -------------------------------------------- | 1619| 14800000 | The inner error is occurred. | 1620 1621**示例:** 1622 1623```js 1624import dataSharePredicates from '@ohos.data.dataSharePredicates' 1625const valueBucket = { 1626 "NAME": "Rose", 1627 "AGE": 22, 1628 "SALARY": 200.5, 1629 "CODES": new Uint8Array([1, 2, 3, 4, 5]), 1630}; 1631let predicates = new dataSharePredicates.DataSharePredicates(); 1632predicates.equalTo("NAME", "Lisa"); 1633let promise = store.update("EMPLOYEE", valueBucket, predicates); 1634promise.then(async (rows) => { 1635 console.info(`Updated row count: ${rows}`); 1636}).catch((err) => { 1637 console.error(`Updated failed, code is ${err.code},message is ${err.message}`); 1638}) 1639``` 1640 1641### delete 1642 1643delete(predicates: RdbPredicates, callback: AsyncCallback<number>):void 1644 1645根据RdbPredicates的指定实例对象从数据库中删除数据,使用callback异步回调。 1646 1647**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1648 1649**参数:** 1650 1651| 参数名 | 类型 | 必填 | 说明 | 1652| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 1653| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 1654| callback | AsyncCallback<number> | 是 | 指定callback回调函数。返回受影响的行数。 | 1655 1656**错误码:** 1657 1658以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1659 1660| **错误码ID** | **错误信息** | 1661| ------------ | -------------------------------------------- | 1662| 14800000 | The inner error is occurred. | 1663 1664**示例:** 1665 1666```js 1667let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1668predicates.equalTo("NAME", "Lisa"); 1669store.delete(predicates, function (err, rows) { 1670 if (err) { 1671 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 1672 return; 1673 } 1674 console.info(`Delete rows: ${rows}`); 1675}) 1676``` 1677 1678### delete 1679 1680delete(predicates: RdbPredicates):Promise<number> 1681 1682根据RdbPredicates的指定实例对象从数据库中删除数据,使用Promise异步回调。 1683 1684**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1685 1686**参数:** 1687 1688| 参数名 | 类型 | 必填 | 说明 | 1689| ---------- | ------------------------------------ | ---- | ----------------------------------------- | 1690| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的删除条件。 | 1691 1692**返回值**: 1693 1694| 类型 | 说明 | 1695| --------------------- | ------------------------------- | 1696| Promise<number> | Promise对象。返回受影响的行数。 | 1697 1698**错误码:** 1699 1700以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1701 1702| **错误码ID** | **错误信息** | 1703| ------------ | -------------------------------------------- | 1704| 14800000 | The inner error is occurred. | 1705 1706**示例:** 1707 1708```js 1709let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1710predicates.equalTo("NAME", "Lisa"); 1711let promise = store.delete(predicates); 1712promise.then((rows) => { 1713 console.info(`Delete rows: ${rows}`); 1714}).catch((err) => { 1715 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 1716}) 1717``` 1718 1719### delete 1720 1721delete(table: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback<number>):void 1722 1723根据DataSharePredicates的指定实例对象从数据库中删除数据,使用callback异步回调。 1724 1725**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1726 1727**模型约束:** 此接口仅可在Stage模型下使用。 1728 1729**系统接口:** 此接口为系统接口。 1730 1731**参数:** 1732 1733| 参数名 | 类型 | 必填 | 说明 | 1734| ---------- | ------------------------------------------------------------ | ---- | --------------------------------------------- | 1735| table | string | 是 | 指定的目标表名。 | 1736| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的删除条件。 | 1737| callback | AsyncCallback<number> | 是 | 指定callback回调函数。返回受影响的行数。 | 1738 1739**错误码:** 1740 1741以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1742 1743| **错误码ID** | **错误信息** | 1744| ------------ | -------------------------------------------- | 1745| 14800000 | The inner error is occurred. | 1746 1747**示例:** 1748 1749```js 1750import dataSharePredicates from '@ohos.data.dataSharePredicates' 1751let predicates = new dataSharePredicates.DataSharePredicates(); 1752predicates.equalTo("NAME", "Lisa"); 1753store.delete("EMPLOYEE", predicates, function (err, rows) { 1754 if (err) { 1755 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 1756 return; 1757 } 1758 console.info(`Delete rows: ${rows}`); 1759}) 1760``` 1761 1762### delete 1763 1764delete(table: string, predicates: dataSharePredicates.DataSharePredicates):Promise<number> 1765 1766根据DataSharePredicates的指定实例对象从数据库中删除数据,使用Promise异步回调。 1767 1768**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1769 1770**模型约束:** 此接口仅可在Stage模型下使用。 1771 1772**系统接口:** 此接口为系统接口。 1773 1774**参数:** 1775 1776| 参数名 | 类型 | 必填 | 说明 | 1777| ---------- | ------------------------------------------------------------ | ---- | --------------------------------------------- | 1778| table | string | 是 | 指定的目标表名。 | 1779| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的删除条件。 | 1780 1781**返回值**: 1782 1783| 类型 | 说明 | 1784| --------------------- | ------------------------------- | 1785| Promise<number> | Promise对象。返回受影响的行数。 | 1786 1787**错误码:** 1788 1789以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1790 1791| **错误码ID** | **错误信息** | 1792| ------------ | -------------------------------------------- | 1793| 14800000 | The inner error is occurred. | 1794 1795**示例:** 1796 1797```js 1798import dataSharePredicates from '@ohos.data.dataSharePredicates' 1799let predicates = new dataSharePredicates.DataSharePredicates(); 1800predicates.equalTo("NAME", "Lisa"); 1801let promise = store.delete("EMPLOYEE", predicates); 1802promise.then((rows) => { 1803 console.info(`Delete rows: ${rows}`); 1804}).catch((err) => { 1805 console.error(`Delete failed, code is ${err.code},message is ${err.message}`); 1806}) 1807``` 1808 1809### query 1810 1811query(predicates: RdbPredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void 1812 1813根据指定条件查询数据库中的数据,使用callback异步回调。 1814 1815**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1816 1817**参数:** 1818 1819| 参数名 | 类型 | 必填 | 说明 | 1820| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1821| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 1822| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 1823| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 1824 1825**错误码:** 1826 1827以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1828 1829| **错误码ID** | **错误信息** | 1830| ------------ | -------------------------------------------- | 1831| 14800000 | The inner error is occurred. | 1832 1833**示例:** 1834 1835```js 1836let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1837predicates.equalTo("NAME", "Rose"); 1838store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) { 1839 if (err) { 1840 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 1841 return; 1842 } 1843 console.info(`ResultSet column names: ${resultSet.columnNames}`); 1844 console.info(`ResultSet column count: ${resultSet.columnCount}`); 1845}) 1846``` 1847 1848### query 1849 1850query(predicates: RdbPredicates, columns?: Array<string>):Promise<ResultSet> 1851 1852根据指定条件查询数据库中的数据,使用Promise异步回调。 1853 1854**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1855 1856**参数:** 1857 1858| 参数名 | 类型 | 必填 | 说明 | 1859| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 1860| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象指定的查询条件。 | 1861| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 1862 1863**返回值**: 1864 1865| 类型 | 说明 | 1866| ------------------------------------------------------- | -------------------------------------------------- | 1867| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 1868 1869**错误码:** 1870 1871以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1872 1873| **错误码ID** | **错误信息** | 1874| ------------ | -------------------------------------------- | 1875| 14800000 | The inner error is occurred. | 1876 1877**示例:** 1878 1879 ```js 1880let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 1881predicates.equalTo("NAME", "Rose"); 1882let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 1883promise.then((resultSet) => { 1884 console.info(`ResultSet column names: ${resultSet.columnNames}`); 1885 console.info(`ResultSet column count: ${resultSet.columnCount}`); 1886}).catch((err) => { 1887 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 1888}) 1889 ``` 1890 1891### query 1892 1893query(table: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: AsyncCallback<ResultSet>):void 1894 1895根据指定条件查询数据库中的数据,使用callback异步回调。 1896 1897**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1898 1899**模型约束:** 此接口仅可在Stage模型下使用。 1900 1901**系统接口:** 此接口为系统接口。 1902 1903**参数:** 1904 1905| 参数名 | 类型 | 必填 | 说明 | 1906| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- | 1907| table | string | 是 | 指定的目标表名。 | 1908| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的查询条件。 | 1909| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 1910| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 1911 1912**错误码:** 1913 1914以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1915 1916| **错误码ID** | **错误信息** | 1917| ------------ | -------------------------------------------- | 1918| 14800000 | The inner error is occurred. | 1919 1920**示例:** 1921 1922```js 1923import dataSharePredicates from '@ohos.data.dataSharePredicates' 1924let predicates = new dataSharePredicates.DataSharePredicates(); 1925predicates.equalTo("NAME", "Rose"); 1926store.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) { 1927 if (err) { 1928 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 1929 return; 1930 } 1931 console.info(`ResultSet column names: ${resultSet.columnNames}`); 1932 console.info(`ResultSet column count: ${resultSet.columnCount}`); 1933}) 1934``` 1935 1936### query 1937 1938query(table: string, predicates: dataSharePredicates.DataSharePredicates, columns?: Array<string>):Promise<ResultSet> 1939 1940根据指定条件查询数据库中的数据,使用Promise异步回调。 1941 1942**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1943 1944**模型约束:** 此接口仅可在Stage模型下使用。 1945 1946**系统接口:** 此接口为系统接口。 1947 1948**参数:** 1949 1950| 参数名 | 类型 | 必填 | 说明 | 1951| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------ | 1952| table | string | 是 | 指定的目标表名。 | 1953| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是 | DataSharePredicates的实例对象指定的查询条件。 | 1954| columns | Array<string> | 否 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 1955 1956**返回值**: 1957 1958| 类型 | 说明 | 1959| ------------------------------------------------------- | -------------------------------------------------- | 1960| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 1961 1962**错误码:** 1963 1964以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 1965 1966| **错误码ID** | **错误信息** | 1967| ------------ | -------------------------------------------- | 1968| 14800000 | The inner error is occurred. | 1969 1970**示例:** 1971 1972```js 1973import dataSharePredicates from '@ohos.data.dataSharePredicates' 1974let predicates = new dataSharePredicates.DataSharePredicates(); 1975predicates.equalTo("NAME", "Rose"); 1976let promise = store.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 1977promise.then((resultSet) => { 1978 console.info(`ResultSet column names: ${resultSet.columnNames}`); 1979 console.info(`ResultSet column count: ${resultSet.columnCount}`); 1980}).catch((err) => { 1981 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 1982}) 1983``` 1984 1985### remoteQuery 1986 1987remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array<string> , callback: AsyncCallback<ResultSet>): void 1988 1989根据指定条件查询远程设备数据库中的数据。使用callback异步回调。 1990 1991> **说明:** 1992> 1993> 其中device通过调用[deviceManager.getTrustedDeviceListSync](js-apis-device-manager.md#gettrusteddevicelistsync)方法得到。deviceManager模块的接口均为系统接口,仅系统应用可用。 1994 1995**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 1996 1997**参数:** 1998 1999| 参数名 | 类型 | 必填 | 说明 | 2000| ---------- | -------------------------------------------- | ---- | --------------------------------------------------------- | 2001| device | string | 是 | 指定的远程设备ID。 | 2002| table | string | 是 | 指定的目标表名。 | 2003| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象,指定查询的条件。 | 2004| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 2005| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 2006 2007**错误码:** 2008 2009以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2010 2011| **错误码ID** | **错误信息** | 2012| ------------ | -------------------------------------------- | 2013| 14800000 | The inner error is occurred. | 2014 2015**示例:** 2016 2017```js 2018import deviceManager from '@ohos.distributedHardware.deviceManager'; 2019let dmInstance = null; 2020let deviceId = null; 2021 2022deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { 2023 if (err) { 2024 console.log("create device manager failed, err=" + err); 2025 return; 2026 } 2027 dmInstance = manager; 2028 let devices = dmInstance.getTrustedDeviceListSync(); 2029 deviceId = devices[0].deviceId; 2030}) 2031 2032let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 2033predicates.greaterThan("id", 0); 2034store.remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], 2035 function(err, resultSet) { 2036 if (err) { 2037 console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`); 2038 return; 2039 } 2040 console.info(`ResultSet column names: ${resultSet.columnNames}`); 2041 console.info(`ResultSet column count: ${resultSet.columnCount}`); 2042 } 2043) 2044``` 2045 2046### remoteQuery 2047 2048remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array<string>): Promise<ResultSet> 2049 2050根据指定条件查询远程设备数据库中的数据。使用Promise异步回调。 2051 2052> **说明:** 2053> 2054> 其中device通过调用[deviceManager.getTrustedDeviceListSync](js-apis-device-manager.md#gettrusteddevicelistsync)方法得到。deviceManager模块的接口均为系统接口,仅系统应用可用。 2055 2056**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2057 2058**参数:** 2059 2060| 参数名 | 类型 | 必填 | 说明 | 2061| ---------- | ------------------------------------ | ---- | ------------------------------------------------ | 2062| device | string | 是 | 指定的远程设备ID。 | 2063| table | string | 是 | 指定的目标表名。 | 2064| predicates | [RdbPredicates](#rdbpredicates) | 是 | RdbPredicates的实例对象,指定查询的条件。 | 2065| columns | Array<string> | 是 | 表示要查询的列。如果值为空,则查询应用于所有列。 | 2066 2067**返回值**: 2068 2069| 类型 | 说明 | 2070| ------------------------------------------------------------ | -------------------------------------------------- | 2071| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 2072 2073**错误码:** 2074 2075以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2076 2077| **错误码ID** | **错误信息** | 2078| ------------ | -------------------------------------------- | 2079| 14800000 | The inner error is occurred. | 2080 2081**示例:** 2082 2083```js 2084import deviceManager from '@ohos.distributedHardware.deviceManager'; 2085let dmInstance = null; 2086let deviceId = null; 2087 2088deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { 2089 if (err) { 2090 console.log("create device manager failed, err=" + err); 2091 return; 2092 } 2093 dmInstance = manager; 2094 let devices = dmInstance.getTrustedDeviceListSync(); 2095 deviceId = devices[0].deviceId; 2096}) 2097 2098let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 2099predicates.greaterThan("id", 0); 2100let promise = store.remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 2101promise.then((resultSet) => { 2102 console.info(`ResultSet column names: ${resultSet.columnNames}`); 2103 console.info(`ResultSet column count: ${resultSet.columnCount}`); 2104}).catch((err) => { 2105 console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`); 2106}) 2107``` 2108 2109### querySql 2110 2111querySql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<ResultSet>):void 2112 2113根据指定SQL语句查询数据库中的数据,使用callback异步回调。 2114 2115**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2116 2117**参数:** 2118 2119| 参数名 | 类型 | 必填 | 说明 | 2120| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 2121| sql | string | 是 | 指定要执行的SQL语句。 | 2122| bindArgs | Array<[ValueType](#valuetype)> | 是 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数需为空数组。 | 2123| callback | AsyncCallback<[ResultSet](#resultset)> | 是 | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 | 2124 2125**错误码:** 2126 2127以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2128 2129| **错误码ID** | **错误信息** | 2130| ------------ | -------------------------------------------- | 2131| 14800000 | The inner error is occurred. | 2132 2133**示例:** 2134 2135```js 2136store.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], function (err, resultSet) { 2137 if (err) { 2138 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 2139 return; 2140 } 2141 console.info(`ResultSet column names: ${resultSet.columnNames}`); 2142 console.info(`ResultSet column count: ${resultSet.columnCount}`); 2143}) 2144``` 2145 2146### querySql 2147 2148querySql(sql: string, bindArgs?: Array<ValueType>):Promise<ResultSet> 2149 2150根据指定SQL语句查询数据库中的数据,使用Promise异步回调。 2151 2152**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2153 2154**参数:** 2155 2156| 参数名 | 类型 | 必填 | 说明 | 2157| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 2158| sql | string | 是 | 指定要执行的SQL语句。 | 2159| bindArgs | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 2160 2161**返回值**: 2162 2163| 类型 | 说明 | 2164| ------------------------------------------------------- | -------------------------------------------------- | 2165| Promise<[ResultSet](#resultset)> | Promise对象。如果操作成功,则返回ResultSet对象。 | 2166 2167**错误码:** 2168 2169以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2170 2171| **错误码ID** | **错误信息** | 2172| ------------ | -------------------------------------------- | 2173| 14800000 | The inner error is occurred. | 2174 2175**示例:** 2176 2177```js 2178let promise = store.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'"); 2179promise.then((resultSet) => { 2180 console.info(`ResultSet column names: ${resultSet.columnNames}`); 2181 console.info(`ResultSet column count: ${resultSet.columnCount}`); 2182}).catch((err) => { 2183 console.error(`Query failed, code is ${err.code},message is ${err.message}`); 2184}) 2185``` 2186 2187### executeSql 2188 2189executeSql(sql: string, bindArgs: Array<ValueType>, callback: AsyncCallback<void>):void 2190 2191执行包含指定参数但不返回值的SQL语句,使用callback异步回调。 2192 2193**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2194 2195**参数:** 2196 2197| 参数名 | 类型 | 必填 | 说明 | 2198| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 2199| sql | string | 是 | 指定要执行的SQL语句。 | 2200| bindArgs | Array<[ValueType](#valuetype)> | 是 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数需为空数组。 | 2201| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 2202 2203**错误码:** 2204 2205以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2206 2207| **错误码ID** | **错误信息** | 2208| ------------ | -------------------------------------------- | 2209| 14800000 | The inner error is occurred. | 2210 2211**示例:** 2212 2213```js 2214const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = ?" 2215store.executeSql(SQL_DELETE_TABLE, ['zhangsan'], function(err) { 2216 if (err) { 2217 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 2218 return; 2219 } 2220 console.info(`Delete table done.`); 2221}) 2222``` 2223 2224### executeSql 2225 2226executeSql(sql: string, bindArgs?: Array<ValueType>):Promise<void> 2227 2228执行包含指定参数但不返回值的SQL语句,使用Promise异步回调。 2229 2230**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2231 2232**参数:** 2233 2234| 参数名 | 类型 | 必填 | 说明 | 2235| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 2236| sql | string | 是 | 指定要执行的SQL语句。 | 2237| bindArgs | Array<[ValueType](#valuetype)> | 否 | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 | 2238 2239**返回值**: 2240 2241| 类型 | 说明 | 2242| ------------------- | ------------------------- | 2243| Promise<void> | 无返回结果的Promise对象。 | 2244 2245**错误码:** 2246 2247以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2248 2249| **错误码ID** | **错误信息** | 2250| ------------ | -------------------------------------------- | 2251| 14800000 | The inner error is occurred. | 2252 2253**示例:** 2254 2255```js 2256const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = 'zhangsan'" 2257let promise = store.executeSql(SQL_DELETE_TABLE); 2258promise.then(() => { 2259 console.info(`Delete table done.`); 2260}).catch((err) => { 2261 console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`); 2262}) 2263``` 2264 2265### beginTransaction 2266 2267beginTransaction():void 2268 2269在开始执行SQL语句之前,开始事务。 2270 2271**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2272 2273**错误码:** 2274 2275以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2276 2277| **错误码ID** | **错误信息** | 2278| ------------ | -------------------------------------------- | 2279| 14800000 | The inner error is occurred. | 2280 2281**示例:** 2282 2283```js 2284import featureAbility from '@ohos.ability.featureAbility' 2285let context = featureAbility.getContext(); 2286const STORE_CONFIG = { 2287 name: "RdbTest.db", 2288 securityLevel: relationalStore.SecurityLevel.S1 2289}; 2290relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) { 2291 if (err) { 2292 console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`); 2293 return; 2294 } 2295 store.beginTransaction(); 2296 const valueBucket = { 2297 "name": "lisi", 2298 "age": 18, 2299 "salary": 100.5, 2300 "blobType": new Uint8Array([1, 2, 3]), 2301 }; 2302 await store.insert("test", valueBucket); 2303 store.commit(); 2304}) 2305``` 2306 2307### commit 2308 2309commit():void 2310 2311提交已执行的SQL语句。 2312 2313**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2314 2315**示例:** 2316 2317```js 2318import featureAbility from '@ohos.ability.featureAbility' 2319let context = featureAbility.getContext(); 2320const STORE_CONFIG = { 2321 name: "RdbTest.db", 2322 securityLevel: relationalStore.SecurityLevel.S1 2323}; 2324relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) { 2325 if (err) { 2326 console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`); 2327 return; 2328 } 2329 store.beginTransaction(); 2330 const valueBucket = { 2331 "name": "lisi", 2332 "age": 18, 2333 "salary": 100.5, 2334 "blobType": new Uint8Array([1, 2, 3]), 2335 }; 2336 await store.insert("test", valueBucket); 2337 store.commit(); 2338}) 2339``` 2340 2341### rollBack 2342 2343rollBack():void 2344 2345回滚已经执行的SQL语句。 2346 2347**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2348 2349**示例:** 2350 2351```js 2352import featureAbility from '@ohos.ability.featureAbility' 2353let context = featureAbility.getContext(); 2354const STORE_CONFIG = { 2355 name: "RdbTest.db", 2356 securityLevel: relationalStore.SecurityLevel.S1 2357}; 2358relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) { 2359 if (err) { 2360 console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`); 2361 return; 2362 } 2363 try { 2364 store.beginTransaction() 2365 const valueBucket = { 2366 "id": 1, 2367 "name": "lisi", 2368 "age": 18, 2369 "salary": 100.5, 2370 "blobType": new Uint8Array([1, 2, 3]), 2371 }; 2372 await store.insert("test", valueBucket); 2373 store.commit(); 2374 } catch (err) { 2375 console.error(`Transaction failed, code is ${err.code},message is ${err.message}`); 2376 store.rollBack(); 2377 } 2378}) 2379``` 2380 2381### backup 2382 2383backup(destName:string, callback: AsyncCallback<void>):void 2384 2385以指定名称备份数据库,使用callback异步回调。 2386 2387**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2388 2389**参数:** 2390 2391| 参数名 | 类型 | 必填 | 说明 | 2392| -------- | ------------------------- | ---- | ------------------------ | 2393| destName | string | 是 | 指定数据库的备份文件名。 | 2394| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 2395 2396**错误码:** 2397 2398以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2399 2400| **错误码ID** | **错误信息** | 2401| ------------ | -------------------------------------------- | 2402| 14800000 | The inner error is occurred. | 2403 2404**示例:** 2405 2406```js 2407store.backup("dbBackup.db", function(err) { 2408 if (err) { 2409 console.error(`Backup failed, code is ${err.code},message is ${err.message}`); 2410 return; 2411 } 2412 console.info(`Backup success.`); 2413}) 2414``` 2415 2416### backup 2417 2418backup(destName:string): Promise<void> 2419 2420以指定名称备份数据库,使用Promise异步回调。 2421 2422**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2423 2424**参数:** 2425 2426| 参数名 | 类型 | 必填 | 说明 | 2427| -------- | ------ | ---- | ------------------------ | 2428| destName | string | 是 | 指定数据库的备份文件名。 | 2429 2430**错误码:** 2431 2432以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2433 2434| **错误码ID** | **错误信息** | 2435| ------------ | -------------------------------------------- | 2436| 14800000 | The inner error is occurred. | 2437 2438**返回值**: 2439 2440| 类型 | 说明 | 2441| ------------------- | ------------------------- | 2442| Promise<void> | 无返回结果的Promise对象。 | 2443 2444**示例:** 2445 2446```js 2447let promiseBackup = store.backup("dbBackup.db"); 2448promiseBackup.then(()=>{ 2449 console.info(`Backup success.`); 2450}).catch((err)=>{ 2451 console.error(`Backup failed, code is ${err.code},message is ${err.message}`); 2452}) 2453``` 2454 2455### restore 2456 2457restore(srcName:string, callback: AsyncCallback<void>):void 2458 2459从指定的数据库备份文件恢复数据库,使用callback异步回调。 2460 2461**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2462 2463**参数:** 2464 2465| 参数名 | 类型 | 必填 | 说明 | 2466| -------- | ------------------------- | ---- | ------------------------ | 2467| srcName | string | 是 | 指定数据库的备份文件名。 | 2468| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 2469 2470**错误码:** 2471 2472以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2473 2474| **错误码ID** | **错误信息** | 2475| ------------ | -------------------------------------------- | 2476| 14800000 | The inner error is occurred. | 2477 2478**示例:** 2479 2480```js 2481store.restore("dbBackup.db", function(err) { 2482 if (err) { 2483 console.error(`Restore failed, code is ${err.code},message is ${err.message}`); 2484 return; 2485 } 2486 console.info(`Restore success.`); 2487}) 2488``` 2489 2490### restore 2491 2492restore(srcName:string): Promise<void> 2493 2494从指定的数据库备份文件恢复数据库,使用Promise异步回调。 2495 2496**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2497 2498**参数:** 2499 2500| 参数名 | 类型 | 必填 | 说明 | 2501| ------- | ------ | ---- | ------------------------ | 2502| srcName | string | 是 | 指定数据库的备份文件名。 | 2503 2504**返回值**: 2505 2506| 类型 | 说明 | 2507| ------------------- | ------------------------- | 2508| Promise<void> | 无返回结果的Promise对象。 | 2509 2510**错误码:** 2511 2512以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2513 2514| **错误码ID** | **错误信息** | 2515| ------------ | -------------------------------------------- | 2516| 14800000 | The inner error is occurred. | 2517 2518**示例:** 2519 2520```js 2521let promiseRestore = store.restore("dbBackup.db"); 2522promiseRestore.then(()=>{ 2523 console.info(`Restore success.`); 2524}).catch((err)=>{ 2525 console.error(`Restore failed, code is ${err.code},message is ${err.message}`); 2526}) 2527``` 2528 2529### setDistributedTables 2530 2531setDistributedTables(tables: Array<string>, callback: AsyncCallback<void>): void 2532 2533设置分布式列表,使用callback异步回调。 2534 2535**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 2536 2537**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2538 2539**参数:** 2540 2541| 参数名 | 类型 | 必填 | 说明 | 2542| -------- | ------------------------- | ---- | ---------------------- | 2543| tables | Array<string> | 是 | 要设置的分布式列表表名 | 2544| callback | AsyncCallback<void> | 是 | 指定callback回调函数。 | 2545 2546**错误码:** 2547 2548以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2549 2550| **错误码ID** | **错误信息** | 2551| ------------ | -------------------------------------------- | 2552| 14800000 | The inner error is occurred. | 2553 2554**示例:** 2555 2556```js 2557store.setDistributedTables(["EMPLOYEE"], function (err) { 2558 if (err) { 2559 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 2560 return; 2561 } 2562 console.info(`SetDistributedTables successfully.`); 2563}) 2564``` 2565 2566### setDistributedTables 2567 2568 setDistributedTables(tables: Array<string>): Promise<void> 2569 2570设置分布式列表,使用Promise异步回调。 2571 2572**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 2573 2574**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2575 2576**参数:** 2577 2578| 参数名 | 类型 | 必填 | 说明 | 2579| ------ | ------------------- | ---- | ------------------------ | 2580| tables | Array<string> | 是 | 要设置的分布式列表表名。 | 2581 2582**返回值**: 2583 2584| 类型 | 说明 | 2585| ------------------- | ------------------------- | 2586| Promise<void> | 无返回结果的Promise对象。 | 2587 2588**错误码:** 2589 2590以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2591 2592| **错误码ID** | **错误信息** | 2593| ------------ | -------------------------------------------- | 2594| 14800000 | The inner error is occurred. | 2595 2596**示例:** 2597 2598```js 2599let promise = store.setDistributedTables(["EMPLOYEE"]); 2600promise.then(() => { 2601 console.info(`SetDistributedTables successfully.`); 2602}).catch((err) => { 2603 console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`); 2604}) 2605``` 2606 2607### obtainDistributedTableName 2608 2609obtainDistributedTableName(device: string, table: string, callback: AsyncCallback<string>): void 2610 2611根据远程设备的本地表名获取指定远程设备的分布式表名。在查询远程设备数据库时,需要使用分布式表名, 使用callback异步回调。 2612 2613> **说明:** 2614> 2615> 其中device通过调用[deviceManager.getTrustedDeviceListSync](js-apis-device-manager.md#gettrusteddevicelistsync)方法得到。deviceManager模块的接口均为系统接口,仅系统应用可用。 2616 2617**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 2618 2619**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2620 2621**参数:** 2622 2623| 参数名 | 类型 | 必填 | 说明 | 2624| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 2625| device | string | 是 | 远程设备ID 。 | 2626| table | string | 是 | 远程设备的本地表名。 | 2627| callback | AsyncCallback<string> | 是 | 指定的callback回调函数。如果操作成功,返回远程设备的分布式表名。 | 2628 2629**错误码:** 2630 2631以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2632 2633| **错误码ID** | **错误信息** | 2634| ------------ | -------------------------------------------- | 2635| 14800000 | The inner error is occurred. | 2636 2637**示例:** 2638 2639```js 2640import deviceManager from '@ohos.distributedHardware.deviceManager'; 2641let dmInstance = null; 2642let deviceId = null; 2643 2644deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { 2645 if (err) { 2646 console.log("create device manager failed, err=" + err); 2647 return; 2648 } 2649 dmInstance = manager; 2650 let devices = dmInstance.getTrustedDeviceListSync(); 2651 deviceId = devices[0].deviceId; 2652}) 2653 2654store.obtainDistributedTableName(deviceId, "EMPLOYEE", function (err, tableName) { 2655 if (err) { 2656 console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`); 2657 return; 2658 } 2659 console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`); 2660}) 2661``` 2662 2663### obtainDistributedTableName 2664 2665 obtainDistributedTableName(device: string, table: string): Promise<string> 2666 2667根据远程设备的本地表名获取指定远程设备的分布式表名。在查询远程设备数据库时,需要使用分布式表名,使用Promise异步回调。 2668 2669> **说明:** 2670> 2671> 其中device通过调用[deviceManager.getTrustedDeviceListSync](js-apis-device-manager.md#gettrusteddevicelistsync)方法得到。deviceManager模块的接口均为系统接口,仅系统应用可用。 2672 2673**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 2674 2675**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2676 2677**参数:** 2678 2679| 参数名 | 类型 | 必填 | 说明 | 2680| ------ | ------ | ---- | -------------------- | 2681| device | string | 是 | 远程设备ID。 | 2682| table | string | 是 | 远程设备的本地表名。 | 2683 2684**返回值**: 2685 2686| 类型 | 说明 | 2687| --------------------- | ----------------------------------------------------- | 2688| Promise<string> | Promise对象。如果操作成功,返回远程设备的分布式表名。 | 2689 2690**错误码:** 2691 2692以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2693 2694| **错误码ID** | **错误信息** | 2695| ------------ | -------------------------------------------- | 2696| 14800000 | The inner error is occurred. | 2697 2698**示例:** 2699 2700```js 2701import deviceManager from '@ohos.distributedHardware.deviceManager'; 2702let dmInstance = null; 2703let deviceId = null; 2704 2705deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { 2706 if (err) { 2707 console.log("create device manager failed, err=" + err); 2708 return; 2709 } 2710 dmInstance = manager; 2711 let devices = dmInstance.getTrustedDeviceListSync(); 2712 deviceId = devices[0].deviceId; 2713}) 2714 2715let promise = store.obtainDistributedTableName(deviceId, "EMPLOYEE"); 2716promise.then((tableName) => { 2717 console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`); 2718}).catch((err) => { 2719 console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`); 2720}) 2721``` 2722 2723### sync 2724 2725sync(mode: SyncMode, predicates: RdbPredicates, callback: AsyncCallback<Array<[string, number]>>): void 2726 2727在设备之间同步数据, 使用callback异步回调。 2728 2729**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 2730 2731**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2732 2733**参数:** 2734 2735| 参数名 | 类型 | 必填 | 说明 | 2736| ---------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ | 2737| mode | [SyncMode](#syncmode) | 是 | 指同步模式。该值可以是推、拉。 | 2738| predicates | [RdbPredicates](#rdbpredicates) | 是 | 约束同步数据和设备。 | 2739| callback | AsyncCallback<Array<[string, number]>> | 是 | 指定的callback回调函数,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 | 2740 2741**错误码:** 2742 2743以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2744 2745| **错误码ID** | **错误信息** | 2746| ------------ | -------------------------------------------- | 2747| 14800000 | The inner error is occurred. | 2748 2749**示例:** 2750 2751```js 2752import deviceManager from '@ohos.distributedHardware.deviceManager'; 2753let dmInstance = null; 2754let deviceIds = []; 2755 2756deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { 2757 if (err) { 2758 console.log("create device manager failed, err=" + err); 2759 return; 2760 } 2761 dmInstance = manager; 2762 let devices = dmInstance.getTrustedDeviceListSync(); 2763 for (var i = 0; i < devices.length; i++) { 2764 deviceIds[i] = devices[i].deviceId; 2765 } 2766}) 2767 2768let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 2769predicates.inDevices(deviceIds); 2770store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates, function (err, result) { 2771 if (err) { 2772 console.error(`Sync failed, code is ${err.code},message is ${err.message}`); 2773 return; 2774 } 2775 console.info(`Sync done.`); 2776 for (let i = 0; i < result.length; i++) { 2777 console.info(`device= ${result[i][0]}, status= ${result[i][1]}`); 2778 } 2779}) 2780``` 2781 2782### sync 2783 2784 sync(mode: SyncMode, predicates: RdbPredicates): Promise<Array<[string, number]>> 2785 2786在设备之间同步数据,使用Promise异步回调。 2787 2788**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC 2789 2790**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2791 2792**参数:** 2793 2794| 参数名 | 类型 | 必填 | 说明 | 2795| ---------- | ------------------------------------ | ---- | ------------------------------ | 2796| mode | [SyncMode](#syncmode) | 是 | 指同步模式。该值可以是推、拉。 | 2797| predicates | [RdbPredicates](#rdbpredicates) | 是 | 约束同步数据和设备。 | 2798 2799**返回值**: 2800 2801| 类型 | 说明 | 2802| -------------------------------------------- | ------------------------------------------------------------ | 2803| Promise<Array<[string, number]>> | Promise对象,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 | 2804 2805**错误码:** 2806 2807以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2808 2809| **错误码ID** | **错误信息** | 2810| ------------ | -------------------------------------------- | 2811| 14800000 | The inner error is occurred. | 2812 2813**示例:** 2814 2815```js 2816import deviceManager from '@ohos.distributedHardware.deviceManager'; 2817let dmInstance = null; 2818let deviceIds = []; 2819 2820deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => { 2821 if (err) { 2822 console.log("create device manager failed, err=" + err); 2823 return; 2824 } 2825 dmInstance = manager; 2826 let devices = dmInstance.getTrustedDeviceListSync(); 2827 for (var i = 0; i < devices.length; i++) { 2828 deviceIds[i] = devices[i].deviceId; 2829 } 2830}) 2831 2832let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); 2833predicates.inDevices(deviceIds); 2834let promise = store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates); 2835promise.then((result) =>{ 2836 console.info(`Sync done.`); 2837 for (let i = 0; i < result.length; i++) { 2838 console.info(`device= ${result[i][0]}, status= ${result[i][1]}`); 2839 } 2840}).catch((err) => { 2841 console.error(`Sync failed, code is ${err.code},message is ${err.message}`); 2842}) 2843``` 2844 2845### on('dataChange') 2846 2847on(event: 'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void 2848 2849注册数据库的观察者。当分布式数据库中的数据发生更改时,将调用回调。 2850 2851**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2852 2853**参数:** 2854 2855| 参数名 | 类型 | 必填 | 说明 | 2856| -------- | ----------------------------------- | ---- | ------------------------------------------- | 2857| event | string | 是 | 取值为'dataChange',表示数据更改。 | 2858| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 2859| observer | Callback<Array<string>> | 是 | 指分布式数据库中数据更改事件的观察者。Array<string>为数据库中的数据发生改变的对端设备ID。 | 2860 2861**示例:** 2862 2863```js 2864function storeObserver(devices) { 2865 for (let i = 0; i < devices.length; i++) { 2866 console.info(`device= ${devices[i]} data changed`); 2867 } 2868} 2869try { 2870 store.on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 2871} catch (err) { 2872 console.error(`Register observer failed, code is ${err.code},message is ${err.message}`); 2873} 2874``` 2875 2876### off('dataChange') 2877 2878off(event:'dataChange', type: SubscribeType, observer: Callback<Array<string>>): void 2879 2880从数据库中删除指定类型的指定观察者, 使用callback异步回调。 2881 2882**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2883 2884**参数:** 2885 2886| 参数名 | 类型 | 必填 | 说明 | 2887| -------- | ---------------------------------- | ---- | ------------------------------------------ | 2888| event | string | 是 | 取值为'dataChange',表示数据更改。 | 2889| type | [SubscribeType](#subscribetype) | 是 | 订阅类型。 | 2890| observer | Callback<Array<string>> | 是 | 指已注册的数据更改观察者。Array<string>为数据库中的数据发生改变的对端设备ID。 | 2891 2892**示例:** 2893 2894```js 2895function storeObserver(devices) { 2896 for (let i = 0; i < devices.length; i++) { 2897 console.info(`device= ${devices[i]} data changed`); 2898 } 2899} 2900try { 2901 store.off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver); 2902} catch (err) { 2903 console.error(`Unregister observer failed, code is ${err.code},message is ${err.message}`); 2904} 2905``` 2906 2907## ResultSet 2908 2909提供通过查询数据库生成的数据库结果集的访问方法。结果集是指用户调用关系型数据库查询接口之后返回的结果集合,提供了多种灵活的数据访问方式,以便用户获取各项数据。 2910 2911### 使用说明 2912 2913首先需要获取resultSet对象。 2914 2915```js 2916let resultSet = null; 2917let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 2918predicates.equalTo("AGE", 18); 2919let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 2920promise.then((result) => { 2921 resultSet = result; 2922 console.info(`resultSet columnNames: ${resultSet.columnNames}`); 2923 console.info(`resultSet columnCount: ${resultSet.columnCount}`); 2924}); 2925``` 2926 2927### 属性 2928 2929**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2930 2931| 名称 | 类型 | 必填 | 说明 | 2932| ------------ | ------------------- | ---- | -------------------------------- | 2933| columnNames | Array<string> | 是 | 获取结果集中所有列的名称。 | 2934| columnCount | number | 是 | 获取结果集中的列数。 | 2935| rowCount | number | 是 | 获取结果集中的行数。 | 2936| rowIndex | number | 是 | 获取结果集当前行的索引。 | 2937| isAtFirstRow | boolean | 是 | 检查结果集是否位于第一行。 | 2938| isAtLastRow | boolean | 是 | 检查结果集是否位于最后一行。 | 2939| isEnded | boolean | 是 | 检查结果集是否位于最后一行之后。 | 2940| isStarted | boolean | 是 | 检查指针是否移动过。 | 2941| isClosed | boolean | 是 | 检查当前结果集是否关闭。 | 2942 2943### getColumnIndex 2944 2945getColumnIndex(columnName: string): number 2946 2947根据指定的列名获取列索引。 2948 2949**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2950 2951**参数:** 2952 2953| 参数名 | 类型 | 必填 | 说明 | 2954| ---------- | ------ | ---- | -------------------------- | 2955| columnName | string | 是 | 表示结果集中指定列的名称。 | 2956 2957**返回值:** 2958 2959| 类型 | 说明 | 2960| ------ | ------------------ | 2961| number | 返回指定列的索引。 | 2962 2963**错误码:** 2964 2965以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 2966 2967| **错误码ID** | **错误信息** | 2968| ------------ | ------------------------------------------------------------ | 2969| 14800013 | The column value is null or the column type is incompatible. | 2970 2971**示例:** 2972 2973 ```js 2974resultSet.goToFirstRow(); 2975const id = resultSet.getLong(resultSet.getColumnIndex("ID")); 2976const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 2977const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 2978const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 2979 ``` 2980 2981### getColumnName 2982 2983getColumnName(columnIndex: number): string 2984 2985根据指定的列索引获取列名。 2986 2987**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 2988 2989**参数:** 2990 2991| 参数名 | 类型 | 必填 | 说明 | 2992| ----------- | ------ | ---- | -------------------------- | 2993| columnIndex | number | 是 | 表示结果集中指定列的索引。 | 2994 2995**返回值:** 2996 2997| 类型 | 说明 | 2998| ------ | ------------------ | 2999| string | 返回指定列的名称。 | 3000 3001**错误码:** 3002 3003以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3004 3005| **错误码ID** | **错误信息** | 3006| ------------ | ------------------------------------------------------------ | 3007| 14800013 | The column value is null or the column type is incompatible. | 3008 3009**示例:** 3010 3011 ```js 3012const id = resultSet.getColumnName(0); 3013const name = resultSet.getColumnName(1); 3014const age = resultSet.getColumnName(2); 3015 ``` 3016 3017### goTo 3018 3019goTo(offset:number): boolean 3020 3021向前或向后转至结果集的指定行,相对于其当前位置偏移。 3022 3023**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3024 3025**参数:** 3026 3027| 参数名 | 类型 | 必填 | 说明 | 3028| ------ | ------ | ---- | ---------------------------- | 3029| offset | number | 是 | 表示相对于当前位置的偏移量。 | 3030 3031**返回值:** 3032 3033| 类型 | 说明 | 3034| ------- | --------------------------------------------- | 3035| boolean | 如果成功移动结果集,则为true;否则返回false。 | 3036 3037**错误码:** 3038 3039以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3040 3041| **错误码ID** | **错误信息** | 3042| ------------ | ------------------------------------------------------------ | 3043| 14800012 | The result set is empty or the specified location is invalid. | 3044 3045**示例:** 3046 3047 ```js 3048let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3049let promise= store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 3050promise.then((resultSet) => { 3051 resultSet.goTo(1); 3052 resultSet.close(); 3053}).catch((err) => { 3054 console.error(`query failed, code is ${err.code},message is ${err.message}`); 3055}); 3056 ``` 3057 3058### goToRow 3059 3060goToRow(position: number): boolean 3061 3062转到结果集的指定行。 3063 3064**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3065 3066**参数:** 3067 3068| 参数名 | 类型 | 必填 | 说明 | 3069| -------- | ------ | ---- | ------------------------ | 3070| position | number | 是 | 表示要移动到的指定位置。 | 3071 3072**返回值:** 3073 3074| 类型 | 说明 | 3075| ------- | --------------------------------------------- | 3076| boolean | 如果成功移动结果集,则为true;否则返回false。 | 3077 3078**错误码:** 3079 3080以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3081 3082| **错误码ID** | **错误信息** | 3083| ------------ | ------------------------------------------------------------ | 3084| 14800012 | The result set is empty or the specified location is invalid. | 3085 3086**示例:** 3087 3088 ```js 3089let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3090let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 3091promise.then((resultSet) => { 3092 resultSet.goToRow(5); 3093 resultSet.close(); 3094}).catch((err) => { 3095 console.error(`query failed, code is ${err.code},message is ${err.message}`); 3096}); 3097 ``` 3098 3099### goToFirstRow 3100 3101goToFirstRow(): boolean 3102 3103 3104转到结果集的第一行。 3105 3106**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3107 3108**返回值:** 3109 3110| 类型 | 说明 | 3111| ------- | --------------------------------------------- | 3112| boolean | 如果成功移动结果集,则为true;否则返回false。 | 3113 3114**错误码:** 3115 3116以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3117 3118| **错误码ID** | **错误信息** | 3119| ------------ | ------------------------------------------------------------ | 3120| 14800012 | The result set is empty or the specified location is invalid. | 3121 3122**示例:** 3123 3124 ```js 3125let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3126let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 3127promise.then((resultSet) => { 3128 resultSet.goToFirstRow(); 3129 resultSet.close(); 3130}).catch((err) => { 3131 console.error(`query failed, code is ${err.code},message is ${err.message}`); 3132}); 3133 ``` 3134 3135### goToLastRow 3136 3137goToLastRow(): boolean 3138 3139转到结果集的最后一行。 3140 3141**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3142 3143**返回值:** 3144 3145| 类型 | 说明 | 3146| ------- | --------------------------------------------- | 3147| boolean | 如果成功移动结果集,则为true;否则返回false。 | 3148 3149**错误码:** 3150 3151以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3152 3153| **错误码ID** | **错误信息** | 3154| ------------ | ------------------------------------------------------------ | 3155| 14800012 | The result set is empty or the specified location is invalid. | 3156 3157**示例:** 3158 3159 ```js 3160let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3161let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 3162promise.then((resultSet) => { 3163 resultSet.goToLastRow(); 3164 resultSet.close(); 3165}).catch((err) => { 3166 console.error(`query failed, code is ${err.code},message is ${err.message}`); 3167}); 3168 ``` 3169 3170### goToNextRow 3171 3172goToNextRow(): boolean 3173 3174转到结果集的下一行。 3175 3176**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3177 3178**返回值:** 3179 3180| 类型 | 说明 | 3181| ------- | --------------------------------------------- | 3182| boolean | 如果成功移动结果集,则为true;否则返回false。 | 3183 3184**错误码:** 3185 3186以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3187 3188| **错误码ID** | **错误信息** | 3189| ------------ | ------------------------------------------------------------ | 3190| 14800012 | The result set is empty or the specified location is invalid. | 3191 3192**示例:** 3193 3194 ```js 3195let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3196let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 3197promise.then((resultSet) => { 3198 resultSet.goToNextRow(); 3199 resultSet.close(); 3200}).catch((err) => { 3201 console.error(`query failed, code is ${err.code},message is ${err.message}`); 3202}); 3203 ``` 3204 3205### goToPreviousRow 3206 3207goToPreviousRow(): boolean 3208 3209转到结果集的上一行。 3210 3211**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3212 3213**返回值:** 3214 3215| 类型 | 说明 | 3216| ------- | --------------------------------------------- | 3217| boolean | 如果成功移动结果集,则为true;否则返回false。 | 3218 3219**错误码:** 3220 3221以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3222 3223| **错误码ID** | **错误信息** | 3224| ------------ | ------------------------------------------------------------ | 3225| 14800012 | The result set is empty or the specified location is invalid. | 3226 3227**示例:** 3228 3229 ```js 3230let predicates = new relationalStore.RdbPredicates("EMPLOYEE"); 3231let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 3232promise.then((resultSet) => { 3233 resultSet.goToPreviousRow(); 3234 resultSet.close(); 3235}).catch((err) => { 3236 console.error(`query failed, code is ${err.code},message is ${err.message}`); 3237}); 3238 ``` 3239 3240### getBlob 3241 3242getBlob(columnIndex: number): Uint8Array 3243 3244以字节数组的形式获取当前行中指定列的值。 3245 3246**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3247 3248**参数:** 3249 3250| 参数名 | 类型 | 必填 | 说明 | 3251| ----------- | ------ | ---- | ----------------------- | 3252| columnIndex | number | 是 | 指定的列索引,从0开始。 | 3253 3254**返回值:** 3255 3256| 类型 | 说明 | 3257| ---------- | -------------------------------- | 3258| Uint8Array | 以字节数组的形式返回指定列的值。 | 3259 3260**错误码:** 3261 3262以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3263 3264| **错误码ID** | **错误信息** | 3265| ------------ | ------------------------------------------------------------ | 3266| 14800013 | The column value is null or the column type is incompatible. | 3267 3268**示例:** 3269 3270 ```js 3271const codes = resultSet.getBlob(resultSet.getColumnIndex("CODES")); 3272 ``` 3273 3274### getString 3275 3276getString(columnIndex: number): string 3277 3278以字符串形式获取当前行中指定列的值。 3279 3280**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3281 3282**参数:** 3283 3284| 参数名 | 类型 | 必填 | 说明 | 3285| ----------- | ------ | ---- | ----------------------- | 3286| columnIndex | number | 是 | 指定的列索引,从0开始。 | 3287 3288**返回值:** 3289 3290| 类型 | 说明 | 3291| ------ | ---------------------------- | 3292| string | 以字符串形式返回指定列的值。 | 3293 3294**错误码:** 3295 3296以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3297 3298| **错误码ID** | **错误信息** | 3299| ------------ | ------------------------------------------------------------ | 3300| 14800013 | The column value is null or the column type is incompatible. | 3301 3302**示例:** 3303 3304 ```js 3305const name = resultSet.getString(resultSet.getColumnIndex("NAME")); 3306 ``` 3307 3308### getLong 3309 3310getLong(columnIndex: number): number 3311 3312以Long形式获取当前行中指定列的值。 3313 3314**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3315 3316**参数:** 3317 3318| 参数名 | 类型 | 必填 | 说明 | 3319| ----------- | ------ | ---- | ----------------------- | 3320| columnIndex | number | 是 | 指定的列索引,从0开始。 | 3321 3322**返回值:** 3323 3324| 类型 | 说明 | 3325| ------ | ------------------------------------------------------------ | 3326| number | 以Long形式返回指定列的值。<br>该接口支持的数据范围是:Number.MIN_SAFE_INTEGER ~ Number.MAX_SAFE_INTEGER,若超出该范围,建议使用[getDouble](#getdouble)。 | 3327 3328**错误码:** 3329 3330以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3331 3332| **错误码ID** | **错误信息** | 3333| ------------ | ------------------------------------------------------------ | 3334| 14800013 | The column value is null or the column type is incompatible. | 3335 3336**示例:** 3337 3338 ```js 3339const age = resultSet.getLong(resultSet.getColumnIndex("AGE")); 3340 ``` 3341 3342### getDouble 3343 3344getDouble(columnIndex: number): number 3345 3346以double形式获取当前行中指定列的值。 3347 3348**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3349 3350**参数:** 3351 3352| 参数名 | 类型 | 必填 | 说明 | 3353| ----------- | ------ | ---- | ----------------------- | 3354| columnIndex | number | 是 | 指定的列索引,从0开始。 | 3355 3356**返回值:** 3357 3358| 类型 | 说明 | 3359| ------ | ---------------------------- | 3360| number | 以double形式返回指定列的值。 | 3361 3362**错误码:** 3363 3364以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3365 3366| **错误码ID** | **错误信息** | 3367| ------------ | ------------------------------------------------------------ | 3368| 14800013 | The column value is null or the column type is incompatible. | 3369 3370**示例:** 3371 3372 ```js 3373const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); 3374 ``` 3375 3376### isColumnNull 3377 3378isColumnNull(columnIndex: number): boolean 3379 3380检查当前行中指定列的值是否为null。 3381 3382**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3383 3384**参数:** 3385 3386| 参数名 | 类型 | 必填 | 说明 | 3387| ----------- | ------ | ---- | ----------------------- | 3388| columnIndex | number | 是 | 指定的列索引,从0开始。 | 3389 3390**返回值:** 3391 3392| 类型 | 说明 | 3393| ------- | --------------------------------------------------------- | 3394| boolean | 如果当前行中指定列的值为null,则返回true,否则返回false。 | 3395 3396**错误码:** 3397 3398以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3399 3400| **错误码ID** | **错误信息** | 3401| ------------ | ------------------------------------------------------------ | 3402| 14800013 | The column value is null or the column type is incompatible. | 3403 3404**示例:** 3405 3406 ```js 3407const isColumnNull = resultSet.isColumnNull(resultSet.getColumnIndex("CODES")); 3408 ``` 3409 3410### close 3411 3412close(): void 3413 3414关闭结果集。 3415 3416**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core 3417 3418**示例:** 3419 3420 ```js 3421let predicatesClose = new relationalStore.RdbPredicates("EMPLOYEE"); 3422let promiseClose = store.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"]); 3423promiseClose.then((resultSet) => { 3424 resultSet.close(); 3425}).catch((err) => { 3426 console.error(`resultset close failed, code is ${err.code},message is ${err.message}`); 3427}); 3428 ``` 3429 3430**错误码:** 3431 3432以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)。 3433 3434| **错误码ID** | **错误信息** | 3435| ------------ | ------------------------------------------------------------ | 3436| 14800012 | The result set is empty or the specified location is invalid. | 3437