• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# resultSet (结果集)
2<!--Kit: ArkData-->
3<!--Subsystem: DistributedDataManager-->
4<!--Owner: @baijidong-->
5<!--Designer: @widecode; @htt1997-->
6<!--Tester: @yippo; @logic42-->
7<!--Adviser: @ge-yafang-->
8
9结果集是指用户调用关系型数据库查询接口之后返回的结果集合,提供了多种灵活的数据访问方式,以便用户获取各项数据。
10
11> **说明:**
12>
13> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
14>
15> 从API Version 9开始,该接口不再维护,推荐使用新接口[@ohos.data.relationalStore#ResultSet](arkts-apis-data-relationalStore-ResultSet.md)。
16
17## ResultSet
18
19提供通过查询数据库生成的数据库结果集的访问方法。
20
21### 使用说明
22
23需要通过[RdbStore.query()](js-apis-data-rdb.md#query)获取resultSet对象。
24
25```js
26import dataRdb from '@ohos.data.rdb';
27let predicates = new dataRdb.RdbPredicates("EMPLOYEE");
28predicates.equalTo("AGE", 18);
29let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
30promise.then((resultSet) => {
31  console.log(TAG + "resultSet columnNames:" + resultSet.columnNames);
32  console.log(TAG + "resultSet columnCount:" + resultSet.columnCount);
33});
34```
35
36### 属性
37
38**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
39
40| 名称 | 类型 | 必填 | 说明 |
41| -------- | -------- | -------- | -------- |
42| columnNames | Array&lt;string&gt; | 是 | 获取结果集中所有列的名称。 |
43| columnCount | number | 是 | 获取结果集中的列数。 |
44| rowCount | number | 是 | 获取结果集中的行数。 |
45| rowIndex | number | 是 | 获取结果集当前行的索引。 |
46| isAtFirstRow | boolean | 是 | 检查结果集是否位于第一行。 |
47| isAtLastRow | boolean | 是 | 检查结果集是否位于最后一行。 |
48| isEnded | boolean | 是 | 检查结果集是否位于最后一行之后。 |
49| isStarted | boolean | 是 | 检查指针是否移动过。 |
50| isClosed | boolean | 是 | 检查当前结果集是否关闭。 |
51
52### getColumnIndex
53
54getColumnIndex(columnName: string): number
55
56根据指定的列名获取列索引。
57
58**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
59
60**参数:**
61
62  | 参数名 | 类型 | 必填 | 说明 |
63  | -------- | -------- | -------- | -------- |
64  | columnName | string | 是 | 表示结果集中指定列的名称。 |
65
66**返回值:**
67
68  | 类型 | 说明 |
69  | -------- | -------- |
70  | number | 返回指定列的索引。 |
71
72**示例:**
73
74```js
75const success = resultSet.goToFirstRow();
76if (success) {
77  const id = resultSet.getLong(resultSet.getColumnIndex("ID"));
78  const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
79  const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
80  const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
81}
82```
83
84### getColumnName
85
86getColumnName(columnIndex: number): string
87
88根据指定的列索引获取列名。
89
90**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
91
92**参数:**
93
94  | 参数名 | 类型 | 必填 | 说明 |
95  | -------- | -------- | -------- | -------- |
96  | columnIndex | number | 是 | 表示结果集中指定列的索引。 |
97
98**返回值:**
99
100  | 类型 | 说明 |
101  | -------- | -------- |
102  | string | 返回指定列的名称。 |
103
104**示例:**
105
106```js
107const id = resultSet.getColumnName(0);
108const name = resultSet.getColumnName(1);
109const age = resultSet.getColumnName(2);
110```
111
112### goTo
113
114goTo(offset:number): boolean
115
116向前或向后转至结果集的指定行,相对于其当前位置偏移。
117
118**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
119
120**参数:**
121
122  | 参数名 | 类型 | 必填 | 说明 |
123  | -------- | -------- | -------- | -------- |
124  | offset | number | 是 | 表示相对于当前位置的偏移量。 |
125
126**返回值:**
127
128  | 类型 | 说明 |
129  | -------- | -------- |
130  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
131
132**示例:**
133
134```js
135let predicatesgoto = new dataRdb.RdbPredicates("EMPLOYEE");
136let promisequerygoto = rdbStore.query(predicatesgoto, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
137promisequerygoto.then((resultSet) => {
138  resultSet.goTo(1);
139  resultSet.close();
140}).catch((err) => {
141  console.log('query failed');
142});
143```
144
145### goToRow
146
147goToRow(position: number): boolean
148
149转到结果集的指定行。
150
151**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
152
153**参数:**
154
155  | 参数名 | 类型 | 必填 | 说明 |
156  | -------- | -------- | -------- | -------- |
157  | position | number | 是 | 表示要移动到的指定位置。 |
158
159**返回值:**
160
161  | 类型 | 说明 |
162  | -------- | -------- |
163  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
164
165**示例:**
166
167```js
168let predicatesgotorow = new dataRdb.RdbPredicates("EMPLOYEE");
169let promisequerygotorow = rdbStore.query(predicatesgotorow, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
170promisequerygotorow.then((resultSet) => {
171  resultSet.goToRow(5);
172  resultSet.close();
173}).catch((err) => {
174  console.log('query failed');
175});
176```
177
178### goToFirstRow
179
180goToFirstRow(): boolean
181
182转到结果集的第一行。
183
184**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
185
186**返回值:**
187
188  | 类型 | 说明 |
189  | -------- | -------- |
190  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
191
192**示例:**
193
194```js
195let predicatesgoFirst = new dataRdb.RdbPredicates("EMPLOYEE");
196let promisequerygoFirst = rdbStore.query(predicatesgoFirst, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
197promisequerygoFirst.then((resultSet) => {
198  resultSet.goToFirstRow();
199  resultSet.close();
200}).catch((err) => {
201  console.log('query failed');
202});
203```
204
205### goToLastRow
206
207goToLastRow(): boolean
208
209转到结果集的最后一行。
210
211**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
212
213**返回值:**
214
215  | 类型 | 说明 |
216  | -------- | -------- |
217  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
218
219**示例:**
220
221```js
222let predicatesgoLast = new dataRdb.RdbPredicates("EMPLOYEE");
223let promisequerygoLast = rdbStore.query(predicatesgoLast, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
224promisequerygoLast.then((resultSet) => {
225  resultSet.goToLastRow();
226  resultSet.close();
227}).catch((err) => {
228  console.log('query failed');
229});
230```
231
232### goToNextRow
233
234goToNextRow(): boolean
235
236转到结果集的下一行。
237
238**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
239
240**返回值:**
241
242  | 类型 | 说明 |
243  | -------- | -------- |
244  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
245
246**示例:**
247
248```js
249let predicatesgoNext = new dataRdb.RdbPredicates("EMPLOYEE");
250let promisequerygoNext = rdbStore.query(predicatesgoNext, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
251promisequerygoNext.then((resultSet) => {
252  resultSet.goToNextRow();
253  resultSet.close();
254}).catch((err) => {
255  console.log('query failed');
256});
257```
258
259### goToPreviousRow
260
261goToPreviousRow(): boolean
262
263转到结果集的上一行。
264
265**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
266
267**返回值:**
268
269  | 类型 | 说明 |
270  | -------- | -------- |
271  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
272
273**示例:**
274
275```js
276let predicatesgoPrev = new dataRdb.RdbPredicates("EMPLOYEE");
277let promisequerygoPrev = rdbStore.query(predicatesgoPrev, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
278promisequerygoPrev.then((resultSet) => {
279  resultSet.goToPreviousRow();
280  resultSet.close();
281}).catch((err) => {
282  console.log('query failed');
283});
284```
285
286### getBlob
287
288getBlob(columnIndex: number): Uint8Array
289
290以字节数组的形式获取当前行中指定列的值。
291
292**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
293
294**参数:**
295
296  | 参数名 | 类型 | 必填 | 说明 |
297  | -------- | -------- | -------- | -------- |
298  | columnIndex | number | 是 | 指定的列索引,从0开始。 |
299
300**返回值:**
301
302  | 类型 | 说明 |
303  | -------- | -------- |
304  | Uint8Array | 以字节数组的形式返回指定列的值。 |
305
306**示例:**
307
308```js
309const codes = resultSet.getBlob(resultSet.getColumnIndex("CODES"));
310```
311
312### getString
313
314getString(columnIndex: number): string
315
316以字符串形式获取当前行中指定列的值。
317
318**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
319
320**参数:**
321
322  | 参数名 | 类型 | 必填 | 说明 |
323  | -------- | -------- | -------- | -------- |
324  | columnIndex | number | 是 | 指定的列索引,从0开始。 |
325
326**返回值:**
327
328  | 类型 | 说明 |
329  | -------- | -------- |
330  | string | 以字符串形式返回指定列的值。 |
331
332**示例:**
333
334```js
335const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
336```
337
338### getLong
339
340getLong(columnIndex: number): number
341
342以Long形式获取当前行中指定列的值。
343
344**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
345
346**参数:**
347
348  | 参数名 | 类型 | 必填 | 说明 |
349  | -------- | -------- | -------- | -------- |
350  | columnIndex | number | 是 | 指定的列索引,从0开始。 |
351
352**返回值:**
353
354| 类型 | 说明 |
355| -------- | -------- |
356| number | 以Long形式返回指定列的值。<br/>该接口支持的数据范围是:Number.MIN_SAFE_INTEGER ~ Number.MAX_SAFE_INTEGER,若超出该范围,建议使用[getDouble](#getdouble)。 |
357
358**示例:**
359
360```js
361const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
362```
363
364### getDouble
365
366getDouble(columnIndex: number): number
367
368以double形式获取当前行中指定列的值。
369
370**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
371
372**参数:**
373
374  | 参数名 | 类型 | 必填 | 说明 |
375  | -------- | -------- | -------- | -------- |
376  | columnIndex | number | 是 | 指定的列索引,从0开始。 |
377
378**返回值:**
379
380  | 类型 | 说明 |
381  | -------- | -------- |
382  | number | 以double形式返回指定列的值。 |
383
384**示例:**
385
386```js
387const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
388```
389
390### isColumnNull
391
392isColumnNull(columnIndex: number): boolean
393
394检查当前行中指定列的值是否为null。
395
396**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
397
398**参数:**
399
400  | 参数名 | 类型 | 必填 | 说明 |
401  | -------- | -------- | -------- | -------- |
402  | columnIndex | number | 是 | 指定的列索引,从0开始。 |
403
404**返回值:**
405
406  | 类型 | 说明 |
407  | -------- | -------- |
408  | boolean | 如果当前行中指定列的值为null,则返回true,否则返回false。 |
409
410**示例:**
411
412```js
413const isColumnNull = resultSet.isColumnNull(resultSet.getColumnIndex("CODES"));
414```
415
416### close
417
418close(): void
419
420关闭结果集。
421
422**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
423
424**示例:**
425
426```js
427let predicatesClose = new dataRdb.RdbPredicates("EMPLOYEE");
428let promiseClose = rdbStore.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
429promiseClose.then((resultSet) => {
430  resultSet.close();
431}).catch((err) => {
432  console.log('resultset close failed');
433});
434```
435
436