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 81 <tt class="methodname">Db::get()</tt> 82 and a secondary database. Instead, you must use 83 84 <span><tt class="methodname">Db::pget()</tt>.</span> 85 Also, in that case the primary and secondary key given on the call to 86 87 <tt class="methodname">Db::pget()</tt> 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="cxx_index8"></a> 108 <pre class="programlisting">#include <db_cxx.h> 109 110... 111 112Db my_database(NULL, 0); 113Db my_index(NULL, 0); 114 115 116// Get a cursor on the secondary database 117Dbc *cursorp; 118my_index.cursor(NULL, &cursorp, 0); 119 120 121// Name to delete 122char *search_name = "John Doe"; 123 124// Instantiate Dbts as normal 125Dbt key(search_name, strlen(search_name) + 1); 126Dbt data; 127 128 129// Position the cursor 130while (cursorp->get(&key, &data, DB_SET) == 0) 131 cursorp->del(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