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