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                <tt class="methodname">DB-&gt;get()</tt>
43                
44                or
45                <tt class="methodname">DB-&gt;pget()</tt>
46                
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="c_index6"></a>
63      <pre class="programlisting">#include &lt;db.h&gt;
64#include &lt;string.h&gt;
65
66...
67
68DB *my_secondary_database;
69DBT key; /* Used for the search key */
70DBT pkey, pdata; /* Used to return the primary key and data */
71char *search_name = "John Doe";
72
73/* Primary and secondary database opens omitted for brevity */
74                                                                                                                                     
75/* Zero out the DBTs before using them. */
76memset(&amp;key, 0, sizeof(DBT));
77memset(&amp;pkey, 0, sizeof(DBT));
78memset(&amp;pdata, 0, sizeof(DBT));
79                                                                                                                                     
80key.data = search_name;
81key.size = strlen(search_name) + 1;
82                                                                                                                                     
83/* Returns the key from the secondary database, and the data from the 
84 * associated primary database entry.
85 */
86my_secondary_database-&gt;get(my_secondary_database, NULL, 
87  &amp;key, &amp;pdata, 0);
88
89/* Returns the key from the secondary database, and the key and data 
90 * from the associated primary database entry.
91 */
92my_secondary_database-&gt;pget(my_secondary_database, NULL, 
93  &amp;key, &amp;pkey, &amp;pdata, 0); </pre>
94      <p>
95        Note that, just like 
96            
97            <span>a primary database,</span>
98            
99        if your secondary database supports duplicate records then
100             
101            <span>
102                <tt class="methodname">DB-&gt;get()</tt> 
103                 
104                and
105                <tt class="methodname">DB-&gt;pget()</tt> 
106                 
107            </span>
108        only return the first record found in a matching duplicates set. If you 
109        want to see all the records related to a specific secondary key, then use a
110            
111            <span>
112                cursor opened on the secondary database. Cursors are described in 
113                <a href="Cursors.html">Using Cursors</a>.
114            </span>
115     </p>
116    </div>
117    <div class="navfooter">
118      <hr />
119      <table width="100%" summary="Navigation footer">
120        <tr>
121          <td width="40%" align="left"><a accesskey="p" href="keyCreator.html">Prev</a> </td>
122          <td width="20%" align="center">
123            <a accesskey="u" href="indexes.html">Up</a>
124          </td>
125          <td width="40%" align="right"> <a accesskey="n" href="secondaryDelete.html">Next</a></td>
126        </tr>
127        <tr>
128          <td width="40%" align="left" valign="top">Implementing Key 
129        
130        Extractors
131         </td>
132          <td width="20%" align="center">
133            <a accesskey="h" href="index.html">Home</a>
134          </td>
135          <td width="40%" align="right" valign="top"> Deleting Secondary Database Records</td>
136        </tr>
137      </table>
138    </div>
139  </body>
140</html>
141