<?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.62.4" /> <link rel="home" href="index.html" title="Getting Started with Berkeley DB" /> <link rel="up" href="index.html" title="Getting Started with Berkeley DB" /> <link rel="previous" href="CoreDbUsage.html" title="Database Example" /> <link rel="next" href="usingDbt.html" title="Reading and Writing Database Records" /> </head> <body> <div class="navheader"> <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> </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><tt class="classname">DBT</tt> 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> <tt class="classname">DBT</tt> structures provide a <tt class="literal">void *</tt> 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 <tt class="classname">DBT</tt> 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></div> </div> <p> Each database record is comprised of two <span><tt class="classname">DBT</tt> 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 (<tt class="literal">int</tt>, <tt class="literal">float</tt>, 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 <tt class="literal">void *</tt> returned in the <tt class="methodname">DBT</tt> 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 <tt class="literal">DB_DBT_USERMEM</tt> flag. Be aware that when we do this, we must also identify how much user memory is available through the use of the <tt class="literal">ulen</tt> 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"> <a accesskey="u" href="index.html">Up</a> </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>