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="CoreDbUsage.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="CoreDbUsage.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 <span> 52 create the environment handle using <tt class="classname"></tt>, and then 53 </span> 54 open it. At open time, you must identify the directory in 55 which it resides. This directory must exist prior to the open attempt. 56 You can also identify open properties, such as whether the environment can be 57 created if it does not already exist. 58 </p> 59 <p> 60 You will also need to initialize the in-memory cache when you open your environment. 61 </p> 62 <p> 63 For example, to 64 <span>create an environment handle and</span> 65 open an environment: 66 </p> 67 <a id="c_env1"></a> 68 <pre class="programlisting">#include <db.h> 69... 70DB_ENV *myEnv; /* Env structure handle */ 71DB *dbp; /* DB structure handle */ 72u_int32_t db_flags; /* database open flags */ 73u_int32_t env_flags; /* env open flags */ 74int ret; /* function return value */ 75 76/* 77 Create an environment object and initialize it for error 78 reporting. 79*/ 80ret = db_env_create(&myEnv, 0); 81if (ret != 0) { 82 fprintf(stderr, "Error creating env handle: %s\n", db_strerror(ret)); 83 return -1; 84} 85 86/* Open the environment. */ 87env_flags = DB_CREATE | /* If the environment does not exist, 88 * create it. */ 89 DB_INIT_MPOOL; /* Initialize the in-memory cache. */ 90 91ret = myEnv->open(myEnv, /* DB_ENV ptr */ 92 "/export1/testEnv", /* env home directory */ 93 env_flags, /* Open flags */ 94 0); /* File mode (default) */ 95if (ret != 0) { 96 fprintf(stderr, "Environment open failed: %s", db_strerror(ret)); 97 return -1; 98} </pre> 99 <p> 100 Once an environment is opened, you can open databases in it. Note that by default databases 101 are stored in the environment's home directory, or relative to that directory if you 102 provide any sort of a path in the database's file name: 103 </p> 104 <a id="c_env2"></a> 105 <pre class="programlisting">/* 106 * Initialize the DB structure. Pass the pointer 107 * to the environment in which this DB is opened. 108 */ 109ret = db_create(&dbp, myEnv, 0); 110if (ret != 0) { 111 /* Error handling goes here */ 112} 113 114/* Database open flags */ 115db_flags = DB_CREATE; /* If the database does not exist, 116 * create it.*/ 117 118/* open the database */ 119ret = dbp->open(dbp, /* DB structure pointer */ 120 NULL, /* Transaction pointer */ 121 "my_db.db", /* On-disk file that holds the database. */ 122 NULL, /* Optional logical database name */ 123 DB_BTREE, /* Database access method */ 124 db_flags, /* Open flags */ 125 0); /* File mode (using defaults) */ 126if (ret != 0) { 127 /* Error handling goes here */ 128}</pre> 129 <p> 130 When you are done with an environment, you must close it. Before you close an environment, 131 make sure you close any opened databases. 132 </p> 133 <a id="c_env3"></a> 134 <pre class="programlisting">/* 135* Close the database and environment 136*/ 137 138if (dbp != NULL) { 139 dbp->close(dbp, 0); 140} 141 142if (myEnv != NULL) { 143 myEnv->close(myEnv, 0); 144} </pre> 145 </div> 146 <div class="navfooter"> 147 <hr /> 148 <table width="100%" summary="Navigation footer"> 149 <tr> 150 <td width="40%" align="left"><a accesskey="p" href="dbErrorReporting.html">Prev</a> </td> 151 <td width="20%" align="center"> 152 <a accesskey="u" href="DB.html">Up</a> 153 </td> 154 <td width="40%" align="right"> <a accesskey="n" href="CoreDbUsage.html">Next</a></td> 155 </tr> 156 <tr> 157 <td width="40%" align="left" valign="top">Error Reporting Functions </td> 158 <td width="20%" align="center"> 159 <a accesskey="h" href="index.html">Home</a> 160 </td> 161 <td width="40%" align="right" valign="top"> Database Example</td> 162 </tr> 163 </table> 164 </div> 165 </body> 166</html> 167