1# Result Set 2 3A 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. 4 5> **NOTE**<br/> 6> 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. 8 9 10## Usage 11 12You need to use [RdbStore.query()](js-apis-data-rdb.md#query) to obtain a **resultSet** object. 13 14```js 15import dataRdb from '@ohos.data.rdb'; 16let predicates = new dataRdb.RdbPredicates("EMPLOYEE") 17predicates.equalTo("AGE", 18) 18let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]) 19promise.then((resultSet) => { 20 console.log(TAG + "resultSet columnNames:" + resultSet.columnNames); 21 console.log(TAG + "resultSet columnCount:" + resultSet.columnCount);}) 22``` 23 24## ResultSet 25 26Provides methods to access the result set, which is obtained by querying the RDB store. 27 28 29### Attributes 30 31**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 32 33| Name| Type| Mandatory| Description| 34| -------- | -------- | -------- | -------- | 35| columnNames | Array<string> | Yes| Names of all columns in the result set.| 36| columnCount | number | Yes| Number of columns in the result set.| 37| rowCount | number | Yes| Number of rows in the result set.| 38| rowIndex | number | Yes| Index of the current row in the result set.| 39| isAtFirstRow | boolean | Yes| Whether the cursor is in the first row of the result set.| 40| isAtLastRow | boolean | Yes| Whether the cursor is in the last row of the result set.| 41| isEnded | boolean | Yes| Whether the cursor is after the last row of the result set.| 42| isStarted | boolean | Yes| Whether the cursor has been moved.| 43| isClosed | boolean | Yes| Whether the result set is closed.| 44 45 46### getColumnIndex 47 48getColumnIndex(columnName: string): number 49 50Obtains the column index based on the column name. 51 52**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 53 54**Parameters** 55 | Name| Type| Mandatory| Description| 56 | -------- | -------- | -------- | -------- | 57 | columnName | string | Yes| Column name specified.| 58 59**Return value** 60 | Type| Description| 61 | -------- | -------- | 62 | number | Index of the column obtained.| 63 64**Example** 65 ```js 66 resultSet.goToFirstRow() 67 const id = resultSet.getLong(resultSet.getColumnIndex("ID")) 68 const name = resultSet.getString(resultSet.getColumnIndex("NAME")) 69 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")) 70 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")) 71 ``` 72 73 74### getColumnName 75 76getColumnName(columnIndex: number): string 77 78Obtains the column name based on the column index. 79 80**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 81 82**Parameters** 83 | Name| Type| Mandatory| Description| 84 | -------- | -------- | -------- | -------- | 85 | columnIndex | number | Yes| Column index specified.| 86 87**Return value** 88 | Type| Description| 89 | -------- | -------- | 90 | string | Column name obtained.| 91 92**Example** 93 ```js 94 const id = resultSet.getColumnName(0) 95 const name = resultSet.getColumnName(1) 96 const age = resultSet.getColumnName(2) 97 ``` 98 99 100### goTo 101 102goTo(offset:number): boolean 103 104Moves the cursor to the row based on the specified offset. 105 106**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 107 108**Parameters** 109 | Name| Type| Mandatory| Description| 110 | -------- | -------- | -------- | -------- | 111 | offset | number | Yes| Offset relative to the current position.| 112 113**Return value** 114 | Type| Description| 115 | -------- | -------- | 116 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 117 118**Example** 119 ```js 120 let predicatesgoto = new dataRdb.RdbPredicates("EMPLOYEE") 121 let promisequerygoto = rdbStore.query(predicatesgoto, ["ID", "NAME", "AGE", "SALARY", "CODES"]) 122 promisequerygoto.then((resultSet) { 123 resultSet.goTo(1) 124 resultSet.close() 125 }).catch((err) => { 126 console.log('query failed') 127 }) 128 ``` 129 130 131### goToRow 132 133goToRow(position: number): boolean 134 135Moves the cursor to the specified row in the result set. 136 137**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 138 139**Parameters** 140 | Name| Type| Mandatory| Description| 141 | -------- | -------- | -------- | -------- | 142 | position | number | Yes| Position to which the cursor is to be moved.| 143 144**Return value** 145 | Type| Description| 146 | -------- | -------- | 147 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 148 149**Example** 150 ```js 151 let predicatesgotorow = new dataRdb.RdbPredicates("EMPLOYEE") 152 let promisequerygotorow = rdbStore.query(predicatesgotorow, ["ID", "NAME", "AGE", "SALARY", "CODES"]) 153 promisequerygotorow.then((resultSet) { 154 resultSet.goToRow(5) 155 resultSet.close() 156 }).catch((err) => { 157 console.log('query failed') 158 }) 159 ``` 160 161 162### goToFirstRow 163 164goToFirstRow(): boolean 165 166 167Moves the cursor to the first row of the result set. 168 169**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 170 171**Return value** 172 | Type| Description| 173 | -------- | -------- | 174 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 175 176**Example** 177 ```js 178 let predicatesgoFirst = new dataRdb.RdbPredicates("EMPLOYEE") 179 let promisequerygoFirst = rdbStore.query(predicatesgoFirst, ["ID", "NAME", "AGE", "SALARY", "CODES"]) 180 promisequerygoFirst.then((resultSet) { 181 resultSet.goToFirstRow() 182 resultSet.close() 183 }).catch((err) => { 184 console.log('query failed') 185 }) 186 ``` 187 188 189### goToLastRow 190 191goToLastRow(): boolean 192 193Moves the cursor to the last row of the result set. 194 195**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 196 197**Return value** 198 | Type| Description| 199 | -------- | -------- | 200 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 201 202**Example** 203 ```js 204 let predicatesgoLast = new dataRdb.RdbPredicates("EMPLOYEE") 205 let promisequerygoLast = rdbStore.query(predicatesgoLast, ["ID", "NAME", "AGE", "SALARY", "CODES"]) 206 promisequerygoLast.then((resultSet) { 207 resultSet.goToLastRow() 208 resultSet.close() 209 }).catch((err) => { 210 console.log('query failed') 211 }) 212 ``` 213 214 215### goToNextRow 216 217goToNextRow(): boolean 218 219Moves the cursor to the next row in the result set. 220 221**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 222 223**Return value** 224 | Type| Description| 225 | -------- | -------- | 226 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 227 228**Example** 229 ```js 230 let predicatesgoNext = new dataRdb.RdbPredicates("EMPLOYEE") 231 let promisequerygoNext = rdbStore.query(predicatesgoNext, ["ID", "NAME", "AGE", "SALARY", "CODES"]) 232 promisequerygoNext.then((resultSet) { 233 resultSet.goToNextRow() 234 resultSet.close() 235 }).catch((err) => { 236 console.log('query failed') 237 }) 238 ``` 239 240 241### goToPreviousRow 242 243goToPreviousRow(): boolean 244 245Moves the cursor to the previous row in the result set. 246 247**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 248 249**Return value** 250 | Type| Description| 251 | -------- | -------- | 252 | boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 253 254**Example** 255 ```js 256 let predicatesgoPrev = new dataRdb.RdbPredicates("EMPLOYEE") 257 let promisequerygoPrev = rdbStore.query(predicatesgoPrev, ["ID", "NAME", "AGE", "SALARY", "CODES"]) 258 promisequerygoPrev.then((resultSet) { 259 resultSet.goToPreviousRow() 260 resultSet.close() 261 }).catch((err) => { 262 console.log('query failed') 263 }) 264 ``` 265 266 267### getBlob 268 269getBlob(columnIndex: number): Uint8Array 270 271Obtains the value in the specified column in the current row as a byte array. 272 273**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 274 275**Parameters** 276 | Name| Type| Mandatory| Description| 277 | -------- | -------- | -------- | -------- | 278 | columnIndex | number | Yes| Index of the specified column, starting from 0.| 279 280**Return value** 281 | Type| Description| 282 | -------- | -------- | 283 | Uint8Array | Value in the specified column as a byte array.| 284 285**Example** 286 ```js 287 const codes = resultSet.getBlob(resultSet.getColumnIndex("CODES")) 288 ``` 289 290 291### getString 292 293getString(columnIndex: number): string 294 295Obtains the value in the specified column in the current row as a string. 296 297**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 298 299**Parameters** 300 | Name| Type| Mandatory| Description| 301 | -------- | -------- | -------- | -------- | 302 | columnIndex | number | Yes| Index of the specified column, starting from 0.| 303 304**Return value** 305 | Type| Description| 306 | -------- | -------- | 307 | string | Value in the specified column as a string.| 308 309**Example** 310 ```js 311 const name = resultSet.getString(resultSet.getColumnIndex("NAME")) 312 ``` 313 314 315### getLong 316 317getLong(columnIndex: number): number 318 319Obtains the value in the specified column in the current row as a Long. 320 321**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 322 323**Parameters** 324 | Name| Type| Mandatory| Description| 325 | -------- | -------- | -------- | -------- | 326 | columnIndex | number | Yes| Index of the specified column, starting from 0.| 327 328**Return value** 329 | Type| Description| 330 | -------- | -------- | 331 | number | Value in the specified column as a Long.| 332 333**Example** 334 ```js 335 const age = resultSet.getLong(resultSet.getColumnIndex("AGE")) 336 ``` 337 338 339### getDouble 340 341getDouble(columnIndex: number): number 342 343Obtains the value in the specified column in the current row as a double. 344 345**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 346 347**Parameters** 348 | Name| Type| Mandatory| Description| 349 | -------- | -------- | -------- | -------- | 350 | columnIndex | number | Yes| Index of the specified column, starting from 0.| 351 352**Return value** 353 | Type| Description| 354 | -------- | -------- | 355 | number | Value in the specified column as a double.| 356 357**Example** 358 ```js 359 const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")) 360 ``` 361 362 363### isColumnNull 364 365isColumnNull(columnIndex: number): boolean 366 367Checks whether the value in the specified column of the current row is null. 368 369**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 370 371**Parameters** 372 | Name| Type| Mandatory| Description| 373 | -------- | -------- | -------- | -------- | 374 | columnIndex | number | Yes| Index of the specified column, starting from 0.| 375 376**Return value** 377 | Type| Description| 378 | -------- | -------- | 379 | boolean | Returns **true** if the value is null; returns **false** otherwise.| 380 381**Example** 382 ```js 383 const isColumnNull = resultSet.isColumnNull(resultSet.getColumnIndex("CODES")) 384 ``` 385 386 387### close 388 389close(): void 390 391Closes this result set. 392 393**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core 394 395**Example** 396 ```js 397 let predicatesClose = new dataRdb.RdbPredicates("EMPLOYEE") 398 let promiseClose = rdbStore.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"]) 399 promiseClose.then((resultSet) { 400 resultSet.close() 401 }).catch((err) => { 402 console.log('Failed to close resultset') 403 }) 404 ``` 405