js-apis-data-relationalStore.md 153.6 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

**错误码:**

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

L
LiRui 已提交
41 42 43 44 45
| **错误码ID** | **错误信息**                                                |
| ------------ | ----------------------------------------------------------- |
| 14800010     | Failed to open or delete database by invalid database path. |
| 14800011     | Failed to open database by database corrupted.              |
| 14800000     | Inner error.                                                |
46 47 48 49 50 51

**示例:**

FA模型示例:

```js
52

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

55 56
var store;

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

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

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

Stage模型示例:

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

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

100
## relationalStore.getRdbStore
101

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

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

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

**参数:**

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

**返回值**

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

**错误码:**

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

L
LiRui 已提交
125 126 127 128 129
| **错误码ID** | **错误信息**                                                |
| ------------ | ----------------------------------------------------------- |
| 14800010     | Failed to open or delete database by invalid database path. |
| 14800011     | Failed to open database by database corrupted.              |
| 14800000     | Inner error.                                                |
130 131 132 133 134 135 136

**示例:**

FA模型示例:

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

138 139
var store;

140
// 获取context
141
let context = featureAbility.getContext();
142 143

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

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

Stage模型示例:

```ts
160
import UIAbility from '@ohos.app.ability.UIAbility'
161 162

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

181
## relationalStore.deleteRdbStore
182 183 184 185 186 187 188 189 190 191 192

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

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

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

**参数:**

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

**错误码:**

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

L
LiRui 已提交
201 202 203 204
| **错误码ID** | **错误信息**                                                |
| ------------ | ----------------------------------------------------------- |
| 14800010     | Failed to open or delete database by invalid database path. |
| 14800000     | Inner error.                                                |
205 206 207 208 209 210 211

**示例:**

FA模型示例:

```js
import featureAbility from '@ohos.ability.featureAbility'
212 213

// 获取context
214 215
let context = featureAbility.getContext()

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

Stage模型示例:

```ts
228
import UIAbility from '@ohos.app.ability.UIAbility'
229 230

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

243
## relationalStore.deleteRdbStore
244 245 246 247 248 249 250 251 252 253 254

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

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

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

**参数**

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

**返回值**

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

**错误码:**

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

L
LiRui 已提交
268 269 270 271
| **错误码ID** | **错误信息**                                                |
| ------------ | ----------------------------------------------------------- |
| 14800010     | Failed to open or delete database by invalid database path. |
| 14800000     | Inner error.                                                |
272 273 274 275 276 277 278

**示例:**

FA模型示例:

```js
import featureAbility from '@ohos.ability.featureAbility'
279 280

// 获取context
281
let context = featureAbility.getContext();
282

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

Stage模型示例:

```ts
294
import UIAbility from '@ohos.app.ability.UIAbility'
295 296

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

308
## StoreConfig
309 310 311 312 313 314 315 316

管理关系数据库配置。

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

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

320
## SecurityLevel
321 322 323

数据库的安全级别枚举。

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

328 329 330 331 332 333 334 335 336
**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

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

R
renjiecui 已提交
337 338
## AssetStatus<sup>10+</sup>

R
renjiecui 已提交
339
描述资产附件的状态枚举。请使用枚举名称而非枚举值。
R
renjiecui 已提交
340 341 342 343 344

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

| 名称                              | 值   | 说明             |
| ------------------------------- | --- | -------------- |
R
renjiecui 已提交
345
| ASSET_NORMAL     | -   | 表示资产状态正常。      |
R
renjiecui 已提交
346
| ASSET_INSERT | - | 表示资产需要插入到云端。 |
R
renjiecui 已提交
347
| ASSET_UPDATE | - | 表示资产需要更新到云端。 |
R
renjiecui 已提交
348
| ASSET_DELETE | - | 表示资产需要在云端删除。 |
R
renjiecui 已提交
349 350
| ASSET_ABNORMAL    | -   | 表示资产状态异常。      |
| ASSET_DOWNLOADING | -   | 表示资产正在下载到本地设备。 |
R
renjiecui 已提交
351 352 353

## Asset<sup>10+</sup>

R
renjiecui 已提交
354
记录资产附件(文件、图片、视频等类型文件)的相关信息。资产类型的相关接口暂不支持Datashare。
R
renjiecui 已提交
355 356 357 358 359 360

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

