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

A
Annie_wang 已提交
3 4
A result set is a set of results returned after the relational database (RDB) query APIs are called. You can use the **resultset** APIs to obtain required data.

A
Annie_wang 已提交
5
> **NOTE**<br/>
A
Annie_wang 已提交
6
> 
Z
zengyawen 已提交
7
> 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 已提交
8

A
annie_wangli 已提交
9

Z
zengyawen 已提交
10
## Usage
Z
zengyawen 已提交
11

A
Annie_wang 已提交
12
You need to use [RdbStore.query()](js-apis-data-rdb.md#query) to obtain a **resultSet** object.
Z
zengyawen 已提交
13

A
Annie_wang 已提交
14
```js
Z
zengyawen 已提交
15 16 17 18 19
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 已提交
20 21
    console.log(TAG + "resultSet columnNames:" + resultSet.columnNames);
    console.log(TAG + "resultSet columnCount:" + resultSet.columnCount);})
Z
zengyawen 已提交
22 23
```

Z
zengyawen 已提交
24 25
## ResultSet

A
Annie_wang 已提交
26
Provides methods to access the result set, which is obtained by querying the RDB store.
Z
zengyawen 已提交
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


### 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 已提交
54
**Parameters**
Z
zengyawen 已提交
55 56 57 58
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnName | string | Yes| Column name specified.|

A
Annie_wang 已提交
59
**Return value**
Z
zengyawen 已提交
60 61 62 63
  | Type| Description|
  | -------- | -------- |
  | number | Index of the column obtained.|

A
Annie_wang 已提交
64 65
**Example**
  ```js
Z
zengyawen 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  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 已提交
82
**Parameters**
Z
zengyawen 已提交
83 84 85 86
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Column index specified.|

A
Annie_wang 已提交
87
**Return value**
Z
zengyawen 已提交
88 89 90 91
  | Type| Description|
  | -------- | -------- |
  | string | Column name obtained.|

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


### goTo

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

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

Z
zengyawen 已提交
106 107
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

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

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

A
Annie_wang 已提交
118 119
**Example**
  ```js
Z
zengyawen 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133
  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 已提交
134 135 136

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

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

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

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

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


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

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


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

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

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

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


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

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

Z
zengyawen 已提交
193 194 195 196
Moves the cursor to the last row of the result set.

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

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

A
Annie_wang 已提交
202 203
**Example**
  ```js
Z
zengyawen 已提交
204 205 206 207 208 209 210 211 212
  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 已提交
213 214


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

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

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

Z
zengyawen 已提交
221 222
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

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

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


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

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

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

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

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

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


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

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

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

Z
zengyawen 已提交
273 274
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

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

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

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


### getString

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

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

Z
zengyawen 已提交
297 298
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

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

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

A
Annie_wang 已提交
309 310
**Example**
  ```js
Z
zengyawen 已提交
311 312 313 314 315 316 317 318 319 320 321 322
  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 已提交
323
**Parameters**
Z
zengyawen 已提交
324 325 326 327
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|

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

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

Z
zengyawen 已提交
338 339 340 341 342 343 344 345 346

### 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 已提交
347
**Parameters**
Z
zengyawen 已提交
348 349 350 351
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|

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

A
Annie_wang 已提交
357 358
**Example**
  ```js
Z
zengyawen 已提交
359 360 361 362 363 364 365 366 367 368 369 370
  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 已提交
371
**Parameters**
Z
zengyawen 已提交
372 373 374 375
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|

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

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


### close

close(): void

Closes this result set.

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

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