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