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