1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Getting Records Using the Cursor</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
<link rel="home" href="index.html" title="Getting Started with Berkeley DB" />
<link rel="up" href="Cursors.html" title="Chapter 4. Using Cursors" />
<link rel="previous" href="Cursors.html" title="Chapter 4. Using Cursors" />
<link rel="next" href="PutEntryWCursor.html" title="Putting Records Using Cursors" />
</head>
<body>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Getting Records Using the Cursor</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="Cursors.html">Prev</a> </td>
<th width="60%" align="center">Chapter 4. Using Cursors</th>
<td width="20%" align="right"> <a accesskey="n" href="PutEntryWCursor.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="Positioning"></a>Getting Records Using the Cursor</h2>
</div>
</div>
<div></div>
</div>
<p>
To iterate over database records, from the first record to
the last, simply open the cursor and then use the
<tt class="methodname">DBC->c_get()</tt>
method.
<span>Note that you need to supply the
<tt class="literal">DB_NEXT</tt> flag to this method.</span>
For example:
</p>
<a id="c_cursor3"></a>
<pre class="programlisting">#include <db.h>
#include <string.h>
...
DB *my_database;
DBC *cursorp;
DBT key, data;
int ret;
/* Database open omitted for clarity */
/* Get a cursor */
my_database->cursor(my_database, NULL, &cursorp, 0);
/* Initialize our DBTs. */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
/* Iterate over the database, retrieving each record in turn. */
while ((ret = cursorp->c_get(cursorp, &key, &data, DB_NEXT)) == 0) {
/* Do interesting things with the DBTs here. */
}
if (ret != DB_NOTFOUND) {
/* Error handling goes here */
}
/* Cursors must be closed */
if (cursorp != NULL)
cursorp->c_close(cursorp);
if (my_database != NULL)
my_database->close(my_database, 0);</pre>
<p>
To iterate over the database from the last record to the first, use
<tt class="literal">DB_PREV</tt> instead of <tt class="literal">DB_NEXT</tt>:
</p>
<a id="c_cursor4"></a>
<pre class="programlisting">#include <db.h>
#include <string.h>
...
DB *my_database;
DBC *cursorp;
DBT key, data;
int ret;
/* Database open omitted for clarity */
/* Get a cursor */
my_database->cursor(my_database, NULL, &cursorp, 0);
/* Initialize our DBTs. */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
/* Iterate over the database, retrieving each record in turn. */
while ((ret = cursorp->c_get(cursorp, &key,
&data, DB_PREV)) == 0) {
/* Do interesting things with the DBTs here. */
}
if (ret != DB_NOTFOUND) {
/* Error handling goes here */
}
// Cursors must be closed
if (cursorp != NULL)
cursorp->c_close(cursorp);
if (my_database != NULL)
my_database->close(my_database, 0);</pre>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="cursorsearch"></a>Searching for Records</h3>
</div>
</div>
<div></div>
</div>
<p>
You can use cursors to search for database records. You can search based
on just a key, or you can search based on both the key and the data.
You can also perform partial matches if your database supports sorted
duplicate sets. In all cases, the key and data parameters of these
methods are filled with the key and data values of the database record
to which the cursor is positioned as a result of the search.
</p>
<p>
Also, if the search fails, then cursor's state is left unchanged
and
<tt class="literal">DB_NOTFOUND</tt>
is returned.
</p>
<p>
To use a cursor to search for a record, use
<span>DBT->c_get()<tt class="methodname"></tt>.</span>
When you use this method, you can provide the following flags:
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
Notice in the following list that the cursor flags use the
keyword <tt class="literal">SET</tt> when the cursor examines just the key
portion of the records (in this case, the cursor is set to the
record whose key matches the value provided to the cursor).
Moreover, when the cursor uses the keyword <tt class="literal">GET</tt>,
then the cursor is positioned to both the key
<span class="emphasis"><em>and</em></span> the data values provided to the cursor.
</p>
<p>
Regardless of the keyword you use to get a record with a cursor, the
cursor's key and data
<span><tt class="classname">DBT</tt>s</span>
are filled with the data retrieved from the record to which the
cursor is positioned.
</p>
</div>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
<tt class="literal">DB_SET</tt>
</p>
<p>
Moves the cursor to the first record in the database with
the specified key.
</p>
</li>
<li>
<p>
<tt class="literal">DB_SET_RANGE</tt>
</p>
<p>
<span>Identical to
<tt class="literal">DB_SET</tt>
<tt class="methodname">Cursor.getSearchKey()</tt>
unless you are using the BTree access. In this case, the cursor
moves</span>
to the first record in the database whose
key is greater than or equal to the specified key. This comparison
is determined by the
<span>comparison function</span>
that you provide for the database. If no
<span>comparison function</span>
is provided, then the default lexicographical sorting is used.
</p>
<p>
For example, suppose you have database records that use the
following
<span>strings</span>
as keys:
</p>
<pre class="programlisting">Alabama
Alaska
Arizona</pre>
<p>
Then providing a search key of <tt class="literal">Alaska</tt> moves the
cursor to the second key noted above. Providing a key of
<tt class="literal">Al</tt> moves the cursor to the first key (<tt class="literal">Alabama</tt>), providing
a search key of <tt class="literal">Alas</tt> moves the cursor to the second key
(<tt class="literal">Alaska</tt>), and providing a key of <tt class="literal">Ar</tt> moves the
cursor to the last key (<tt class="literal">Arizona</tt>).
</p>
</li>
<li>
<p>
<tt class="literal">DB_GET_BOTH</tt>
</p>
<p>
Moves the cursor to the first record in the database that uses
the specified key and data.
</p>
</li>
<li>
<p>
<tt class="literal">DB_GET_BOTH_RANGE</tt>
</p>
<p>
Moves the cursor to the first record in the database whose key is
greater than or equal to the specified key. If the database supports
duplicate records, then on matching the key, the cursor is moved to
the duplicate record with the smallest data that is greater than or
equal to the specified data.
</p>
<p>
For example,
<span>suppose your database uses BTree
and it has </span>
database records that use the following key/data pairs:
</p>
<pre class="programlisting">Alabama/Athens
Alabama/Florence
Alaska/Anchorage
Alaska/Fairbanks
Arizona/Avondale
Arizona/Florence </pre>
<p>then providing:</p>
<div class="informaltable">
<table border="1" width="80%">
<colgroup>
<col />
<col />
<col />
</colgroup>
<thead>
<tr>
<th>a search key of ...</th>
<th>and a search data of ...</th>
<th>moves the cursor to ...</th>
</tr>
</thead>
<tbody>
<tr>
<td>Al</td>
<td>Fl</td>
<td>Alabama/Florence</td>
</tr>
<tr>
<td>Ar</td>
<td>Fl</td>
<td>Arizona/Florence</td>
</tr>
<tr>
<td>Al</td>
<td>Fa</td>
<td>Alaska/Fairbanks</td>
</tr>
<tr>
<td>Al</td>
<td>A</td>
<td>Alabama/Athens</td>
</tr>
</tbody>
</table>
</div>
</li>
</ul>
</div>
<p>
For example, assuming a database containing sorted duplicate records of
U.S. States/U.S Cities key/data pairs (both as
<span>strings),</span>
then the following code fragment can be used to position the cursor
to any record in the database and print its key/data values:
</p>
<a id="c_cursor5"></a>
<pre class="programlisting">#include <db.h>
#include <string.h>
...
DBC *cursorp;
DBT key, data;
DB *dbp;
int ret;
char *search_data = "Fa";
char *search_key = "Al";
/* database open omitted for clarity */
/* Get a cursor */
dbp->cursor(dbp, NULL, &cursorp, 0);
/* Set up our DBTs */
key.data = search_key;
key.size = strlen(search_key) + 1;
data.data = search_data;
data.size = strlen(search_data) + 1;
/*
* Position the cursor to the first record in the database whose
* key and data begin with the correct strings.
*/
ret = cursorp->c_get(cursorp, &key, &data, DB_GET_BOTH_RANGE);
if (!ret) {
/* Do something with the data */
} else {
/* Error handling goes here */
}
/* Close the cursor */
if (cursorp != NULL)
cursorp->c_close(cursorp);
/* Close the database */
if (dbp != NULL)
dbp->close(dbp, 0); </pre>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="getdups"></a>Working with Duplicate Records</h3>
</div>
</div>
<div></div>
</div>
<p>
A record is a duplicate of another record if the two records share the
same key. For duplicate records, only the data portion of the record is unique.
</p>
<p>
Duplicate records are supported only for the BTree or Hash access methods.
For information on configuring your database to use duplicate records,
see <a href="btree.html#duplicateRecords">Allowing Duplicate Records</a>.
</p>
<p>
If your database supports duplicate records, then it can potentially
contain multiple records that share the same key.
<span>By default, normal database
get operations will only return the first such record in a set
of duplicate records. Typically, subsequent duplicate records are
accessed using a cursor.
</span>
The following
<span><tt class="methodname">DBC->c_get()</tt> flags</span>
are interesting when working with databases that support duplicate records:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
<span>
<tt class="literal">DB_NEXT</tt>,
<tt class="literal">DB_PREV</tt>
</span>
</p>
<p>
Shows the next/previous record in the database, regardless of
whether it is a duplicate of the current record. For an example of
using these methods, see <a href="Positioning.html">Getting Records Using the Cursor</a>.
</p>
</li>
<li>
<p>
<tt class="literal">DB_GET_BOTH_RANGE</tt>
</p>
<p>
Useful for seeking the cursor to a specific record, regardless of
whether it is a duplicate record. See <a href="Positioning.html#cursorsearch">Searching for Records</a> for more
information.
</p>
</li>
<li>
<p>
<span>
<tt class="literal">DB_NEXT_NODUP</tt>,
<tt class="literal">DB_PREV_NODUP</tt>
</span>
</p>
<p>
Gets the next/previous non-duplicate record in the database. This
allows you to skip over all the duplicates in a set of duplicate
records. If you call
<span>
<tt class="methodname">c_get()</tt>
with <tt class="literal">DB_PREV_NODUP</tt>,
</span>
then the cursor is positioned to the last record for the previous
key in the database. For example, if you have the following records
in your database:
</p>
<pre class="programlisting">Alabama/Athens
Alabama/Florence
Alaska/Anchorage
Alaska/Fairbanks
Arizona/Avondale
Arizona/Florence</pre>
<p>
and your cursor is positioned to <tt class="literal">Alaska/Fairbanks</tt>,
and you then call
<span>
<tt class="methodname">c_get()</tt>
with <tt class="literal">DB_PREV_NODUP</tt>,
</span>
then the cursor is positioned to Alabama/Florence. Similarly, if
you call
<span>
<tt class="methodname">c_get()</tt>
with <tt class="literal">DB_NEXT_NODUP</tt>,
</span>
then the cursor is positioned to the first record corresponding to
the next key in the database.
</p>
<p>
If there is no next/previous key in the database, then
<tt class="literal">DB_NOTFOUND</tt>
is returned, and the cursor is left unchanged.
</p>
</li>
<li>
<p>
<tt class="literal">DB_NEXT_DUP</tt>
</p>
<p>
Gets the
<span>next</span>
record that shares the current key. If the
cursor is positioned at the last record in the duplicate set and
you call
<span>
<tt class="methodname">DBC->c_get()</tt>
with <tt class="literal">DB_NEXT_DUP</tt>,
</span>
then
<tt class="literal">DB_NOTFOUND</tt>
is returned and the cursor is left unchanged.
</p>
</li>
</ul>
</div>
<p>
For example, the following code fragment positions a cursor to a key
<span>and displays it and all its
duplicates.</span>
</p>
<a id="c_cursor6"></a>
<pre class="programlisting">#include <db.h>
#include <string.h>
...
DB *dbp;
DBC *cursorp;
DBT key, data;
int ret;
char *search_key = "Al";
/* database open omitted for clarity */
/* Get a cursor */
dbp->cursor(dbp, NULL, &cursorp, 0);
/* Set up our DBTs */
key.data = search_key;
key.size = strlen(search_key) + 1;
/*
* Position the cursor to the first record in the database whose
* key and data begin with the correct strings.
*/
ret = cursorp->c_get(cursorp, &key, &data, DB_SET);
while (ret != DB_NOTFOUND) {
printf("key: %s, data: %s\n", (char *)key.data, (char *)data.data);
ret = cursorp->c_get(cursorp, &key, &data, DB_NEXT_DUP);
}
/* Close the cursor */
if (cursorp != NULL)
cursorp->c_close(cursorp);
/* Close the database */
if (dbp != NULL)
dbp->close(dbp, 0); </pre>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="Cursors.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="Cursors.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="PutEntryWCursor.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 4. Using Cursors </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Putting Records Using Cursors</td>
</tr>
</table>
</div>
</body>
</html>
|