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 2. Databases</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="gettingit.html" title="Getting and Using DB " /> 12 <link rel="next" href="coredbclose.html" title="Closing Databases" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Chapter 2. Databases</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="gettingit.html">Prev</a> </td> 22 <th width="60%" align="center"> </th> 23 <td width="20%" align="right"> <a accesskey="n" href="coredbclose.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="DB"></a>Chapter 2. Databases</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="DB.html#DBOpen">Opening Databases</a> 45 </span> 46 </dt> 47 <dt> 48 <span class="sect1"> 49 <a href="coredbclose.html">Closing Databases</a> 50 </span> 51 </dt> 52 <dt> 53 <span class="sect1"> 54 <a href="DBOpenFlags.html">Database Open Flags</a> 55 </span> 56 </dt> 57 <dt> 58 <span class="sect1"> 59 <a href="CoreDBAdmin.html">Administrative Methods</a> 60 </span> 61 </dt> 62 <dt> 63 <span class="sect1"> 64 <a href="dbErrorReporting.html">Error Reporting Functions</a> 65 </span> 66 </dt> 67 <dt> 68 <span class="sect1"> 69 <a href="CoreEnvUsage.html">Managing Databases in Environments</a> 70 </span> 71 </dt> 72 <dt> 73 <span class="sect1"> 74 <a href="CoreDbCXXUsage.html">Database Example</a> 75 </span> 76 </dt> 77 </dl> 78 </div> 79 <p>In Berkeley DB, a database is a collection of <span class="emphasis"><em>records</em></span>. Records, 80 in turn, consist of key/data pairings. 81 </p> 82 <p> 83 Conceptually, you can think of a 84 85 <span>database</span> 86 as containing a two-column table where column 1 contains a key and column 2 87 contains data. Both the key and the data are managed using 88 89 90 <tt class="classname">Dbt</tt> 91 <span>class instances</span> 92 93 (see <a href="DBEntry.html">Database Records</a> for details on this 94 <span>class</span> 95 ). 96 So, fundamentally, using a DB 97 98 <span>database</span> 99 involves putting, getting, and deleting database records, which in turns involves efficiently 100 managing information 101 <span>encapsulated by </span> 102 103 104 105 106 <tt class="classname">Dbt</tt> 107 108 <span>objects.</span> 109 110 The next several chapters of this book are dedicated to those activities. 111 </p> 112 <div class="sect1" lang="en" xml:lang="en"> 113 <div class="titlepage"> 114 <div> 115 <div> 116 <h2 class="title" style="clear: both"><a id="DBOpen"></a>Opening Databases</h2> 117 </div> 118 </div> 119 <div></div> 120 </div> 121 <p> 122 You open a database by instantiating a <tt class="classname">Db</tt> object 123 and then calling its <tt class="methodname">open()</tt> method. 124 </p> 125 <p> 126 Note that by default, DB does not create databases if they do not already exist. 127 To override this behavior, specify the 128 <a href="DBOpenFlags.html" title="Database Open Flags"><tt class="literal">DB_CREATE</tt></a> flag on the 129 <tt class="methodname">open()</tt> method. 130 </p> 131 <p> 132 The following code fragment illustrates a database open: 133 134 </p> 135 <a id="cxx_db1"></a> 136 <pre class="programlisting">#include <db_cxx.h> 137 138... 139 140Db db(NULL, 0); // Instantiate the Db object 141 142u_int32_t oFlags = DB_CREATE; // Open flags; 143 144try { 145 // Open the database 146 db.open(NULL, // Transaction pointer 147 "my_db.db", // Database file name 148 NULL, // Optional logical database name 149 DB_BTREE, // Database access method 150 oFlags, // Open flags 151 0); // File mode (using defaults) 152// DbException is not subclassed from std::exception, so 153// need to catch both of these. 154} catch(DbException &e) { 155 // Error handling code goes here 156} catch(std::exception &e) { 157 // Error handling code goes here 158} </pre> 159 </div> 160 </div> 161 <div class="navfooter"> 162 <hr /> 163 <table width="100%" summary="Navigation footer"> 164 <tr> 165 <td width="40%" align="left"><a accesskey="p" href="gettingit.html">Prev</a> </td> 166 <td width="20%" align="center"> 167 <a accesskey="u" href="index.html">Up</a> 168 </td> 169 <td width="40%" align="right"> <a accesskey="n" href="coredbclose.html">Next</a></td> 170 </tr> 171 <tr> 172 <td width="40%" align="left" valign="top">Getting and Using DB </td> 173 <td width="20%" align="center"> 174 <a accesskey="h" href="index.html">Home</a> 175 </td> 176 <td width="40%" align="right" valign="top"> Closing Databases</td> 177 </tr> 178 </table> 179 </div> 180 </body> 181</html> 182