<?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="CoreDbCXXUsage.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="CoreDbCXXUsage.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="DbCXXUsage.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> class objects.</span> Therefore, to access a DB record, you need two such <span>objects,</span> one for the key and one for the data. </p> <p> <tt class="classname">Dbt</tt> objects provide a <tt class="literal">void *</tt> data member that you use to point to your data, and another member that identifies the data length. They can therefore be used to store anything from simple primitive data to complex class objects 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> objects</span> — one for the key and another for the data. </p> <a id="cxx_dbt1"></a> <pre class="programlisting">#include <db_cxx.h> #include <string.h> ... float money = 122.45; char *description = "Grocery bill."; Dbt key(&money, sizeof(float)); Dbt data(description, strlen(description)+1); </pre> <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="cxx_dbt2"></a> <pre class="programlisting">#include <db_cxx.h> #include <string.h> ... Dbt key, data; float money; char *description; key.set_data(&money); key.set_ulen(sizeof(float)); key.set_flags(DB_DBT_USERMEM); // Database retrieval code goes here // Money is set into the memory that we supplied. description = (char *)data.get_data();</pre> </div> </div> <div class="navfooter"> <hr /> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"><a accesskey="p" href="CoreDbCXXUsage.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>