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