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