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">
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.73.2" />
9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
10 <link rel="up" href="databases.html" title="Chapter 2. Databases" />
11 <link rel="prev" href="CoreEnvUsage.html" title="Managing Databases in Environments" />
12 <link rel="next" href="DBEntry.html" title="Chapter 3. Database Records" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">Database Example</th>
24 <td width="20%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 2. Databases</th>
26 <td width="20%" align="right"> <a accesskey="n" href="DBEntry.html">Next</a></td>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
35 <h2 class="title" style="clear: both"><a id="CoreDbCXXUsage"></a>Database Example</h2>
40 Throughout this book we will build a couple of applications that load
41 and retrieve inventory data from DB databases. While we are not yet ready to
42 begin reading from or writing to our databases, we can at least create
43 the class that we will use to manage our databases.
46 Note that subsequent examples in this book will build on this code to
47 perform the more interesting work of writing to and reading from the
51 Note that you can find the complete implementation of these functions
54 <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_cxx/getting_started</pre>
56 where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you
57 placed your DB distribution.
62 <b>Example 2.1 MyDb Class</b>
64 <div class="example-contents">
66 To manage our database open and close activities, we encapsulate them
67 in the <code class="classname">MyDb</code> class. There are several good reasons
68 to do this, the most important being that we can ensure our databases are
69 closed by putting that activity in the <code class="classname">MyDb</code>
73 To begin, we create our class definition:
76 <pre class="programlisting">// File: MyDb.hpp
77 #include <db_cxx.h>
82 // Constructor requires a path to the database,
83 // and a database name.
84 MyDb(std::string &path, std::string &dbName);
86 // Our destructor just calls our private close method.
89 inline Db &getDb() {return db_;}
93 std::string dbFileName_;
96 // Make sure the default constructor is private
97 // We don't want it used.
98 MyDb() : db_(NULL, 0) {}
100 // We put our database close activity here.
101 // This is called from our destructor. In
102 // a more complicated example, we might want
103 // to make this method public, but a private
104 // method is more appropriate for this example.
108 Next we need the implementation for the constructor:
110 <a id="cxx_db12"></a>
111 <pre class="programlisting">// File: MyDb.cpp
114 // Class constructor. Requires a path to the location
115 // where the database is located, and a database name
116 MyDb::MyDb(std::string &path, std::string &dbName)
117 : db_(NULL, 0), // Instantiate Db object
118 dbFileName_(path + dbName), // Database file name
119 cFlags_(DB_CREATE) // If the database doesn't yet exist,
120 // allow it to be created.
124 // Redirect debugging information to std::cerr
125 db_.set_error_stream(&std::cerr);
128 db_.open(NULL, dbFileName_.c_str(), NULL, DB_BTREE, cFlags_, 0);
130 // DbException is not a subclass of std::exception, so we
131 // need to catch them both.
132 catch(DbException &e)
134 std::cerr << "Error opening database: " << dbFileName_ << "\n";
135 std::cerr << e.what() << std::endl;
137 catch(std::exception &e)
139 std::cerr << "Error opening database: " << dbFileName_ << "\n";
140 std::cerr << e.what() << std::endl;
144 And then we need the implementation for the
145 <code class="methodname">close()</code> method:
148 <a id="cxx_db12.1"></a>
149 <pre class="programlisting">// Private member used to close a database. Called from the class
158 std::cout << "Database " << dbFileName_
159 << " is closed." << std::endl;
161 catch(DbException &e)
163 std::cerr << "Error closing database: " << dbFileName_ << "\n";
164 std::cerr << e.what() << std::endl;
166 catch(std::exception &e)
168 std::cerr << "Error closing database: " << dbFileName_ << "\n";
169 std::cerr << e.what() << std::endl;
174 <br class="example-break" />
176 <div class="navfooter">
178 <table width="100%" summary="Navigation footer">
180 <td width="40%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a> </td>
181 <td width="20%" align="center">
182 <a accesskey="u" href="databases.html">Up</a>
184 <td width="40%" align="right"> <a accesskey="n" href="DBEntry.html">Next</a></td>
187 <td width="40%" align="left" valign="top">Managing Databases in Environments </td>
188 <td width="20%" align="center">
189 <a accesskey="h" href="index.html">Home</a>
191 <td width="40%" align="right" valign="top"> Chapter 3. Database Records</td>