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="CoreDbUsage.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 <tt class="classname">DBT</tt> 90 91 92 <span>structures</span> 93 (see <a href="DBEntry.html">Database Records</a> for details on this 94 95 <span>structure</span>). 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 102 <span>contained in </span> 103 104 105 <tt class="classname">DBT</tt> 106 107 108 109 <span>structures.</span> 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 To open a database, you must first use the <tt class="function">db_create()</tt> function to 123 initialize a <tt class="classname">DB</tt> handle. 124 Once you have initialized the <tt class="classname">DB</tt> 125 handle, you use its <tt class="methodname">open()</tt> method to open the database. 126 </p> 127 <p> 128 Note that by default, DB does not create databases if they do not already exist. 129 To override this behavior, specify the 130 <a href="DBOpenFlags.html" title="Database Open Flags"><tt class="literal">DB_CREATE</tt></a> flag on the 131 <tt class="methodname">open()</tt> method. 132 </p> 133 <p> 134 The following code fragment illustrates a database open: 135 136 </p> 137 <a id="c_db1"></a> 138 <pre class="programlisting">#include <db.h> 139 140... 141 142DB *dbp; /* DB structure handle */ 143u_int32_t flags; /* database open flags */ 144int ret; /* function return value */ 145 146/* Initialize the structure. This 147 * database is not opened in an environment, 148 * so the environment pointer is NULL. */ 149ret = db_create(&dbp, NULL, 0); 150if (ret != 0) { 151 /* Error handling goes here */ 152} 153 154/* Database open flags */ 155flags = DB_CREATE; /* If the database does not exist, 156 * create it.*/ 157 158/* open the database */ 159ret = dbp->open(dbp, /* DB structure pointer */ 160 NULL, /* Transaction pointer */ 161 "my_db.db", /* On-disk file that holds the database. */ 162 NULL, /* Optional logical database name */ 163 DB_BTREE, /* Database access method */ 164 flags, /* Open flags */ 165 0); /* File mode (using defaults) */ 166if (ret != 0) { 167 /* Error handling goes here */ 168}</pre> 169 </div> 170 </div> 171 <div class="navfooter"> 172 <hr /> 173 <table width="100%" summary="Navigation footer"> 174 <tr> 175 <td width="40%" align="left"><a accesskey="p" href="gettingit.html">Prev</a> </td> 176 <td width="20%" align="center"> 177 <a accesskey="u" href="index.html">Up</a> 178 </td> 179 <td width="40%" align="right"> <a accesskey="n" href="coredbclose.html">Next</a></td> 180 </tr> 181 <tr> 182 <td width="40%" align="left" valign="top">Getting and Using DB </td> 183 <td width="20%" align="center"> 184 <a accesskey="h" href="index.html">Home</a> 185 </td> 186 <td width="40%" align="right" valign="top"> Closing Databases</td> 187 </tr> 188 </table> 189 </div> 190 </body> 191</html> 192