js-apis-data-relationalStore.md 123.1 KB
Newer Older
1 2
# @ohos.data.relationalStore (关系型数据库)

L
worker  
lihuihui 已提交
3
关系型数据库(Relational Database,RDB)是一种基于关系模型来管理数据的数据库。关系型数据库基于SQLite组件提供了一套完整的对本地数据库进行管理的机制,对外提供了一系列的增、删、改、查等接口,也可以直接运行用户输入的SQL语句来满足复杂的场景需要。不支持Worker线程。
4 5 6

该模块提供以下关系型数据库相关的常用功能:

7 8
- [RdbPredicates](#rdbpredicates): 数据库中用来代表数据实体的性质、特征或者数据实体之间关系的词项,主要用来定义数据库的操作条件。
- [RdbStore](#rdbstore):提供管理关系数据库(RDB)方法的接口。
L
LiRui 已提交
9
- [ResultSet](#resultset):提供用户调用关系型数据库查询接口之后返回的结果集合。
10 11 12 13 14 15 16 17

> **说明:**
> 
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

## 导入模块

```js
18
import relationalStore from '@ohos.data.relationalStore'
19 20
```

21
## relationalStore.getRdbStore
22

23
getRdbStore(context: Context, config: StoreConfig, callback: AsyncCallback<RdbStore>): void
24 25 26 27 28 29 30 31 32

获得一个相关的RdbStore,操作关系型数据库,用户可以根据自己的需求配置RdbStore的参数,然后通过RdbStore调用相关接口可以执行相关的数据操作,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                                           | 必填 | 说明                                                         |
| -------- | ---------------------------------------------- | ---- | ------------------------------------------------------------ |
W
wangxiyue 已提交
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&lt;[RdbStore](#rdbstore)&gt; | 是   | 指定callback回调函数,返回RdbStore对象。                   |
36 37 38 39 40 41 42

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
P
PaDaBoo 已提交
43
| 14800010     | If failed delete database by invalid database name.  |
44
| 14800011     | If failed open database by database corrupted.     |
45 46 47 48 49 50

**示例:**

FA模型示例:

```js
51

52
import featureAbility from '@ohos.ability.featureAbility'
53

54 55
var store;

56
// 获取context
57
let context = featureAbility.getContext();
58 59

const STORE_CONFIG = {
60 61 62 63 64 65 66
  name: "RdbTest.db",
  securityLevel: relationalStore.SecurityLevel.S1
};

relationalStore.getRdbStore(context, STORE_CONFIG, function (err, rdbStore) {
  store = rdbStore;
  if (err) {
G
ge-yafang 已提交
67
    console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
68 69 70
    return;
  }
  console.info(`Get RdbStore successfully.`);
71 72 73 74 75 76
})
```

Stage模型示例:

```ts
77
import UIAbility from '@ohos.app.ability.UIAbility'
78 79

class EntryAbility extends UIAbility {
80 81 82 83 84 85
  onWindowStageCreate(windowStage) {
    var store;
    const STORE_CONFIG = {
      name: "RdbTest.db",
      securityLevel: relationalStore.SecurityLevel.S1
    };
86
        
87 88 89
    relationalStore.getRdbStore(this.context, STORE_CONFIG, function (err, rdbStore) {
      store = rdbStore;
      if (err) {
G
ge-yafang 已提交
90
        console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
91 92 93 94 95
        return;
      }
      console.info(`Get RdbStore successfully.`);
    })
  }
96 97 98
}
```

99
## relationalStore.getRdbStore
100

101
getRdbStore(context: Context, config: StoreConfig): Promise&lt;RdbStore&gt;
102 103 104 105 106 107 108 109 110

获得一个相关的RdbStore,操作关系型数据库,用户可以根据自己的需求配置RdbStore的参数,然后通过RdbStore调用相关接口可以执行相关的数据操作,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名  | 类型                             | 必填 | 说明                                                         |
| ------- | -------------------------------- | ---- | ------------------------------------------------------------ |
W
wangxiyue 已提交
111
| context | Context                          | 是   | 应用的上下文。 <br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的应用Context定义见[Context](js-apis-inner-application-uiAbilityContext.md)。 |
112 113 114 115 116 117
| config  | [StoreConfig](#storeconfig) | 是   | 与此RDB存储相关的数据库配置。                                |

**返回值**

| 类型                                      | 说明                              |
| ----------------------------------------- | --------------------------------- |
118
| Promise&lt;[RdbStore](#rdbstore)&gt; | Promise对象。返回RdbStore对象。 |
119 120 121 122 123 124 125

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
P
PaDaBoo 已提交
126
| 14800010     | If failed delete database by invalid database name. |
127
| 14800011     | If failed open database by database corrupted.     |
128 129 130 131 132 133 134

**示例:**

FA模型示例:

```js
import featureAbility from '@ohos.ability.featureAbility'
135

136 137
var store;

138
// 获取context
139
let context = featureAbility.getContext();
140 141

const STORE_CONFIG = {
142 143 144
  name: "RdbTest.db",
  securityLevel: relationalStore.SecurityLevel.S1
};
145

146
let promise = relationalStore.getRdbStore(context, STORE_CONFIG);
147
promise.then(async (rdbStore) => {
148 149
  store = rdbStore;
  console.info(`Get RdbStore successfully.`);
150
}).catch((err) => {
G
ge-yafang 已提交
151
  console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
152 153 154 155 156 157
})
```

Stage模型示例:

```ts
158
import UIAbility from '@ohos.app.ability.UIAbility'
159 160

class EntryAbility extends UIAbility {
161 162 163 164 165 166
  onWindowStageCreate(windowStage) {
    var store;
    const STORE_CONFIG = {
      name: "RdbTest.db",
      securityLevel: relationalStore.SecurityLevel.S1
    };
167
        
168 169 170 171 172
    let promise = relationalStore.getRdbStore(this.context, STORE_CONFIG);
    promise.then(async (rdbStore) => {
      store = rdbStore;
      console.info(`Get RdbStore successfully.`)
    }).catch((err) => {
G
ge-yafang 已提交
173
      console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
174 175
    })
  }
176 177 178
}
```

179
## relationalStore.deleteRdbStore
180 181 182 183 184 185 186 187 188 189 190

deleteRdbStore(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void

删除数据库,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
W
wangxiyue 已提交
191
| context  | Context                   | 是   | 应用的上下文。 <br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的应用Context定义见[Context](js-apis-inner-application-uiAbilityContext.md)。 |
192 193 194 195 196 197 198 199 200
| name     | string                    | 是   | 数据库名称。                                                 |
| callback | AsyncCallback&lt;void&gt; | 是   | 指定callback回调函数。                                       |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
P
PaDaBoo 已提交
201
| 14800010     | If failed delete database by invalid database name. |
202 203 204 205 206 207 208

**示例:**

FA模型示例:

```js
import featureAbility from '@ohos.ability.featureAbility'
209 210

// 获取context
211 212
let context = featureAbility.getContext()

213 214
relationalStore.deleteRdbStore(context, "RdbTest.db", function (err) {
  if (err) {
L
delete  
lihuihui 已提交
215
    console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`);
216 217 218
    return;
  }
  console.info(`Delete RdbStore successfully.`);
219 220 221 222 223 224
})
```

Stage模型示例:

```ts
225
import UIAbility from '@ohos.app.ability.UIAbility'
226 227

class EntryAbility extends UIAbility {
228 229 230
  onWindowStageCreate(windowStage){
    relationalStore.deleteRdbStore(this.context, "RdbTest.db", function (err) {
      if (err) {
L
delete  
lihuihui 已提交
231
        console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`);
232 233 234 235 236
        return;
      }
      console.info(`Delete RdbStore successfully.`);
    })
  }
237 238 239
}
```

240
## relationalStore.deleteRdbStore
241 242 243 244 245 246 247 248 249 250 251

deleteRdbStore(context: Context, name: string): Promise&lt;void&gt;

使用指定的数据库文件配置删除数据库,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数**

| 参数名  | 类型    | 必填 | 说明                                                         |
| ------- | ------- | ---- | ------------------------------------------------------------ |
W
wangxiyue 已提交
252
| context | Context | 是   | 应用的上下文。 <br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的应用Context定义见[Context](js-apis-inner-application-uiAbilityContext.md)。 |
253 254 255 256 257 258 259 260 261 262 263 264 265 266
| name    | string  | 是   | 数据库名称。                                                 |

**返回值**

| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
P
PaDaBoo 已提交
267
| 14800010     | If failed delete database by invalid database name. |
268 269 270 271 272 273 274

**示例:**

FA模型示例:

```js
import featureAbility from '@ohos.ability.featureAbility'
275 276

// 获取context
277
let context = featureAbility.getContext();
278

279
let promise = relationalStore.deleteRdbStore(context, "RdbTest.db");
280
promise.then(()=>{
281
  console.info(`Delete RdbStore successfully.`);
282
}).catch((err) => {
L
delete  
lihuihui 已提交
283
  console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`);
284 285 286 287 288 289
})
```

Stage模型示例:

```ts
290
import UIAbility from '@ohos.app.ability.UIAbility'
291 292

class EntryAbility extends UIAbility {
293 294 295 296 297
  onWindowStageCreate(windowStage){
    let promise = relationalStore.deleteRdbStore(this.context, "RdbTest.db");
    promise.then(()=>{
      console.info(`Delete RdbStore successfully.`);
    }).catch((err) => {
L
delete  
lihuihui 已提交
298
      console.error(`Delete RdbStore failed, code is ${err.code},message is ${err.message}`);
299 300
    })
  }
301 302 303
}
```

304
## StoreConfig
305 306 307 308 309 310 311 312

管理关系数据库配置。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

| 名称        | 类型          | 必填 | 说明                                                      |
| ------------- | ------------- | ---- | --------------------------------------------------------- |
| name          | string        | 是   | 数据库文件名。                                            |
P
PaDaBoo 已提交
313
| securityLevel | [SecurityLevel](#securitylevel) | 是   | 设置数据库安全级别                                        |
L
delete  
lihuihui 已提交
314
| encrypt       | boolean       | 否   | 指定数据库是否加密,默认不加密。<br/> true:加密。<br/> false:非加密。 |
315

316
## SecurityLevel
317 318 319

数据库的安全级别枚举。

L
lihuihui 已提交
320 321
> **说明:**
>
L
query  
lihuihui 已提交
322
> 若需要进行同步操作,数据库安全等级应不高于对端设备安全等级,具体可见[跨设备同步访问控制机制](../../database/sync-app-data-across-devices-overview.md#跨设备同步访问控制机制)。
L
lihuihui 已提交
323

324 325 326 327 328 329 330 331 332
**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

| 名称 | 值   | 说明                                                         |
| ---- | ---- | ------------------------------------------------------------ |
| S1   | 1    | 表示数据库的安全级别为低级别,当数据泄露时会产生较低影响。例如,包含壁纸等系统数据的数据库。 |
| S2   | 2    | 表示数据库的安全级别为中级别,当数据泄露时会产生较大影响。例如,包含录音、视频等用户生成数据或通话记录等信息的数据库。 |
| S3   | 3    | 表示数据库的安全级别为高级别,当数据泄露时会产生重大影响。例如,包含用户运动、健康、位置等信息的数据库。 |
| S4   | 4    | 表示数据库的安全级别为关键级别,当数据泄露时会产生严重影响。例如,包含认证凭据、财务数据等信息的数据库。 |

333
## ValueType
334 335 336 337 338 339 340 341 342 343 344

用于表示允许的数据字段类型。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

| 类型    | 说明                 |
| ------- | -------------------- |
| number  | 表示值类型为数字。   |
| string  | 表示值类型为字符。   |
| boolean | 表示值类型为布尔值。 |

345
## ValuesBucket
346 347 348 349 350 351 352 353 354

用于存储键值对的类型。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

| 键类型 | 值类型                                                      |
| ------ | ----------------------------------------------------------- |
| string | [ValueType](#valuetype)\|&nbsp;Uint8Array&nbsp;\|&nbsp;null |

355
## SyncMode
356 357 358

指数据库同步模式。

P
PaDaBoo 已提交
359
**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
360 361 362 363 364 365

| 名称           | 值   | 说明                               |
| -------------- | ---- | ---------------------------------- |
| SYNC_MODE_PUSH | 0    | 表示数据从本地设备推送到远程设备。 |
| SYNC_MODE_PULL | 1    | 表示数据从远程设备拉至本地设备。   |

366
## SubscribeType
367 368 369 370 371 372 373 374 375 376 377

描述订阅类型。

**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

| 名称                  | 值   | 说明               |
| --------------------- | ---- | ------------------ |
| SUBSCRIBE_TYPE_REMOTE | 0    | 订阅远程数据更改。 |

L
lihuihui 已提交
378 379 380 381 382 383 384 385
## ConflictResolution<sup>10+</sup>

插入和修改接口的冲突解决方式。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

| 名称                 | 值   | 说明                                                         |
| -------------------- | ---- | ------------------------------------------------------------ |
P
PaDaBoo 已提交
386
| ON_CONFLICT_NONE | 0 | 表示当冲突发生时,不做任何处理。 |
L
lihuihui 已提交
387 388 389 390 391 392
| ON_CONFLICT_ROLLBACK | 1    | 表示当冲突发生时,中止SQL语句并回滚当前事务。                |
| ON_CONFLICT_ABORT    | 2    | 表示当冲突发生时,中止当前SQL语句,并撤销当前 SQL 语句所做的任何更改,但是由同一事务中先前的 SQL 语句引起的更改被保留并且事务保持活动状态。 |
| ON_CONFLICT_FAIL     | 3    | 表示当冲突发生时,中止当前 SQL 语句。但它不会撤销失败的 SQL 语句的先前更改,也不会结束事务。 |
| ON_CONFLICT_IGNORE   | 4    | 表示当冲突发生时,跳过包含违反约束的行并继续处理 SQL 语句的后续行。 |
| ON_CONFLICT_REPLACE  | 5    | 表示当冲突发生时,在插入或更新当前行之前删除导致约束违例的预先存在的行,并且命令会继续正常执行。 |

393
## RdbPredicates
394 395 396

表示关系型数据库(RDB)的谓词。该类确定RDB中条件表达式的值是true还是false。

397
### constructor
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

constructor(name: string)

构造函数。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明         |
| ------ | ------ | ---- | ------------ |
| name   | string | 是   | 数据库表名。 |

**示例:**

```js
414
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
415 416
```

417
### inDevices
418 419 420 421 422

inDevices(devices: Array&lt;string&gt;): RdbPredicates

同步分布式数据库时连接到组网内指定的远程设备。

L
device  
lihuihui 已提交
423 424 425 426
> **说明:**
>
> 其中devices通过调用[deviceManager.getTrustedDeviceListSync](js-apis-device-manager.md#gettrusteddevicelistsync)方法得到。deviceManager模块的接口均为系统接口,仅系统应用可用。

427 428 429 430 431 432 433 434 435 436 437 438
**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名  | 类型                | 必填 | 说明                       |
| ------- | ------------------- | ---- | -------------------------- |
| devices | Array&lt;string&gt; | 是   | 指定的组网内的远程设备ID。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
439
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
440 441 442 443

**示例:**

```js
L
device  
lihuihui 已提交
444 445
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
446
let deviceIds = [];
L
device  
lihuihui 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459

deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => {
    if (err) {
        console.log("create device manager failed, err=" + err);
        return;
    }
    dmInstance = manager;
    let devices = dmInstance.getTrustedDeviceListSync();
    for (var i = 0; i < devices.length; i++) {
        deviceIds[i] = devices[i].deviceId;
    }
})
                                  
460
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
L
device  
lihuihui 已提交
461
predicates.inDevices(deviceIds);
462 463
```

464
### inAllDevices
465 466 467 468 469 470 471 472 473 474 475 476

inAllDevices(): RdbPredicates


同步分布式数据库时连接到组网内所有的远程设备。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
477
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
478 479 480 481

**示例:**

```js
482 483
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.inAllDevices();
484 485
```

486
### equalTo
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

equalTo(field: string, value: ValueType): RdbPredicates


配置谓词以匹配数据字段为ValueType且值等于指定值的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                    | 必填 | 说明                   |
| ------ | ----------------------- | ---- | ---------------------- |
| field  | string                  | 是   | 数据库表中的列名。     |
| value  | [ValueType](#valuetype) | 是   | 指示要与谓词匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
506
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
507 508 509 510

**示例:**

```js
511 512
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "lisi");
513 514 515
```


516
### notEqualTo
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

notEqualTo(field: string, value: ValueType): RdbPredicates


配置谓词以匹配数据字段为ValueType且值不等于指定值的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                    | 必填 | 说明                   |
| ------ | ----------------------- | ---- | ---------------------- |
| field  | string                  | 是   | 数据库表中的列名。     |
| value  | [ValueType](#valuetype) | 是   | 指示要与谓词匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
536
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
537 538 539 540

**示例:**

```js
541 542
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.notEqualTo("NAME", "lisi");
543 544 545
```


546
### beginWrap
547 548 549 550 551 552 553 554 555 556 557 558

beginWrap(): RdbPredicates


向谓词添加左括号。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**返回值**

| 类型                                 | 说明                      |
| ------------------------------------ | ------------------------- |
559
| [RdbPredicates](#rdbpredicates) | 返回带有左括号的Rdb谓词。 |
560 561 562 563

**示例:**

```js
564
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
565 566 567 568 569 570 571 572
predicates.equalTo("NAME", "lisi")
    .beginWrap()
    .equalTo("AGE", 18)
    .or()
    .equalTo("SALARY", 200.5)
    .endWrap()
```

573
### endWrap
574 575 576 577 578 579 580 581 582 583 584

endWrap(): RdbPredicates

向谓词添加右括号。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**返回值**

| 类型                                 | 说明                      |
| ------------------------------------ | ------------------------- |
585
| [RdbPredicates](#rdbpredicates) | 返回带有右括号的Rdb谓词。 |
586 587 588 589

**示例:**

```js
590
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
591 592 593 594 595 596 597 598
predicates.equalTo("NAME", "lisi")
    .beginWrap()
    .equalTo("AGE", 18)
    .or()
    .equalTo("SALARY", 200.5)
    .endWrap()
```

599
### or
600 601 602 603 604 605 606 607 608 609 610

or(): RdbPredicates

将或条件添加到谓词中。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**返回值**

| 类型                                 | 说明                      |
| ------------------------------------ | ------------------------- |
611
| [RdbPredicates](#rdbpredicates) | 返回带有或条件的Rdb谓词。 |
612 613 614 615

**示例:**

```js
616
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
617 618 619 620 621
predicates.equalTo("NAME", "Lisa")
    .or()
    .equalTo("NAME", "Rose")
```

622
### and
623 624 625 626 627 628 629 630 631 632 633

and(): RdbPredicates

向谓词添加和条件。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**返回值**

| 类型                                 | 说明                      |
| ------------------------------------ | ------------------------- |
634
| [RdbPredicates](#rdbpredicates) | 返回带有和条件的Rdb谓词。 |
635 636 637 638

**示例:**

```js
639
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
640 641 642 643 644
predicates.equalTo("NAME", "Lisa")
    .and()
    .equalTo("SALARY", 200.5)
```

645
### contains
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

contains(field: string, value: string): RdbPredicates

配置谓词以匹配数据字段为string且value包含指定值的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明                   |
| ------ | ------ | ---- | ---------------------- |
| field  | string | 是   | 数据库表中的列名。     |
| value  | string | 是   | 指示要与谓词匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
664
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
665 666 667 668

**示例:**

```js
669 670
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.contains("NAME", "os");
671 672
```

673
### beginsWith
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691

beginsWith(field: string, value: string): RdbPredicates

配置谓词以匹配数据字段为string且值以指定字符串开头的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明                   |
| ------ | ------ | ---- | ---------------------- |
| field  | string | 是   | 数据库表中的列名。     |
| value  | string | 是   | 指示要与谓词匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
692
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
693 694 695 696

**示例:**

```js
697 698
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.beginsWith("NAME", "os");
699 700
```

701
### endsWith
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

endsWith(field: string, value: string): RdbPredicates

配置谓词以匹配数据字段为string且值以指定字符串结尾的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明                   |
| ------ | ------ | ---- | ---------------------- |
| field  | string | 是   | 数据库表中的列名。     |
| value  | string | 是   | 指示要与谓词匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
720
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
721 722 723 724

**示例:**

```js
725 726
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.endsWith("NAME", "se");
727 728
```

729
### isNull
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

isNull(field: string): RdbPredicates

配置谓词以匹配值为null的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| field  | string | 是   | 数据库表中的列名。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
747
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
748 749 750 751

**示例**

```js
752 753
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.isNull("NAME");
754 755
```

756
### isNotNull
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

isNotNull(field: string): RdbPredicates

配置谓词以匹配值不为null的指定字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| field  | string | 是   | 数据库表中的列名。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
774
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
775 776 777 778

**示例:**

```js
779 780
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.isNotNull("NAME");
781 782
```

783
### like
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

like(field: string, value: string): RdbPredicates

配置谓词以匹配数据字段为string且值类似于指定字符串的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明                   |
| ------ | ------ | ---- | ---------------------- |
| field  | string | 是   | 数据库表中的列名。     |
| value  | string | 是   | 指示要与谓词匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
802
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
803 804 805 806

**示例:**

```js
807 808
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.like("NAME", "%os%");
809 810
```

811
### glob
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

glob(field: string, value: string): RdbPredicates

配置RdbPredicates匹配数据字段为string的指定字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明                                                         |
| ------ | ------ | ---- | ------------------------------------------------------------ |
| field  | string | 是   | 数据库表中的列名。                                           |
| value  | string | 是   | 指示要与谓词匹配的值。<br>支持通配符,*表示0个、1个或多个数字或字符,?表示1个数字或字符。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
830
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
831 832 833 834

**示例:**

```js
835 836
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.glob("NAME", "?h*g");
837 838
```

839
### between
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858

between(field: string, low: ValueType, high: ValueType): RdbPredicates

将谓词配置为匹配数据字段为ValueType且value在给定范围内的指定字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                    | 必填 | 说明                       |
| ------ | ----------------------- | ---- | -------------------------- |
| field  | string                  | 是   | 数据库表中的列名。         |
| low    | [ValueType](#valuetype) | 是   | 指示与谓词匹配的最小值。   |
| high   | [ValueType](#valuetype) | 是   | 指示要与谓词匹配的最大值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
859
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
860 861 862 863

**示例:**

```js
864 865
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.between("AGE", 10, 50);
866 867
```

868
### notBetween
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887

notBetween(field: string, low: ValueType, high: ValueType): RdbPredicates

配置RdbPredicates以匹配数据字段为ValueType且value超出给定范围的指定字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                    | 必填 | 说明                       |
| ------ | ----------------------- | ---- | -------------------------- |
| field  | string                  | 是   | 数据库表中的列名。         |
| low    | [ValueType](#valuetype) | 是   | 指示与谓词匹配的最小值。   |
| high   | [ValueType](#valuetype) | 是   | 指示要与谓词匹配的最大值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
888
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
889 890 891 892

**示例:**

```js
893 894
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.notBetween("AGE", 10, 50);
895 896
```

897
### greaterThan
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

greaterThan(field: string, value: ValueType): RdbPredicates

配置谓词以匹配数据字段为ValueType且值大于指定值的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                    | 必填 | 说明                   |
| ------ | ----------------------- | ---- | ---------------------- |
| field  | string                  | 是   | 数据库表中的列名。     |
| value  | [ValueType](#valuetype) | 是   | 指示要与谓词匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
916
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
917 918 919 920

**示例:**

```js
921 922
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.greaterThan("AGE", 18);
923 924
```

925
### lessThan
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

lessThan(field: string, value: ValueType): RdbPredicates

配置谓词以匹配数据字段为valueType且value小于指定值的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                    | 必填 | 说明                   |
| ------ | ----------------------- | ---- | ---------------------- |
| field  | string                  | 是   | 数据库表中的列名。     |
| value  | [ValueType](#valuetype) | 是   | 指示要与谓词匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
944
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
945 946 947 948

**示例:**

```js
949 950
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.lessThan("AGE", 20);
951 952
```

953
### greaterThanOrEqualTo
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971

greaterThanOrEqualTo(field: string, value: ValueType): RdbPredicates

配置谓词以匹配数据字段为ValueType且value大于或等于指定值的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                    | 必填 | 说明                   |
| ------ | ----------------------- | ---- | ---------------------- |
| field  | string                  | 是   | 数据库表中的列名。     |
| value  | [ValueType](#valuetype) | 是   | 指示要与谓词匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
972
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
973 974 975 976

**示例:**

```js
977 978
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.greaterThanOrEqualTo("AGE", 18);
979 980
```

981
### lessThanOrEqualTo
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

lessThanOrEqualTo(field: string, value: ValueType): RdbPredicates

配置谓词以匹配数据字段为ValueType且value小于或等于指定值的字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                    | 必填 | 说明                   |
| ------ | ----------------------- | ---- | ---------------------- |
| field  | string                  | 是   | 数据库表中的列名。     |
| value  | [ValueType](#valuetype) | 是   | 指示要与谓词匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1000
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1001 1002 1003 1004

**示例:**

```js
1005 1006
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.lessThanOrEqualTo("AGE", 20);
1007 1008
```

1009
### orderByAsc
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026

orderByAsc(field: string): RdbPredicates

配置谓词以匹配其值按升序排序的列。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| field  | string | 是   | 数据库表中的列名。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1027
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1028 1029 1030 1031

**示例:**

```js
1032 1033
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.orderByAsc("NAME");
1034 1035
```

1036
### orderByDesc
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

orderByDesc(field: string): RdbPredicates

配置谓词以匹配其值按降序排序的列。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| field  | string | 是   | 数据库表中的列名。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1054
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1055 1056 1057 1058

**示例:**

```js
1059 1060
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.orderByDesc("AGE");
1061 1062
```

1063
### distinct
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

distinct(): RdbPredicates

配置谓词以过滤重复记录并仅保留其中一个。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**返回值**

| 类型                                 | 说明                           |
| ------------------------------------ | ------------------------------ |
1075
| [RdbPredicates](#rdbpredicates) | 返回可用于过滤重复记录的谓词。 |
1076 1077 1078 1079

**示例:**

```js
1080 1081
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose").distinct();
1082 1083
```

1084
### limitAs
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

limitAs(value: number): RdbPredicates

设置最大数据记录数的谓词。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明             |
| ------ | ------ | ---- | ---------------- |
| value  | number | 是   | 最大数据记录数。 |

**返回值**

| 类型                                 | 说明                                 |
| ------------------------------------ | ------------------------------------ |
1102
| [RdbPredicates](#rdbpredicates) | 返回可用于设置最大数据记录数的谓词。 |
1103 1104 1105 1106

**示例:**

```js
1107 1108
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose").limitAs(3);
1109 1110
```

1111
### offsetAs
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128

offsetAs(rowOffset: number): RdbPredicates

配置RdbPredicates以指定返回结果的起始位置。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名    | 类型   | 必填 | 说明                               |
| --------- | ------ | ---- | ---------------------------------- |
| rowOffset | number | 是   | 返回结果的起始位置,取值为正整数。 |

**返回值**

| 类型                                 | 说明                                 |
| ------------------------------------ | ------------------------------------ |
1129
| [RdbPredicates](#rdbpredicates) | 返回具有指定返回结果起始位置的谓词。 |
1130 1131 1132 1133

**示例:**

```js
1134 1135
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose").offsetAs(3);
1136 1137
```

1138
### groupBy
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

groupBy(fields: Array&lt;string&gt;): RdbPredicates

配置RdbPredicates按指定列分组查询结果。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                | 必填 | 说明                 |
| ------ | ------------------- | ---- | -------------------- |
| fields | Array&lt;string&gt; | 是   | 指定分组依赖的列名。 |

**返回值**

| 类型                                 | 说明                   |
| ------------------------------------ | ---------------------- |
1156
| [RdbPredicates](#rdbpredicates) | 返回分组查询列的谓词。 |
1157 1158 1159 1160

**示例:**

```js
1161 1162
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.groupBy(["AGE", "NAME"]);
1163 1164
```

1165
### indexedBy
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

indexedBy(field: string): RdbPredicates

配置RdbPredicates以指定索引列。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明           |
| ------ | ------ | ---- | -------------- |
| field  | string | 是   | 索引列的名称。 |

**返回值**


| 类型                                 | 说明                                  |
| ------------------------------------ | ------------------------------------- |
1184
| [RdbPredicates](#rdbpredicates) | 返回具有指定索引列的RdbPredicates。 |
1185 1186 1187 1188

**示例:**

```js
1189 1190
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.indexedBy("SALARY_INDEX");
1191 1192
```

1193
### in
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

in(field: string, value: Array&lt;ValueType&gt;): RdbPredicates

配置RdbPredicates以匹配数据字段为ValueType数组且值在给定范围内的指定字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                                 | 必填 | 说明                                    |
| ------ | ------------------------------------ | ---- | --------------------------------------- |
| field  | string                               | 是   | 数据库表中的列名。                      |
| value  | Array&lt;[ValueType](#valuetype)&gt; | 是   | 以ValueType型数组形式指定的要匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1212
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1213 1214 1215 1216

**示例:**

```js
1217 1218
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.in("AGE", [18, 20]);
1219 1220
```

1221
### notIn
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239

notIn(field: string, value: Array&lt;ValueType&gt;): RdbPredicates

将RdbPredicates配置为匹配数据字段为ValueType且值超出给定范围的指定字段。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                                 | 必填 | 说明                                  |
| ------ | ------------------------------------ | ---- | ------------------------------------- |
| field  | string                               | 是   | 数据库表中的列名。                    |
| value  | Array&lt;[ValueType](#valuetype)&gt; | 是   | 以ValueType数组形式指定的要匹配的值。 |

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1240
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1241 1242 1243 1244

**示例:**

```js
1245 1246
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.notIn("NAME", ["Lisa", "Rose"]);
1247 1248
```

1249
## RdbStore
1250 1251 1252

提供管理关系数据库(RDB)方法的接口。

L
lihuihui 已提交
1253
在使用以下相关接口前,请使用[executeSql](#executesql)接口初始化数据库表结构和相关数据。
1254

1255
### 属性<sup>10+</sup>
L
leiiyb 已提交
1256 1257 1258 1259 1260

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

| 名称         | 类型            | 必填 | 说明                             |
| ------------ | ----------- | ---- | -------------------------------- |
1261 1262 1263 1264 1265 1266
| version<sup>10+</sup>  | number | 是   | 设置和获取数据库版本,值为大于0的正整数。       |

**示例:**

```js
// 设置数据库版本
1267
store.version = 3;
1268
// 获取数据库版本
1269
console.info(`RdbStore version is ${store.version}`);
1270
```
L
leiiyb 已提交
1271

1272
### insert
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

insert(table: string, values: ValuesBucket, callback: AsyncCallback&lt;number&gt;):void

向目标表中插入一行数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                          | 必填 | 说明                                                       |
| -------- | ----------------------------- | ---- | ---------------------------------------------------------- |
| table    | string                        | 是   | 指定的目标表名。                                           |
| values   | [ValuesBucket](#valuesbucket) | 是   | 表示要插入到表中的数据行。                                 |
| callback | AsyncCallback&lt;number&gt;   | 是   | 指定callback回调函数。如果操作成功,返回行ID;否则返回-1。 |

1288 1289 1290 1291 1292 1293 1294 1295
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1296 1297 1298 1299
**示例:**

```js
const valueBucket = {
1300 1301 1302 1303 1304 1305 1306
  "NAME": "Lisa",
  "AGE": 18,
  "SALARY": 100.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5]),
};
store.insert("EMPLOYEE", valueBucket, function (err, rowId) {
  if (err) {
G
ge-yafang 已提交
1307
    console.error(`Insert is failed, code is ${err.code},message is ${err.message}`);
1308 1309 1310
    return;
  }
  console.info(`Insert is successful, rowId = ${rowId}`);
1311 1312 1313
})
```

L
lihuihui 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
### insert<sup>10+</sup>

insert(table: string, values: ValuesBucket,  conflict: ConflictResolution, callback: AsyncCallback&lt;number&gt;):void

向目标表中插入一行数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                                        | 必填 | 说明                                                       |
| -------- | ------------------------------------------- | ---- | ---------------------------------------------------------- |
| table    | string                                      | 是   | 指定的目标表名。                                           |
| values   | [ValuesBucket](#valuesbucket)               | 是   | 表示要插入到表中的数据行。                                 |
P
PaDaBoo 已提交
1328
| conflict | [ConflictResolution](#conflictresolution10) | 是   | 指定冲突解决方式。                                         |
L
lihuihui 已提交
1329 1330
| callback | AsyncCallback&lt;number&gt;                 | 是   | 指定callback回调函数。如果操作成功,返回行ID;否则返回-1。 |

1331 1332 1333 1334 1335 1336 1337 1338
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

L
lihuihui 已提交
1339 1340 1341 1342
**示例:**

```js
const valueBucket = {
1343 1344 1345 1346 1347 1348 1349
  "NAME": "Lisa",
  "AGE": 18,
  "SALARY": 100.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5]),
};
store.insert("EMPLOYEE", valueBucket, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE, function (err, rowId) {
  if (err) {
G
ge-yafang 已提交
1350
    console.error(`Insert is failed, code is ${err.code},message is ${err.message}`);
1351 1352 1353
    return;
  }
  console.info(`Insert is successful, rowId = ${rowId}`);
L
lihuihui 已提交
1354 1355 1356
})
```

1357
### insert
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377

insert(table: string, values: ValuesBucket):Promise&lt;number&gt;

向目标表中插入一行数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                          | 必填 | 说明                       |
| ------ | ----------------------------- | ---- | -------------------------- |
| table  | string                        | 是   | 指定的目标表名。           |
| values | [ValuesBucket](#valuesbucket) | 是   | 表示要插入到表中的数据行。 |

**返回值**

| 类型                  | 说明                                              |
| --------------------- | ------------------------------------------------- |
| Promise&lt;number&gt; | Promise对象。如果操作成功,返回行ID;否则返回-1。 |

1378 1379 1380 1381 1382 1383 1384 1385
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1386 1387 1388 1389
**示例:**

```js
const valueBucket = {
1390 1391 1392 1393 1394 1395
  "NAME": "Lisa",
  "AGE": 18,
  "SALARY": 100.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5]),
};
let promise = store.insert("EMPLOYEE", valueBucket);
1396
promise.then((rowId) => {
1397 1398
  console.info(`Insert is successful, rowId = ${rowId}`);
}).catch((err) => {
G
ge-yafang 已提交
1399
  console.error(`Insert is failed, code is ${err.code},message is ${err.message}`);
1400 1401 1402
})
```

L
lihuihui 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
### insert<sup>10+</sup>

insert(table: string, values: ValuesBucket,  conflict: ConflictResolution):Promise&lt;number&gt;

向目标表中插入一行数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                                        | 必填 | 说明                       |
| -------- | ------------------------------------------- | ---- | -------------------------- |
| table    | string                                      | 是   | 指定的目标表名。           |
| values   | [ValuesBucket](#valuesbucket)               | 是   | 表示要插入到表中的数据行。 |
P
PaDaBoo 已提交
1417
| conflict | [ConflictResolution](#conflictresolution10) | 是   | 指定冲突解决方式。         |
L
lihuihui 已提交
1418 1419 1420 1421 1422 1423 1424

**返回值**

| 类型                  | 说明                                              |
| --------------------- | ------------------------------------------------- |
| Promise&lt;number&gt; | Promise对象。如果操作成功,返回行ID;否则返回-1。 |

1425 1426 1427 1428 1429 1430 1431 1432
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

L
lihuihui 已提交
1433 1434 1435 1436
**示例:**

```js
const valueBucket = {
1437 1438 1439 1440 1441 1442
  "NAME": "Lisa",
  "AGE": 18,
  "SALARY": 100.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5]),
};
let promise = store.insert("EMPLOYEE", valueBucket, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE);
L
lihuihui 已提交
1443
promise.then((rowId) => {
1444 1445
  console.info(`Insert is successful, rowId = ${rowId}`);
}).catch((err) => {
G
ge-yafang 已提交
1446
  console.error(`Insert is failed, code is ${err.code},message is ${err.message}`);
L
lihuihui 已提交
1447 1448 1449
})
```

1450
### batchInsert
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

batchInsert(table: string, values: Array&lt;ValuesBucket&gt;, callback: AsyncCallback&lt;number&gt;):void

向目标表中插入一组数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                                       | 必填 | 说明                                                         |
| -------- | ------------------------------------------ | ---- | ------------------------------------------------------------ |
| table    | string                                     | 是   | 指定的目标表名。                                             |
| values   | Array&lt;[ValuesBucket](#valuesbucket)&gt; | 是   | 表示要插入到表中的一组数据。                                 |
| callback | AsyncCallback&lt;number&gt;                | 是   | 指定callback回调函数。如果操作成功,返回插入的数据个数,否则返回-1。 |

1466 1467 1468 1469 1470 1471 1472 1473
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1474 1475 1476 1477
**示例:**

```js
const valueBucket1 = {
1478 1479 1480 1481 1482
  "NAME": "Lisa",
  "AGE": 18,
  "SALARY": 100.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5])
};
1483
const valueBucket2 = {
1484 1485 1486 1487 1488
  "NAME": "Jack",
  "AGE": 19,
  "SALARY": 101.5,
  "CODES": new Uint8Array([6, 7, 8, 9, 10])
};
1489
const valueBucket3 = {
1490 1491 1492 1493 1494
  "NAME": "Tom",
  "AGE": 20,
  "SALARY": 102.5,
  "CODES": new Uint8Array([11, 12, 13, 14, 15])
};
1495 1496

