1# @ohos.data.dataShareResultSet (DataShare Result Set) 2 3The **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. 4 5> **NOTE** 6> 7> - 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. 8> 9> - The APIs provided by this module are system APIs. 10> 11> - The APIs of this module can be used only in the stage model. 12 13 14## Modules to Import 15 16```ts 17import DataShareResultSet from '@ohos.data.DataShareResultSet'; 18``` 19 20## Usage 21 22You can call [query()](js-apis-data-dataShare.md#query) to obtain the **DataShareResultSet** object. 23 24```ts 25import dataShare from '@ohos.data.dataShare'; 26import dataSharePredicates from '@ohos.data.dataSharePredicates' 27import { BusinessError } from '@ohos.base' 28 29let dataShareHelper: dataShare.DataShareHelper | undefined = undefined; 30let uri = ("datashare:///com.samples.datasharetest.DataShare"); 31dataShare.createDataShareHelper(this.context, uri, (err, data) => { 32 if (err != undefined) { 33 console.error("createDataShareHelper fail, error message : " + err); 34 } else { 35 console.info("createDataShareHelper end, data : " + data); 36 dataShareHelper = data; 37 } 38}); 39 40let columns = ["*"]; 41let da = new dataSharePredicates.DataSharePredicates(); 42let resultSet: DataShareResultSet | undefined = undefined; 43da.equalTo("name", "ZhangSan"); 44if (dataShareHelper != undefined) { 45 (dataShareHelper as dataShare.DataShareHelper).query(uri, da, columns).then((data: DataShareResultSet) => { 46 console.info("query end, data : " + data); 47 resultSet = data; 48 }).catch((err: BusinessError) => { 49 console.error("query fail, error message : " + err); 50 }); 51} 52``` 53 54## DataShareResultSet 55Provides methods for accessing the result sets generated by querying the database. 56 57### Attributes 58 59**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 60 61| Name | Type | Mandatory| Description | 62| ----------- | ------------- | ---- | ------------------------ | 63| columnNames | Array<string> | Yes | Names of all columns in the result set. | 64| columnCount | number | Yes | Number of columns in the result set. | 65| rowCount | number | Yes | Number of rows in the result set. | 66| isClosed | boolean | Yes | Whether the result set is closed.| 67 68### goToFirstRow 69 70goToFirstRow(): boolean 71 72Moves to the first row of the result set. 73 74**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 75 76**Return value** 77 78| Type | Description | 79| :------ | --------------------------------------------- | 80| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 81 82**Example** 83 84```ts 85if (resultSet != undefined) { 86 let isGoToFirstRow = (resultSet as DataShareResultSet).goToFirstRow(); 87 console.info('resultSet.goToFirstRow: ' + isGoToFirstRow); 88} 89``` 90 91### goToLastRow 92 93goToLastRow(): boolean 94 95Moves to the last row of the result set. 96 97**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 98 99**Return value** 100 101| Type| Description| 102| -------- | -------- | 103| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 104 105**Example** 106 107```ts 108if (resultSet != undefined) { 109 let isGoToLastRow = (resultSet as DataShareResultSet).goToLastRow(); 110 console.info('resultSet.goToLastRow: ' + isGoToLastRow); 111} 112``` 113 114### goToNextRow 115 116goToNextRow(): boolean 117 118Moves to the next row in the result set. 119 120**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 121 122**Return value** 123 124| Type | Description | 125| ------- | --------------------------------------------- | 126| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 127 128**Example** 129 130```ts 131if (resultSet != undefined) { 132 let isGoToNextRow = (resultSet as DataShareResultSet).goToNextRow(); 133 console.info('resultSet.goToNextRow: ' + isGoToNextRow); 134} 135``` 136 137### goToPreviousRow 138 139goToPreviousRow(): boolean 140 141Moves to the previous row in the result set. 142 143**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 144 145**Return value** 146 147| Type | Description | 148| ------- | --------------------------------------------- | 149| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 150 151**Example** 152 153```ts 154if (resultSet != undefined) { 155 let isGoToPreviousRow = (resultSet as DataShareResultSet).goToPreviousRow(); 156 console.info('resultSet.goToPreviousRow: ' + isGoToPreviousRow); 157} 158``` 159 160### goTo 161 162goTo(offset:number): boolean 163 164Moves based on the specified offset. 165 166**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 167 168**Parameters** 169 170| **Name**| **Type**| **Mandatory**| Description | 171| ---------- | -------- | -------- | ------------------------------------------------------------ | 172| offset | number | Yes | Offset relative to the current position. A negative value means to move backward, and a positive value means to move forward.| 173 174**Return value** 175 176| Type | Description | 177| ------- | --------------------------------------------- | 178| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 179 180**Example** 181 182```ts 183let goToNum = 1; 184if (resultSet != undefined) { 185 let isGoTo = (resultSet as DataShareResultSet).goTo(goToNum); 186 console.info('resultSet.goTo: ' + isGoTo); 187} 188``` 189 190### goToRow 191 192goToRow(position: number): boolean 193 194Moves to the specified row in the result set. 195 196**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 197 198**Parameters** 199 200| **Name**| **Type**| **Mandatory**| Description | 201| ---------- | -------- | -------- | ------------------------ | 202| position | number | Yes | Destination position to move to.| 203 204**Return value** 205 206| Type | Description | 207| ------- | --------------------------------------------- | 208| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 209 210**Example** 211 212```ts 213let goToRowNum = 2; 214if (resultSet != undefined) { 215 let isGoToRow = (resultSet as DataShareResultSet).goToRow(goToRowNum); 216 console.info('resultSet.goToRow: ' + isGoToRow); 217} 218``` 219 220### getBlob 221 222getBlob(columnIndex: number): Uint8Array 223 224Obtains the value in the form of a byte array based on the specified column and the current row. 225 226**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 227 228**Parameters** 229 230| **Name** | **Type**| **Mandatory**| Description | 231| ----------- | -------- | -------- | ----------------------- | 232| columnIndex | number | Yes | Index of the target column, starting from 0.| 233 234**Return value** 235 236| Type | Description | 237| ---------- | -------------------------------- | 238| Uint8Array | Value obtained.| 239 240**Example** 241 242```ts 243let columnIndex = 1; 244if (resultSet != undefined) { 245 let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow(); 246 let getBlob = (resultSet as DataShareResultSet).getBlob(columnIndex); 247 console.info('resultSet.getBlob: ' + getBlob); 248} 249``` 250 251### getString 252 253getString(columnIndex: number): string 254 255Obtains the value in the form of a string based on the specified column and the current row. 256 257**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 258 259**Parameters** 260 261| **Name** | **Type**| **Mandatory**| Description | 262| ----------- | -------- | -------- | ----------------------- | 263| columnIndex | number | Yes | Index of the target column, starting from 0.| 264 265**Return value** 266 267| Type | Description | 268| ------ | ---------------------------- | 269| string | Value obtained.| 270 271**Example** 272 273```ts 274let columnIndex = 1; 275if (resultSet != undefined) { 276 let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow(); 277 let getString = (resultSet as DataShareResultSet).getString(columnIndex); 278 console.info('resultSet.getString: ' + getString); 279} 280``` 281 282### getLong 283 284getLong(columnIndex: number): number 285 286Obtains the value in the form of a long integer based on the specified column and the current row. 287 288**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 289 290**Parameters** 291 292| **Name** | **Type**| **Mandatory**| Description | 293| ----------- | -------- | -------- | ----------------------- | 294| columnIndex | number | Yes | Index of the target column, starting from 0.| 295 296**Return value** 297 298| Type | Description | 299| ------ | -------------------------- | 300| number | Value obtained.| 301 302**Example** 303 304```ts 305let columnIndex = 1; 306if (resultSet != undefined) { 307 let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow(); 308 let getLong = (resultSet as DataShareResultSet).getLong(columnIndex); 309 console.info('resultSet.getLong: ' + getLong); 310} 311``` 312 313### getDouble 314 315getDouble(columnIndex: number): number 316 317Obtains the value in the form of a double-precision floating-point number based on the specified column and the current row. 318 319**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 320 321**Parameters** 322 323| **Name** | **Type**| **Mandatory**| Description | 324| ----------- | -------- | -------- | ----------------------- | 325| columnIndex | number | Yes | Index of the target column, starting from 0.| 326 327**Return value** 328 329| Type | Description | 330| ------ | ---------------------------- | 331| number | Value obtained.| 332 333**Example** 334 335```ts 336let columnIndex = 1; 337if (resultSet != undefined) { 338 let goToFirstRow = (resultSet as DataShareResultSet).goToFirstRow(); 339 let getDouble = (resultSet as DataShareResultSet).getDouble(columnIndex); 340 console.info('resultSet.getDouble: ' + getDouble); 341} 342``` 343 344### close 345 346close(): void 347 348Closes this result set. 349 350**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 351 352**Example** 353 354```ts 355if (resultSet != undefined) { 356 (resultSet as DataShareResultSet).close(); 357} 358``` 359 360### getColumnIndex 361 362getColumnIndex(columnName: string): number 363 364Obtains the column index based on the column name. 365 366**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 367 368**Parameters** 369 370| **Name**| **Type**| **Mandatory**| Description | 371| ---------- | -------- | -------- | -------------------------- | 372| columnName | string | Yes | Column name.| 373 374**Return value** 375 376| Type | Description | 377| ------ | ------------------ | 378| number | Column index obtained.| 379 380**Example** 381 382```ts 383let ColumnName = "name"; 384if (resultSet != undefined) { 385 let getColumnIndex = (resultSet as DataShareResultSet).getColumnIndex(ColumnName); 386 console.info('resultSet.getColumnIndex: ' + getColumnIndex); 387} 388``` 389 390### getColumnName 391 392getColumnName(columnIndex: number): string 393 394Obtains the column name based on the column index. 395 396**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 397 398**Parameters** 399 400| **Name** | **Type**| **Mandatory**| Description | 401| ----------- | -------- | -------- | -------------------------- | 402| columnIndex | number | Yes | Column index.| 403 404**Return value** 405 406| Type | Description | 407| ------ | ------------------ | 408| string | Column name obtained.| 409 410**Example** 411 412```ts 413let columnIndex = 1; 414if (resultSet != undefined) { 415 let getColumnName = (resultSet as DataShareResultSet).getColumnName(columnIndex); 416 console.info('resultSet.getColumnName: ' + getColumnName); 417} 418``` 419 420### getDataType 421 422getDataType(columnIndex: number): DataType 423 424Obtains the data type based on the specified column index. 425 426**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 427 428**Parameters** 429 430| **Name** | **Type**| **Mandatory**| Description | 431| ----------- | -------- | -------- | -------------------------- | 432| columnIndex | number | Yes | Column index.| 433 434**Return value** 435 436| Type | Description | 437| --------------------- | ------------------ | 438| [DataType](#datatype) | Data type obtained.| 439 440**Example** 441 442```ts 443let columnIndex = 1; 444if (resultSet != undefined) { 445 let getDataType = (resultSet as DataShareResultSet).getDataType(columnIndex); 446 console.info('resultSet.getDataType: ' + getDataType); 447} 448``` 449 450## DataType 451 452Enumerates the data types. 453 454**System capability**: SystemCapability.DistributedDataManager.DataShare.Core 455 456| Name | Value| Description | 457| ----------- | ------ | -------------------- | 458| TYPE_NULL | 0 | Null. | 459| TYPE_LONG | 1 | Long integer. | 460| TYPE_DOUBLE | 2 | Double-precision floating-point number.| 461| TYPE_STRING | 3 | String.| 462| TYPE_BLOB | 4 | Byte array.| 463