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

Z
zengyawen 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
> 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 11 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 21
```


Z
zengyawen 已提交
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103



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

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

- Return value
  | Type| Description|
  | -------- | -------- |
  | number | Index of the column obtained.|

- Example
  ```
  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

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

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

- Example
  ```
  const id = resultSet.getColumnName(0)
  const name = resultSet.getColumnName(1)
  const age = resultSet.getColumnName(2)
  ```


### goTo

goTo(offset:number): boolean
Z
zengyawen 已提交
104 105 106

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

Z
zengyawen 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | offset | number | Yes| Offset relative to the current position.|

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

- Example
  ```
  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 已提交
135 136 137

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

Z
zengyawen 已提交
138
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
Z
zengyawen 已提交
139

Z
zengyawen 已提交
140 141 142 143 144 145 146 147 148
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | position | number | Yes| Position to which the cursor is to be moved.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
149

Z
zengyawen 已提交
150 151 152 153 154 155 156 157 158 159 160
- Example
  ```
  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 已提交
161 162


Z
zengyawen 已提交
163
### goToFirstRow
Z
zengyawen 已提交
164

Z
zengyawen 已提交
165
goToFirstRow(): boolean
Z
zengyawen 已提交
166 167


Z
zengyawen 已提交
168
Moves the cursor to the first row of the result set.
Z
zengyawen 已提交
169

Z
zengyawen 已提交
170
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
Z
zengyawen 已提交
171

Z
zengyawen 已提交
172 173 174 175
- Return value
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
176

Z
zengyawen 已提交
177 178 179 180 181 182 183 184 185 186 187
- Example
  ```
  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 已提交
188 189


Z
zengyawen 已提交
190
### goToLastRow
Z
zengyawen 已提交
191

Z
zengyawen 已提交
192
goToLastRow(): boolean
Z
zengyawen 已提交
193

Z
zengyawen 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
Moves the cursor to the last row of the result set.

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

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

- Example
  ```
  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 已提交
214 215


Z
zengyawen 已提交
216 217 218
### goToNextRow

goToNextRow(): boolean
Z
zengyawen 已提交
219 220 221

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

Z
zengyawen 已提交
222 223 224 225 226 227 228 229 230 231 232 233
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

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

- Example
  ```
  let predicatesgoNext = new dataRdb.RdbPredicates("EMPLOYEE")
  let promisequerygoNext = rdbStore.query(predicatesgoNext, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promisequerygoNext.then((resultSet) {
A
annie_wangli 已提交
234 235
      resultSet.goToNextRow()
      resultSet.close()
Z
zengyawen 已提交
236 237 238 239
  }).catch((err) => {
      console.log('query failed')
  })
  ```
Z
zengyawen 已提交
240 241


Z
zengyawen 已提交
242
### goToPreviousRow
Z
zengyawen 已提交
243

Z
zengyawen 已提交
244
goToPreviousRow(): boolean
Z
zengyawen 已提交
245 246 247

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

Z
zengyawen 已提交
248
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
Z
zengyawen 已提交
249

Z
zengyawen 已提交
250 251 252 253
- Return value
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
254

Z
zengyawen 已提交
255 256 257 258 259 260 261 262 263 264 265
- Example
  ```
  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 已提交
266 267


Z
zengyawen 已提交
268
### getBlob
Z
zengyawen 已提交
269

Z
zengyawen 已提交
270
getBlob(columnIndex: number): Uint8Array
Z
zengyawen 已提交
271 272 273

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

Z
zengyawen 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

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

- Return value
  | Type| Description|
  | -------- | -------- |
  | Uint8Array | Value in the specified column as a byte array.|

- Example
  ```
  const codes = resultSet.getBlob(resultSet.getColumnIndex("CODES"))
  ```


### getString

getString(columnIndex: number): string
Z
zengyawen 已提交
295 296 297

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

Z
zengyawen 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

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

- Return value
  | Type| Description|
  | -------- | -------- |
  | string | Value in the specified column as a string.|

- Example
  ```
  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

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

- Return value
  | Type| Description|
  | -------- | -------- |
  | number | Value in the specified column as a Long.|

- Example
  ```
  const age = resultSet.getLong(resultSet.getColumnIndex("AGE"))
  ```
Z
zengyawen 已提交
338

Z
zengyawen 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

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

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

- Return value
  | Type| Description|
  | -------- | -------- |
  | number | Value in the specified column as a double.|

- Example
  ```
  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

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

- Return value
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the value is null; returns **false** otherwise.|

- Example
  ```
  const isColumnNull = resultSet.isColumnNull(resultSet.getColumnIndex("CODES"))
  ```


### close

close(): void

Closes this result set.

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

- Example
  ```
  let predicatesclose = new dataRdb.RdbPredicates("EMPLOYEE")
  let predicatesclose = rdbStore.query(predicatesclose, ["ID", "NAME", "AGE", "SALARY", "CODES"])
  promisequerygoPrev.then((resultSet) {
      resultSet.close()
  }).catch((err) => {
      console.log('query failed')
  })
  ```