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>Using Cursors with 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="secondaryDelete.html" title="Deleting Secondary Database Records" />
12    <link rel="next" href="joins.html" title="Database Joins" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">
19        
20        <span>Using Cursors with Secondary Databases</span>
21    </th>
22        </tr>
23        <tr>
24          <td width="20%" align="left"><a accesskey="p" href="secondaryDelete.html">Prev</a>��</td>
25          <th width="60%" align="center">Chapter��5.��Secondary Databases</th>
26          <td width="20%" align="right">��<a accesskey="n" href="joins.html">Next</a></td>
27        </tr>
28      </table>
29      <hr />
30    </div>
31    <div class="sect1" lang="en" xml:lang="en">
32      <div class="titlepage">
33        <div>
34          <div>
35            <h2 class="title" style="clear: both"><a id="secondaryCursor"></a>
36        
37        <span>Using Cursors with Secondary Databases</span>
38    </h2>
39          </div>
40        </div>
41      </div>
42      <p>
43        Just like cursors on a primary database, you can use 
44             
45            <span>cursors on secondary databases</span> 
46        to iterate over the records in a secondary database. Like
47
48          
49         <span>cursors used with primary databases,</span> 
50        
51        you can also use 
52          
53         <span>cursors with secondary databases</span> 
54        to search for specific records in a database, to seek to the first 
55        or last record in the database, to get the next duplicate record, 
56            
57        and so forth. For a complete description on cursors and their capabilities, see
58        <a class="xref" href="Cursors.html" title="Chapter��4.��Using Cursors">Using Cursors</a>.
59    </p>
60      <p>
61        However, when you use 
62             
63            <span>cursors with secondary databases:</span> 
64    </p>
65      <div class="itemizedlist">
66        <ul type="disc">
67          <li>
68            <p>
69            Any data returned is the data contained on the primary database
70            record referenced by the secondary record.
71        </p>
72          </li>
73          <li>
74            <p>
75            You cannot use <code class="literal">DB_GET_BOTH</code> and related flags with
76                <code class="methodname">DB-&gt;get()</code>
77                
78            and a secondary database. Instead, you must use 
79                <span><code class="methodname">DB-&gt;pget()</code>.</span>
80                
81            Also, in that case the primary and secondary key given on the call to 
82                <code class="methodname">DB-&gt;pget()</code>
83                
84            must match the secondary key and associated primary record key in
85            order for that primary record to be returned as a result of the
86            call.
87        </p>
88          </li>
89        </ul>
90      </div>
91      <p>
92        For example, suppose you are using the databases, classes, and key
93        
94        <span>extractors</span>
95        described in <a class="xref" href="keyCreator.html" title="Implementing Key Extractors">Implementing Key 
96        
97        <span>Extractors</span>
98        </a>. 
99        Then the following searches for a person's
100        name in the secondary database, and deletes all secondary and primary
101        records that use that name.
102    </p>
103      <a id="c_index8"></a>
104      <pre class="programlisting">#include &lt;db.h&gt;
105#include &lt;string.h&gt;
106
107...
108                                                                                                                                     
109DB *sdbp;          /* Secondary DB handle */
110DBC *cursorp;      /* Cursor */
111DBT key, data;     /* DBTs used for the delete */
112char *search_name = "John Doe"; /* Name to delete */
113                                                                                                                                     
114/* Primary and secondary database opens omitted for brevity. */
115
116/* Get a cursor on the secondary database */
117sdbp-&gt;cursor(sdbp, NULL, &amp;cursorp, 0);
118
119/*
120 * Zero out the DBT before using it.
121 */
122memset(&amp;key, 0, sizeof(DBT));
123memset(&amp;data, 0, sizeof(DBT));
124
125key.data = search_name;
126key.size = strlen(search_name) + 1;
127
128 
129/* Position the cursor */
130while (cursorp-&gt;get(cursorp, &amp;key, &amp;data, DB_SET) == 0)
131    cursorp-&gt;del(cursorp, 0); </pre>
132    </div>
133    <div class="navfooter">
134      <hr />
135      <table width="100%" summary="Navigation footer">
136        <tr>
137          <td width="40%" align="left"><a accesskey="p" href="secondaryDelete.html">Prev</a>��</td>
138          <td width="20%" align="center">
139            <a accesskey="u" href="indexes.html">Up</a>
140          </td>
141          <td width="40%" align="right">��<a accesskey="n" href="joins.html">Next</a></td>
142        </tr>
143        <tr>
144          <td width="40%" align="left" valign="top">Deleting Secondary Database Records��</td>
145          <td width="20%" align="center">
146            <a accesskey="h" href="index.html">Home</a>
147          </td>
148          <td width="40%" align="right" valign="top">��Database Joins</td>
149        </tr>
150      </table>
151    </div>
152  </body>
153</html>
154