• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# resultSet (结果集)
2
3结果集是指用户调用关系型数据库查询接口之后返回的结果集合,提供了多种灵活的数据访问方式,以便用户获取各项数据。
4
5> **说明:**
6>
7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 从API Version 9开始,该接口不再维护,推荐使用新接口[@ohos.data.relationalStore#ResultSet](js-apis-data-relationalStore.md#resultset)。
10
11## ResultSet
12
13提供通过查询数据库生成的数据库结果集的访问方法。
14
15### 使用说明
16
17需要通过[RdbStore.query()](js-apis-data-rdb.md#query)获取resultSet对象。
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### 属性
31
32**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
33
34| 名称 | 类型 | 必填 | 说明 |
35| -------- | -------- | -------- | -------- |
36| columnNames | Array<string> | 是 | 获取结果集中所有列的名称。 |
37| columnCount | number | 是 | 获取结果集中的列数。 |
38| rowCount | number | 是 | 获取结果集中的行数。 |
39| rowIndex | number | 是 | 获取结果集当前行的索引。 |
40| isAtFirstRow | boolean | 是 | 检查结果集是否位于第一行。 |
41| isAtLastRow | boolean | 是 | 检查结果集是否位于最后一行。 |
42| isEnded | boolean | 是 | 检查结果集是否位于最后一行之后。 |
43| isStarted | boolean | 是 | 检查指针是否移动过。 |
44| isClosed | boolean | 是 | 检查当前结果集是否关闭。 |
45
46### getColumnIndex
47
48getColumnIndex(columnName: string): number
49
50根据指定的列名获取列索引。
51
52**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
53
54**参数:**
55
56  | 参数名 | 类型 | 必填 | 说明 |
57  | -------- | -------- | -------- | -------- |
58  | columnName | string | 是 | 表示结果集中指定列的名称。 |
59
60**返回值:**
61
62  | 类型 | 说明 |
63  | -------- | -------- |
64  | number | 返回指定列的索引。 |
65
66**示例:**
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
82根据指定的列索引获取列名。
83
84**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
85
86**参数:**
87
88  | 参数名 | 类型 | 必填 | 说明 |
89  | -------- | -------- | -------- | -------- |
90  | columnIndex | number | 是 | 表示结果集中指定列的索引。 |
91
92**返回值:**
93
94  | 类型 | 说明 |
95  | -------- | -------- |
96  | string | 返回指定列的名称。 |
97
98**示例:**
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
110向前或向后转至结果集的指定行,相对于其当前位置偏移。
111
112**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
113
114**参数:**
115
116  | 参数名 | 类型 | 必填 | 说明 |
117  | -------- | -------- | -------- | -------- |
118  | offset | number | 是 | 表示相对于当前位置的偏移量。 |
119
120**返回值:**
121
122  | 类型 | 说明 |
123  | -------- | -------- |
124  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
125
126**示例:**
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
143转到结果集的指定行。
144
145**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
146
147**参数:**
148
149  | 参数名 | 类型 | 必填 | 说明 |
150  | -------- | -------- | -------- | -------- |
151  | position | number | 是 | 表示要移动到的指定位置。 |
152
153**返回值:**
154
155  | 类型 | 说明 |
156  | -------- | -------- |
157  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
158
159**示例:**
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
176转到结果集的第一行。
177
178**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
179
180**返回值:**
181
182  | 类型 | 说明 |
183  | -------- | -------- |
184  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
185
186**示例:**
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
203转到结果集的最后一行。
204
205**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
206
207**返回值:**
208
209  | 类型 | 说明 |
210  | -------- | -------- |
211  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
212
213**示例:**
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
230转到结果集的下一行。
231
232**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
233
234**返回值:**
235
236  | 类型 | 说明 |
237  | -------- | -------- |
238  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
239
240**示例:**
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
257转到结果集的上一行。
258
259**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
260
261**返回值:**
262
263  | 类型 | 说明 |
264  | -------- | -------- |
265  | boolean | 如果成功移动结果集,则为true;否则返回false。 |
266
267**示例:**
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
284以字节数组的形式获取当前行中指定列的值。
285
286**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
287
288**参数:**
289
290  | 参数名 | 类型 | 必填 | 说明 |
291  | -------- | -------- | -------- | -------- |
292  | columnIndex | number | 是 | 指定的列索引,从0开始。 |
293
294**返回值:**
295
296  | 类型 | 说明 |
297  | -------- | -------- |
298  | Uint8Array | 以字节数组的形式返回指定列的值。 |
299
300**示例:**
301
302```js
303const codes = resultSet.getBlob(resultSet.getColumnIndex("CODES"));
304```
305
306### getString
307
308getString(columnIndex: number): string
309
310以字符串形式获取当前行中指定列的值。
311
312**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
313
314**参数:**
315
316  | 参数名 | 类型 | 必填 | 说明 |
317  | -------- | -------- | -------- | -------- |
318  | columnIndex | number | 是 | 指定的列索引,从0开始。 |
319
320**返回值:**
321
322  | 类型 | 说明 |
323  | -------- | -------- |
324  | string | 以字符串形式返回指定列的值。 |
325
326**示例:**
327
328```js
329const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
330```
331
332### getLong
333
334getLong(columnIndex: number): number
335
336以Long形式获取当前行中指定列的值。
337
338**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
339
340**参数:**
341
342  | 参数名 | 类型 | 必填 | 说明 |
343  | -------- | -------- | -------- | -------- |
344  | columnIndex | number | 是 | 指定的列索引,从0开始。 |
345
346**返回值:**
347
348| 类型 | 说明 |
349| -------- | -------- |
350| number | 以Long形式返回指定列的值。<br/>该接口支持的数据范围是:Number.MIN_SAFE_INTEGER ~ Number.MAX_SAFE_INTEGER,若超出该范围,建议使用[getDouble](#getdouble)。 |
351
352**示例:**
353
354```js
355const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
356```
357
358### getDouble
359
360getDouble(columnIndex: number): number
361
362以double形式获取当前行中指定列的值。
363
364**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
365
366**参数:**
367
368  | 参数名 | 类型 | 必填 | 说明 |
369  | -------- | -------- | -------- | -------- |
370  | columnIndex | number | 是 | 指定的列索引,从0开始。 |
371
372**返回值:**
373
374  | 类型 | 说明 |
375  | -------- | -------- |
376  | number | 以double形式返回指定列的值。 |
377
378**示例:**
379
380```js
381const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
382```
383
384### isColumnNull
385
386isColumnNull(columnIndex: number): boolean
387
388检查当前行中指定列的值是否为null。
389
390**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
391
392**参数:**
393
394  | 参数名 | 类型 | 必填 | 说明 |
395  | -------- | -------- | -------- | -------- |
396  | columnIndex | number | 是 | 指定的列索引,从0开始。 |
397
398**返回值:**
399
400  | 类型 | 说明 |
401  | -------- | -------- |
402  | boolean | 如果当前行中指定列的值为null,则返回true,否则返回false。 |
403
404**示例:**
405
406```js
407const isColumnNull = resultSet.isColumnNull(resultSet.getColumnIndex("CODES"));
408```
409
410### close
411
412close(): void
413
414关闭结果集。
415
416**系统能力:** SystemCapability.DistributedDataManager.RelationalStore.Core
417
418**示例:**
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
430