js-apis-data-resultset.md 11.0 KB
Newer Older
Z
zengyawen 已提交
1
# Result Set
Z
zengyawen 已提交
2

A
Annie_wang 已提交
3
> **NOTE**<br/>
Z
zengyawen 已提交
4
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
5

A
annie_wangli 已提交
6

Z
zengyawen 已提交
7
## Usage
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9
You need to use [RdbStore.query()](js-apis-data-rdb.md#query) to obtain the **resultSet** object.
Z
zengyawen 已提交
10

A
Annie_wang 已提交
11
```js
Z
zengyawen 已提交
12 13 14 15 16
import dataRdb from '@ohos.data.rdb';
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("AGE", 18)
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"])
promise.then((resultSet) => {
Z
zengyawen 已提交
17 18
    console.log(TAG + "resultSet columnNames:" + resultSet.columnNames);
    console.log(TAG + "resultSet columnCount:" + resultSet.columnCount);})
Z
zengyawen 已提交
19 20
```

Z
zengyawen 已提交
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 49 50
## ResultSet

Provides methods to access the result set, which is obtained by querying the relational database (RDB) store.


### Attributes

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

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| columnNames | Array&lt;string&gt; | 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.|
| rowIndex | number | Yes| Index of the current row in the result set.|
| isAtFirstRow | boolean | Yes| Whether the cursor is in the first row of the result set.|
| isAtLastRow | boolean | Yes| Whether the cursor is in the last row of the result set.|
| isEnded | boolean | Yes| Whether the cursor is after the last row of the result set.|
| isStarted | boolean | Yes| Whether the cursor has been moved.|
| isClosed | boolean | Yes| Whether the result set is closed.|


### getColumnIndex

getColumnIndex(columnName: string): number

Obtains the column index based on the column name.

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

A
Annie_wang 已提交
51
**Parameters**
Z
zengyawen 已提交
52 53 54 55
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnName | string | Yes| Column name specified.|

A
Annie_wang 已提交
56
**Return value**
Z
zengyawen 已提交
57 58 59 60
  | Type| Description|
  | -------- | -------- |
  | number | Index of the column obtained.|

A
Annie_wang 已提交
61 62
**Example**
  ```js
Z
zengyawen 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  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"))
  ```


### getColumnName

getColumnName(columnIndex: number): string

Obtains the column name based on the column index.

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

A
Annie_wang 已提交
79
**Parameters**
Z
zengyawen 已提交
80 81 82 83
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Column index specified.|

A
Annie_wang 已提交
84
**Return value**
Z
zengyawen 已提交
85 86 87 88
  | Type| Description|
  | -------- | -------- |
  | string | Column name obtained.|

A
Annie_wang 已提交
89 90
**Example**
  ```js
Z
zengyawen 已提交
91 92 93 94 95 96 97 98 99
  const id = resultSet.getColumnName(0)
  const name = resultSet.getColumnName(1)
  const age = resultSet.getColumnName(2)
  ```


### goTo

goTo(offset:number): boolean
Z
zengyawen 已提交
100 101 102

Moves the cursor to the row based on the specified offset.

Z
zengyawen 已提交
103 104
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
105
**Parameters**
Z
zengyawen 已提交
106 107 108 109
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | offset | number | Yes| Offset relative to the current position.|

A
Annie_wang 已提交
110
**Return value**
Z
zengyawen 已提交
111 112 113 114
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

A
Annie_wang 已提交
115 116
**Example**
  ```js
Z
zengyawen 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130
  let predicatesgoto = new dataRdb.RdbPredicates("EMPLOYEE")
  let promisequerygoto = rdbStore.query(predicatesgoto, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promisequerygoto.then((resultSet) {
      resultSet.goTo(1)
      resultSet.close()
  }).catch((err) => {
      console.log('query failed')
  })
  ```


### goToRow

goToRow(position: number): boolean
Z
zengyawen 已提交
131 132 133

Moves the cursor to the specified row in the result set.

Z
zengyawen 已提交
134
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
Z
zengyawen 已提交
135

A
Annie_wang 已提交
136
**Parameters**
Z
zengyawen 已提交
137 138 139 140
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | position | number | Yes| Position to which the cursor is to be moved.|

A
Annie_wang 已提交
141
**Return value**
Z
zengyawen 已提交
142 143 144
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
145

A
Annie_wang 已提交
146 147
**Example**
  ```js
Z
zengyawen 已提交
148 149 150 151 152 153 154 155 156
  let predicatesgotorow = new dataRdb.RdbPredicates("EMPLOYEE")
  let promisequerygotorow = rdbStore.query(predicatesgotorow, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promisequerygotorow.then((resultSet) {
      resultSet.goToRow(5)
      resultSet.close()
  }).catch((err) => {
      console.log('query failed')
  })
  ```
Z
zengyawen 已提交
157 158


Z
zengyawen 已提交
159
### goToFirstRow
Z
zengyawen 已提交
160

Z
zengyawen 已提交
161
goToFirstRow(): boolean
Z
zengyawen 已提交
162 163


Z
zengyawen 已提交
164
Moves the cursor to the first row of the result set.
Z
zengyawen 已提交
165

Z
zengyawen 已提交
166
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
Z
zengyawen 已提交
167

A
Annie_wang 已提交
168
**Return value**
Z
zengyawen 已提交
169 170 171
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
172

A
Annie_wang 已提交
173 174
**Example**
  ```js
Z
zengyawen 已提交
175 176 177 178 179 180 181 182 183
  let predicatesgoFirst = new dataRdb.RdbPredicates("EMPLOYEE")
  let promisequerygoFirst = rdbStore.query(predicatesgoFirst, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promisequerygoFirst.then((resultSet) {
      resultSet.goToFirstRow()
      resultSet.close()
  }).catch((err) => {
      console.log('query failed')
  })
  ```
Z
zengyawen 已提交
184 185


Z
zengyawen 已提交
186
### goToLastRow
Z
zengyawen 已提交
187

Z
zengyawen 已提交
188
goToLastRow(): boolean
Z
zengyawen 已提交
189

Z
zengyawen 已提交
190 191 192 193
Moves the cursor to the last row of the result set.

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

A
Annie_wang 已提交
194
**Return value**
Z
zengyawen 已提交
195 196 197 198
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

A
Annie_wang 已提交
199 200
**Example**
  ```js
Z
zengyawen 已提交
201 202 203 204 205 206 207 208 209
  let predicatesgoLast = new dataRdb.RdbPredicates("EMPLOYEE")
  let promisequerygoLast = rdbStore.query(predicatesgoLast, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promisequerygoLast.then((resultSet) {
      resultSet.goToLastRow()
      resultSet.close()
  }).catch((err) => {
      console.log('query failed')
  })
  ```
Z
zengyawen 已提交
210 211


Z
zengyawen 已提交
212 213 214
### goToNextRow

goToNextRow(): boolean
Z
zengyawen 已提交
215 216 217

Moves the cursor to the next row in the result set.

Z
zengyawen 已提交
218 219
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
220
**Return value**
Z
zengyawen 已提交
221 222 223 224
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|

A
Annie_wang 已提交
225 226
**Example**
  ```js
Z
zengyawen 已提交
227 228 229
  let predicatesgoNext = new dataRdb.RdbPredicates("EMPLOYEE")
  let promisequerygoNext = rdbStore.query(predicatesgoNext, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promisequerygoNext.then((resultSet) {
A
annie_wangli 已提交
230 231
      resultSet.goToNextRow()
      resultSet.close()
Z
zengyawen 已提交
232 233 234 235
  }).catch((err) => {
      console.log('query failed')
  })
  ```
Z
zengyawen 已提交
236 237


Z
zengyawen 已提交
238
### goToPreviousRow
Z
zengyawen 已提交
239

Z
zengyawen 已提交
240
goToPreviousRow(): boolean
Z
zengyawen 已提交
241 242 243

Moves the cursor to the previous row in the result set.

Z
zengyawen 已提交
244
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
Z
zengyawen 已提交
245

A
Annie_wang 已提交
246
**Return value**
Z
zengyawen 已提交
247 248 249
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
250

A
Annie_wang 已提交
251 252
**Example**
  ```js
Z
zengyawen 已提交
253 254 255 256 257 258 259 260 261
  let predicatesgoPrev = new dataRdb.RdbPredicates("EMPLOYEE")
  let promisequerygoPrev = rdbStore.query(predicatesgoPrev, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promisequerygoPrev.then((resultSet) {
      resultSet.goToPreviousRow()
      resultSet.close()
  }).catch((err) => {
      console.log('query failed')
  })
  ```
Z
zengyawen 已提交
262 263


Z
zengyawen 已提交
264
### getBlob
Z
zengyawen 已提交
265

Z
zengyawen 已提交
266
getBlob(columnIndex: number): Uint8Array
Z
zengyawen 已提交
267 268 269

Obtains the value in the specified column in the current row as a byte array.

Z
zengyawen 已提交
270 271
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
272
**Parameters**
Z
zengyawen 已提交
273 274 275 276
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|

A
Annie_wang 已提交
277
**Return value**
Z
zengyawen 已提交
278 279 280 281
  | Type| Description|
  | -------- | -------- |
  | Uint8Array | Value in the specified column as a byte array.|

A
Annie_wang 已提交
282 283
**Example**
  ```js
Z
zengyawen 已提交
284 285 286 287 288 289 290
  const codes = resultSet.getBlob(resultSet.getColumnIndex("CODES"))
  ```


### getString

getString(columnIndex: number): string
Z
zengyawen 已提交
291 292 293

Obtains the value in the specified column in the current row as a string.

Z
zengyawen 已提交
294 295
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
296
**Parameters**
Z
zengyawen 已提交
297 298 299 300
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|

A
Annie_wang 已提交
301
**Return value**
Z
zengyawen 已提交
302 303 304 305
  | Type| Description|
  | -------- | -------- |
  | string | Value in the specified column as a string.|

A
Annie_wang 已提交
306 307
**Example**
  ```js
Z
zengyawen 已提交
308 309 310 311 312 313 314 315 316 317 318 319
  const name = resultSet.getString(resultSet.getColumnIndex("NAME"))
  ```


### getLong

getLong(columnIndex: number): number

Obtains the value in the specified column in the current row as a Long.

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

A
Annie_wang 已提交
320
**Parameters**
Z
zengyawen 已提交
321 322 323 324
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|

A
Annie_wang 已提交
325
**Return value**
Z
zengyawen 已提交
326 327 328 329
  | Type| Description|
  | -------- | -------- |
  | number | Value in the specified column as a Long.|

A
Annie_wang 已提交
330 331
**Example**
  ```js
Z
zengyawen 已提交
332 333
  const age = resultSet.getLong(resultSet.getColumnIndex("AGE"))
  ```
Z
zengyawen 已提交
334

Z
zengyawen 已提交
335 336 337 338 339 340 341 342 343

### getDouble

getDouble(columnIndex: number): number

Obtains the value in the specified column in the current row as a double.

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

A
Annie_wang 已提交
344
**Parameters**
Z
zengyawen 已提交
345 346 347 348
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|

A
Annie_wang 已提交
349
**Return value**
Z
zengyawen 已提交
350 351 352 353
  | Type| Description|
  | -------- | -------- |
  | number | Value in the specified column as a double.|

A
Annie_wang 已提交
354 355
**Example**
  ```js
Z
zengyawen 已提交
356 357 358 359 360 361 362 363 364 365 366 367
  const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"))
  ```


### isColumnNull

isColumnNull(columnIndex: number): boolean

Checks whether the value in the specified column of the current row is null.

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

A
Annie_wang 已提交
368
**Parameters**
Z
zengyawen 已提交
369 370 371 372
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|

A
Annie_wang 已提交
373
**Return value**
Z
zengyawen 已提交
374 375 376 377
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the value is null; returns **false** otherwise.|

A
Annie_wang 已提交
378 379
**Example**
  ```js
Z
zengyawen 已提交
380 381 382 383 384 385 386 387 388 389 390 391
  const isColumnNull = resultSet.isColumnNull(resultSet.getColumnIndex("CODES"))
  ```


### close

close(): void

Closes this result set.

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

A
Annie_wang 已提交
392 393 394 395 396
**Example**
  ```js
  let predicatesClose = new dataRdb.RdbPredicates("EMPLOYEE")
  let promiseClose = rdbStore.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promiseClose.then((resultSet) {
Z
zengyawen 已提交
397 398
      resultSet.close()
  }).catch((err) => {
A
Annie_wang 已提交
399
      console.log('Failed to close resultset')
Z
zengyawen 已提交
400 401
  })
  ```