1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Chapter 3. Database Records</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
<link rel="up" href="index.html" title="Getting Started with Berkeley DB" />
<link rel="prev" href="CoreDbUsage.html" title="Database Example" />
<link rel="next" href="usingDbt.html" title="Reading and Writing Database Records" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 11.2.5.3</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Chapter 3. Database Records</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="CoreDbUsage.html">Prev</a> </td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> <a accesskey="n" href="usingDbt.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="chapter" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a id="DBEntry"></a>Chapter 3. Database Records</h2>
</div>
</div>
</div>
<div class="toc">
<p>
<b>Table of Contents</b>
</p>
<dl>
<dt>
<span class="sect1">
<a href="DBEntry.html#usingDbEntry">Using Database Records</a>
</span>
</dt>
<dt>
<span class="sect1">
<a href="usingDbt.html">Reading and Writing Database Records</a>
</span>
</dt>
<dd>
<dl>
<dt>
<span class="sect2">
<a href="usingDbt.html#databaseWrite">Writing Records to the Database</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="usingDbt.html#CoreDatabaseRead">Getting Records from the Database</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="usingDbt.html#recordDelete">Deleting Records</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="usingDbt.html#datapersist">Data Persistence</a>
</span>
</dt>
</dl>
</dd>
<dt>
<span class="sect1">
<a href="cstructs.html">Using C Structures with DB</a>
</span>
</dt>
<dd>
<dl>
<dt>
<span class="sect2">
<a href="cstructs.html#cstructdynamic">C Structures with Pointers</a>
</span>
</dt>
</dl>
</dd>
<dt>
<span class="sect1">
<a href="DbUsage.html">Database Usage Example</a>
</span>
</dt>
</dl>
</div>
<p>
DB records contain two parts — a key and some data. Both the key
and its corresponding data are
encapsulated in
<span><code class="classname">DBT</code> structures.</span>
Therefore, to access a DB record, you need two such
<span>structures,</span>
one for the key and
one for the data.
</p>
<p>
<code class="classname">DBT</code> structures provide a <code class="literal">void *</code>
field that you use to point to your data, and another field that identifies
the data length. They can therefore be used to store anything from simple
primitive data to complex structures so long as the information you want to
store resides in a single contiguous block of memory.
</p>
<p>
This chapter describes
<code class="classname">DBT</code>
usage. It also
introduces storing and retrieving key/value pairs from a database.
</p>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="usingDbEntry"></a>Using Database Records</h2>
</div>
</div>
</div>
<p>
Each database record is comprised of two
<span><code class="classname">DBT</code> structures</span>
— one for the key and another for the data.
</p>
<p>
To store a database record where the key and/or the data are primitive
data (<code class="literal">int</code>, <code class="literal">float</code>, and so forth),
or where the key and/or the data contain an array, we need only to point
to the memory location where that data resides and identify its
length. For example:
</p>
<a id="c_dbt1"></a>
<pre class="programlisting">#include <db.h>
#include <string.h>
...
DBT key, data;
float money = 122.45;
char *description = "Grocery bill.";
/* Zero out the DBTs before using them. */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
key.data = &money;
key.size = sizeof(float);
data.data = description;
data.size = strlen(description) + 1; </pre>
<p>
To retrieve the record, simply assign the <code class="literal">void *</code> returned in the
<code class="methodname">DBT</code>
to the appropriate variable.
</p>
<p>
Note that in the following example we do not allow DB to assign the
memory for the retrieval of the money value. The reason why is that some
systems may require float values to have a specific alignment, and the
memory as returned by DB
may not be properly aligned (the same problem may exist for structures
on some systems). We tell DB to use our memory instead of its
own by specifying the <code class="literal">DB_DBT_USERMEM</code> flag. Be aware that
when we do this, we must also identify how much user memory is available
through the use of the <code class="literal">ulen</code> field.
</p>
<a id="c_dbt2"></a>
<pre class="programlisting">#include <db.h>
#include <string.h>
...
float money;
DBT key, data;
char *description;
/* Initialize the DBTs */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
key.data = &money;
key.ulen = sizeof(float);
key.flags = DB_DBT_USERMEM;
/* Database retrieval code goes here */
/*
* Money is set into the memory that we supplied.
*/
description = data.data;</pre>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="CoreDbUsage.html">Prev</a> </td>
<td width="20%" align="center"> </td>
<td width="40%" align="right"> <a accesskey="n" href="usingDbt.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Database Example </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Reading and Writing Database Records</td>
</tr>
</table>
</div>
</body>
</html>
|