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