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>Database Example</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="DB.html" title="Chapter��2.��Databases" />
11    <link rel="previous" href="CoreEnvUsage.html" title="Managing Databases in Environments" />
12    <link rel="next" href="DBEntry.html" title="Chapter��3.��Database Records" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Database Example</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��2.��Databases</th>
23          <td width="20%" align="right">��<a accesskey="n" href="DBEntry.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="sect1" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title" style="clear: both"><a id="CoreDbCXXUsage"></a>Database Example</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <p>
38        Throughout this book we will build a couple of applications that load
39        and retrieve inventory data from DB databases. While we are not yet ready to
40        begin reading from or writing to our databases, we can at least create
41        the class that we will use to manage our databases.
42    </p>
43      <p>
44        Note that subsequent examples in this book will build on this code to
45        perform the more interesting work of writing to and reading from the
46        databases. 
47    </p>
48      <p>
49        Note that you can find the complete implementation of these functions
50        in:
51    </p>
52      <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_cxx/getting_started</pre>
53      <p>
54        where <tt class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></tt> is the location where you
55        placed your DB distribution.  
56    </p>
57      <div class="example">
58        <a id="MyDb-cxx"></a>
59        <p class="title">
60          <b>Example 2.1 MyDb Class</b>
61        </p>
62        <p>
63            To manage our database open and close activities, we encapsulate them
64            in the <tt class="classname">MyDb</tt> class. There are several good reasons
65            to do this, the most important being that we can ensure our databases are
66            closed by putting that activity in the <tt class="classname">MyDb</tt>
67            class destructor.
68        </p>
69        <p>
70            To begin, we create our class definition:
71        </p>
72        <a id="cxx_db11"></a>
73        <pre class="programlisting">// File: MyDb.hpp
74#include &lt;db_cxx.h&gt;
75                                                                                                                                    
76class MyDb
77{
78public:
79    // Constructor requires a path to the database,
80    // and a database name.
81    MyDb(std::string &amp;path, std::string &amp;dbName);
82                                                                                                                                    
83    // Our destructor just calls our private close method.
84    ~MyDb() { close(); }
85                                                                                                                                    
86    inline Db &amp;getDb() {return db_;}
87                                                                                                                                    
88private:
89    Db db_;
90    std::string dbFileName_;
91    u_int32_t cFlags_;
92                                                                                                                                    
93    // Make sure the default constructor is private
94    // We don't want it used.
95    MyDb() : db_(NULL, 0) {}
96                                                                                                                                    
97    // We put our database close activity here.
98    // This is called from our destructor. In
99    // a more complicated example, we might want
100    // to make this method public, but a private
101    // method is more appropriate for this example.
102    void close();
103}; </pre>
104        <p>
105        Next we need the implementation for the constructor:
106    </p>
107        <a id="cxx_db12"></a>
108        <pre class="programlisting">// File: MyDb.cpp
109#include "MyDb.hpp"
110
111// Class constructor. Requires a path to the location
112// where the database is located, and a database name
113MyDb::MyDb(std::string &amp;path, std::string &amp;dbName)
114    : db_(NULL, 0),               // Instantiate Db object
115      dbFileName_(path + dbName), // Database file name
116      cFlags_(DB_CREATE)          // If the database doesn't yet exist,
117                                  // allow it to be created.
118{
119    try
120    {
121        // Redirect debugging information to std::cerr
122        db_.set_error_stream(&amp;std::cerr);
123                                                                                                                                    
124        // Open the database
125        db_.open(NULL, dbFileName_.c_str(), NULL, DB_BTREE, cFlags_, 0);
126    }
127    // DbException is not a subclass of std::exception, so we
128    // need to catch them both.
129    catch(DbException &amp;e)
130    {
131        std::cerr &lt;&lt; "Error opening database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
132        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
133    }
134    catch(std::exception &amp;e)
135    {
136        std::cerr &lt;&lt; "Error opening database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
137        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
138    }
139}</pre>
140        <p>
141    And then we need the implementation for the 
142   <tt class="methodname">close()</tt> method:
143   
144</p>
145        <a id="cxx_db12.1"></a>
146        <pre class="programlisting">// Private member used to close a database. Called from the class
147// destructor.
148void
149MyDb::close()
150{
151    // Close the db
152    try
153    {
154        db_.close(0);
155        std::cout &lt;&lt; "Database " &lt;&lt; dbFileName_
156                  &lt;&lt; " is closed." &lt;&lt; std::endl;
157    }
158    catch(DbException &amp;e)
159    {
160        std::cerr &lt;&lt; "Error closing database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
161        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
162    }
163    catch(std::exception &amp;e)
164    {
165        std::cerr &lt;&lt; "Error closing database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
166        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
167    }
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="CoreEnvUsage.html">Prev</a>��</td>
176          <td width="20%" align="center">
177            <a accesskey="u" href="DB.html">Up</a>
178          </td>
179          <td width="40%" align="right">��<a accesskey="n" href="DBEntry.html">Next</a></td>
180        </tr>
181        <tr>
182          <td width="40%" align="left" valign="top">Managing Databases in Environments��</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">��Chapter��3.��Database Records</td>
187        </tr>
188      </table>
189    </div>
190  </body>
191</html>
192