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.73.2" /> 9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB" /> 10 <link rel="up" href="DB.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" /> 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> 36 <p> 37 Throughout this book we will build a couple of applications that load 38 and retrieve inventory data from DB databases. While we are not yet ready to 39 begin reading from or writing to our databases, we can at least create 40 the class that we will use to manage our databases. 41 </p> 42 <p> 43 Note that subsequent examples in this book will build on this code to 44 perform the more interesting work of writing to and reading from the 45 databases. 46 </p> 47 <p> 48 Note that you can find the complete implementation of these functions 49 in: 50 </p> 51 <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_cxx/getting_started</pre> 52 <p> 53 where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you 54 placed your DB distribution. 55 </p> 56 <div class="example"> 57 <a id="MyDb-cxx"></a> 58 <p class="title"> 59 <b>Example 2.1 MyDb Class</b> 60 </p> 61 <div class="example-contents"> 62 <p> 63 To manage our database open and close activities, we encapsulate them 64 in the <code class="classname">MyDb</code> 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 <code class="classname">MyDb</code> 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 <db_cxx.h> 75 76class MyDb 77{ 78public: 79 // Constructor requires a path to the database, 80 // and a database name. 81 MyDb(std::string &path, std::string &dbName); 82 83 // Our destructor just calls our private close method. 84 ~MyDb() { close(); } 85 86 inline Db &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 &path, std::string &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(&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 &e) 130 { 131 std::cerr << "Error opening database: " << dbFileName_ << "\n"; 132 std::cerr << e.what() << std::endl; 133 } 134 catch(std::exception &e) 135 { 136 std::cerr << "Error opening database: " << dbFileName_ << "\n"; 137 std::cerr << e.what() << std::endl; 138 } 139}</pre> 140 <p> 141 And then we need the implementation for the 142 <code class="methodname">close()</code> 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 << "Database " << dbFileName_ 156 << " is closed." << std::endl; 157 } 158 catch(DbException &e) 159 { 160 std::cerr << "Error closing database: " << dbFileName_ << "\n"; 161 std::cerr << e.what() << std::endl; 162 } 163 catch(std::exception &e) 164 { 165 std::cerr << "Error closing database: " << dbFileName_ << "\n"; 166 std::cerr << e.what() << std::endl; 167 } 168} </pre> 169 </div> 170 </div> 171 <br class="example-break" /> 172 </div> 173 <div class="navfooter"> 174 <hr /> 175 <table width="100%" summary="Navigation footer"> 176 <tr> 177 <td width="40%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a>��</td> 178 <td width="20%" align="center"> 179 <a accesskey="u" href="DB.html">Up</a> 180 </td> 181 <td width="40%" align="right">��<a accesskey="n" href="DBEntry.html">Next</a></td> 182 </tr> 183 <tr> 184 <td width="40%" align="left" valign="top">Managing Databases in Environments��</td> 185 <td width="20%" align="center"> 186 <a accesskey="h" href="index.html">Home</a> 187 </td> 188 <td width="40%" align="right" valign="top">��Chapter��3.��Database Records</td> 189 </tr> 190 </table> 191 </div> 192 </body> 193</html> 194