let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3);
1497 1498
store.batchInsert("EMPLOYEE", valueBuckets, function(err, insertNum) {
  if (err) {
G
ge-yafang 已提交
1499
    console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`);
1500 1501 1502
    return;
  }
  console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`);
1503 1504 1505
})
```

1506
### batchInsert
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526

batchInsert(table: string, values: Array&lt;ValuesBucket&gt;):Promise&lt;number&gt;

向目标表中插入一组数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                                       | 必填 | 说明                         |
| ------ | ------------------------------------------ | ---- | ---------------------------- |
| table  | string                                     | 是   | 指定的目标表名。             |
| values | Array&lt;[ValuesBucket](#valuesbucket)&gt; | 是   | 表示要插入到表中的一组数据。 |

**返回值**

| 类型                  | 说明                                                        |
| --------------------- | ----------------------------------------------------------- |
| Promise&lt;number&gt; | Promise对象。如果操作成功,返回插入的数据个数,否则返回-1。 |

1527 1528 1529 1530 1531 1532 1533 1534
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1535 1536 1537 1538
**示例:**

```js
const valueBucket1 = {
1539 1540 1541 1542 1543
  "NAME": "Lisa",
  "AGE": 18,
  "SALARY": 100.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5])
};
1544
const valueBucket2 = {
1545 1546 1547 1548 1549
  "NAME": "Jack",
  "AGE": 19,
  "SALARY": 101.5,
  "CODES": new Uint8Array([6, 7, 8, 9, 10])
};
1550
const valueBucket3 = {
1551 1552 1553 1554 1555
  "NAME": "Tom",
  "AGE": 20,
  "SALARY": 102.5,
  "CODES": new Uint8Array([11, 12, 13, 14, 15])
};
1556 1557

