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