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>Reading Secondary Databases</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="indexes.html" title="Chapter 5. Secondary Databases" />
11    <link rel="previous" href="keyCreator.html" title="Implementing Key &#10;        &#10;        Extractors&#10;        " />
12    <link rel="next" href="secondaryDelete.html" title="Deleting Secondary Database Records" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Reading Secondary Databases</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="keyCreator.html">Prev</a> </td>
22          <th width="60%" align="center">Chapter 5. Secondary Databases</th>
23          <td width="20%" align="right"> <a accesskey="n" href="secondaryDelete.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="readSecondary"></a>Reading Secondary Databases</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <p>
38        Like a primary database, you can read records from your secondary
39        database either by using the 
40            
41            <span>
42                
43                <tt class="methodname">Db::get()</tt>
44                or
45                
46                <tt class="methodname">Db::pget()</tt>
47            methods, 
48            </span>
49        or by using  
50             
51            <span>a cursor on the secondary database.</span> 
52            
53        The main difference between reading secondary and primary databases is that when
54        you read a secondary database record, the secondary record's data is not
55        returned to you. Instead, the primary key and data corresponding to the
56        secondary key are returned to you.
57    </p>
58      <p>
59        For example, assuming your secondary database contains keys related
60         to a person's full name:
61    </p>
62      <a id="cxx_index6"></a>
63      <pre class="programlisting">#include &lt;db_cxx.h&gt;
64#include &lt;string.h&gt;
65
66...
67
68
69// The string to search for
70char *search_name = "John Doe";
71
72// Instantiate our Dbt's
73Dbt key(search_name, strlen(search_name) + 1);
74Dbt pkey, pdata; // Primary key and data
75
76                                                                                                                                     
77Db my_secondary_database(NULL, 0);
78// Primary and secondary database opens omitted for brevity
79                                                                                                                                     
80// Returns the key from the secondary database, and the data from the 
81// associated primary database entry.
82my_secondary_database.get(NULL, &amp;key, &amp;pdata, 0);
83
84// Returns the key from the secondary database, and the key and data 
85// from the associated primary database entry.
86my_secondary_database.pget(NULL, &amp;key, &amp;pkey, &amp;pdata, 0);</pre>
87      <p>
88        Note that, just like 
89            
90            <span>a primary database,</span>
91            
92        if your secondary database supports duplicate records then
93             
94            <span>
95                 
96                <tt class="methodname">Db::get()</tt> 
97                and
98                 
99                <tt class="methodname">Db::pget()</tt> 
100            </span>
101        only return the first record found in a matching duplicates set. If you 
102        want to see all the records related to a specific secondary key, then use a
103            
104            <span>
105                cursor opened on the secondary database. Cursors are described in 
106                <a href="Cursors.html">Using Cursors</a>.
107            </span>
108     </p>
109    </div>
110    <div class="navfooter">
111      <hr />
112      <table width="100%" summary="Navigation footer">
113        <tr>
114          <td width="40%" align="left"><a accesskey="p" href="keyCreator.html">Prev</a> </td>
115          <td width="20%" align="center">
116            <a accesskey="u" href="indexes.html">Up</a>
117          </td>
118          <td width="40%" align="right"> <a accesskey="n" href="secondaryDelete.html">Next</a></td>
119        </tr>
120        <tr>
121          <td width="40%" align="left" valign="top">Implementing Key 
122        
123        Extractors
124         </td>
125          <td width="20%" align="center">
126            <a accesskey="h" href="index.html">Home</a>
127          </td>
128          <td width="40%" align="right" valign="top"> Deleting Secondary Database Records</td>
129        </tr>
130      </table>
131    </div>
132  </body>
133</html>
134