let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3);
1558
let promise = store.batchInsert("EMPLOYEE", valueBuckets);
1559
promise.then((insertNum) => {
1560 1561
  console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`);
}).catch((err) => {
G
ge-yafang 已提交
1562
  console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`);
1563 1564 1565
})
```

1566
### update
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

update(values: ValuesBucket, predicates: RdbPredicates, callback: AsyncCallback&lt;number&gt;):void

根据RdbPredicates的指定实例对象更新数据库中的数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名     | 类型                                 | 必填 | 说明                                                         |
| ---------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
| values     | [ValuesBucket](#valuesbucket)        | 是   | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 |
1579
| predicates | [RdbPredicates](#rdbpredicates) | 是   | RdbPredicates的实例对象指定的更新条件。                    |
1580 1581
| callback   | AsyncCallback&lt;number&gt;          | 是   | 指定的callback回调方法。返回受影响的行数。                   |

1582 1583 1584 1585 1586 1587 1588 1589
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1590 1591 1592 1593
**示例:**

```js
const valueBucket = {
1594 1595 1596 1597 1598 1599 1600 1601 1602
  "NAME": "Rose",
  "AGE": 22,
  "SALARY": 200.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5]),
};
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa");
store.update(valueBucket, predicates, function (err, rows) {
  if (err) {
G
ge-yafang 已提交
1603
    console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
1604 1605 1606
    return;
  }
  console.info(`Updated row count: ${rows}`);
1607 1608 1609
})
```

L
lihuihui 已提交
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
### update<sup>10+</sup>

update(values: ValuesBucket, predicates: RdbPredicates, conflict: ConflictResolution, callback: AsyncCallback&lt;number&gt;):void

根据RdbPredicates的指定实例对象更新数据库中的数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名     | 类型                                        | 必填 | 说明                                                         |
| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ |
| values     | [ValuesBucket](#valuesbucket)               | 是   | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 |
1623
| predicates | [RdbPredicates](#rdbpredicates)            | 是   | RdbPredicates的实例对象指定的更新条件。                      |
P
PaDaBoo 已提交
1624
| conflict   | [ConflictResolution](#conflictresolution10) | 是   | 指定冲突解决方式。                                           |
L
lihuihui 已提交
1625 1626
| callback   | AsyncCallback&lt;number&gt;                 | 是   | 指定的callback回调方法。返回受影响的行数。                   |

1627 1628 1629 1630 1631 1632 1633 1634
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

L
lihuihui 已提交
1635 1636 1637 1638
**示例:**

```js
const valueBucket = {
1639 1640 1641 1642 1643 1644 1645 1646 1647
  "NAME": "Rose",
  "AGE": 22,
  "SALARY": 200.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5]),
};
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa");
store.update(valueBucket, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE, function (err, rows) {
  if (err) {
G
ge-yafang 已提交
1648
    console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
1649 1650 1651
    return;
  }
  console.info(`Updated row count: ${rows}`);
L
lihuihui 已提交
1652 1653 1654
})
```

1655
### update
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667

update(values: ValuesBucket, predicates: RdbPredicates):Promise&lt;number&gt;

根据RdbPredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名       | 类型                                 | 必填 | 说明                                                         |
| ------------ | ------------------------------------ | ---- | ------------------------------------------------------------ |
| values       | [ValuesBucket](#valuesbucket)        | 是   | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 |
1668
| predicates | [RdbPredicates](#rdbpredicates) | 是   | RdbPredicates的实例对象指定的更新条件。                    |
1669 1670 1671 1672 1673 1674 1675

**返回值**

| 类型                  | 说明                                      |
| --------------------- | ----------------------------------------- |
| Promise&lt;number&gt; | 指定的Promise回调方法。返回受影响的行数。 |

1676 1677 1678 1679 1680 1681 1682 1683
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1684 1685 1686 1687
**示例:**

```js
const valueBucket = {
1688 1689 1690 1691 1692 1693 1694 1695
  "NAME": "Rose",
  "AGE": 22,
  "SALARY": 200.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5]),
};
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa");
let promise = store.update(valueBucket, predicates);
P
PaDaBoo 已提交
1696
promise.then(async (rows) => {
1697
  console.info(`Updated row count: ${rows}`);
1698
}).catch((err) => {
G
ge-yafang 已提交
1699
  console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
1700 1701 1702
})
```

L
lihuihui 已提交
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
### update<sup>10+</sup>

update(values: ValuesBucket, predicates: RdbPredicates, conflict: ConflictResolution):Promise&lt;number&gt;

根据RdbPredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名     | 类型                                        | 必填 | 说明                                                         |
| ---------- | ------------------------------------------- | ---- | ------------------------------------------------------------ |
| values     | [ValuesBucket](#valuesbucket)               | 是   | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 |
1716
| predicates | [RdbPredicates](#rdbpredicates)            | 是   | RdbPredicates的实例对象指定的更新条件。                      |
P
PaDaBoo 已提交
1717
| conflict   | [ConflictResolution](#conflictresolution10) | 是   | 指定冲突解决方式。                                           |
L
lihuihui 已提交
1718 1719 1720 1721 1722 1723 1724

**返回值**

| 类型                  | 说明                                      |
| --------------------- | ----------------------------------------- |
| Promise&lt;number&gt; | 指定的Promise回调方法。返回受影响的行数。 |

1725 1726 1727 1728 1729 1730 1731 1732
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

L
lihuihui 已提交
1733 1734 1735 1736
**示例:**

```js
const valueBucket = {
1737 1738 1739 1740 1741 1742 1743 1744
  "NAME": "Rose",
  "AGE": 22,
  "SALARY": 200.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5]),
};
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa");
let promise = store.update(valueBucket, predicates, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE);
P
PaDaBoo 已提交
1745
promise.then(async (rows) => {
1746
  console.info(`Updated row count: ${rows}`);
L
lihuihui 已提交
1747
}).catch((err) => {
G
ge-yafang 已提交
1748
  console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
L
lihuihui 已提交
1749 1750 1751
})
```

1752
### update
1753 1754 1755 1756 1757 1758 1759

update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback&lt;number&gt;):void

根据DataSharePredicates的指定实例对象更新数据库中的数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

G
ge-yafang 已提交
1760 1761
**模型约束:** 此接口仅可在Stage模型下可用。

1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
**系统接口:** 此接口为系统接口。

**参数:**

| 参数名     | 类型                                                         | 必填 | 说明                                                         |
| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| table      | string                                                       | 是   | 指定的目标表名。                                             |
| values     | [ValuesBucket](#valuesbucket)                                | 是   | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 |
| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是   | DataSharePredicates的实例对象指定的更新条件。                |
| callback   | AsyncCallback&lt;number&gt;                                  | 是   | 指定的callback回调方法。返回受影响的行数。                   |

1773 1774 1775 1776 1777 1778 1779 1780
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1781 1782 1783 1784 1785 1786 1787 1788 1789
**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
const valueBucket = {
    "NAME": "Rose",
    "AGE": 22,
    "SALARY": 200.5,
    "CODES": new Uint8Array([1, 2, 3, 4, 5]),
1790 1791 1792 1793 1794
};
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
store.update("EMPLOYEE", valueBucket, predicates, function (err, rows) {
  if (err) {
G
ge-yafang 已提交
1795
    console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
1796 1797 1798
    return;
  }
  console.info(`Updated row count: ${rows}`);
1799 1800 1801
})
```

