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