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