js-apis-data-resultset.md 11.4 KB
Newer Older
A
Annie_wang 已提交
1
# resultSet
Z
zengyawen 已提交
2

A
Annie_wang 已提交
3 4
A 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.

A
Annie_wang 已提交
5
> **NOTE**<br/>
A
Annie_wang 已提交
6
> 
A
annie_wangli 已提交
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.
A
Annie_wang 已提交
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).
Z
zengyawen 已提交
10

A
Annie_wang 已提交
11
## ResultSet
A
Annie_wang 已提交
12 13 14 15 16 17

Provides methods to access the result set, which is obtained by querying the RDB store.

### Usage

You need to obtain a **resultSet** object by using [RdbStore.query()](js-apis-data-rdb.md#query).
Z
zengyawen 已提交
18

A
Annie_wang 已提交
19
```js
Z
zengyawen 已提交
20
import dataRdb from '@ohos.data.rdb';
A
Annie_wang 已提交
21 22 23
let predicates = new dataRdb.RdbPredicates("EMPLOYEE");
predicates.equalTo("AGE", 18);
let promise = rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
Z
zengyawen 已提交
24
promise.then((resultSet) => {
A
annie_wangli 已提交
25
    console.log(TAG + "resultSet columnNames:" + resultSet.columnNames);
A
Annie_wang 已提交
26 27
    console.log(TAG + "resultSet columnCount:" + resultSet.columnCount);
});
Z
zengyawen 已提交
28 29
```

A
Annie_wang 已提交
30
### Attributes
A
annie_wangli 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| columnNames | Array&lt;string&gt; | Yes| Names of all columns in the result set.|
| columnCount | number | Yes| Number of columns in the result set.|
| rowCount | number | Yes| Number of rows in the result set.|
| rowIndex | number | Yes| Index of the current row in the result set.|
| isAtFirstRow | boolean | Yes| Whether the cursor is in the first row of the result set.|
| isAtLastRow | boolean | Yes| Whether the cursor is in the last row of the result set.|
| isEnded | boolean | Yes| Whether the cursor is after the last row of the result set.|
| isStarted | boolean | Yes| Whether the cursor has been moved.|
| isClosed | boolean | Yes| Whether the result set is closed.|

A
Annie_wang 已提交
46
### getColumnIndex
A
annie_wangli 已提交
47 48 49 50 51 52 53

getColumnIndex(columnName: string): number

Obtains the column index based on the column name.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
54
**Parameters**
A
Annie_wang 已提交
55

A
Annie_wang 已提交
56 57 58
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnName | string | Yes| Column name specified.|
A
annie_wangli 已提交
59

A
Annie_wang 已提交
60
**Return value**
A
Annie_wang 已提交
61

A
Annie_wang 已提交
62 63 64
  | Type| Description|
  | -------- | -------- |
  | number | Index of the column obtained.|
A
annie_wangli 已提交
65

A
Annie_wang 已提交
66
**Example**
A
Annie_wang 已提交
67

A
Annie_wang 已提交
68
  ```js
A
Annie_wang 已提交
69 70 71 72 73
  resultSet.goToFirstRow();
  const id = resultSet.getLong(resultSet.getColumnIndex("ID"));
  const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
  const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
  const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
A
annie_wangli 已提交
74 75
  ```

A
Annie_wang 已提交
76
### getColumnName
A
annie_wangli 已提交
77 78 79 80 81 82 83

getColumnName(columnIndex: number): string

Obtains the column name based on the column index.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
84
**Parameters**
A
Annie_wang 已提交
85

A
Annie_wang 已提交
86 87 88
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Column index specified.|
A
annie_wangli 已提交
89

A
Annie_wang 已提交
90
**Return value**
A
Annie_wang 已提交
91

A
Annie_wang 已提交
92 93 94
  | Type| Description|
  | -------- | -------- |
  | string | Column name obtained.|
A
annie_wangli 已提交
95

A
Annie_wang 已提交
96
**Example**
A
Annie_wang 已提交
97

A
Annie_wang 已提交
98
  ```js
A
Annie_wang 已提交
99 100 101
  const id = resultSet.getColumnName(0);
  const name = resultSet.getColumnName(1);
  const age = resultSet.getColumnName(2);
A
annie_wangli 已提交
102 103
  ```

A
Annie_wang 已提交
104
### goTo
A
annie_wangli 已提交
105 106

goTo(offset:number): boolean
Z
zengyawen 已提交
107 108 109

Moves the cursor to the row based on the specified offset.

A
annie_wangli 已提交
110 111
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
112
**Parameters**
A
Annie_wang 已提交
113

A
Annie_wang 已提交
114 115 116
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | offset | number | Yes| Offset relative to the current position.|
A
annie_wangli 已提交
117

A
Annie_wang 已提交
118
**Return value**
A
Annie_wang 已提交
119

A
Annie_wang 已提交
120 121 122
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
123

A
Annie_wang 已提交
124
**Example**
A
Annie_wang 已提交
125

A
Annie_wang 已提交
126
  ```js
A
Annie_wang 已提交
127 128
  let predicatesgoto = new dataRdb.RdbPredicates("EMPLOYEE");
  let promisequerygoto = rdbStore.query(predicatesgoto, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
A
Annie_wang 已提交
129
  promisequerygoto.then((resultSet) => {
A
Annie_wang 已提交
130 131
      resultSet.goTo(1);
      resultSet.close();
A
annie_wangli 已提交
132
  }).catch((err) => {
A
Annie_wang 已提交
133 134
      console.log('query failed');
  });
A
annie_wangli 已提交
135 136
  ```

A
Annie_wang 已提交
137
### goToRow
A
annie_wangli 已提交
138 139

goToRow(position: number): boolean
Z
zengyawen 已提交
140 141 142

Moves the cursor to the specified row in the result set.

A
annie_wangli 已提交
143
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
Z
zengyawen 已提交
144

A
Annie_wang 已提交
145
**Parameters**
A
Annie_wang 已提交
146

A
Annie_wang 已提交
147 148 149
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | position | number | Yes| Position to which the cursor is to be moved.|
A
annie_wangli 已提交
150

A
Annie_wang 已提交
151
**Return value**
A
Annie_wang 已提交
152

A
Annie_wang 已提交
153 154 155
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
156

A
Annie_wang 已提交
157
**Example**
A
Annie_wang 已提交
158

A
Annie_wang 已提交
159
  ```js
A
Annie_wang 已提交
160 161
  let predicatesgotorow = new dataRdb.RdbPredicates("EMPLOYEE");
  let promisequerygotorow = rdbStore.query(predicatesgotorow, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
A
Annie_wang 已提交
162
  promisequerygotorow.then((resultSet) => {
A
Annie_wang 已提交
163 164
      resultSet.goToRow(5);
      resultSet.close();
A
annie_wangli 已提交
165
  }).catch((err) => {
A
Annie_wang 已提交
166 167
      console.log('query failed');
  });
A
annie_wangli 已提交
168
  ```
Z
zengyawen 已提交
169

A
Annie_wang 已提交
170
### goToFirstRow
Z
zengyawen 已提交
171

A
annie_wangli 已提交
172
goToFirstRow(): boolean
Z
zengyawen 已提交
173

A
annie_wangli 已提交
174
Moves the cursor to the first row of the result set.
Z
zengyawen 已提交
175

A
annie_wangli 已提交
176
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
Z
zengyawen 已提交
177

A
Annie_wang 已提交
178
**Return value**
A
Annie_wang 已提交
179

A
Annie_wang 已提交
180 181 182
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
183

A
Annie_wang 已提交
184
**Example**
A
Annie_wang 已提交
185

A
Annie_wang 已提交
186
  ```js
A
Annie_wang 已提交
187 188
  let predicatesgoFirst = new dataRdb.RdbPredicates("EMPLOYEE");
  let promisequerygoFirst = rdbStore.query(predicatesgoFirst, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
A
Annie_wang 已提交
189
  promisequerygoFirst.then((resultSet) => {
A
Annie_wang 已提交
190 191
      resultSet.goToFirstRow();
      resultSet.close();
A
annie_wangli 已提交
192
  }).catch((err) => {
A
Annie_wang 已提交
193 194
      console.log('query failed');
  });
A
annie_wangli 已提交
195
  ```
Z
zengyawen 已提交
196

A
Annie_wang 已提交
197
### goToLastRow
Z
zengyawen 已提交
198

A
annie_wangli 已提交
199
goToLastRow(): boolean
Z
zengyawen 已提交
200

A
annie_wangli 已提交
201 202 203 204
Moves the cursor to the last row of the result set.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
205
**Return value**
A
Annie_wang 已提交
206

A
Annie_wang 已提交
207 208 209
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
210

A
Annie_wang 已提交
211
**Example**
A
Annie_wang 已提交
212

A
Annie_wang 已提交
213
  ```js
A
Annie_wang 已提交
214 215
  let predicatesgoLast = new dataRdb.RdbPredicates("EMPLOYEE");
  let promisequerygoLast = rdbStore.query(predicatesgoLast, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
A
Annie_wang 已提交
216
  promisequerygoLast.then((resultSet) => {
A
Annie_wang 已提交
217 218
      resultSet.goToLastRow();
      resultSet.close();
A
annie_wangli 已提交
219
  }).catch((err) => {
A
Annie_wang 已提交
220 221
      console.log('query failed');
  });
A
annie_wangli 已提交
222
  ```
Z
zengyawen 已提交
223

A
Annie_wang 已提交
224
### goToNextRow
A
annie_wangli 已提交
225 226

goToNextRow(): boolean
Z
zengyawen 已提交
227 228 229

Moves the cursor to the next row in the result set.

A
annie_wangli 已提交
230 231
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
232
**Return value**
A
Annie_wang 已提交
233

A
Annie_wang 已提交
234 235 236
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
A
annie_wangli 已提交
237

A
Annie_wang 已提交
238
**Example**
A
Annie_wang 已提交
239

A
Annie_wang 已提交
240
  ```js
A
Annie_wang 已提交
241 242
  let predicatesgoNext = new dataRdb.RdbPredicates("EMPLOYEE");
  let promisequerygoNext = rdbStore.query(predicatesgoNext, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
A
Annie_wang 已提交
243
  promisequerygoNext.then((resultSet) => {
A
Annie_wang 已提交
244 245
      resultSet.goToNextRow();
      resultSet.close();
A
annie_wangli 已提交
246
  }).catch((err) => {
A
Annie_wang 已提交
247 248
      console.log('query failed');
  });
A
annie_wangli 已提交
249
  ```
Z
zengyawen 已提交
250

A
Annie_wang 已提交
251
### goToPreviousRow
Z
zengyawen 已提交
252

A
annie_wangli 已提交
253
goToPreviousRow(): boolean
Z
zengyawen 已提交
254 255 256

Moves the cursor to the previous row in the result set.

A
annie_wangli 已提交
257
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
Z
zengyawen 已提交
258

A
Annie_wang 已提交
259
**Return value**
A
Annie_wang 已提交
260

A
Annie_wang 已提交
261 262 263
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
264

A
Annie_wang 已提交
265
**Example**
A
Annie_wang 已提交
266

A
Annie_wang 已提交
267
  ```js
A
Annie_wang 已提交
268 269
  let predicatesgoPrev = new dataRdb.RdbPredicates("EMPLOYEE");
  let promisequerygoPrev = rdbStore.query(predicatesgoPrev, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
A
Annie_wang 已提交
270
  promisequerygoPrev.then((resultSet) => {
A
Annie_wang 已提交
271 272
      resultSet.goToPreviousRow();
      resultSet.close();
A
annie_wangli 已提交
273
  }).catch((err) => {
A
Annie_wang 已提交
274 275
      console.log('query failed');
  });
A
annie_wangli 已提交
276
  ```
Z
zengyawen 已提交
277

A
Annie_wang 已提交
278
### getBlob
Z
zengyawen 已提交
279

A
annie_wangli 已提交
280
getBlob(columnIndex: number): Uint8Array
Z
zengyawen 已提交
281 282 283

Obtains the value in the specified column in the current row as a byte array.

A
annie_wangli 已提交
284 285
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
286
**Parameters**
A
Annie_wang 已提交
287

A
Annie_wang 已提交
288 289 290
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|
A
annie_wangli 已提交
291

A
Annie_wang 已提交
292
**Return value**
A
Annie_wang 已提交
293

A
Annie_wang 已提交
294 295 296
  | Type| Description|
  | -------- | -------- |
  | Uint8Array | Value in the specified column as a byte array.|
A
annie_wangli 已提交
297

A
Annie_wang 已提交
298
**Example**
A
Annie_wang 已提交
299

A
Annie_wang 已提交
300
  ```js
A
Annie_wang 已提交
301
  const codes = resultSet.getBlob(resultSet.getColumnIndex("CODES"));
A
annie_wangli 已提交
302 303
  ```

A
Annie_wang 已提交
304
### getString
A
annie_wangli 已提交
305 306

getString(columnIndex: number): string
Z
zengyawen 已提交
307 308 309

Obtains the value in the specified column in the current row as a string.

A
annie_wangli 已提交
310 311
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
312
**Parameters**
A
Annie_wang 已提交
313

A
Annie_wang 已提交
314 315 316
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|
A
annie_wangli 已提交
317

A
Annie_wang 已提交
318
**Return value**
A
Annie_wang 已提交
319

A
Annie_wang 已提交
320 321 322
  | Type| Description|
  | -------- | -------- |
  | string | Value in the specified column as a string.|
A
annie_wangli 已提交
323

A
Annie_wang 已提交
324
**Example**
A
Annie_wang 已提交
325

A
Annie_wang 已提交
326
  ```js
A
Annie_wang 已提交
327
  const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
A
annie_wangli 已提交
328 329
  ```

A
Annie_wang 已提交
330
### getLong
A
annie_wangli 已提交
331 332 333 334 335 336 337

getLong(columnIndex: number): number

Obtains the value in the specified column in the current row as a Long.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
338
**Parameters**
A
Annie_wang 已提交
339

A
Annie_wang 已提交
340 341 342
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|
A
annie_wangli 已提交
343

A
Annie_wang 已提交
344
**Return value**
A
Annie_wang 已提交
345

A
Annie_wang 已提交
346 347 348
  | Type| Description|
  | -------- | -------- |
  | number | Value in the specified column as a Long.|
A
annie_wangli 已提交
349

A
Annie_wang 已提交
350
**Example**
A
Annie_wang 已提交
351

A
Annie_wang 已提交
352
  ```js
A
Annie_wang 已提交
353
  const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
A
annie_wangli 已提交
354
  ```
Z
zengyawen 已提交
355

A
Annie_wang 已提交
356
### getDouble
A
annie_wangli 已提交
357 358 359 360 361 362 363

getDouble(columnIndex: number): number

Obtains the value in the specified column in the current row as a double.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
364
**Parameters**
A
Annie_wang 已提交
365

A
Annie_wang 已提交
366 367 368
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|
A
annie_wangli 已提交
369

A
Annie_wang 已提交
370
**Return value**
A
Annie_wang 已提交
371

A
Annie_wang 已提交
372 373 374
  | Type| Description|
  | -------- | -------- |
  | number | Value in the specified column as a double.|
A
annie_wangli 已提交
375

A
Annie_wang 已提交
376
**Example**
A
Annie_wang 已提交
377

A
Annie_wang 已提交
378
  ```js
A
Annie_wang 已提交
379
  const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
A
annie_wangli 已提交
380 381
  ```

A
Annie_wang 已提交
382
### isColumnNull
A
annie_wangli 已提交
383 384 385 386 387 388 389

isColumnNull(columnIndex: number): boolean

Checks whether the value in the specified column of the current row is null.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
390
**Parameters**
A
Annie_wang 已提交
391

A
Annie_wang 已提交
392 393 394
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | columnIndex | number | Yes| Index of the specified column, starting from 0.|
A
annie_wangli 已提交
395

A
Annie_wang 已提交
396
**Return value**
A
Annie_wang 已提交
397

A
Annie_wang 已提交
398 399 400
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the value is null; returns **false** otherwise.|
A
annie_wangli 已提交
401

A
Annie_wang 已提交
402
**Example**
A
Annie_wang 已提交
403

A
Annie_wang 已提交
404
  ```js
A
Annie_wang 已提交
405
  const isColumnNull = resultSet.isColumnNull(resultSet.getColumnIndex("CODES"));
A
annie_wangli 已提交
406 407
  ```

A
Annie_wang 已提交
408
### close
A
annie_wangli 已提交
409 410 411 412 413 414 415

close(): void

Closes this result set.

**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core

A
Annie_wang 已提交
416
**Example**
A
Annie_wang 已提交
417

A
Annie_wang 已提交
418
  ```js
A
Annie_wang 已提交
419 420
  let predicatesClose = new dataRdb.RdbPredicates("EMPLOYEE");
  let promiseClose = rdbStore.query(predicatesClose, ["ID", "NAME", "AGE", "SALARY", "CODES"]);
A
Annie_wang 已提交
421
  promiseClose.then((resultSet) => {
A
Annie_wang 已提交
422
      resultSet.close();
A
annie_wangli 已提交
423
  }).catch((err) => {
A
Annie_wang 已提交
424
      console.log('Failed to close the resultset');
A
Annie_wang 已提交
425
  });
A
annie_wangli 已提交
426
  ```