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