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