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