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>Chapter��3.��Database Records</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="index.html" title="Getting Started with Berkeley DB" /> 11 <link rel="prev" href="CoreDbCXXUsage.html" title="Database Example" /> 12 <link rel="next" href="usingDbt.html" title="Reading and Writing 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">Chapter��3.��Database Records</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="CoreDbCXXUsage.html">Prev</a>��</td> 22 <th width="60%" align="center">��</th> 23 <td width="20%" align="right">��<a accesskey="n" href="usingDbt.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="chapter" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title"><a id="DBEntry"></a>Chapter��3.��Database Records</h2> 33 </div> 34 </div> 35 </div> 36 <div class="toc"> 37 <p> 38 <b>Table of Contents</b> 39 </p> 40 <dl> 41 <dt> 42 <span class="sect1"> 43 <a href="DBEntry.html#usingDbEntry">Using Database Records</a> 44 </span> 45 </dt> 46 <dt> 47 <span class="sect1"> 48 <a href="usingDbt.html">Reading and Writing Database Records</a> 49 </span> 50 </dt> 51 <dd> 52 <dl> 53 <dt> 54 <span class="sect2"> 55 <a href="usingDbt.html#databaseWrite">Writing Records to the Database</a> 56 </span> 57 </dt> 58 <dt> 59 <span class="sect2"> 60 <a href="usingDbt.html#CoreDatabaseRead">Getting Records from the Database</a> 61 </span> 62 </dt> 63 <dt> 64 <span class="sect2"> 65 <a href="usingDbt.html#recordDelete">Deleting Records</a> 66 </span> 67 </dt> 68 <dt> 69 <span class="sect2"> 70 <a href="usingDbt.html#datapersist">Data Persistence</a> 71 </span> 72 </dt> 73 </dl> 74 </dd> 75 <dt> 76 <span class="sect1"> 77 <a href="DbCXXUsage.html">Database Usage Example</a> 78 </span> 79 </dt> 80 </dl> 81 </div> 82 <p> 83 DB records contain two parts ��� a key and some data. Both the key 84 and its corresponding data are 85 encapsulated in 86 87 88 <span><code class="classname">Dbt</code> class objects.</span> 89 Therefore, to access a DB record, you need two such 90 91 <span>objects,</span> one for the key and 92 one for the data. 93 </p> 94 <p> 95 <code class="classname">Dbt</code> objects provide a <code class="literal">void *</code> 96 data member that you use to point to your data, and another member that identifies 97 the data length. They can therefore be used to store anything from simple 98 primitive data to complex class objects so long as the information you want to 99 store resides in a single contiguous block of memory. 100 </p> 101 <p> 102 This chapter describes 103 104 <code class="classname">Dbt</code> 105 usage. It also 106 introduces storing and retrieving key/value pairs from a database. 107 </p> 108 <div class="sect1" lang="en" xml:lang="en"> 109 <div class="titlepage"> 110 <div> 111 <div> 112 <h2 class="title" style="clear: both"><a id="usingDbEntry"></a>Using Database Records</h2> 113 </div> 114 </div> 115 </div> 116 <p> 117 Each database record is comprised of two 118 119 120 <span><code class="classname">Dbt</code> objects</span> 121 ��� one for the key and another for the data. 122 123 124 </p> 125 <a id="cxx_dbt1"></a> 126 <pre class="programlisting">#include <db_cxx.h> 127#include <string.h> 128 129... 130 131float money = 122.45; 132char *description = "Grocery bill."; 133 134Dbt key(&money, sizeof(float)); 135Dbt data(description, strlen(description)+1); </pre> 136 <p> 137 Note that in the following example we do not allow DB to assign the 138 memory for the retrieval of the money value. The reason why is that some 139 systems may require float values to have a specific alignment, and the 140 memory as returned by DB 141 may not be properly aligned (the same problem may exist for structures 142 on some systems). We tell DB to use our memory instead of its 143 own by specifying the <code class="literal">DB_DBT_USERMEM</code> flag. Be aware that 144 when we do this, we must also identify how much user memory is available 145 through the use of the <code class="literal">ulen</code> field. 146</p> 147 <a id="cxx_dbt2"></a> 148 <pre class="programlisting">#include <db_cxx.h> 149#include <string.h> 150 151... 152 153Dbt key, data; 154float money; 155char *description; 156 157key.set_data(&money); 158key.set_ulen(sizeof(float)); 159key.set_flags(DB_DBT_USERMEM); 160 161// Database retrieval code goes here 162 163// Money is set into the memory that we supplied. 164description = (char *)data.get_data();</pre> 165 </div> 166 </div> 167 <div class="navfooter"> 168 <hr /> 169 <table width="100%" summary="Navigation footer"> 170 <tr> 171 <td width="40%" align="left"><a accesskey="p" href="CoreDbCXXUsage.html">Prev</a>��</td> 172 <td width="20%" align="center">��</td> 173 <td width="40%" align="right">��<a accesskey="n" href="usingDbt.html">Next</a></td> 174 </tr> 175 <tr> 176 <td width="40%" align="left" valign="top">Database Example��</td> 177 <td width="20%" align="center"> 178 <a accesskey="h" href="index.html">Home</a> 179 </td> 180 <td width="40%" align="right" valign="top">��Reading and Writing Database Records</td> 181 </tr> 182 </table> 183 </div> 184 </body> 185</html> 186