js-apis-data-DataShareResultSet.md 11.5 KB
Newer Older
A
Annie_wang 已提交
1
# @ohos.data.dataShareResultSet (DataShare Result Set)
A
Annie_wang 已提交
2

A
Annie_wang 已提交
3
The **DataShareResultSet** module provides APIs for accessing the result set obtained from the database. You can access the values in the specified rows or the value of the specified data type.
A
Annie_wang 已提交
4

A
Annie_wang 已提交
5
> **NOTE**
A
Annie_wang 已提交
6
>
A
Annie_wang 已提交
7 8 9
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>
> The APIs provided by this module are system APIs.
A
Annie_wang 已提交
10 11 12 13 14 15 16 17


## Modules to Import

```ts
import DataShareResultSet from '@ohos.data.DataShareResultSet';
```

A
Annie_wang 已提交
18
## Usage
A
Annie_wang 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

You can call [query()](js-apis-data-dataShare.md#query) to obtain the **DataShareResultSet** object.

```ts
import dataShare from '@ohos.data.dataShare';
import dataSharePredicates from '@ohos.data.dataSharePredicates'

let dataShareHelper;
let uri = ("datashare:///com.samples.datasharetest.DataShare");
await dataShare.createDataShareHelper(this.context, uri, (err, data) => {
	if (err != undefined) {
        console.info("createDataShareHelper fail, error message : " + err);
    } else {
        console.info("createDataShareHelper end, data : " + data);
        dataShareHelper = data;
    }
});

let columns = ["*"];
let da = new dataSharePredicates.DataSharePredicates();
let resultSet;
da.equalTo("name", "ZhangSan");
dataShareHelper.query(uri, da, columns).then((data) => {
    console.log("query end, data : " + data);
    resultSet = data;
}).catch((err) => {
	console.log("query fail, error message : " + err);
});
```

A
Annie_wang 已提交
49 50 51 52
## DataShareResultSet
Provides methods for accessing the result sets generated by querying the database.

### Attributes
A
Annie_wang 已提交
53 54 55 56 57 58 59 60 61 62

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

| Name       | Type     | Mandatory| Description                    |
| ----------- | ------------- | ---- | ------------------------ |
| columnNames | Array<string> | Yes  | Names of all columns in the result set.  |
| columnCount | number        | Yes  | Number of columns in the result set.        |
| rowCount    | number        | Yes  | Number of rows in the result set.        |
| isClosed    | boolean       | Yes  | Whether the result set is closed.|

A
Annie_wang 已提交
63
### goToFirstRow
A
Annie_wang 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

goToFirstRow(): boolean

Moves to the first row of the result set.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Return value**

| Type   | Description                                         |
| :------ | --------------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```ts
let isGoTOFirstRow = resultSet.goToFirstRow();
console.info('resultSet.goToFirstRow: ' + isGoTOFirstRow);
```

A
Annie_wang 已提交
84
### goToLastRow
A
Annie_wang 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

goToLastRow(): boolean

Moves to the last row of the result set.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```ts
let isGoToLastRow = resultSet.goToLastRow();
console.info('resultSet.goToLastRow: ' + isGoToLastRow);
```

A
Annie_wang 已提交
105
### goToNextRow
A
Annie_wang 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

goToNextRow(): boolean

Moves to the next row in the result set.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Return value**

| Type   | Description                                         |
| ------- | --------------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```ts
let isGoToNextRow = resultSet.goToNextRow();
console.info('resultSet.goToNextRow: ' + isGoToNextRow);
```

A
Annie_wang 已提交
126
### goToPreviousRow
A
Annie_wang 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

goToPreviousRow(): boolean

Moves to the previous row in the result set.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Return value**

| Type   | Description                                         |
| ------- | --------------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```ts
let isGoToPreviousRow = resultSet.goToPreviousRow();
console.info('resultSet.goToPreviousRow: ' + isGoToPreviousRow);
```

A
Annie_wang 已提交
147
### goTo
A
Annie_wang 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

goTo(offset:number): boolean

Moves based on the specified offset.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Parameters**

| **Name**| **Type**| **Mandatory**| Description                                                        |
| ---------- | -------- | -------- | ------------------------------------------------------------ |
| offset     | number   | Yes      | Offset relative to the current position. A negative value means to move backward, and a positive value means to move forward.|

**Return value**

| Type   | Description                                         |
| ------- | --------------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```ts
let goToNum = 1;
let isGoTo = resultSet.goTo(goToNum);
console.info('resultSet.goTo: ' + isGoTo);
```

A
Annie_wang 已提交
175
### goToRow
A
Annie_wang 已提交
176 177 178 179 180 181 182 183 184 185 186

goToRow(position: number): boolean

Moves to the specified row in the result set.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Parameters**

| **Name**| **Type**| **Mandatory**| Description                    |
| ---------- | -------- | -------- | ------------------------ |
A
Annie_wang 已提交
187
| position   | number   | Yes      | Destination position to move to.|
A
Annie_wang 已提交
188 189 190 191 192 193 194 195 196 197

**Return value**

| Type   | Description                                         |
| ------- | --------------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

**Example**

```ts
A
Annie_wang 已提交
198
let goToRowNum = 2;
A
Annie_wang 已提交
199 200 201 202
let isGoToRow = resultSet.goToRow(goToRowNum);
console.info('resultSet.goToRow: ' + isGoToRow);
```

A
Annie_wang 已提交
203
### getBlob
A
Annie_wang 已提交
204 205 206

getBlob(columnIndex: number): Uint8Array

A
Annie_wang 已提交
207
Obtains the value in the form of a byte array based on the specified column and the current row.
A
Annie_wang 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Parameters**

| **Name** | **Type**| **Mandatory**| Description                   |
| ----------- | -------- | -------- | ----------------------- |
| columnIndex | number   | Yes      | Index of the target column, starting from 0.|

**Return value**

| Type      | Description                            |
| ---------- | -------------------------------- |
| Uint8Array | Value obtained.|

**Example**

```ts
A
Annie_wang 已提交
226
let columnIndex = 1;
A
Annie_wang 已提交
227 228 229 230 231
let goToFirstRow = resultSet.goToFirstRow();
let getBlob = resultSet.getBlob(columnIndex);
console.info('resultSet.getBlob: ' + getBlob);
```

A
Annie_wang 已提交
232
### getString
A
Annie_wang 已提交
233

A
Annie_wang 已提交
234
getString(columnIndex: number): string
A
Annie_wang 已提交
235

A
Annie_wang 已提交
236
Obtains the value in the form of a string based on the specified column and the current row.
A
Annie_wang 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Parameters**

| **Name** | **Type**| **Mandatory**| Description                   |
| ----------- | -------- | -------- | ----------------------- |
| columnIndex | number   | Yes      | Index of the target column, starting from 0.|

**Return value**

| Type  | Description                        |
| ------ | ---------------------------- |
| string | Value obtained.|

**Example**

```ts
A
Annie_wang 已提交
255
let columnIndex = 1;
A
Annie_wang 已提交
256 257 258 259 260
let goToFirstRow = resultSet.goToFirstRow();
let getString = resultSet.getString(columnIndex);
console.info('resultSet.getString: ' + getString);
```

A
Annie_wang 已提交
261
### getLong
A
Annie_wang 已提交
262 263 264

getLong(columnIndex: number): number

A
Annie_wang 已提交
265
Obtains the value in the form of a long integer based on the specified column and the current row.
A
Annie_wang 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Parameters**

| **Name** | **Type**| **Mandatory**| Description                   |
| ----------- | -------- | -------- | ----------------------- |
| columnIndex | number   | Yes      | Index of the target column, starting from 0.|

**Return value**

| Type  | Description                      |
| ------ | -------------------------- |
| number | Value obtained.|

**Example**

```ts
A
Annie_wang 已提交
284
let columnIndex = 1;
A
Annie_wang 已提交
285 286 287 288 289
let goToFirstRow = resultSet.goToFirstRow();
let getLong = resultSet.getLong(columnIndex);
console.info('resultSet.getLong: ' + getLong);
```

A
Annie_wang 已提交
290
### getDouble
A
Annie_wang 已提交
291 292 293

getDouble(columnIndex: number): number

A
Annie_wang 已提交
294
Obtains the value in the form of a double-precision floating-point number based on the specified column and the current row.
A
Annie_wang 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Parameters**

| **Name** | **Type**| **Mandatory**| Description                   |
| ----------- | -------- | -------- | ----------------------- |
| columnIndex | number   | Yes      | Index of the target column, starting from 0.|

**Return value**

| Type  | Description                        |
| ------ | ---------------------------- |
| number | Value obtained.|

**Example**

```ts
A
Annie_wang 已提交
313
let columnIndex = 1;
A
Annie_wang 已提交
314 315 316 317 318
let goToFirstRow = resultSet.goToFirstRow();
let getDouble = resultSet.getDouble(columnIndex);
console.info('resultSet.getDouble: ' + getDouble);
```

A
Annie_wang 已提交
319
### close
A
Annie_wang 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332

close(): void

Closes this result set.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Example**

```ts
resultSet.close();
```

A
Annie_wang 已提交
333
### getColumnIndex
A
Annie_wang 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

getColumnIndex(columnName: string): number

Obtains the column index based on the column name.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Parameters**

| **Name**| **Type**| **Mandatory**| Description                      |
| ---------- | -------- | -------- | -------------------------- |
| columnName | string   | Yes      | Column name.|

**Return value**

| Type  | Description              |
| ------ | ------------------ |
| number | Column index obtained.|

**Example**

```ts
A
Annie_wang 已提交
356 357
let ColumnName = "name";
let getColumnIndex = resultSet.getColumnIndex(ColumnName);
A
Annie_wang 已提交
358 359 360
console.info('resultSet.getColumnIndex: ' + getColumnIndex);
```

A
Annie_wang 已提交
361
### getColumnName
A
Annie_wang 已提交
362

A
Annie_wang 已提交
363
getColumnName(columnIndex: number): string
A
Annie_wang 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

Obtains the column name based on the column index.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Parameters**

| **Name** | **Type**| **Mandatory**| Description                      |
| ----------- | -------- | -------- | -------------------------- |
| columnIndex | number   | Yes      | Column index.|

**Return value**

| Type  | Description              |
| ------ | ------------------ |
| string | Column name obtained.|

**Example**

```ts
A
Annie_wang 已提交
384 385
let columnIndex = 1;
let getColumnName = resultSet.getColumnName(columnIndex);
A
Annie_wang 已提交
386 387 388
console.info('resultSet.getColumnName: ' + getColumnName);
```

A
Annie_wang 已提交
389
### getDataType
A
Annie_wang 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

getDataType(columnIndex: number): DataType

Obtains the data type based on the specified column index.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

**Parameters**

| **Name** | **Type**| **Mandatory**| Description                      |
| ----------- | -------- | -------- | -------------------------- |
| columnIndex | number   | Yes      | Column index.|

**Return value**

| Type                 | Description              |
| --------------------- | ------------------ |
| [DataType](#datatype) | Data type obtained.|

**Example**

```ts
let columnIndex = 1;
let getDataType = resultSet.getDataType(columnIndex);
console.info('resultSet.getDataType: ' + getDataType);
```

## DataType

Enumerates the data types.

**System capability**: SystemCapability.DistributedDataManager.DataShare.Core

| Name       | Value| Description                |
| ----------- | ------ | -------------------- |
| TYPE_NULL   | 0      | Null.    |
| TYPE_LONG   | 1      | Long integer.  |
| TYPE_DOUBLE | 2      | Double-precision floating-point number.|
| TYPE_STRING | 3      | String.|
| TYPE_BLOB   | 4      | Byte array.|