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="CoreDbUsage.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="CoreDbUsage.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="cstructs.html">Using C Structures with DB</a> 78 </span> 79 </dt> 80 <dd> 81 <dl> 82 <dt> 83 <span class="sect2"> 84 <a href="cstructs.html#cstructdynamic">C Structures with Pointers</a> 85 </span> 86 </dt> 87 </dl> 88 </dd> 89 <dt> 90 <span class="sect1"> 91 <a href="DbUsage.html">Database Usage Example</a> 92 </span> 93 </dt> 94 </dl> 95 </div> 96 <p> 97 DB records contain two parts ��� a key and some data. Both the key 98 and its corresponding data are 99 encapsulated in 100 101 <span><code class="classname">DBT</code> structures.</span> 102 103 Therefore, to access a DB record, you need two such 104 <span>structures,</span> 105 one for the key and 106 one for the data. 107 </p> 108 <p> 109 <code class="classname">DBT</code> structures provide a <code class="literal">void *</code> 110 field that you use to point to your data, and another field that identifies 111 the data length. They can therefore be used to store anything from simple 112 primitive data to complex structures so long as the information you want to 113 store resides in a single contiguous block of memory. 114 </p> 115 <p> 116 This chapter describes 117 <code class="classname">DBT</code> 118 119 usage. It also 120 introduces storing and retrieving key/value pairs from a database. 121 </p> 122 <div class="sect1" lang="en" xml:lang="en"> 123 <div class="titlepage"> 124 <div> 125 <div> 126 <h2 class="title" style="clear: both"><a id="usingDbEntry"></a>Using Database Records</h2> 127 </div> 128 </div> 129 </div> 130 <p> 131 Each database record is comprised of two 132 133 <span><code class="classname">DBT</code> structures</span> 134 135 ��� one for the key and another for the data. 136 137 138 </p> 139 <p> 140 To store a database record where the key and/or the data are primitive 141 data (<code class="literal">int</code>, <code class="literal">float</code>, and so forth), 142 or where the key and/or the data contain an array, we need only to point 143 to the memory location where that data resides and identify its 144 length. For example: 145 </p> 146 <a id="c_dbt1"></a> 147 <pre class="programlisting">#include <db.h> 148#include <string.h> 149 150... 151 152DBT key, data; 153float money = 122.45; 154char *description = "Grocery bill."; 155 156/* Zero out the DBTs before using them. */ 157memset(&key, 0, sizeof(DBT)); 158memset(&data, 0, sizeof(DBT)); 159 160key.data = &money; 161key.size = sizeof(float); 162 163data.data = description; 164data.size = strlen(description) + 1; </pre> 165 <p> 166 To retrieve the record, simply assign the <code class="literal">void *</code> returned in the 167 <code class="methodname">DBT</code> 168 169 to the appropriate variable. 170</p> 171 <p> 172 Note that in the following example we do not allow DB to assign the 173 memory for the retrieval of the money value. The reason why is that some 174 systems may require float values to have a specific alignment, and the 175 memory as returned by DB 176 may not be properly aligned (the same problem may exist for structures 177 on some systems). We tell DB to use our memory instead of its 178 own by specifying the <code class="literal">DB_DBT_USERMEM</code> flag. Be aware that 179 when we do this, we must also identify how much user memory is available 180 through the use of the <code class="literal">ulen</code> field. 181</p> 182 <a id="c_dbt2"></a> 183 <pre class="programlisting">#include <db.h> 184#include <string.h> 185 186... 187 188float money; 189DBT key, data; 190char *description; 191 192/* Initialize the DBTs */ 193memset(&key, 0, sizeof(DBT)); 194memset(&data, 0, sizeof(DBT)); 195 196key.data = &money; 197key.ulen = sizeof(float); 198key.flags = DB_DBT_USERMEM; 199 200/* Database retrieval code goes here */ 201 202/* 203 * Money is set into the memory that we supplied. 204 */ 205description = data.data;</pre> 206 </div> 207 </div> 208 <div class="navfooter"> 209 <hr /> 210 <table width="100%" summary="Navigation footer"> 211 <tr> 212 <td width="40%" align="left"><a accesskey="p" href="CoreDbUsage.html">Prev</a>��</td> 213 <td width="20%" align="center">��</td> 214 <td width="40%" align="right">��<a accesskey="n" href="usingDbt.html">Next</a></td> 215 </tr> 216 <tr> 217 <td width="40%" align="left" valign="top">Database Example��</td> 218 <td width="20%" align="center"> 219 <a accesskey="h" href="index.html">Home</a> 220 </td> 221 <td width="40%" align="right" valign="top">��Reading and Writing Database Records</td> 222 </tr> 223 </table> 224 </div> 225 </body> 226</html> 227