1802
### update
1803 1804 1805 1806 1807 1808 1809

update(table: string, values: ValuesBucket, predicates: dataSharePredicates.DataSharePredicates):Promise&lt;number&gt;

根据DataSharePredicates的指定实例对象更新数据库中的数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

G
ge-yafang 已提交
1810 1811
**模型约束:** 此接口仅可在Stage模型下可用。

1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
**系统接口:** 此接口为系统接口。

**参数:**

| 参数名     | 类型                                                         | 必填 | 说明                                                         |
| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| table      | string                                                       | 是   | 指定的目标表名。                                             |
| values     | [ValuesBucket](#valuesbucket)                                | 是   | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 |
| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是   | DataSharePredicates的实例对象指定的更新条件。                |

**返回值**

| 类型                  | 说明                                      |
| --------------------- | ----------------------------------------- |
| Promise&lt;number&gt; | 指定的Promise回调方法。返回受影响的行数。 |

1828 1829 1830 1831 1832 1833 1834 1835
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1836 1837 1838 1839 1840
**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
const valueBucket = {
1841 1842 1843 1844 1845 1846 1847 1848
  "NAME": "Rose",
  "AGE": 22,
  "SALARY": 200.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5]),
};
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
let promise = store.update("EMPLOYEE", valueBucket, predicates);
P
PaDaBoo 已提交
1849
promise.then(async (rows) => {
1850
  console.info(`Updated row count: ${rows}`);
1851
}).catch((err) => {
G
ge-yafang 已提交
1852
  console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
1853 1854 1855
})
```

1856
### delete
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867

delete(predicates: RdbPredicates, callback: AsyncCallback&lt;number&gt;):void

根据RdbPredicates的指定实例对象从数据库中删除数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名     | 类型                                 | 必填 | 说明                                      |
| ---------- | ------------------------------------ | ---- | ----------------------------------------- |
1868
| predicates | [RdbPredicates](#rdbpredicates) | 是   | RdbPredicates的实例对象指定的删除条件。 |
1869 1870
| callback   | AsyncCallback&lt;number&gt;          | 是   | 指定callback回调函数。返回受影响的行数。  |

1871 1872 1873 1874 1875 1876 1877 1878
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1879 1880 1881
**示例:**

```js
1882 1883 1884 1885
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa");
store.delete(predicates, function (err, rows) {
  if (err) {
G
ge-yafang 已提交
1886
    console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
1887 1888 1889
    return;
  }
  console.info(`Delete rows: ${rows}`);
1890 1891 1892
})
```

1893
### delete
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904

delete(predicates: RdbPredicates):Promise&lt;number&gt;

根据RdbPredicates的指定实例对象从数据库中删除数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名     | 类型                                 | 必填 | 说明                                      |
| ---------- | ------------------------------------ | ---- | ----------------------------------------- |
1905
| predicates | [RdbPredicates](#rdbpredicates) | 是   | RdbPredicates的实例对象指定的删除条件。 |
1906 1907 1908 1909 1910 1911 1912

**返回值**

| 类型                  | 说明                            |
| --------------------- | ------------------------------- |
| Promise&lt;number&gt; | Promise对象。返回受影响的行数。 |

1913 1914 1915 1916 1917 1918 1919 1920
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1921 1922 1923
**示例:**

```js
1924 1925 1926
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa");
let promise = store.delete(predicates);
1927
promise.then((rows) => {
1928
  console.info(`Delete rows: ${rows}`);
1929
}).catch((err) => {
G
ge-yafang 已提交
1930
  console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
1931 1932 1933
})
```

1934
### delete
1935 1936 1937 1938 1939 1940 1941

delete(table: string, predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback&lt;number&gt;):void

根据DataSharePredicates的指定实例对象从数据库中删除数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

G
ge-yafang 已提交
1942 1943
**模型约束:** 此接口仅可在Stage模型下可用。

1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
**系统接口:** 此接口为系统接口。

**参数:**

| 参数名     | 类型                                                         | 必填 | 说明                                          |
| ---------- | ------------------------------------------------------------ | ---- | --------------------------------------------- |
| table      | string                                                       | 是   | 指定的目标表名。                              |
| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是   | DataSharePredicates的实例对象指定的删除条件。 |
| callback   | AsyncCallback&lt;number&gt;                                  | 是   | 指定callback回调函数。返回受影响的行数。      |

1954 1955 1956 1957 1958 1959 1960 1961
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

1962 1963 1964 1965
**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
1966 1967 1968 1969
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
store.delete("EMPLOYEE", predicates, function (err, rows) {
  if (err) {
G
ge-yafang 已提交
1970
    console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
1971 1972 1973
    return;
  }
  console.info(`Delete rows: ${rows}`);
1974 1975 1976
})
```