| 名称          | 类型                          | 必填  | 说明           |
| ----------- | --------------------------- | --- | ------------ |
| name        | string                      | 是   | 资产的名称。       |
R
renjiecui 已提交
361 362
| uri         | string                      | 是   | 资产的uri,在系统里的绝对路径。       |
| path        | string                      | 是   | 资产在应用沙箱里的路径。       |
R
renjiecui 已提交
363
| createTime  | string                      | 是   | 资产被创建出来的时间。   |
R
renjiecui 已提交
364
| modifyTime  | string                      | 是   | 资产最后一次被修改的时间。 |
R
renjiecui 已提交
365 366
| size        | string                      | 是   | 资产占用空间的大小。    |
| status      | [AssetStatus](#assetstatus10) | 否   | 资产的状态,默认值为ASSET_NORMAL。        |
R
renjiecui 已提交
367 368 369

## Assets<sup>10+</sup>

R
renjiecui 已提交
370
表示[Asset](#asset10)类型的数组。
R
renjiecui 已提交
371 372 373

| 类型    | 说明                 |
| ------- | -------------------- |
R
renjiecui 已提交
374
| [Asset](#asset10)[] | 表示Asset类型的数组。   |
R
renjiecui 已提交
375

376
## ValueType
377 378 379 380 381 382 383

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

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

| 类型    | 说明                 |
| ------- | -------------------- |
R
renjiecui 已提交
384
| null<sup>10+</sup>    | 表示值类型为空。   |
385 386 387
| number  | 表示值类型为数字。   |
| string  | 表示值类型为字符。   |
| boolean | 表示值类型为布尔值。 |
R
renjiecui 已提交
388
| Uint8Array<sup>10+</sup>           | 表示值类型为Uint8类型的数组。            |
R
renjiecui 已提交
389
| Asset<sup>10+</sup>  | 表示值类型为附件[Asset](#asset10)。     |
R
renjiecui 已提交
390
| Assets<sup>10+</sup> | 表示值类型为附件数组[Assets](#assets10)。 |
391

392
## ValuesBucket
393

W
wangdengze 已提交
394
用于存储键值对的类型。该类型不是多线程安全的,如果应用中存在多线程同时操作该类派生出的实例,注意加锁保护。
395 396 397

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

R
renjiecui 已提交
398 399 400
| 键类型 | 值类型                   |
| ------ | ----------------------- |
| string | [ValueType](#valuetype) |
401

402
## SyncMode
403 404 405 406 407

指数据库同步模式。

| 名称           | 值   | 说明                               |
| -------------- | ---- | ---------------------------------- |
R
renjiecui 已提交
408 409
| SYNC_MODE_PUSH                       | 0   | 表示数据从本地设备推送到远程设备。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core                     |
| SYNC_MODE_PULL                       | 1   | 表示数据从远程设备拉至本地设备。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core                      |
R
renjiecui 已提交
410 411 412
| SYNC_MODE_TIME_FIRST<sup>10+</sup>   | -   | 表示数据从修改时间较近的一端同步到修改时间较远的一端。请使用枚举名称而非枚举值。暂不支持端云手动同步。 <br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client |
| SYNC_MODE_NATIVE_FIRST<sup>10+</sup> | -   | 表示数据从本地设备同步到云端。请使用枚举名称而非枚举值。暂不支持端云手动同步。 <br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client             |
| SYNC_MODE_CLOUD_FIRST<sup>10+</sup>  | -   | 表示数据从云端同步到本地设备。请使用枚举名称而非枚举值。暂不支持端云手动同步。 <br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client             |
413

414
## SubscribeType
415 416 417 418 419 420 421

描述订阅类型。

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

| 名称                  | 值   | 说明               |
| --------------------- | ---- | ------------------ |
L
LiRui 已提交
422 423 424
| SUBSCRIBE_TYPE_REMOTE | 0    | 订阅远程数据更改。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core |
| SUBSCRIBE_TYPE_CLOUD<sup>10+</sup> | -  | 订阅云端数据更改。请使用枚举名称而非枚举值。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client |
| SUBSCRIBE_TYPE_CLOUD_DETAILS<sup>10+</sup> | -  | 订阅云端数据更改详情。请使用枚举名称而非枚举值。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client |
425

R
renjiecui 已提交
426 427
## ChangeType<sup>10+</sup>

R
renjiecui 已提交
428
描述数据变更类型的枚举。请使用枚举名称而非枚举值。
R
renjiecui 已提交
429 430 431

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

R
renjiecui 已提交
432 433
**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

R
renjiecui 已提交
434 435
| 名称                         | 值   | 说明                         |
| -------------------------- | --- | -------------------------- |
R
renjiecui 已提交
436 437
| DATA_CHANGE  | -   | 表示是数据发生变更。   |
| ASSET_CHANGE | -   | 表示是资产附件发生了变更。 |
R
renjiecui 已提交
438 439 440 441 442 443 444 445 446

## ChangeInfo<sup>10+</sup>

记录端云同步过程详情。

**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client

| 名称       | 类型                                 | 必填  | 说明                                                                                                                   |
| -------- | ---------------------------------- | --- | -------------------------------------------------------------------------------------------------------------------- |
R
renjiecui 已提交
447
| table    | string                             | 是   | 表示发生变化的表的名称。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core                                   |
R
renjiecui 已提交
448
| type     | [ChangeType](#changetype10)          | 是   | 表示发生变化的数据的类型,数据或者资产附件发生变化。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core  |
R
renjiecui 已提交
449 450 451
| inserted | Array\<string\> \| Array\<number\> | 是   | 记录插入数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示插入数据的行号。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core  |
| updated  | Array\<string\> \| Array\<number\> | 是   | 记录更新数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示更新数据的行号。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core  |
| deleted  | Array\<string\> \| Array\<number\> | 是   | 记录删除数据的位置,如果该表的主键是string类型,该值是主键的值,否则该值表示删除数据的行号。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core  |
R
renjiecui 已提交
452 453 454

## DistributedType<sup>10+</sup>

R
renjiecui 已提交
455
描述表的分布式类型的枚举。请使用枚举名称而非枚举值。
R
renjiecui 已提交
456

R
renjiecui 已提交
457 458
**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

R
renjiecui 已提交
459 460
| 名称                | 值   | 说明                                                                                                 |
| ------------------ | --- | -------------------------------------------------------------------------------------------------- |
R
renjiecui 已提交
461 462
| DISTRIBUTED_DEVICE | -  | 表示在不同设备之间分布式的数据库表。<br>**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core               |
| DISTRIBUTED_CLOUD  | -   | 表示在设备和云端之间分布式的数据库表。<br>**系统能力:** SystemCapability.DistributedDataManager.CloudSync.Client |
R
renjiecui 已提交
463 464 465 466 467 468 469

## DistributedConfig<sup>10+</sup>

记录表的分布式配置信息。

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

R
renjiecui 已提交
470 471
| 名称     | 类型    | 必填 | 说明                                                         |
| -------- | ------- | ---- | ------------------------------------------------------------ |
R
renjiecui 已提交
472
| autoSync | boolean | 是   | 该值为true时,表示该表支持自动同步和手动同步;该值为false时,表示该表只支持手动同步,不支持自动同步。 |
R
renjiecui 已提交
473

L
lihuihui 已提交
474 475 476 477 478 479 480 481
## ConflictResolution<sup>10+</sup>

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

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

| 名称                 | 值   | 说明                                                         |
| -------------------- | ---- | ------------------------------------------------------------ |
P
PaDaBoo 已提交
482
| ON_CONFLICT_NONE | 0 | 表示当冲突发生时,不做任何处理。 |
L
lihuihui 已提交
483 484 485 486 487 488
| 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    | 表示当冲突发生时,在插入或更新当前行之前删除导致约束违例的预先存在的行,并且命令会继续正常执行。 |

489
## RdbPredicates
490

W
wangdengze 已提交
491
表示关系型数据库(RDB)的谓词。该类确定RDB中条件表达式的值是true还是false。该类型不是多线程安全的,如果应用中存在多线程同时操作该类派生出的实例,注意加锁保护。
492

493
### constructor
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

constructor(name: string)

构造函数。

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

**参数:**

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

**示例:**

```js
510
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
511 512
```

513
### inDevices
514 515 516 517 518

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

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

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

523 524 525 526 527 528 529 530 531 532 533 534
**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core

**参数:**

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

**返回值**

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

**示例:**

```js
L
device  
lihuihui 已提交
540 541
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
542
let deviceIds = [];
L
device  
lihuihui 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555

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;
    }
})
                                  
556
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
L
device  
lihuihui 已提交
557
predicates.inDevices(deviceIds);
558 559
```

560
### inAllDevices
561 562 563 564 565 566 567 568 569 570 571 572

inAllDevices(): RdbPredicates


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

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
573
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
574 575 576 577

**示例:**

```js
578 579
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.inAllDevices();
580 581
```

582
### equalTo
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601

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


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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
602
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
603 604 605 606

**示例:**

```js
607 608
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "lisi");
609 610 611
```


612
### notEqualTo
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

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


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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
632
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
633 634 635 636

**示例:**

```js
637 638
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.notEqualTo("NAME", "lisi");
639 640 641
```


642
### beginWrap
643 644 645 646 647 648 649 650 651 652 653 654

beginWrap(): RdbPredicates


向谓词添加左括号。

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

**返回值**

| 类型                                 | 说明                      |
| ------------------------------------ | ------------------------- |
655
| [RdbPredicates](#rdbpredicates) | 返回带有左括号的Rdb谓词。 |
656 657 658 659

**示例:**

```js
660
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
661 662 663 664 665 666 667 668
predicates.equalTo("NAME", "lisi")
    .beginWrap()
    .equalTo("AGE", 18)
    .or()
    .equalTo("SALARY", 200.5)
    .endWrap()
```

669
### endWrap
670 671 672 673 674 675 676 677 678 679 680

endWrap(): RdbPredicates

向谓词添加右括号。

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

**返回值**

| 类型                                 | 说明                      |
| ------------------------------------ | ------------------------- |
681
| [RdbPredicates](#rdbpredicates) | 返回带有右括号的Rdb谓词。 |
682 683 684 685

**示例:**

```js
686
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
687 688 689 690 691 692 693 694
predicates.equalTo("NAME", "lisi")
    .beginWrap()
    .equalTo("AGE", 18)
    .or()
    .equalTo("SALARY", 200.5)
    .endWrap()
```

695
### or
696 697 698 699 700 701 702 703 704 705 706

or(): RdbPredicates

将或条件添加到谓词中。

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

**返回值**

| 类型                                 | 说明                      |
| ------------------------------------ | ------------------------- |
707
| [RdbPredicates](#rdbpredicates) | 返回带有或条件的Rdb谓词。 |
708 709 710 711

**示例:**

```js
712
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
713 714 715 716 717
predicates.equalTo("NAME", "Lisa")
    .or()
    .equalTo("NAME", "Rose")
```

718
### and
719 720 721 722 723 724 725 726 727 728 729

and(): RdbPredicates

向谓词添加和条件。

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

**返回值**

| 类型                                 | 说明                      |
| ------------------------------------ | ------------------------- |
730
| [RdbPredicates](#rdbpredicates) | 返回带有和条件的Rdb谓词。 |
731 732 733 734

**示例:**

```js
735
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
736 737 738 739 740
predicates.equalTo("NAME", "Lisa")
    .and()
    .equalTo("SALARY", 200.5)
```

741
### contains
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
760
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
761 762 763 764

**示例:**

```js
765 766
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.contains("NAME", "os");
767 768
```

769
### beginsWith
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
788
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
789 790 791 792

**示例:**

```js
793 794
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.beginsWith("NAME", "os");
795 796
```

797
### endsWith
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
816
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
817 818 819 820

**示例:**

```js
821 822
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.endsWith("NAME", "se");
823 824
```

825
### isNull
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842

isNull(field: string): RdbPredicates

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
843
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
844 845 846 847

**示例**

```js
848 849
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.isNull("NAME");
850 851
```

852
### isNotNull
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869

isNotNull(field: string): RdbPredicates

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
870
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
871 872 873 874

**示例:**

```js
875 876
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.isNotNull("NAME");
877 878
```

879
### like
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
898
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
899 900 901 902

**示例:**

```js
903 904
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.like("NAME", "%os%");
905 906
```

907
### glob
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
926
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
927 928 929 930

**示例:**

```js
931 932
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.glob("NAME", "?h*g");
933 934
```

935
### between
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
955
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
956 957 958 959

**示例:**

```js
960 961
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.between("AGE", 10, 50);
962 963
```

964
### notBetween
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
984
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
985 986 987 988

**示例:**

```js
989 990
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.notBetween("AGE", 10, 50);
991 992
```

993
### greaterThan
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1012
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1013 1014 1015 1016

**示例:**

```js
1017 1018
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.greaterThan("AGE", 18);
1019 1020
```

1021
### lessThan
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1040
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1041 1042 1043 1044

**示例:**

```js
1045 1046
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.lessThan("AGE", 20);
1047 1048
```

1049
### greaterThanOrEqualTo
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1068
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1069 1070 1071 1072

**示例:**

```js
1073 1074
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.greaterThanOrEqualTo("AGE", 18);
1075 1076
```

1077
### lessThanOrEqualTo
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1096
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1097 1098 1099 1100

**示例:**

```js
1101 1102
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.lessThanOrEqualTo("AGE", 20);
1103 1104
```

1105
### orderByAsc
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122

orderByAsc(field: string): RdbPredicates

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1123
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1124 1125 1126 1127

**示例:**

```js
1128 1129
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.orderByAsc("NAME");
1130 1131
```

1132
### orderByDesc
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149

orderByDesc(field: string): RdbPredicates

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1150
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1151 1152 1153 1154

**示例:**

```js
1155 1156
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.orderByDesc("AGE");
1157 1158
```

1159
### distinct
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

distinct(): RdbPredicates

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

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

**返回值**

| 类型                                 | 说明                           |
| ------------------------------------ | ------------------------------ |
1171
| [RdbPredicates](#rdbpredicates) | 返回可用于过滤重复记录的谓词。 |
1172 1173 1174 1175

**示例:**

```js
1176 1177
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose").distinct();
1178 1179
```

1180
### limitAs
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

limitAs(value: number): RdbPredicates

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                                 |
| ------------------------------------ | ------------------------------------ |
1198
| [RdbPredicates](#rdbpredicates) | 返回可用于设置最大数据记录数的谓词。 |
1199 1200 1201 1202

**示例:**

```js
1203 1204
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose").limitAs(3);
1205 1206
```

1207
### offsetAs
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224

offsetAs(rowOffset: number): RdbPredicates

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                                 |
| ------------------------------------ | ------------------------------------ |
1225
| [RdbPredicates](#rdbpredicates) | 返回具有指定返回结果起始位置的谓词。 |
1226 1227 1228 1229

**示例:**

```js
1230 1231
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose").offsetAs(3);
1232 1233
```

1234
### groupBy
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                   |
| ------------------------------------ | ---------------------- |
1252
| [RdbPredicates](#rdbpredicates) | 返回分组查询列的谓词。 |
1253 1254 1255 1256

**示例:**

```js
1257 1258
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.groupBy(["AGE", "NAME"]);
1259 1260
```

1261
### indexedBy
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279

indexedBy(field: string): RdbPredicates

配置RdbPredicates以指定索引列。

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

**参数:**

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

**返回值**


| 类型                                 | 说明                                  |
| ------------------------------------ | ------------------------------------- |
1280
| [RdbPredicates](#rdbpredicates) | 返回具有指定索引列的RdbPredicates。 |
1281 1282 1283 1284

**示例:**

```js
1285 1286
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.indexedBy("SALARY_INDEX");
1287 1288
```

1289
### in
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1308
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1309 1310 1311 1312

**示例:**

```js
1313 1314
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.in("AGE", [18, 20]);
1315 1316
```

1317
### notIn
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335

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

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

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

**参数:**

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

**返回值**

| 类型                                 | 说明                       |
| ------------------------------------ | -------------------------- |
1336
| [RdbPredicates](#rdbpredicates) | 返回与指定字段匹配的谓词。 |
1337 1338 1339 1340

**示例:**

```js
1341 1342
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.notIn("NAME", ["Lisa", "Rose"]);
1343 1344
```

1345
## RdbStore
1346 1347 1348

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

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

1351
### 属性<sup>10+</sup>
L
leiiyb 已提交
1352 1353 1354 1355 1356

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

| 名称         | 类型            | 必填 | 说明                             |
| ------------ | ----------- | ---- | -------------------------------- |
1357 1358 1359 1360 1361 1362
| version<sup>10+</sup>  | number | 是   | 设置和获取数据库版本,值为大于0的正整数。       |

**示例:**

```js
// 设置数据库版本
1363
store.version = 3;
1364
// 获取数据库版本
1365
console.info(`RdbStore version is ${store.version}`);
1366
```
L
leiiyb 已提交
1367

1368
### insert
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

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。 |

1384 1385 1386 1387
**错误码:**

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

L
LiRui 已提交
1388 1389 1390 1391
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1392

1393 1394 1395 1396
**示例:**

```js
const valueBucket = {
1397 1398 1399 1400 1401 1402 1403
  "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 已提交
1404
    console.error(`Insert is failed, code is ${err.code},message is ${err.message}`);
1405 1406 1407
    return;
  }
  console.info(`Insert is successful, rowId = ${rowId}`);
1408 1409 1410
})
```

L
lihuihui 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
### 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 已提交
1425
| conflict | [ConflictResolution](#conflictresolution10) | 是   | 指定冲突解决方式。                                         |
L
lihuihui 已提交
1426 1427
| callback | AsyncCallback&lt;number&gt;                 | 是   | 指定callback回调函数。如果操作成功,返回行ID;否则返回-1。 |

1428 1429 1430 1431
**错误码:**

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

L
LiRui 已提交
1432 1433 1434 1435
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1436

L
lihuihui 已提交
1437 1438 1439 1440
**示例:**

```js
const valueBucket = {
1441 1442 1443 1444 1445 1446 1447
  "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 已提交
1448
    console.error(`Insert is failed, code is ${err.code},message is ${err.message}`);
1449 1450 1451
    return;
  }
  console.info(`Insert is successful, rowId = ${rowId}`);
L
lihuihui 已提交
1452 1453 1454
})
```

1455
### insert
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475

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。 |

1476 1477 1478 1479
**错误码:**

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

L
LiRui 已提交
1480 1481 1482 1483
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1484

1485 1486 1487 1488
**示例:**

```js
const valueBucket = {
1489 1490 1491 1492 1493 1494
  "NAME": "Lisa",
  "AGE": 18,
  "SALARY": 100.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5]),
};
let promise = store.insert("EMPLOYEE", valueBucket);
1495
promise.then((rowId) => {
1496 1497
  console.info(`Insert is successful, rowId = ${rowId}`);
}).catch((err) => {
G
ge-yafang 已提交
1498
  console.error(`Insert is failed, code is ${err.code},message is ${err.message}`);
1499 1500 1501
})
```

L
lihuihui 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
### 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 已提交
1516
| conflict | [ConflictResolution](#conflictresolution10) | 是   | 指定冲突解决方式。         |
L
lihuihui 已提交
1517 1518 1519 1520 1521 1522 1523

**返回值**

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

1524 1525 1526 1527
**错误码:**

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

L
LiRui 已提交
1528 1529 1530 1531
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1532

L
lihuihui 已提交
1533 1534 1535 1536
**示例:**

```js
const valueBucket = {
1537 1538 1539 1540 1541 1542
  "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 已提交
1543
promise.then((rowId) => {
1544 1545
  console.info(`Insert is successful, rowId = ${rowId}`);
}).catch((err) => {
G
ge-yafang 已提交
1546
  console.error(`Insert is failed, code is ${err.code},message is ${err.message}`);
L
lihuihui 已提交
1547 1548 1549
})
```

1550
### batchInsert
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565

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。 |

1566 1567 1568 1569
**错误码:**

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

L
LiRui 已提交
1570 1571 1572 1573
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1574

1575 1576 1577 1578
**示例:**

```js
const valueBucket1 = {
1579 1580 1581 1582 1583
  "NAME": "Lisa",
  "AGE": 18,
  "SALARY": 100.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5])
};
1584
const valueBucket2 = {
1585 1586 1587 1588 1589
  "NAME": "Jack",
  "AGE": 19,
  "SALARY": 101.5,
  "CODES": new Uint8Array([6, 7, 8, 9, 10])
};
1590
const valueBucket3 = {
1591 1592 1593 1594 1595
  "NAME": "Tom",
  "AGE": 20,
  "SALARY": 102.5,
  "CODES": new Uint8Array([11, 12, 13, 14, 15])
};
1596 1597

let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3);
1598 1599
store.batchInsert("EMPLOYEE", valueBuckets, function(err, insertNum) {
  if (err) {
G
ge-yafang 已提交
1600
    console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`);
1601 1602 1603
    return;
  }
  console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`);
1604 1605 1606
})
```

1607
### batchInsert
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627

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。 |

1628 1629 1630 1631
**错误码:**

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

L
LiRui 已提交
1632 1633 1634 1635
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1636

1637 1638 1639 1640
**示例:**

```js
const valueBucket1 = {
1641 1642 1643 1644 1645
  "NAME": "Lisa",
  "AGE": 18,
  "SALARY": 100.5,
  "CODES": new Uint8Array([1, 2, 3, 4, 5])
};
1646
const valueBucket2 = {
1647 1648 1649 1650 1651
  "NAME": "Jack",
  "AGE": 19,
  "SALARY": 101.5,
  "CODES": new Uint8Array([6, 7, 8, 9, 10])
};
1652
const valueBucket3 = {
1653 1654 1655 1656 1657
  "NAME": "Tom",
  "AGE": 20,
  "SALARY": 102.5,
  "CODES": new Uint8Array([11, 12, 13, 14, 15])
};
1658 1659

let valueBuckets = new Array(valueBucket1, valueBucket2, valueBucket3);
1660
let promise = store.batchInsert("EMPLOYEE", valueBuckets);
1661
promise.then((insertNum) => {
1662 1663
  console.info(`batchInsert is successful, the number of values that were inserted = ${insertNum}`);
}).catch((err) => {
G
ge-yafang 已提交
1664
  console.error(`batchInsert is failed, code is ${err.code},message is ${err.message}`);
1665 1666 1667
})
```

1668
### update
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680

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

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

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

**参数:**

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

1684 1685 1686 1687
**错误码:**

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

L
LiRui 已提交
1688 1689 1690 1691
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1692

1693 1694 1695 1696
**示例:**

```js
const valueBucket = {
1697 1698 1699 1700 1701 1702 1703 1704 1705
  "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 已提交
1706
    console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
1707 1708 1709
    return;
  }
  console.info(`Updated row count: ${rows}`);
1710 1711 1712
})
```

L
lihuihui 已提交
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
### 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指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 |
1726
| predicates | [RdbPredicates](#rdbpredicates)            | 是   | RdbPredicates的实例对象指定的更新条件。                      |
P
PaDaBoo 已提交
1727
| conflict   | [ConflictResolution](#conflictresolution10) | 是   | 指定冲突解决方式。                                           |
L
lihuihui 已提交
1728 1729
| callback   | AsyncCallback&lt;number&gt;                 | 是   | 指定的callback回调方法。返回受影响的行数。                   |

1730 1731 1732 1733
**错误码:**

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

L
LiRui 已提交
1734 1735 1736 1737
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1738

L
lihuihui 已提交
1739 1740 1741 1742
**示例:**

```js
const valueBucket = {
1743 1744 1745 1746 1747 1748 1749 1750 1751
  "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 已提交
1752
    console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
1753 1754 1755
    return;
  }
  console.info(`Updated row count: ${rows}`);
L
lihuihui 已提交
1756 1757 1758
})
```

1759
### update
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771

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

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

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

**参数:**

| 参数名       | 类型                                 | 必填 | 说明                                                         |
| ------------ | ------------------------------------ | ---- | ------------------------------------------------------------ |
| values       | [ValuesBucket](#valuesbucket)        | 是   | values指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 |
1772
| predicates | [RdbPredicates](#rdbpredicates) | 是   | RdbPredicates的实例对象指定的更新条件。                    |
1773 1774 1775 1776 1777 1778 1779

**返回值**

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

1780 1781 1782 1783
**错误码:**

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

L
LiRui 已提交
1784 1785 1786 1787
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1788

1789 1790 1791 1792
**示例:**

```js
const valueBucket = {
1793 1794 1795 1796 1797 1798 1799 1800
  "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 已提交
1801
promise.then(async (rows) => {
1802
  console.info(`Updated row count: ${rows}`);
1803
}).catch((err) => {
G
ge-yafang 已提交
1804
  console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
1805 1806 1807
})
```

L
lihuihui 已提交
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
### 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指示数据库中要更新的数据行。键值对与数据库表的列名相关联。 |
1821
| predicates | [RdbPredicates](#rdbpredicates)            | 是   | RdbPredicates的实例对象指定的更新条件。                      |
P
PaDaBoo 已提交
1822
| conflict   | [ConflictResolution](#conflictresolution10) | 是   | 指定冲突解决方式。                                           |
L
lihuihui 已提交
1823 1824 1825 1826 1827 1828 1829

**返回值**

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

1830 1831 1832 1833
**错误码:**

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

L
LiRui 已提交
1834 1835 1836 1837
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1838

L
lihuihui 已提交
1839 1840 1841 1842
**示例:**

```js
const valueBucket = {
1843 1844 1845 1846 1847 1848 1849 1850
  "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 已提交
1851
promise.then(async (rows) => {
1852
  console.info(`Updated row count: ${rows}`);
L
lihuihui 已提交
1853
}).catch((err) => {
G
ge-yafang 已提交
1854
  console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
L
lihuihui 已提交
1855 1856 1857
})
```

1858
### update
1859 1860 1861 1862 1863 1864 1865

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

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

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

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

1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
**系统接口:** 此接口为系统接口。

**参数:**

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

1879 1880 1881 1882
**错误码:**

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

L
LiRui 已提交
1883 1884 1885 1886
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1887

1888 1889 1890 1891 1892 1893 1894 1895 1896
**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
const valueBucket = {
    "NAME": "Rose",
    "AGE": 22,
    "SALARY": 200.5,
    "CODES": new Uint8Array([1, 2, 3, 4, 5]),
1897 1898 1899 1900 1901
};
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
store.update("EMPLOYEE", valueBucket, predicates, function (err, rows) {
  if (err) {
G
ge-yafang 已提交
1902
    console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
1903 1904 1905
    return;
  }
  console.info(`Updated row count: ${rows}`);
1906 1907 1908
})
```

1909
### update
1910 1911 1912 1913 1914 1915 1916

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

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

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

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

1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
**系统接口:** 此接口为系统接口。

**参数:**

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

**返回值**

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

1935 1936 1937 1938
**错误码:**

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

L
LiRui 已提交
1939 1940 1941 1942
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1943

1944 1945 1946 1947 1948
**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
const valueBucket = {
1949 1950 1951 1952 1953 1954 1955 1956
  "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 已提交
1957
promise.then(async (rows) => {
1958
  console.info(`Updated row count: ${rows}`);
1959
}).catch((err) => {
G
ge-yafang 已提交
1960
  console.error(`Updated failed, code is ${err.code},message is ${err.message}`);
1961 1962 1963
})
```

1964
### delete
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975

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

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

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

**参数:**

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

1979 1980 1981 1982
**错误码:**

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

L
LiRui 已提交
1983 1984 1985 1986
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
1987

1988 1989 1990
**示例:**

```js
1991 1992 1993 1994
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa");
store.delete(predicates, function (err, rows) {
  if (err) {
G
ge-yafang 已提交
1995
    console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
1996 1997 1998
    return;
  }
  console.info(`Delete rows: ${rows}`);
1999 2000 2001
})
```

2002
### delete
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013

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

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

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

**参数:**

| 参数名     | 类型                                 | 必填 | 说明                                      |
| ---------- | ------------------------------------ | ---- | ----------------------------------------- |
2014
| predicates | [RdbPredicates](#rdbpredicates) | 是   | RdbPredicates的实例对象指定的删除条件。 |
2015 2016 2017 2018 2019 2020 2021

**返回值**

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

2022 2023 2024 2025
**错误码:**

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

L
LiRui 已提交
2026 2027 2028 2029
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
2030

2031 2032 2033
**示例:**

```js
2034 2035 2036
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Lisa");
let promise = store.delete(predicates);
2037
promise.then((rows) => {
2038
  console.info(`Delete rows: ${rows}`);
2039
}).catch((err) => {
G
ge-yafang 已提交
2040
  console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
2041 2042 2043
})
```

2044
### delete
2045 2046 2047 2048 2049 2050 2051

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

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

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

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

2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
**系统接口:** 此接口为系统接口。

**参数:**

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

2064 2065 2066 2067
**错误码:**

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

L
LiRui 已提交
2068 2069 2070 2071
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
2072

2073 2074 2075 2076
**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
2077 2078 2079 2080
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
store.delete("EMPLOYEE", predicates, function (err, rows) {
  if (err) {
G
ge-yafang 已提交
2081
    console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
2082 2083 2084
    return;
  }
  console.info(`Delete rows: ${rows}`);
2085 2086 2087
})
```

2088
### delete
2089 2090 2091 2092 2093 2094 2095

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

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

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

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

2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
**系统接口:** 此接口为系统接口。

**参数:**

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

**返回值**

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

2113 2114 2115 2116
**错误码:**

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

L
LiRui 已提交
2117 2118 2119 2120
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
2121

2122 2123 2124 2125
**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
2126 2127 2128
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Lisa");
let promise = store.delete("EMPLOYEE", predicates);
2129
promise.then((rows) => {
2130
  console.info(`Delete rows: ${rows}`);
2131
}).catch((err) => {
G
ge-yafang 已提交
2132
  console.error(`Delete failed, code is ${err.code},message is ${err.message}`);
2133 2134 2135
})
```

2136
### query
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147

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

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

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

**参数:**

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

L
LiRui 已提交
2152 2153 2154 2155 2156 2157 2158 2159
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2160 2161 2162
**示例:**

```js
2163 2164 2165 2166
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 已提交
2167
    console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2168 2169
    return;
  }
M
MangTsang 已提交
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
  console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
  // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。
  while(resultSet.goToNextRow()) {
    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"));
    console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
  }
  // 释放数据集的内存
  resultSet.close();
2181 2182 2183
})
```

2184
### query
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195

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

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

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

**参数:**

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

L
LiRui 已提交
2199 2200 2201 2202 2203 2204 2205 2206
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2207 2208 2209 2210
**返回值**

| 类型                                                    | 说明                                               |
| ------------------------------------------------------- | -------------------------------------------------- |
P
PaDaBoo 已提交
2211
| Promise&lt;[ResultSet](#resultset)&gt; | Promise对象。如果操作成功,则返回ResultSet对象。 |
2212 2213 2214 2215

**示例:**

  ```js
2216 2217 2218
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
predicates.equalTo("NAME", "Rose");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
2219
promise.then((resultSet) => {
M
MangTsang 已提交
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
  console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
  // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。
  while(resultSet.goToNextRow()) {
    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"));
    console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
  }
  // 释放数据集的内存
  resultSet.close();
2231
}).catch((err) => {
G
ge-yafang 已提交
2232
  console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2233 2234 2235
})
  ```

2236
### query
2237 2238 2239 2240 2241 2242 2243

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 已提交
2244 2245
**模型约束:** 此接口仅可在Stage模型下可用。

2246 2247 2248 2249 2250 2251 2252 2253 2254
**系统接口:** 此接口为系统接口。

**参数:**

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

L
LiRui 已提交
2257 2258 2259 2260 2261 2262 2263 2264
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2265 2266 2267 2268
**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
2269 2270 2271 2272
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 已提交
2273
    console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2274 2275
    return;
  }
M
MangTsang 已提交
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
  console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
  // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。
  while(resultSet.goToNextRow()) {
    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"));
    console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
  }
  // 释放数据集的内存
  resultSet.close();
2287 2288 2289
})
```

2290
### query
2291 2292 2293 2294 2295 2296 2297

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

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

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

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

2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
**系统接口:** 此接口为系统接口。

**参数:**

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

**返回值**

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

L
LiRui 已提交
2316 2317 2318 2319 2320 2321 2322 2323
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2324 2325 2326 2327
**示例:**

```js
import dataSharePredicates from '@ohos.data.dataSharePredicates'
2328 2329 2330
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo("NAME", "Rose");
let promise = store.query("EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
2331
promise.then((resultSet) => {
M
MangTsang 已提交
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
  console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
  // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。
  while(resultSet.goToNextRow()) {
    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"));
    console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
  }
  // 释放数据集的内存
  resultSet.close();
2343
}).catch((err) => {
G
ge-yafang 已提交
2344
  console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2345 2346 2347
})
```

2348
### remoteQuery
2349 2350 2351 2352 2353

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

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

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

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

2360 2361
**参数:**

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

L
LiRui 已提交
2370 2371 2372 2373 2374 2375 2376 2377
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2378 2379 2380
**示例:**

```js
L
device  
lihuihui 已提交
2381 2382
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
2383
let deviceId = null;
L
device  
lihuihui 已提交
2384 2385 2386 2387 2388 2389 2390 2391

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 已提交
2392
    deviceId = devices[0].deviceId;
L
device  
lihuihui 已提交
2393 2394
})

2395 2396
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.greaterThan("id", 0);
L
device  
lihuihui 已提交
2397
store.remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"],
2398
  function(err, resultSet) {
2399
    if (err) {
G
ge-yafang 已提交
2400
      console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`);
2401
      return;
2402
    }
M
MangTsang 已提交
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413
    console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
    // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。
    while(resultSet.goToNextRow()) {
      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"));
      console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
    }
    // 释放数据集的内存
    resultSet.close();
2414 2415
  }
)
2416 2417
```

2418
### remoteQuery
2419 2420 2421 2422 2423

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

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

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

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

2430 2431 2432 2433
**参数:**

| 参数名     | 类型                                 | 必填 | 说明                                             |
| ---------- | ------------------------------------ | ---- | ------------------------------------------------ |
L
device  
lihuihui 已提交
2434
| device     | string                               | 是   | 指定的远程设备ID。                   |
2435
| table      | string                               | 是   | 指定的目标表名。                                 |
2436
| predicates | [RdbPredicates](#rdbpredicates) | 是   | RdbPredicates的实例对象,指定查询的条件。      |
2437 2438 2439 2440 2441 2442
| columns    | Array&lt;string&gt;                  | 是   | 表示要查询的列。如果值为空,则查询应用于所有列。 |

**返回值**

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

L
LiRui 已提交
2445 2446 2447 2448 2449 2450 2451 2452
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2453 2454 2455
**示例:**

```js
L
device  
lihuihui 已提交
2456 2457
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
2458
let deviceId = null;
L
device  
lihuihui 已提交
2459 2460 2461 2462 2463 2464 2465 2466

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 已提交
2467
    deviceId = devices[0].deviceId;
L
device  
lihuihui 已提交
2468 2469
})

2470 2471
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.greaterThan("id", 0);
L
lihuihui 已提交
2472
let promise = store.remoteQuery(deviceId, "EMPLOYEE", predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
2473
promise.then((resultSet) => {
M
MangTsang 已提交
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
  console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
  // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。
  while(resultSet.goToNextRow()) {
    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"));
    console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
  }
  // 释放数据集的内存
  resultSet.close();
2485
}).catch((err) => {
G
ge-yafang 已提交
2486
  console.error(`Failed to remoteQuery, code is ${err.code},message is ${err.message}`);
2487 2488 2489
})
```

2490
### querySql
2491 2492 2493 2494 2495 2496 2497 2498 2499

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

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

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

**参数:**

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

L
LiRui 已提交
2506 2507 2508 2509 2510 2511 2512 2513
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2514 2515 2516
**示例:**

```js
2517 2518
store.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = ?", ['sanguo'], function (err, resultSet) {
  if (err) {
G
ge-yafang 已提交
2519
    console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2520 2521
    return;
  }
M
MangTsang 已提交
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
  console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
  // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。
  while(resultSet.goToNextRow()) {
    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"));
    console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
  }
  // 释放数据集的内存
  resultSet.close();
2533 2534 2535
})
```

2536
### querySql
2537 2538 2539 2540 2541 2542 2543 2544 2545

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

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

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

**参数:**

L
query  
lihuihui 已提交
2546 2547 2548 2549
| 参数名   | 类型                                 | 必填 | 说明                                                         |
| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
| sql      | string                               | 是   | 指定要执行的SQL语句。                                        |
| bindArgs | Array&lt;[ValueType](#valuetype)&gt; | 否   | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 |
2550 2551 2552 2553 2554

**返回值**

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

L
LiRui 已提交
2557 2558 2559 2560 2561 2562 2563 2564
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2565 2566 2567
**示例:**

```js
L
RDB  
lihuihui 已提交
2568
let promise = store.querySql("SELECT * FROM EMPLOYEE CROSS JOIN BOOK WHERE BOOK.NAME = 'sanguo'");
2569
promise.then((resultSet) => {
M
MangTsang 已提交
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
  console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
  // resultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。
  while(resultSet.goToNextRow()) {
    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"));
    console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
  }
  // 释放数据集的内存
  resultSet.close();
2581
}).catch((err) => {
G
ge-yafang 已提交
2582
  console.error(`Query failed, code is ${err.code},message is ${err.message}`);
2583 2584 2585
})
```

2586
### executeSql
2587 2588 2589 2590 2591 2592 2593 2594 2595

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

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

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

**参数:**

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

2602 2603 2604 2605
**错误码:**

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

L
LiRui 已提交
2606 2607 2608 2609
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
2610

2611 2612 2613
**示例:**

```js
L
RDB  
lihuihui 已提交
2614 2615
const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = ?"
store.executeSql(SQL_DELETE_TABLE, ['zhangsan'], function(err) {
2616
  if (err) {
G
ge-yafang 已提交
2617
    console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`);
2618 2619
    return;
  }
L
RDB  
lihuihui 已提交
2620
  console.info(`Delete table done.`);
2621 2622 2623
})
```

2624
### executeSql
2625 2626 2627 2628 2629 2630 2631 2632 2633

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

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

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

**参数:**

L
query  
lihuihui 已提交
2634 2635 2636 2637
| 参数名   | 类型                                 | 必填 | 说明                                                         |
| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
| sql      | string                               | 是   | 指定要执行的SQL语句。                                        |
| bindArgs | Array&lt;[ValueType](#valuetype)&gt; | 否   | SQL语句中参数的值。该值与sql参数语句中的占位符相对应。当sql参数语句完整时,该参数不填。 |
2638 2639 2640 2641 2642 2643 2644

**返回值**

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

2645 2646 2647 2648
**错误码:**

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

L
LiRui 已提交
2649 2650 2651 2652
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
2653

2654 2655 2656
**示例:**

```js
L
RDB  
lihuihui 已提交
2657 2658
const SQL_DELETE_TABLE = "DELETE FROM test WHERE name = 'zhangsan'"
let promise = store.executeSql(SQL_DELETE_TABLE);
2659
promise.then(() => {
L
RDB  
lihuihui 已提交
2660
    console.info(`Delete table done.`);
2661
}).catch((err) => {
G
ge-yafang 已提交
2662
    console.error(`ExecuteSql failed, code is ${err.code},message is ${err.message}`);
2663 2664 2665
})
```

2666
### beginTransaction
2667 2668 2669 2670 2671 2672 2673

beginTransaction():void

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

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

2674 2675 2676 2677
**错误码:**

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

L
LiRui 已提交
2678 2679 2680 2681
| **错误码ID** | **错误信息**                                 |
| ------------ | -------------------------------------------- |
| 14800047     | The WAL file size exceeds the default limit. |
| 14800000     | Inner error.                                 |
2682

2683 2684 2685 2686
**示例:**

```js
import featureAbility from '@ohos.ability.featureAbility'
2687 2688 2689 2690 2691 2692 2693
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 已提交
2694
    console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`);
2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
    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();
2706 2707 2708
})
```

2709
### commit
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720

commit():void

提交已执行的SQL语句。

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

**示例:**

```js
import featureAbility from '@ohos.ability.featureAbility'
2721 2722 2723 2724 2725 2726 2727
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 已提交
2728
     console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`);
2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739
     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();
2740 2741 2742
})
```

2743
### rollBack
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754

rollBack():void

回滚已经执行的SQL语句。

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

**示例:**

```js
import featureAbility from '@ohos.ability.featureAbility'
2755 2756 2757 2758 2759 2760 2761
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 已提交
2762
    console.error(`GetRdbStore failed, code is ${err.code},message is ${err.message}`);
2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776
    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 已提交
2777
    console.error(`Transaction failed, code is ${err.code},message is ${err.message}`);
2778 2779
    store.rollBack();
  }
2780 2781 2782
})
```

2783
### backup
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797

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

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

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

**参数:**

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

L
LiRui 已提交
2798 2799 2800 2801 2802 2803 2804 2805
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2806 2807 2808
**示例:**

```js
2809 2810
store.backup("dbBackup.db", function(err) {
  if (err) {
G
ge-yafang 已提交
2811
    console.error(`Backup failed, code is ${err.code},message is ${err.message}`);
2812 2813 2814
    return;
  }
  console.info(`Backup success.`);
2815 2816 2817
})
```

2818
### backup
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837

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

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

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

**参数:**

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

**返回值**

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

L
LiRui 已提交
2838 2839 2840 2841 2842 2843 2844 2845
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2846 2847 2848
**示例:**

```js
2849
let promiseBackup = store.backup("dbBackup.db");
2850
promiseBackup.then(()=>{
2851
  console.info(`Backup success.`);
2852
}).catch((err)=>{
G
ge-yafang 已提交
2853
  console.error(`Backup failed, code is ${err.code},message is ${err.message}`);
2854 2855 2856
})
```

2857
### restore
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871

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

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

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

**参数:**

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

L
LiRui 已提交
2872 2873 2874 2875 2876 2877 2878 2879
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2880 2881 2882
**示例:**

```js
2883 2884
store.restore("dbBackup.db", function(err) {
  if (err) {
G
ge-yafang 已提交
2885
    console.error(`Restore failed, code is ${err.code},message is ${err.message}`);
2886 2887 2888
    return;
  }
  console.info(`Restore success.`);
2889 2890 2891
})
```

2892
### restore
2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911

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

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

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

**参数:**

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

**返回值**

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

L
LiRui 已提交
2912 2913 2914 2915 2916 2917 2918 2919
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2920 2921 2922
**示例:**

```js
2923
let promiseRestore = store.restore("dbBackup.db");
2924
promiseRestore.then(()=>{
2925
  console.info(`Restore success.`);
2926
}).catch((err)=>{
G
ge-yafang 已提交
2927
  console.error(`Restore failed, code is ${err.code},message is ${err.message}`);
2928 2929 2930
})
```

2931
### setDistributedTables
2932 2933 2934

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

R
renjiecui 已提交
2935
设置分布式数据库表,使用callback异步回调。
2936 2937 2938 2939 2940 2941 2942 2943 2944

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

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

**参数:**

| 参数名   | 类型                      | 必填 | 说明                   |
| -------- | ------------------------- | ---- | ---------------------- |
R
renjiecui 已提交
2945
| tables   | Array&lt;string&gt;       | 是   | 要设置的分布式数据库表表名。 |
2946 2947
| callback | AsyncCallback&lt;void&gt; | 是   | 指定callback回调函数。 |

L
LiRui 已提交
2948 2949 2950 2951 2952 2953 2954 2955
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

2956 2957 2958
**示例:**

```js
2959 2960
store.setDistributedTables(["EMPLOYEE"], function (err) {
  if (err) {
G
ge-yafang 已提交
2961
    console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`);
2962 2963 2964
    return;
  }
  console.info(`SetDistributedTables successfully.`);
2965 2966 2967
})
```

R
renjiecui 已提交
2968
### setDistributedTables
R
renjiecui 已提交
2969

R
renjiecui 已提交
2970
 setDistributedTables(tables: Array&lt;string&gt;): Promise&lt;void&gt;
R
renjiecui 已提交
2971

R
renjiecui 已提交
2972
设置分布式数据库表,使用Promise异步回调。
R
renjiecui 已提交
2973 2974 2975 2976 2977 2978 2979

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

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

**参数:**

R
renjiecui 已提交
2980 2981
| 参数名 | 类型                     | 必填 | 说明                     |
| ------ | ------------------------ | ---- | ------------------------ |
R
renjiecui 已提交
2982
| tables | ArrayArray&lt;string&gt; | 是   | 要设置的分布式数据库表表名。 |
R
renjiecui 已提交
2983 2984 2985 2986 2987 2988

**返回值**

| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
R
renjiecui 已提交
2989 2990 2991

**错误码:**

R
renjiecui 已提交
2992
以下错误码的详细介绍请参见[关系型数据库错误码](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/errorcodes/errorcode-data-rdb.md)
R
renjiecui 已提交
2993 2994 2995 2996 2997 2998 2999 3000

| **错误码ID** | **错误信息** |
| ------------ | ------------ |
| 14800000     | Inner error. |

**示例:**

```js
R
renjiecui 已提交
3001 3002
let promise = store.setDistributedTables(["EMPLOYEE"]);
promise.then(() => {
R
renjiecui 已提交
3003
  console.info(`SetDistributedTables successfully.`);
R
renjiecui 已提交
3004 3005
}).catch((err) => {
  console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`);
R
renjiecui 已提交
3006 3007 3008
})
```

R
renjiecui 已提交
3009
### setDistributedTables<sup>10+</sup>
3010

R
renjiecui 已提交
3011
setDistributedTables(tables: Array&lt;string&gt;, type: number, config: DistributedConfig, callback: AsyncCallback&lt;void&gt;): void
3012

R
renjiecui 已提交
3013
设置分布式数据库表,使用callback异步回调。
3014 3015 3016 3017 3018 3019 3020

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

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

**参数:**

R
renjiecui 已提交
3021 3022
| 参数名      | 类型                                  | 必填  | 说明              |
| -------- | ----------------------------------- | --- | --------------- |
R
renjiecui 已提交
3023
| tables   | Array&lt;string&gt;                 | 是   | 要设置的分布式数据库表表名。     |
R
renjiecui 已提交
3024 3025 3026
| type     | number | 是   | 表的分布式类型。目前支持的入参值为: relationalStore.DistributedType.DISTRIBUTED_DEVICE、relationalStore.DistributedType.DISTRIBUTED_CLOUD。<br> 当type为relationalStore.DistributedType.DISTRIBUTED_DEVICE时,表示表在不同设备之间分布式。<br> 当type为relationalStore.DistributedType.DISTRIBUTED_CLOUD时,表示表在设备和云端之间分布式。 |
| config | [DistributedConfig](#distributedconfig10) | 是 | 表的分布式配置信息。 |
| callback | AsyncCallback&lt;void&gt;           | 是   | 指定callback回调函数。 |
3027 3028 3029 3030

**示例:**

```js
R
renjiecui 已提交
3031 3032
let config = new DistributedConfig();
config.autoSync = true;
R
renjiecui 已提交
3033 3034 3035 3036 3037
store.setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, config, function (err) {
  if (err) {
    console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`);
    return;
  }
3038
  console.info(`SetDistributedTables successfully.`);
3039 3040
})
```
R
renjiecui 已提交
3041

R
renjiecui 已提交
3042 3043
### setDistributedTables<sup>10+</sup>

R
renjiecui 已提交
3044
 setDistributedTables(tables: Array&lt;string>, type?: number, config?: DistributedConfig): Promise&lt;void>
R
renjiecui 已提交
3045

R
renjiecui 已提交
3046
设置分布式数据库表,使用Promise异步回调。
R
renjiecui 已提交
3047 3048 3049 3050 3051 3052 3053

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

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

**参数:**

R
renjiecui 已提交
3054 3055
| 参数名 | 类型                                      | 必填 | 说明                                                         |
| ------ | ----------------------------------------- | ---- | ------------------------------------------------------------ |
R
renjiecui 已提交
3056
| tables | Array&lt;string&gt;                       | 是   | 要设置的分布式数据库表表名。                                     |
R
renjiecui 已提交
3057 3058 3059 3060 3061 3062 3063 3064
| type   | number                                    | 否   | 表的分布式类型。默认值是relationalStore.DistributedType.DISTRIBUTED_DEVICE。<br> 目前支持的入参值为: relationalStore.DistributedType.DISTRIBUTED_DEVICE、relationalStore.DistributedType.DISTRIBUTED_CLOUD。<br/> 当type为relationalStore.DistributedType.DISTRIBUTED_DEVICE时,表示表在不同设备之间分布式。<br/> 当type为relationalStore.DistributedType.DISTRIBUTED_CLOUD时,表示表在设备和云端之间分布式。 |
| config | [DistributedConfig](#distributedconfig10) | 否   | 表的分布式配置信息。不传入时默认autoSync为false,即只支持手动同步。 |

**返回值**

| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
R
renjiecui 已提交
3065 3066 3067 3068

**示例:**

```js
R
renjiecui 已提交
3069 3070
let config = new DistributedConfig();
config.autoSync = true;
R
renjiecui 已提交
3071 3072
let promise = store.setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, config);
promise.then(() => {
R
renjiecui 已提交
3073
  console.info(`SetDistributedTables successfully.`);
R
renjiecui 已提交
3074 3075
}).catch((err) => {
  console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`);
R
renjiecui 已提交
3076 3077 3078
})
```

3079
### obtainDistributedTableName
3080 3081 3082

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

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

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

L
device  
lihuihui 已提交
3089 3090 3091 3092
**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

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

3093 3094 3095 3096
**参数:**

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

L
LiRui 已提交
3101 3102 3103 3104 3105 3106 3107 3108
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

3109 3110 3111
**示例:**

```js
L
device  
lihuihui 已提交
3112 3113
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
3114
let deviceId = null;
L
device  
lihuihui 已提交
3115 3116 3117 3118 3119 3120 3121 3122

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 已提交
3123
    deviceId = devices[0].deviceId;
L
device  
lihuihui 已提交
3124 3125 3126
})

store.obtainDistributedTableName(deviceId, "EMPLOYEE", function (err, tableName) {
3127
    if (err) {
G
ge-yafang 已提交
3128
        console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`);
3129
        return;
3130
    }
3131
    console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`);
3132 3133 3134
})
```

3135
### obtainDistributedTableName
3136 3137 3138

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

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

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

L
device  
lihuihui 已提交
3145 3146 3147 3148
**需要权限:** ohos.permission.DISTRIBUTED_DATASYNC

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

3149 3150
**参数:**

L
devices  
lihuihui 已提交
3151 3152
| 参数名 | 类型   | 必填 | 说明                 |
| ------ | ------ | ---- | -------------------- |
L
device  
lihuihui 已提交
3153
| device | string | 是   | 远程设备ID。         |
L
devices  
lihuihui 已提交
3154
| table  | string | 是   | 远程设备的本地表名。 |
3155 3156 3157 3158 3159 3160 3161

**返回值**

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

L
LiRui 已提交
3162 3163 3164 3165 3166 3167 3168 3169
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

3170 3171 3172
**示例:**

```js
L
device  
lihuihui 已提交
3173 3174
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
3175
let deviceId = null;
L
device  
lihuihui 已提交
3176 3177 3178 3179 3180 3181 3182 3183

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 已提交
3184
    deviceId = devices[0].deviceId;
L
device  
lihuihui 已提交
3185 3186 3187
})

let promise = store.obtainDistributedTableName(deviceId, "EMPLOYEE");
3188
promise.then((tableName) => {
3189
  console.info(`ObtainDistributedTableName successfully, tableName= ${tableName}`);
3190
}).catch((err) => {
G
ge-yafang 已提交
3191
  console.error(`ObtainDistributedTableName failed, code is ${err.code},message is ${err.message}`);
3192 3193 3194
})
```

3195
### sync
3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208

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

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

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

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

**参数:**

| 参数名     | 类型                                               | 必填 | 说明                                                         |
| ---------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ |
R
renjiecui 已提交
3209
| mode       | [SyncMode](#syncmode)                             | 是   | 指同步模式。该值可以是relationalStore.SyncMode.SYNC_MODE_PUSH、relationalStore.SyncMode.SYNC_MODE_PULL。                               |
3210
| predicates | [RdbPredicates](#rdbpredicates)               | 是   | 约束同步数据和设备。                                         |
3211 3212
| callback   | AsyncCallback&lt;Array&lt;[string, number]&gt;&gt; | 是   | 指定的callback回调函数,用于向调用者发送同步结果。string:设备ID;number:每个设备同步状态,0表示成功,其他值表示失败。 |

L
LiRui 已提交
3213 3214 3215 3216 3217 3218 3219 3220
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

3221 3222 3223
**示例:**

```js
L
device  
lihuihui 已提交
3224 3225
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
3226
let deviceIds = [];
L
device  
lihuihui 已提交
3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239

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;
    }
})

3240
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
L
device  
lihuihui 已提交
3241
predicates.inDevices(deviceIds);
3242 3243
store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates, function (err, result) {
  if (err) {
G
ge-yafang 已提交
3244
    console.error(`Sync failed, code is ${err.code},message is ${err.message}`);
3245 3246 3247 3248 3249 3250
    return;
  }
  console.info(`Sync done.`);
  for (let i = 0; i < result.length; i++) {
    console.info(`device= ${result[i][0]}, status= ${result[i][1]}`);
  }
3251 3252 3253
})
```

3254
### sync
3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267

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

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

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

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

**参数:**

| 参数名     | 类型                                 | 必填 | 说明                           |
| ---------- | ------------------------------------ | ---- | ------------------------------ |
R
renjiecui 已提交
3268
| mode       | [SyncMode](#syncmode)               | 是   | 指同步模式。该值可以是relationalStore.SyncMode.SYNC_MODE_PUSH、relationalStore.SyncMode.SYNC_MODE_PULL。 |
3269
| predicates | [RdbPredicates](#rdbpredicates) | 是   | 约束同步数据和设备。           |
3270 3271 3272 3273 3274 3275 3276

**返回值**

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

L
LiRui 已提交
3277 3278 3279 3280 3281 3282 3283 3284
**错误码:**

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

| **错误码ID** | **错误信息**                 |
| ------------ | ---------------------------- |
| 14800000     | Inner error.                 |

3285 3286 3287
**示例:**

```js
L
device  
lihuihui 已提交
3288 3289
import deviceManager from '@ohos.distributedHardware.deviceManager';
let dmInstance = null;
L
lihuihui 已提交
3290
let deviceIds = [];
L
device  
lihuihui 已提交
3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303

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;
    }
})

3304
let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
L
device  
lihuihui 已提交
3305
predicates.inDevices(deviceIds);
3306
let promise = store.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates);
W
wangxiyue 已提交
3307
promise.then((result) =>{
3308
  console.info(`Sync done.`);
W
wangxiyue 已提交
3309
  for (let i = 0; i < result.length; i++) {
3310 3311
    console.info(`device= ${result[i][0]}, status= ${result[i][1]}`);
  }
3312
}).catch((err) => {
G
ge-yafang 已提交
3313
  console.error(`Sync failed, code is ${err.code},message is ${err.message}`);
3314 3315 3316
})
```

3317
### on('dataChange')
3318

R
renjiecui 已提交
3319 3320
on(event: 'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;): void

R
renjiecui 已提交
3321
注册数据库的数据变更的事件监听。当分布式数据库中的数据发生更改时,将调用回调。
R
renjiecui 已提交
3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349

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

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| event    | string                                                       | 是   | 取值为'dataChange',表示数据更改。                           |
| type     | [SubscribeType](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md#subscribetype) | 是   | 订阅类型。                                                   |
| observer | Callback&lt;Array&lt;string&gt;&gt;                          | 是   | 指分布式数据库中数据更改事件的观察者。Array&lt;string&gt;为数据库中的数据发生改变的对端设备ID。 |

**示例:**

```
function storeObserver(devices) {
  for (let i = 0; i < devices.length; i++) {
    console.info(`device= ${devices[i]} data changed`);
  }
}
try {
  store.on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
} catch (err) {
  console.error(`Register observer failed, code is ${err.code},message is ${err.message}`);
}
```

### on('dataChange')<sup>10+</sup>

R
renjiecui 已提交
3350
on(event: 'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;\| Callback&lt;Array&lt;ChangeInfo&gt;&gt;): void
3351

R
renjiecui 已提交
3352
注册数据库的数据变更的事件监听。当分布式数据库中的数据发生更改时,将调用回调。
3353 3354 3355 3356 3357 3358 3359 3360

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

**参数:**

| 参数名   | 类型                                | 必填 | 说明                                        |
| -------- | ----------------------------------- | ---- | ------------------------------------------- |
| event    | string                              | 是   | 取值为'dataChange',表示数据更改。          |
P
PaDaBoo 已提交
3361
| type     | [SubscribeType](#subscribetype)    | 是   | 订阅类型。 |
R
renjiecui 已提交
3362
| observer | Callback&lt;Array&lt;string&gt;&gt; \| Callback&lt;Array&lt;[ChangeInfo](#changeinfo10)&gt;&gt; | 是   | 回调函数。<br>当type为SUBSCRIBE_TYPE_REMOTE,observer类型需为Callback&lt;Array&lt;string&gt;&gt;,其中Array&lt;string&gt;为数据库中的数据发生改变的对端设备ID。<br> 当type为SUBSCRIBE_TYPE_CLOUD,observer类型需为Callback&lt;Array&lt;string&gt;&gt;,其中Array&lt;string&gt;为数据库中的数据发生改变的云端帐号。 <br> 当type为SUBSCRIBE_TYPE_CLOUD_DETAILS,observer类型需为Callback&lt;Array&lt;ChangeInfo&gt;&gt;,其中Array&lt;ChangeInfo&gt;为数据库端云同步过程的详情。 |
3363 3364 3365 3366 3367

**示例:**

```js
function storeObserver(devices) {
3368 3369 3370
  for (let i = 0; i < devices.length; i++) {
    console.info(`device= ${devices[i]} data changed`);
  }
3371 3372
}
try {
3373
  store.on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
3374
} catch (err) {
G
ge-yafang 已提交
3375
  console.error(`Register observer failed, code is ${err.code},message is ${err.message}`);
3376 3377 3378
}
```

3379
### off('dataChange')
3380

R
renjiecui 已提交
3381 3382
off(event:'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;): void

R
renjiecui 已提交
3383
取消数据变更的事件监听。
R
renjiecui 已提交
3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411

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

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| event    | string                                                       | 是   | 取值为'dataChange',表示数据更改。                           |
| type     | [SubscribeType](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md#subscribetype) | 是   | 订阅类型。                                                   |
| observer | Callback&lt;Array&lt;string&gt;&gt;                          | 是   | 指已注册的数据更改观察者。Array&lt;string&gt;为数据库中的数据发生改变的对端设备ID。 |

**示例:**

```
function storeObserver(devices) {
  for (let i = 0; i < devices.length; i++) {
    console.info(`device= ${devices[i]} data changed`);
  }
}
try {
  store.off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
} catch (err) {
  console.error(`Unregister observer failed, code is ${err.code},message is ${err.message}`);
}
```

### off('dataChange')<sup>10+</sup>

R
renjiecui 已提交
3412
off(event:'dataChange', type: SubscribeType, observer?: Callback&lt;Array&lt;string&gt;&gt;\| Callback&lt;Array&lt;ChangeInfo&gt;&gt;): void
3413

R
renjiecui 已提交
3414
取消数据变更的事件监听。
3415 3416 3417 3418 3419 3420

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

**参数:**

| 参数名   | 类型                                | 必填 | 说明                                        |
P
PaDaBoo 已提交
3421
| -------- | ---------------------------------- | ---- | ------------------------------------------ |
3422
| event    | string                              | 是   | 取值为'dataChange',表示数据更改。          |
P
PaDaBoo 已提交
3423
| type     | [SubscribeType](#subscribetype)     | 是   | 订阅类型。                                 |
R
renjiecui 已提交
3424
| observer | Callback&lt;Array&lt;string&gt;&gt;\| Callback&lt;Array&lt;[ChangeInfo](#changeinfo10)&gt;&gt; | 否 | 回调函数。<br/>当type为SUBSCRIBE_TYPE_REMOTE,observer类型需为Callback&lt;Array&lt;string&gt;&gt;,其中Array&lt;string&gt;为数据库中的数据发生改变的对端设备ID。<br/> 当type为SUBSCRIBE_TYPE_CLOUD,observer类型需为Callback&lt;Array&lt;string&gt;&gt;,其中Array&lt;string&gt;为数据库中的数据发生改变的云端帐号。 <br/> 当type为SUBSCRIBE_TYPE_CLOUD_DETAILS,observer类型需为Callback&lt;Array&lt;ChangeInfo&gt;&gt;,其中Array&lt;ChangeInfo&gt;为数据库端云同步过程的详情。<br> 当observer没有传入时,表示取消当前type类型下所有数据变更的事件监听。 |
3425 3426 3427 3428 3429

**示例:**

```js
function storeObserver(devices) {
3430 3431 3432
  for (let i = 0; i < devices.length; i++) {
    console.info(`device= ${devices[i]} data changed`);
  }
3433 3434
}
try {
3435
  store.off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
3436
} catch (err) {
G
ge-yafang 已提交
3437
  console.error(`Unregister observer failed, code is ${err.code},message is ${err.message}`);
3438 3439 3440
}
```

3441
## ResultSet
3442 3443 3444 3445 3446

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

### 使用说明

3447
首先需要获取resultSet对象。
3448 3449

```js
W
wangxiyue 已提交
3450
let resultSet = null;
3451
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
3452
predicates.equalTo("AGE", 18);
3453
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
W
wangxiyue 已提交
3454 3455
promise.then((result) => {
  resultSet = result;
3456 3457
  console.info(`resultSet columnNames: ${resultSet.columnNames}`);
  console.info(`resultSet columnCount: ${resultSet.columnCount}`);
3458 3459 3460
});
```

3461
### 属性
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476

**系统能力:** 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             | 是   | 检查当前结果集是否关闭。         |

3477
### getColumnIndex
3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502

getColumnIndex(columnName: string): number

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

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

**参数:**

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

**返回值:**

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

**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
3503
| 14800013     | The column value is null or the column type is incompatible. |
3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514

**示例:**

  ```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"));
  ```

3515
### getColumnName
3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540

getColumnName(columnIndex: number): string

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

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

**参数:**

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

**返回值:**

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

**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
3541
| 14800013     | The column value is null or the column type is incompatible. |
3542 3543 3544 3545 3546 3547 3548 3549 3550

**示例:**

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

3551
### goTo
3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576

goTo(offset:number): boolean

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

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

**参数:**

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

**返回值:**

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

**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
L
LiRui 已提交
3577
| 14800012     | The result set is empty or the specified location is invalid. |
3578 3579 3580 3581

**示例:**

  ```js
3582 3583
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise= store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3584
promise.then((resultSet) => {
3585 3586
  resultSet.goTo(1);
  resultSet.close();
3587
}).catch((err) => {
G
ge-yafang 已提交
3588
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3589 3590 3591
});
  ```

3592
### goToRow
3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617

goToRow(position: number): boolean

转到结果集的指定行。

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

**参数:**

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

**返回值:**

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

**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
L
LiRui 已提交
3618
| 14800012     | The result set is empty or the specified location is invalid. |
3619 3620 3621 3622

**示例:**

  ```js
3623 3624
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3625
promise.then((resultSet) => {
W
wangxiyue 已提交
3626
  resultSet.goToRow(5);
3627
  resultSet.close();
3628
}).catch((err) => {
G
ge-yafang 已提交
3629
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3630 3631 3632
});
  ```

3633
### goToFirstRow
3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653

goToFirstRow(): boolean


转到结果集的第一行。

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

**返回值:**

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

**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
L
LiRui 已提交
3654
| 14800012     | The result set is empty or the specified location is invalid. |
3655 3656 3657 3658

**示例:**

  ```js
3659 3660
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3661
promise.then((resultSet) => {
3662 3663
  resultSet.goToFirstRow();
  resultSet.close();
3664
}).catch((err) => {
G
ge-yafang 已提交
3665
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3666 3667 3668
});
  ```

3669
### goToLastRow
3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688

goToLastRow(): boolean

转到结果集的最后一行。

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

**返回值:**

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

**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
L
LiRui 已提交
3689
| 14800012     | The result set is empty or the specified location is invalid. |
3690 3691 3692 3693

**示例:**

  ```js
3694 3695
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3696
promise.then((resultSet) => {
3697 3698
  resultSet.goToLastRow();
  resultSet.close();
3699
}).catch((err) => {
G
ge-yafang 已提交
3700
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3701 3702 3703
});
  ```

3704
### goToNextRow
3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723

goToNextRow(): boolean

转到结果集的下一行。

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

**返回值:**

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

**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
L
LiRui 已提交
3724
| 14800012     | The result set is empty or the specified location is invalid. |
3725 3726 3727 3728

**示例:**

  ```js
3729 3730
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3731
promise.then((resultSet) => {
3732 3733
  resultSet.goToNextRow();
  resultSet.close();
3734
}).catch((err) => {
G
ge-yafang 已提交
3735
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3736 3737 3738
});
  ```

3739
### goToPreviousRow
3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758

goToPreviousRow(): boolean

转到结果集的上一行。

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

**返回值:**

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

**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
L
LiRui 已提交
3759
| 14800012     | The result set is empty or the specified location is invalid. |
3760 3761 3762 3763

**示例:**

  ```js
3764 3765
let predicates = new relationalStore.RdbPredicates("EMPLOYEE");
let promise = store.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
3766
promise.then((resultSet) => {
3767 3768
  resultSet.goToPreviousRow();
  resultSet.close();
3769
}).catch((err) => {
G
ge-yafang 已提交
3770
  console.error(`query failed, code is ${err.code},message is ${err.message}`);
3771 3772 3773
});
  ```

3774
### getBlob
3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793

getBlob(columnIndex: number): Uint8Array

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

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

**参数:**

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

**返回值:**

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

L
LiRui 已提交
3794 3795 3796 3797 3798 3799 3800 3801
**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800013     | The column value is null or the column type is incompatible. |

3802 3803 3804 3805 3806 3807
**示例:**

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

3808
### getString
3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827

getString(columnIndex: number): string

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

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

**参数:**

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

**返回值:**

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

L
LiRui 已提交
3828 3829 3830 3831 3832 3833 3834 3835
**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800013     | The column value is null or the column type is incompatible. |

3836 3837 3838 3839 3840 3841
**示例:**

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

3842
### getLong
3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857

getLong(columnIndex: number): number

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

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

**参数:**

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

**返回值:**

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

L
LiRui 已提交
3862 3863 3864 3865 3866 3867 3868 3869
**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800013     | The column value is null or the column type is incompatible. |

3870 3871 3872 3873 3874 3875
**示例:**

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

3876
### getDouble
3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895

getDouble(columnIndex: number): number

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

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

**参数:**

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

**返回值:**

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

L
LiRui 已提交
3896 3897 3898 3899 3900 3901 3902 3903
**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
| 14800013     | The column value is null or the column type is incompatible. |

3904 3905 3906 3907 3908 3909
**示例:**

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

R
renjiecui 已提交
3910 3911
### getAsset<sup>10+</sup>

R
renjiecui 已提交
3912
getAsset(columnIndex: number): Asset
R
renjiecui 已提交
3913

R
renjiecui 已提交
3914
[Asset](#asset10)形式获取当前行中指定列的值。
R
renjiecui 已提交
3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927

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

**参数:**

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

**返回值:**

| 类型              | 说明                         |
| --------------- | -------------------------- |
R
renjiecui 已提交
3928
| [Asset](#asset10) | 以Asset形式返回指定列的值。 |
R
renjiecui 已提交
3929 3930 3931 3932 3933 3934 3935

**错误码:**

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

| **错误码ID** | **错误信息**                                                     |
| --------- | ------------------------------------------------------------ |
R
renjiecui 已提交
3936
| 14800013  | The column value is null or the column type is incompatible. |
R
renjiecui 已提交
3937 3938 3939 3940 3941 3942 3943 3944 3945

**示例:**

```js
const doc = resultSet.getAsset(resultSet.getColumnIndex("DOC"));
```

### getAssets<sup>10+</sup>

R
renjiecui 已提交
3946
getAssets(columnIndex: number): Assets
R
renjiecui 已提交
3947

R
renjiecui 已提交
3948
[Assets](#assets10)形式获取当前行中指定列的值。
R
renjiecui 已提交
3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959

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

**参数:**

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

**返回值:**

R
renjiecui 已提交
3960 3961
| 类型              | 说明                           |
| ---------------- | ---------------------------- |
R
renjiecui 已提交
3962
| [Assets](#assets10)| 以Assets形式返回指定列的值。 |
R
renjiecui 已提交
3963 3964 3965 3966 3967

**错误码:**

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

R
renjiecui 已提交
3968 3969
| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
R
renjiecui 已提交
3970
| 14800013     | The column value is null or the column type is incompatible. |
R
renjiecui 已提交
3971 3972 3973 3974 3975 3976 3977 3978

**示例:**

```js
const docs = resultSet.getAssets(resultSet.getColumnIndex("DOCS"));
```


3979
### isColumnNull
3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004

isColumnNull(columnIndex: number): boolean

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

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

**参数:**

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

**返回值:**

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

**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
4005
| 14800013     | The column value is null or the column type is incompatible. |
4006 4007 4008 4009 4010 4011 4012

**示例:**

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

4013
### close
4014 4015 4016 4017 4018 4019 4020 4021 4022 4023

close(): void

关闭结果集。

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

**示例:**

  ```js
4024 4025
let predicatesClose = new relationalStore.RdbPredicates("EMPLOYEE");
let promiseClose = store.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
4026
promiseClose.then((resultSet) => {
4027
  resultSet.close();
4028
}).catch((err) => {
G
ge-yafang 已提交
4029
  console.error(`resultset close failed, code is ${err.code},message is ${err.message}`);
4030 4031 4032 4033 4034 4035 4036 4037 4038
});
  ```

**错误码:**

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

| **错误码ID** | **错误信息**                                                 |
| ------------ | ------------------------------------------------------------ |
L
LiRui 已提交
4039
| 14800012     | The result set is empty or the specified location is invalid. |