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>Managing Databases in Environments</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="dbErrorReporting.html" title="Error Reporting Functions" /> 12 <link rel="next" href="CoreDbCXXUsage.html" title="Database Example" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Managing Databases in Environments</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="dbErrorReporting.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��2.��Databases</th> 23 <td width="20%" align="right">��<a accesskey="n" href="CoreDbCXXUsage.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="CoreEnvUsage"></a>Managing Databases in Environments</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 In 38 <span> 39 <a class="xref" href="environments.html" title="Environments">Environments</a>, 40 </span> 41 42 43 we introduced 44 environments. While environments are not used in the example built in this book, 45 they are so commonly used for a wide class of DB applications that it is 46 necessary to show their basic usage, if only from a completeness perspective. 47 </p> 48 <p> 49 To use an environment, you must first 50 51 open it. At open time, you must identify the directory in 52 which it resides. This directory must exist prior to the open attempt. 53 You can also identify open properties, such as whether the environment can be 54 created if it does not already exist. 55 </p> 56 <p> 57 You will also need to initialize the in-memory cache when you open your environment. 58 </p> 59 <p> 60 For example, to 61 <span>create an environment handle and</span> 62 open an environment: 63 </p> 64 <a id="cxx_env1"></a> 65 <pre class="programlisting">#include <db_cxx.h> 66... 67u_int32_t env_flags = DB_CREATE | // If the environment does not 68 // exist, create it. 69 DB_INIT_MPOOL; // Initialize the in-memory cache. 70 71std::string envHome("/export1/testEnv"); 72DbEnv myEnv(0); 73 74try { 75 myEnv.open(envHome.c_str(), env_flags, 0); 76} catch(DbException &e) { 77 std::cerr << "Error opening database environment: " 78 << envHome << std::endl; 79 std::cerr << e.what() << std::endl; 80 exit( -1 ); 81} catch(std::exception &e) { 82 std::cerr << "Error opening database environment: " 83 << envHome << std::endl; 84 std::cerr << e.what() << std::endl; 85 exit( -1 ); 86} </pre> 87 <p> 88 Once an environment is opened, you can open databases in it. Note that by default databases 89 are stored in the environment's home directory, or relative to that directory if you 90 provide any sort of a path in the database's file name: 91 </p> 92 <a id="cxx_env2"></a> 93 <pre class="programlisting">#include <db_cxx.h> 94... 95u_int32_t env_flags = DB_CREATE | // If the environment does not 96 // exist, create it. 97 DB_INIT_MPOOL; // Initialize the in-memory cache. 98std::string envHome("/export1/testEnv"); 99 100<strong class="userinput"><code>u_int32_t db_flags = DB_CREATE; // If the database does not 101 // exist, create it.</code></strong> 102<strong class="userinput"><code>std::string dbName("mydb.db");</code></strong> 103DbEnv myEnv(0); 104<strong class="userinput"><code>Db *myDb;</code></strong> 105 106try { 107 myEnv.open(envHome.c_str(), env_flags, 0); 108 <strong class="userinput"><code>myDb = new Db(&myEnv, 0); 109 myDb->open(NULL, 110 dbName.c_str(), 111 NULL, 112 DB_BTREE, 113 db_flags, 114 0);</code></strong> 115} catch(DbException &e) { 116 std::cerr << "Error opening database environment: " 117 << envHome 118 << " and database " 119 << dbName << std::endl; 120 std::cerr << e.what() << std::endl; 121 exit( -1 ); 122} catch(std::exception &e) { 123 std::cerr << "Error opening database environment: " 124 << envHome 125 << " and database " 126 << dbName << std::endl; 127 std::cerr << e.what() << std::endl; 128 exit( -1 ); 129} </pre> 130 <p> 131 When you are done with an environment, you must close it. Before you close an environment, 132 make sure you close any opened databases. 133 </p> 134 <a id="cxx_env3"></a> 135 <pre class="programlisting">try { 136 if (myDb != NULL) { 137 myDb->close(0); 138 } 139 myEnv.close(0); 140 141} catch(DbException &e) { 142 std::cerr << "Error closing database environment: " 143 << envHome 144 << " or database " 145 << dbName << std::endl; 146 std::cerr << e.what() << std::endl; 147 exit( -1 ); 148} catch(std::exception &e) { 149 std::cerr << "Error closing database environment: " 150 << envHome 151 << " or database " 152 << dbName << std::endl; 153 std::cerr << e.what() << std::endl; 154 exit( -1 ); 155} </pre> 156 </div> 157 <div class="navfooter"> 158 <hr /> 159 <table width="100%" summary="Navigation footer"> 160 <tr> 161 <td width="40%" align="left"><a accesskey="p" href="dbErrorReporting.html">Prev</a>��</td> 162 <td width="20%" align="center"> 163 <a accesskey="u" href="DB.html">Up</a> 164 </td> 165 <td width="40%" align="right">��<a accesskey="n" href="CoreDbCXXUsage.html">Next</a></td> 166 </tr> 167 <tr> 168 <td width="40%" align="left" valign="top">Error Reporting Functions��</td> 169 <td width="20%" align="center"> 170 <a accesskey="h" href="index.html">Home</a> 171 </td> 172 <td width="40%" align="right" valign="top">��Database Example</td> 173 </tr> 174 </table> 175 </div> 176 </body> 177</html> 178