1977
### delete
1978 1979 1980 1981 1982 1983 1984

delete(table: string, predicates: dataSharePredicates.DataSharePredicates):Promise&lt;number&gt;

根据DataSharePredicates的指定实例对象从数据库中删除数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

G
ge-yafang 已提交
1985 1986
**模型约束:** 此接口仅可在Stage模型下可用。

1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
**系统接口:** 此接口为系统接口。

**参数:**

| 参数名     | 类型                                                         | 必填 | 说明                                          |
| ---------- | ------------------------------------------------------------ | ---- | --------------------------------------------- |
| table      | string                                                       | 是   | 指定的目标表名。                              |
| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是   | DataSharePredicates的实例对象指定的删除条件。 |

**返回值**

| 类型                  | 说明                            |
| --------------------- | ------------------------------- |
| Promise&lt;number&gt; | Promise对象。返回受影响的行数。 |

2002 2003 2004 2005 2006 2007 2008 2009
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

2010 2011 2012 2013
**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
2014 2015 2016
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
let promise = store.delete("EMPLOYEE", predicates);
2017
promise.then((rows) => {
2018
  console.info(`Delete rows: ${rows}`);
2019
}).catch((err) => {
G
ge-yafang 已提交
2020
  console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
2021 2022 2023
})
```

2024
### query
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035

query(predicates: RdbPredicates, columns: Array&lt;string&gt;, callback: AsyncCallback&lt;ResultSet&gt;):void

根据指定条件查询数据库中的数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名     | 类型                                                         | 必填 | 说明                                                        |
| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- |
2036
| predicates | [RdbPredicates](#rdbpredicates)                         | 是   | RdbPredicates的实例对象指定的查询条件。                   |
2037
| columns    | Array&lt;string&gt;                                          | 是   | 表示要查询的列。如果值为空,则查询应用于所有列。            |
P
PaDaBoo 已提交
2038
| callback   | AsyncCallback&lt;[ResultSet](#resultset)&gt; | 是   | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 |
2039 2040 2041 2042

**示例:**

```js
2043 2044 2045 2046
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose");
store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
  if (err) {
G
ge-yafang 已提交
2047
    console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2048 2049 2050 2051
    return;
  }
  console.info(`ResultSet column names: ${resultSet.columnNames}`);
  console.info(`ResultSet column count: ${resultSet.columnCount}`);
2052 2053 2054
})
```

2055
### query
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066

query(predicates: RdbPredicates, columns?: Array&lt;string&gt;):Promise&lt;ResultSet&gt;

根据指定条件查询数据库中的数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名     | 类型                                 | 必填 | 说明                                             |
| ---------- | ------------------------------------ | ---- | ------------------------------------------------ |
2067
| predicates | [RdbPredicates](#rdbpredicates) | 是   | RdbPredicates的实例对象指定的查询条件。        |
2068 2069 2070 2071 2072 2073
| columns    | Array&lt;string&gt;                  | 否   | 表示要查询的列。如果值为空,则查询应用于所有列。 |

**返回值**

| 类型                                                    | 说明                                               |
| ------------------------------------------------------- | -------------------------------------------------- |
P
PaDaBoo 已提交
2074
| Promise&lt;[ResultSet](#resultset)&gt; | Promise对象。如果操作成功,则返回ResultSet对象。 |
2075 2076 2077 2078

**示例:**

  ```js
2079 2080 2081
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
2082
promise.then((resultSet) => {
2083 2084
  console.info(`ResultSet column names: ${resultSet.columnNames}`);
  console.info(`ResultSet column count: ${resultSet.columnCount}`);
2085
}).catch((err) => {
G
ge-yafang 已提交
2086
  console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2087 2088 2089
})
  ```

2090
### query
2091 2092 2093 2094 2095 2096 2097

query(table: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array&lt;string&gt;, callback: AsyncCallback&lt;ResultSet&gt;):void

根据指定条件查询数据库中的数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

G
ge-yafang 已提交
2098 2099
**模型约束:** 此接口仅可在Stage模型下可用。

2100 2101 2102 2103 2104 2105 2106 2107 2108
**系统接口:** 此接口为系统接口。

**参数:**

| 参数名     | 类型                                                         | 必填 | 说明                                                        |
| ---------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------------- |
| table      | string                                                       | 是   | 指定的目标表名。                                            |
| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是   | DataSharePredicates的实例对象指定的查询条件。               |
| columns    | Array&lt;string&gt;                                          | 是   | 表示要查询的列。如果值为空,则查询应用于所有列。            |
P
PaDaBoo 已提交
2109
| callback   | AsyncCallback&lt;[ResultSet](#resultset)&gt; | 是   | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 |
2110 2111 2112 2113 2114

**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
2115 2116 2117 2118
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Rose");
store.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
  if (err) {
G
ge-yafang 已提交
2119
    console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2120 2121 2122 2123
    return;
  }
  console.info(`ResultSet column names: ${resultSet.columnNames}`);
  console.info(`ResultSet column count: ${resultSet.columnCount}`);
2124 2125 2126
})
```

2127
### query
2128 2129 2130 2131 2132 2133 2134

query(table: string, predicates: dataSharePredicates.DataSharePredicates, columns?: Array&lt;string&gt;):Promise&lt;ResultSet&gt;

根据指定条件查询数据库中的数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

G
ge-yafang 已提交
2135 2136
**模型约束:** 此接口仅可在Stage模型下可用。

2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
**系统接口:** 此接口为系统接口。

**参数:**

| 参数名     | 类型                                                         | 必填 | 说明                                             |
| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------ |
| table      | string                                                       | 是   | 指定的目标表名。                                 |
| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | 是   | DataSharePredicates的实例对象指定的查询条件。    |
| columns    | Array&lt;string&gt;                                          | 否   | 表示要查询的列。如果值为空,则查询应用于所有列。 |

**返回值**

| 类型                                                    | 说明                                               |
| ------------------------------------------------------- | -------------------------------------------------- |
P
PaDaBoo 已提交
2151
| Promise&lt;[ResultSet](#resultset)&gt; | Promise对象。如果操作成功,则返回ResultSet对象。 |
2152 2153 2154 2155 2156

**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
2157 2158 2159
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Rose");
let promise = store.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
2160
promise.then((resultSet) => {
2161 2162
  console.info(`ResultSet column names: ${resultSet.columnNames}`);
  console.info(`ResultSet column count: ${resultSet.columnCount}`);
2163
}).catch((err) => {
G
ge-yafang 已提交
2164
  console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2165 2166 2167
})
```

2168
### remoteQuery
2169 2170 2171 2172 2173

remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array&lt;string&gt; , callback: AsyncCallback&lt;ResultSet&gt;): void

根据指定条件查询远程设备数据库中的数据。使用callback异步回调。

L
devices  
lihuihui 已提交
2174 2175 2176 2177
> **说明:**
>
> 其中device通过调用[deviceManager.getTrustedDeviceListSync](js-apis-device-manager.md#gettrusteddevicelistsync)方法得到。deviceManager模块的接口均为系统接口,仅系统应用可用。

L
device  
lihuihui 已提交
2178 2179
**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

2180 2181
**参数:**

L
device  
lihuihui 已提交
2182 2183 2184 2185 2186 2187
| 参数名     | 类型                                         | 必填 | 说明                                                      |
| ---------- | -------------------------------------------- | ---- | --------------------------------------------------------- |
| device     | string                                       | 是   | 指定的远程设备ID。                                        |
| table      | string                                       | 是   | 指定的目标表名。                                          |
| predicates | [RdbPredicates](#rdbpredicates)              | 是   | RdbPredicates的实例对象,指定查询的条件。                 |
| columns    | Array&lt;string&gt;                          | 是   | 表示要查询的列。如果值为空,则查询应用于所有列。          |
P
PaDaBoo 已提交
2188
| callback   | AsyncCallback&lt;[ResultSet](#resultset)&gt; | 是   | 指定callback回调函数。如果操作成功,则返回ResultSet对象。 |
2189 2190 2191 2192

**示例:**

```js
L
device  
lihuihui 已提交
2193 2194
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
2195
let deviceId = null;
L
device  
lihuihui 已提交
2196 2197 2198 2199 2200 2201 2202 2203

deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => {
    if (err) {
        console.log("create device manager failed, err=" + err);
        return;
    }
    dmInstance = manager;
    let devices = dmInstance.getTrustedDeviceListSync();
L
lihuihui 已提交
2204
    deviceId = devices[0].deviceId;
L
device  
lihuihui 已提交
2205 2206
})

