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