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