2207 2208
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.greaterThan("id", 0);
L
device  
lihuihui 已提交
2209
store.remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"],
2210
  function(err, resultSet) {
2211
    if (err) {
G
ge-yafang 已提交
2212
      console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`);
2213
      return;
2214
    }
2215 2216 2217 2218
    console.info(`ResultSet column names: ${resultSet.columnNames}`);
    console.info(`ResultSet column count: ${resultSet.columnCount}`);
  }
)
2219 2220
```

2221
### remoteQuery
2222 2223 2224 2225 2226

remoteQuery(device: string, table: string, predicates: RdbPredicates, columns: Array&lt;string&gt;): Promise&lt;ResultSet&gt;

根据指定条件查询远程设备数据库中的数据。使用Promise异步回调。

L
devices  
lihuihui 已提交
2227 2228 2229 2230
> **说明:**
>
> 其中device通过调用[deviceManager.getTrustedDeviceListSync](js-apis-device-manager.md#gettrusteddevicelistsync)方法得到。deviceManager模块的接口均为系统接口,仅系统应用可用。

L
device  
lihuihui 已提交
2231 2232
**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

2233 2234 2235 2236
**参数:**

| 参数名     | 类型                                 | 必填 | 说明                                             |
| ---------- | ------------------------------------ | ---- | ------------------------------------------------ |
L
device  
lihuihui 已提交
2237
| device     | string                               | 是   | 指定的远程设备ID。                   |
2238
| table      | string                               | 是   | 指定的目标表名。                                 |
2239
| predicates | [RdbPredicates](#rdbpredicates) | 是   | RdbPredicates的实例对象,指定查询的条件。      |
2240 2241 2242 2243 2244 2245
| columns    | Array&lt;string&gt;                  | 是   | 表示要查询的列。如果值为空,则查询应用于所有列。 |

**返回值**

| 类型                                                         | 说明                                               |
| ------------------------------------------------------------ | -------------------------------------------------- |
P
PaDaBoo 已提交
2246
| Promise&lt;[ResultSet](#resultset)&gt; | Promise对象。如果操作成功,则返回ResultSet对象。 |
2247 2248 2249 2250

**示例:**

```js
L
device  
lihuihui 已提交
2251 2252
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
2253
let deviceId = null;
L
device  
lihuihui 已提交
2254 2255 2256 2257 2258 2259 2260 2261

deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => {
    if (err) {
        console.log("create device manager failed, err=" + err);
        return;
    }
    dmInstance = manager;
    let devices = dmInstance.getTrustedDeviceListSync();
L
lihuihui 已提交
2262
    deviceId = devices[0].deviceId;
L
device  
lihuihui 已提交
2263 2264
})

2265 2266
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.greaterThan("id", 0);
L
lihuihui 已提交
2267
let promise = store.remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
2268
promise.then((resultSet) => {
2269 2270
  console.info(`ResultSet column names: ${resultSet.columnNames}`);
  console.info(`ResultSet column count: ${resultSet.columnCount}`);
2271
}).catch((err) => {
G
ge-yafang 已提交
2272
  console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`);
2273 2274 2275
})
```

2276
### querySql
2277 2278 2279 2280 2281 2282 2283 2284 2285

querySql(sql: string, bindArgs: Array&lt;ValueType&gt;, callback: AsyncCallback&lt;ResultSet&gt;):void

根据指定SQL语句查询数据库中的数据,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

L
query  
lihuihui 已提交
2286 2287 2288
| 参数名   | 类型                                         | 必填 | 说明                                                         |
| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
| sql      | string                                       | 是   | 指定要执行的SQL语句。                                        |
L
lihuihui 已提交
2289
| bindArgs | Array&lt;[ValueType](#valuetype)&gt;         | 是   | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数需为空数组。 |
L
query  
lihuihui 已提交
2290
| callback | AsyncCallback&lt;[ResultSet](#resultset)&gt; | 是   | 指定callback回调函数。如果操作成功,则返回ResultSet对象。    |
2291 2292 2293 2294

**示例:**

```js
2295 2296
store.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], function (err, resultSet) {
  if (err) {
G
ge-yafang 已提交
2297
    console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2298 2299 2300 2301
    return;
  }
  console.info(`ResultSet column names: ${resultSet.columnNames}`);
  console.info(`ResultSet column count: ${resultSet.columnCount}`);
2302 2303 2304
})
```

2305
### querySql
2306 2307 2308 2309 2310 2311 2312 2313 2314

querySql(sql: string, bindArgs?: Array&lt;ValueType&gt;):Promise&lt;ResultSet&gt;

根据指定SQL语句查询数据库中的数据,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

L
query  
lihuihui 已提交
2315 2316 2317 2318
| 参数名   | 类型                                 | 必填 | 说明                                                         |
| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
| sql      | string                               | 是   | 指定要执行的SQL语句。                                        |
| bindArgs | Array&lt;[ValueType](#valuetype)&gt; | 否   | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 |
2319 2320 2321 2322 2323

**返回值**

| 类型                                                    | 说明                                               |
| ------------------------------------------------------- | -------------------------------------------------- |
P
PaDaBoo 已提交
2324
| Promise&lt;[ResultSet](#resultset)&gt; | Promise对象。如果操作成功,则返回ResultSet对象。 |
2325 2326 2327 2328

**示例:**

```js
L
RDB  
lihuihui 已提交
2329
let promise = store.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'");
2330
promise.then((resultSet) => {
2331 2332
  console.info(`ResultSet column names: ${resultSet.columnNames}`);
  console.info(`ResultSet column count: ${resultSet.columnCount}`);
2333
}).catch((err) => {
G
ge-yafang 已提交
2334
  console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2335 2336 2337
})
```

2338
### executeSql
2339 2340 2341 2342 2343 2344 2345 2346 2347

executeSql(sql: string, bindArgs: Array&lt;ValueType&gt;, callback: AsyncCallback&lt;void&gt;):void

执行包含指定参数但不返回值的SQL语句,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

L
query  
lihuihui 已提交
2348 2349 2350
| 参数名   | 类型                                 | 必填 | 说明                                                         |
| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
| sql      | string                               | 是   | 指定要执行的SQL语句。                                        |
L
lihuihui 已提交
2351
| bindArgs | Array&lt;[ValueType](#valuetype)&gt; | 是   | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数需为空数组。 |
L
query  
lihuihui 已提交
2352
| callback | AsyncCallback&lt;void&gt;            | 是   | 指定callback回调函数。                                       |
2353

2354 2355 2356 2357 2358 2359 2360 2361
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

2362 2363 2364
**示例:**

```js
L
RDB  
lihuihui 已提交
2365 2366
const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = ?"
store.executeSql(SQL_DELETE_TABLE, ['zhangsan'], function(err) {
2367
  if (err) {
G
ge-yafang 已提交
2368
    console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`);
2369 2370
    return;
  }
L
RDB  
lihuihui 已提交
2371
  console.info(`Delete table done.`);
2372 2373 2374
})
```

2375
### executeSql
2376 2377 2378 2379 2380 2381 2382 2383 2384

executeSql(sql: string, bindArgs?: Array&lt;ValueType&gt;):Promise&lt;void&gt;

执行包含指定参数但不返回值的SQL语句,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

L
query  
lihuihui 已提交
2385 2386 2387 2388
| 参数名   | 类型                                 | 必填 | 说明                                                         |
| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
| sql      | string                               | 是   | 指定要执行的SQL语句。                                        |
| bindArgs | Array&lt;[ValueType](#valuetype)&gt; | 否   | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 |
2389 2390 2391 2392 2393 2394 2395

**返回值**

| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |

2396 2397 2398 2399 2400 2401 2402 2403
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

2404 2405 2406
**示例:**

```js
L
RDB  
lihuihui 已提交
2407 2408
const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = 'zhangsan'"
let promise = store.executeSql(SQL_DELETE_TABLE);
2409
promise.then(() => {
L
RDB  
lihuihui 已提交
2410
    console.info(`Delete table done.`);
2411
}).catch((err) => {
G
ge-yafang 已提交
2412
    console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`);
2413 2414 2415
})
```

2416
### beginTransaction
2417 2418 2419 2420 2421 2422 2423

beginTransaction():void

在开始执行SQL语句之前,开始事务。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

2424 2425 2426 2427 2428 2429 2430 2431
**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**            |
| ------------ | ----------------------- |
| 14800047     | The WAL file size exceeds the default limit.|

2432 2433 2434 2435
**示例:**

```js
import featureAbility from '@ohos.ability.featureAbility'
2436 2437 2438 2439 2440 2441 2442
let context = featureAbility.getContext();
const STORE_CONFIG = { 
  name: "RdbTest.db",
  securityLevel: relationalStore.SecurityLevel.S1
};
relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) {
  if (err) {
G
ge-yafang 已提交
2443
    console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`);
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
    return;
  }
  store.beginTransaction();
  const valueBucket = {
    "name": "lisi",
	"age": 18,
	"salary": 100.5,
	"blobType": new Uint8Array([1, 2, 3]),
  };
  await store.insert("test", valueBucket);
  store.commit();
2455 2456 2457
})
```

2458
### commit
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469

commit():void

提交已执行的SQL语句。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**示例:**

```js
import featureAbility from '@ohos.ability.featureAbility'
2470 2471 2472 2473 2474 2475 2476
let context = featureAbility.getContext();
const STORE_CONFIG = { 
  name: "RdbTest.db",
  securityLevel: relationalStore.SecurityLevel.S1
};
relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) {
  if (err) {
G
ge-yafang 已提交
2477
     console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`);
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
     return;
  }
  store.beginTransaction();
  const valueBucket = {
	"name": "lisi",
	"age": 18,
	"salary": 100.5,
	"blobType": new Uint8Array([1, 2, 3]),
  };
  await store.insert("test", valueBucket);
  store.commit();
2489 2490 2491
})
```

2492
### rollBack
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503

rollBack():void

回滚已经执行的SQL语句。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**示例:**

```js
import featureAbility from '@ohos.ability.featureAbility'
2504 2505 2506 2507 2508 2509 2510
let context = featureAbility.getContext();
const STORE_CONFIG = { 
  name: "RdbTest.db",
  securityLevel: relationalStore.SecurityLevel.S1
};
relationalStore.getRdbStore(context, STORE_CONFIG, async function (err, store) {
  if (err) {
G
ge-yafang 已提交
2511
    console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`);
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525
    return;
  }
  try {
    store.beginTransaction()
    const valueBucket = {
	  "id": 1,
	  "name": "lisi",
	  "age": 18,
	  "salary": 100.5,
	  "blobType": new Uint8Array([1, 2, 3]),
	};
	await store.insert("test", valueBucket);
    store.commit();
  } catch (err) {
G
ge-yafang 已提交
2526
    console.error(`Transaction failed, code is ${err.code},message is ${err.message}`);
2527 2528
    store.rollBack();
  }
2529 2530 2531
})
```

2532
### backup
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549

backup(destName:string, callback: AsyncCallback&lt;void&gt;):void

以指定名称备份数据库,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明                     |
| -------- | ------------------------- | ---- | ------------------------ |
| destName | string                    | 是   | 指定数据库的备份文件名。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 指定callback回调函数。   |

**示例:**

```js
2550 2551
store.backup("dbBackup.db", function(err) {
  if (err) {
G
ge-yafang 已提交
2552
    console.error(`Backup failed, code is ${err.code},message is ${err.message}`);
2553 2554 2555
    return;
  }
  console.info(`Backup success.`);
2556 2557 2558
})
```

2559
### backup
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581

backup(destName:string): Promise&lt;void&gt;

以指定名称备份数据库,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型   | 必填 | 说明                     |
| -------- | ------ | ---- | ------------------------ |
| destName | string | 是   | 指定数据库的备份文件名。 |

**返回值**

| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |

**示例:**

```js
2582
let promiseBackup = store.backup("dbBackup.db");
2583
promiseBackup.then(()=>{
2584
  console.info(`Backup success.`);
2585
}).catch((err)=>{
G
ge-yafang 已提交
2586
  console.error(`Backup failed, code is ${err.code},message is ${err.message}`);
2587 2588 2589
})
```

2590
### restore
2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607

restore(srcName:string, callback: AsyncCallback&lt;void&gt;):void

从指定的数据库备份文件恢复数据库,使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明                     |
| -------- | ------------------------- | ---- | ------------------------ |
| srcName  | string                    | 是   | 指定数据库的备份文件名。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 指定callback回调函数。   |

**示例:**

```js
2608 2609
store.restore("dbBackup.db", function(err) {
  if (err) {
G
ge-yafang 已提交
2610
    console.error(`Restore failed, code is ${err.code},message is ${err.message}`);
2611 2612 2613
    return;
  }
  console.info(`Restore success.`);
2614 2615 2616
})
```

2617
### restore
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639

restore(srcName:string): Promise&lt;void&gt;

从指定的数据库备份文件恢复数据库,使用Promise异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名  | 类型   | 必填 | 说明                     |
| ------- | ------ | ---- | ------------------------ |
| srcName | string | 是   | 指定数据库的备份文件名。 |

**返回值**

| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |

**示例:**

```js
2640
let promiseRestore = store.restore("dbBackup.db");
2641
promiseRestore.then(()=>{
2642
  console.info(`Restore success.`);
2643
}).catch((err)=>{
G
ge-yafang 已提交
2644
  console.error(`Restore failed, code is ${err.code},message is ${err.message}`);
2645 2646 2647
})
```

2648
### setDistributedTables
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667

setDistributedTables(tables: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void

设置分布式列表,使用callback异步回调。

**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明                   |
| -------- | ------------------------- | ---- | ---------------------- |
| tables   | Array&lt;string&gt;       | 是   | 要设置的分布式列表表名 |
| callback | AsyncCallback&lt;void&gt; | 是   | 指定callback回调函数。 |

**示例:**

```js
2668 2669
store.setDistributedTables(["EMPLOYEE"], function (err) {
  if (err) {
G
ge-yafang 已提交
2670
    console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`);
2671 2672 2673
    return;
  }
  console.info(`SetDistributedTables successfully.`);
2674 2675 2676
})
```

2677
### setDistributedTables
2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701

 setDistributedTables(tables: Array&lt;string&gt;): Promise&lt;void&gt;

设置分布式列表,使用Promise异步回调。

**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型                | 必填 | 说明                     |
| ------ | ------------------- | ---- | ------------------------ |
| tables | Array&lt;string&gt; | 是   | 要设置的分布式列表表名。 |

**返回值**

| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |

**示例:**

```js
2702
let promise = store.setDistributedTables(["EMPLOYEE"]);
2703
promise.then(() => {
2704
  console.info(`SetDistributedTables successfully.`);
2705
}).catch((err) => {
G
ge-yafang 已提交
2706
  console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`);
2707 2708 2709
})
```

2710
### obtainDistributedTableName
2711 2712 2713

obtainDistributedTableName(device: string, table: string, callback: AsyncCallback&lt;string&gt;): void

L
device  
lihuihui 已提交
2714
根据远程设备的本地表名获取指定远程设备的分布式表名。在查询远程设备数据库时,需要使用分布式表名, 使用callback异步回调。
2715

L
device  
lihuihui 已提交
2716 2717 2718 2719
> **说明:**
>
> 其中device通过调用[deviceManager.getTrustedDeviceListSync](js-apis-device-manager.md#gettrusteddevicelistsync)方法得到。deviceManager模块的接口均为系统接口,仅系统应用可用。

L
device  
lihuihui 已提交
2720 2721 2722 2723
**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

2724 2725 2726 2727
**参数:**

| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
L
device  
lihuihui 已提交
2728
| device   | string                      | 是   | 远程设备ID 。                                                |
L
devices  
lihuihui 已提交
2729
| table    | string                      | 是   | 远程设备的本地表名。                                         |
2730 2731 2732 2733 2734
| callback | AsyncCallback&lt;string&gt; | 是   | 指定的callback回调函数。如果操作成功,返回远程设备的分布式表名。 |

**示例:**

```js
L
device  
lihuihui 已提交
2735 2736
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
2737
let deviceId = null;
L
device  
lihuihui 已提交
2738 2739 2740 2741 2742 2743 2744 2745

deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => {
    if (err) {
        console.log("create device manager failed, err=" + err);
        return;
    }
    dmInstance = manager;
    let devices = dmInstance.getTrustedDeviceListSync();
L
lihuihui 已提交
2746
    deviceId = devices[0].deviceId;
L
device  
lihuihui 已提交
2747 2748 2749
})

store.obtainDistributedTableName(deviceId, "EMPLOYEE", function (err, tableName) {
2750
    if (err) {
G
ge-yafang 已提交
2751
        console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`);
2752
        return;
2753
    }
2754
    console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`);
2755 2756 2757
})
```

2758
### obtainDistributedTableName
2759 2760 2761

 obtainDistributedTableName(device: string, table: string): Promise&lt;string&gt;

L
device  
lihuihui 已提交
2762
根据远程设备的本地表名获取指定远程设备的分布式表名。在查询远程设备数据库时,需要使用分布式表名,使用Promise异步回调。
2763

L
device  
lihuihui 已提交
2764 2765 2766 2767
> **说明:**
>
> 其中device通过调用[deviceManager.getTrustedDeviceListSync](js-apis-device-manager.md#gettrusteddevicelistsync)方法得到。deviceManager模块的接口均为系统接口,仅系统应用可用。

L
device  
lihuihui 已提交
2768 2769 2770 2771
**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

2772 2773
**参数:**

L
devices  
lihuihui 已提交
2774 2775
| 参数名 | 类型   | 必填 | 说明                 |
| ------ | ------ | ---- | -------------------- |
L
device  
lihuihui 已提交
2776
| device | string | 是   | 远程设备ID。         |
L
devices  
lihuihui 已提交
2777
| table  | string | 是   | 远程设备的本地表名。 |
2778 2779 2780 2781 2782 2783 2784 2785 2786 2787

**返回值**

| 类型                  | 说明                                                  |
| --------------------- | ----------------------------------------------------- |
| Promise&lt;string&gt; | Promise对象。如果操作成功,返回远程设备的分布式表名。 |

**示例:**

```js
L
device  
lihuihui 已提交
2788 2789
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
2790
let deviceId = null;
L
device  
lihuihui 已提交
2791 2792 2793 2794 2795 2796 2797 2798

deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => {
    if (err) {
        console.log("create device manager failed, err=" + err);
        return;
    }
    dmInstance = manager;
    let devices = dmInstance.getTrustedDeviceListSync();
L
lihuihui 已提交
2799
    deviceId = devices[0].deviceId;
L
device  
lihuihui 已提交
2800 2801 2802
})

let promise = store.obtainDistributedTableName(deviceId, "EMPLOYEE");
2803
promise.then((tableName) => {
2804
  console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`);
2805
}).catch((err) => {
G
ge-yafang 已提交
2806
  console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`);
2807 2808 2809
})
```

2810
### sync
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823

sync(mode: SyncMode, predicates: RdbPredicates, callback: AsyncCallback&lt;Array&lt;[string, number]&gt;&gt;): void

在设备之间同步数据, 使用callback异步回调。

**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名     | 类型                                               | 必填 | 说明                                                         |
| ---------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ |
2824 2825
| mode       | [SyncMode](#syncmode)                             | 是   | 指同步模式。该值可以是推、拉。                               |
| predicates | [RdbPredicates](#rdbpredicates)               | 是   | 约束同步数据和设备。                                         |
2826 2827 2828 2829 2830
| callback   | AsyncCallback&lt;Array&lt;[string, number]&gt;&gt; | 是   | 指定的callback回调函数,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 |

**示例:**

```js
L
device  
lihuihui 已提交
2831 2832
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
2833
let deviceIds = [];
L
device  
lihuihui 已提交
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846

deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => {
    if (err) {
        console.log("create device manager failed, err=" + err);
        return;
    }
    dmInstance = manager;
    let devices = dmInstance.getTrustedDeviceListSync();
    for (var i = 0; i < devices.length; i++) {
        deviceIds[i] = devices[i].deviceId;
    }
})

2847
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
L
device  
lihuihui 已提交
2848
predicates.inDevices(deviceIds);
2849 2850
store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates, function (err, result) {
  if (err) {
G
ge-yafang 已提交
2851
    console.error(`Sync failed, code is ${err.code},message is ${err.message}`);
2852 2853 2854 2855 2856 2857
    return;
  }
  console.info(`Sync done.`);
  for (let i = 0; i < result.length; i++) {
    console.info(`device= ${result[i][0]}, status= ${result[i][1]}`);
  }
2858 2859 2860
})
```

2861
### sync
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874

 sync(mode: SyncMode, predicates: RdbPredicates): Promise&lt;Array&lt;[string, number]&gt;&gt;

在设备之间同步数据,使用Promise异步回调。

**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名     | 类型                                 | 必填 | 说明                           |
| ---------- | ------------------------------------ | ---- | ------------------------------ |
2875 2876
| mode       | [SyncMode](#syncmode)               | 是   | 指同步模式。该值可以是推、拉。 |
| predicates | [RdbPredicates](#rdbpredicates) | 是   | 约束同步数据和设备。           |
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886

**返回值**

| 类型                                         | 说明                                                         |
| -------------------------------------------- | ------------------------------------------------------------ |
| Promise&lt;Array&lt;[string, number]&gt;&gt; | Promise对象,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 |

**示例:**

```js
L
device  
lihuihui 已提交
2887 2888
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
2889
let deviceIds = [];
L
device  
lihuihui 已提交
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902

deviceManager.createDeviceManager("com.example.appdatamgrverify", (err, manager) => {
    if (err) {
        console.log("create device manager failed, err=" + err);
        return;
    }
    dmInstance = manager;
    let devices = dmInstance.getTrustedDeviceListSync();
    for (var i = 0; i < devices.length; i++) {
        deviceIds[i] = devices[i].deviceId;
    }
})

2903
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
L
device  
lihuihui 已提交
2904
predicates.inDevices(deviceIds);
2905
let promise = store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates);
W
wangxiyue 已提交
2906
promise.then((result) =>{
2907
  console.info(`Sync done.`);
W
wangxiyue 已提交
2908
  for (let i = 0; i < result.length; i++) {
2909 2910
    console.info(`device= ${result[i][0]}, status= ${result[i][1]}`);
  }
2911
}).catch((err) => {
G
ge-yafang 已提交
2912
  console.error(`Sync failed, code is ${err.code},message is ${err.message}`);
2913 2914 2915
})
```

2916
### on('dataChange')
2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928

on(event: 'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;): void

注册数据库的观察者。当分布式数据库中的数据发生更改时,将调用回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                                | 必填 | 说明                                        |
| -------- | ----------------------------------- | ---- | ------------------------------------------- |
| event    | string                              | 是   | 取值为'dataChange',表示数据更改。          |
P
PaDaBoo 已提交
2929
| type     | [SubscribeType](#subscribetype)    | 是   | 订阅类型。 |
L
delete  
lihuihui 已提交
2930
| observer | Callback&lt;Array&lt;string&gt;&gt; | 是   | 指分布式数据库中数据更改事件的观察者。Array&lt;string>为数据库中的数据发生改变的对端设备ID。 |
2931 2932 2933 2934 2935

**示例:**

```js
function storeObserver(devices) {
2936 2937 2938
  for (let i = 0; i < devices.length; i++) {
    console.info(`device= ${devices[i]} data changed`);
  }
2939 2940
}
try {
2941
  store.on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
2942
} catch (err) {
G
ge-yafang 已提交
2943
  console.error(`Register observer failed, code is ${err.code},message is ${err.message}`);
2944 2945 2946
}
```

2947
### off('dataChange')
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957

off(event:'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;): void

从数据库中删除指定类型的指定观察者, 使用callback异步回调。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型                                | 必填 | 说明                                        |
P
PaDaBoo 已提交
2958
| -------- | ---------------------------------- | ---- | ------------------------------------------ |
2959
| event    | string                              | 是   | 取值为'dataChange',表示数据更改。          |
P
PaDaBoo 已提交
2960
| type     | [SubscribeType](#subscribetype)     | 是   | 订阅类型。                                 |
L
delete  
lihuihui 已提交
2961
| observer | Callback&lt;Array&lt;string&gt;&gt; | 是   | 指已注册的数据更改观察者。Array&lt;string>为数据库中的数据发生改变的对端设备ID。 |
2962 2963 2964 2965 2966

**示例:**

```js
function storeObserver(devices) {
2967 2968 2969
  for (let i = 0; i < devices.length; i++) {
    console.info(`device= ${devices[i]} data changed`);
  }
2970 2971
}
try {
2972
  store.off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
2973
} catch (err) {
G
ge-yafang 已提交
2974
  console.error(`Unregister observer failed, code is ${err.code},message is ${err.message}`);
2975 2976 2977
}
```

2978
## ResultSet
2979 2980 2981 2982 2983

提供通过查询数据库生成的数据库结果集的访问方法。结果集是指用户调用关系型数据库查询接口之后返回的结果集合,提供了多种灵活的数据访问方式,以便用户获取各项数据。

### 使用说明

2984
首先需要获取resultSet对象。
2985 2986

```js
W
wangxiyue 已提交
2987
let resultSet = null;
2988
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
2989
predicates.equalTo("AGE", 18);
2990
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
W
wangxiyue 已提交
2991 2992
promise.then((result) => {
  resultSet = result;
2993 2994
  console.info(`resultSet columnNames: ${resultSet.columnNames}`);
  console.info(`resultSet columnCount: ${resultSet.columnCount}`);
2995 2996 2997
});
```

2998
### 属性
2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

| 名称         | 类型            | 必填 | 说明                             |
| ------------ | ------------------- | ---- | -------------------------------- |
| columnNames  | Array&lt;string&gt; | 是   | 获取结果集中所有列的名称。       |
| columnCount  | number              | 是   | 获取结果集中的列数。             |
| rowCount     | number              | 是   | 获取结果集中的行数。             |
| rowIndex     | number              | 是   | 获取结果集当前行的索引。         |
| isAtFirstRow | boolean             | 是   | 检查结果集是否位于第一行。       |
| isAtLastRow  | boolean             | 是   | 检查结果集是否位于最后一行。     |
| isEnded      | boolean             | 是   | 检查结果集是否位于最后一行之后。 |
| isStarted    | boolean             | 是   | 检查指针是否移动过。             |
| isClosed     | boolean             | 是   | 检查当前结果集是否关闭。         |

3014
### getColumnIndex
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039

getColumnIndex(columnName: string): number

根据指定的列名获取列索引。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名     | 类型   | 必填 | 说明                       |
| ---------- | ------ | ---- | -------------------------- |
| columnName | string | 是   | 表示结果集中指定列的名称。 |

**返回值:**

| 类型   | 说明               |
| ------ | ------------------ |
| number | 返回指定列的索引。 |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
3040
| 14800013     | The column value is null or the column type is incompatible. |
3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051

**示例:**

  ```js
resultSet.goToFirstRow();
const id = resultSet.getLong(resultSet.getColumnIndex("ID"));
const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
  ```

3052
### getColumnName
3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077

getColumnName(columnIndex: number): string

根据指定的列索引获取列名。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名      | 类型   | 必填 | 说明                       |
| ----------- | ------ | ---- | -------------------------- |
| columnIndex | number | 是   | 表示结果集中指定列的索引。 |

**返回值:**

| 类型   | 说明               |
| ------ | ------------------ |
| string | 返回指定列的名称。 |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
3078
| 14800013     | The column value is null or the column type is incompatible. |
3079 3080 3081 3082 3083 3084 3085 3086 3087

**示例:**

  ```js
const id = resultSet.getColumnName(0);
const name = resultSet.getColumnName(1);
const age = resultSet.getColumnName(2);
  ```

3088
### goTo
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118

goTo(offset:number): boolean

向前或向后转至结果集的指定行,相对于其当前位置偏移。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名 | 类型   | 必填 | 说明                         |
| ------ | ------ | ---- | ---------------------------- |
| offset | number | 是   | 表示相对于当前位置的偏移量。 |

**返回值:**

| 类型    | 说明                                          |
| ------- | --------------------------------------------- |
| boolean | 如果成功移动结果集,则为true;否则返回false。 |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800012     | The result set is  empty or the specified location is invalid. |

**示例:**

  ```js
3119 3120
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise= store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3121
promise.then((resultSet) => {
3122 3123
  resultSet.goTo(1);
  resultSet.close();
3124
}).catch((err) => {
G
ge-yafang 已提交
3125
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3126 3127 3128
});
  ```

3129
### goToRow
3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159

goToRow(position: number): boolean

转到结果集的指定行。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名   | 类型   | 必填 | 说明                     |
| -------- | ------ | ---- | ------------------------ |
| position | number | 是   | 表示要移动到的指定位置。 |

**返回值:**

| 类型    | 说明                                          |
| ------- | --------------------------------------------- |
| boolean | 如果成功移动结果集,则为true;否则返回false。 |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800012     | The result set is  empty or the specified location is invalid. |

**示例:**

  ```js
3160 3161
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3162
promise.then((resultSet) => {
W
wangxiyue 已提交
3163
  resultSet.goToRow(5);
3164
  resultSet.close();
3165
}).catch((err) => {
G
ge-yafang 已提交
3166
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3167 3168 3169
});
  ```

3170
### goToFirstRow
3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195

goToFirstRow(): boolean


转到结果集的第一行。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**返回值:**

| 类型    | 说明                                          |
| ------- | --------------------------------------------- |
| boolean | 如果成功移动结果集,则为true;否则返回false。 |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800012     | The result set is  empty or the specified location is invalid. |

**示例:**

  ```js
3196 3197
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3198
promise.then((resultSet) => {
3199 3200
  resultSet.goToFirstRow();
  resultSet.close();
3201
}).catch((err) => {
G
ge-yafang 已提交
3202
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3203 3204 3205
});
  ```

3206
### goToLastRow
3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230

goToLastRow(): boolean

转到结果集的最后一行。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**返回值:**

| 类型    | 说明                                          |
| ------- | --------------------------------------------- |
| boolean | 如果成功移动结果集,则为true;否则返回false。 |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800012     | The result set is  empty or the specified location is invalid. |

**示例:**

  ```js
3231 3232
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3233
promise.then((resultSet) => {
3234 3235
  resultSet.goToLastRow();
  resultSet.close();
3236
}).catch((err) => {
G
ge-yafang 已提交
3237
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3238 3239 3240
});
  ```

3241
### goToNextRow
3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265

goToNextRow(): boolean

转到结果集的下一行。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**返回值:**

| 类型    | 说明                                          |
| ------- | --------------------------------------------- |
| boolean | 如果成功移动结果集,则为true;否则返回false。 |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800012     | The result set is  empty or the specified location is invalid. |

**示例:**

  ```js
3266 3267
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3268
promise.then((resultSet) => {
3269 3270
  resultSet.goToNextRow();
  resultSet.close();
3271
}).catch((err) => {
G
ge-yafang 已提交
3272
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3273 3274 3275
});
  ```

3276
### goToPreviousRow
3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300

goToPreviousRow(): boolean

转到结果集的上一行。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**返回值:**

| 类型    | 说明                                          |
| ------- | --------------------------------------------- |
| boolean | 如果成功移动结果集,则为true;否则返回false。 |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800012     | The result set is  empty or the specified location is invalid. |

**示例:**

  ```js
3301 3302
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3303
promise.then((resultSet) => {
3304 3305
  resultSet.goToPreviousRow();
  resultSet.close();
3306
}).catch((err) => {
G
ge-yafang 已提交
3307
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3308 3309 3310
});
  ```

3311
### getBlob
3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336

getBlob(columnIndex: number): Uint8Array

以字节数组的形式获取当前行中指定列的值。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名      | 类型   | 必填 | 说明                    |
| ----------- | ------ | ---- | ----------------------- |
| columnIndex | number | 是   | 指定的列索引,从0开始。 |

**返回值:**

| 类型       | 说明                             |
| ---------- | -------------------------------- |
| Uint8Array | 以字节数组的形式返回指定列的值。 |

**示例:**

  ```js
const codes = resultSet.getBlob(resultSet.getColumnIndex("CODES"));
  ```

3337
### getString
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362

getString(columnIndex: number): string

以字符串形式获取当前行中指定列的值。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名      | 类型   | 必填 | 说明                    |
| ----------- | ------ | ---- | ----------------------- |
| columnIndex | number | 是   | 指定的列索引,从0开始。 |

**返回值:**

| 类型   | 说明                         |
| ------ | ---------------------------- |
| string | 以字符串形式返回指定列的值。 |

**示例:**

  ```js
const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
  ```

3363
### getLong
3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378

getLong(columnIndex: number): number

以Long形式获取当前行中指定列的值。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名      | 类型   | 必填 | 说明                    |
| ----------- | ------ | ---- | ----------------------- |
| columnIndex | number | 是   | 指定的列索引,从0开始。 |

**返回值:**

L
int64  
lihuihui 已提交
3379 3380 3381
| 类型   | 说明                                                         |
| ------ | ------------------------------------------------------------ |
| number | 以Long形式返回指定列的值。<br>该接口支持的数据范围是:Number.MIN_SAFE_INTEGER ~ Number.MAX_SAFE_INTEGER,若超出该范围,建议使用[getDouble](#getdouble)。 |
3382 3383 3384 3385 3386 3387 3388

**示例:**

  ```js
const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
  ```

3389
### getDouble
3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414

getDouble(columnIndex: number): number

以double形式获取当前行中指定列的值。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名      | 类型   | 必填 | 说明                    |
| ----------- | ------ | ---- | ----------------------- |
| columnIndex | number | 是   | 指定的列索引,从0开始。 |

**返回值:**

| 类型   | 说明                         |
| ------ | ---------------------------- |
| number | 以double形式返回指定列的值。 |

**示例:**

  ```js
const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
  ```

3415
### isColumnNull
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440

isColumnNull(columnIndex: number): boolean

检查当前行中指定列的值是否为null。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

| 参数名      | 类型   | 必填 | 说明                    |
| ----------- | ------ | ---- | ----------------------- |
| columnIndex | number | 是   | 指定的列索引,从0开始。 |

**返回值:**

| 类型    | 说明                                                      |
| ------- | --------------------------------------------------------- |
| boolean | 如果当前行中指定列的值为null,则返回true,否则返回false。 |

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
3441
| 14800013     | The column value is null or the column type is incompatible. |
3442 3443 3444 3445 3446 3447 3448

**示例:**

  ```js
const isColumnNull = resultSet.isColumnNull(resultSet.getColumnIndex("CODES"));
  ```

3449
### close
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459

close(): void

关闭结果集。

**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**示例:**

  ```js
3460 3461
let predicatesClose = new relationalStore.RdbPredicates("EMPLOYEE");
let promiseClose = store.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3462
promiseClose.then((resultSet) => {
3463
  resultSet.close();
3464
}).catch((err) => {
G
ge-yafang 已提交
3465
  console.error(`resultset close failed, code is ${err.code},message is ${err.message}`);
3466 3467 3468 3469 3470 3471 3472 3473 3474 3475
});
  ```

**错误码:**

以下错误码的详细介绍请参见[关系型数据库错误码](../errorcodes/errorcode-data-rdb.md)

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800012     | The result set is  empty or the specified location is invalid. |