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>Opening databases within the environment</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="Berkeley DB Programmer's Reference Guide" /> 10 <link rel="up" href="env.html" title="Chapter��9.�� The Berkeley DB Environment" /> 11 <link rel="prev" href="env_create.html" title="Creating a database environment" /> 12 <link rel="next" href="env_error.html" title="Error support" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Opening databases within the environment</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="env_create.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��9.�� 23 The Berkeley DB Environment 24 </th> 25 <td width="20%" align="right">��<a accesskey="n" href="env_error.html">Next</a></td> 26 </tr> 27 </table> 28 <hr /> 29 </div> 30 <div class="sect1" lang="en" xml:lang="en"> 31 <div class="titlepage"> 32 <div> 33 <div> 34 <h2 class="title" style="clear: both"><a id="env_open"></a>Opening databases within the environment</h2> 35 </div> 36 </div> 37 </div> 38 <p>Once the environment has been created, database handles may be created 39and then opened within the environment. This is done by calling the 40<a href="../api_reference/C/dbcreate.html" class="olink">db_create()</a> function and specifying the appropriate environment 41as an argument.</p> 42 <p>File naming, database operations, and error handling will all be done as 43specified for the environment. For example, if the <a href="../api_reference/C/envopen.html#envopen_DB_INIT_LOCK" class="olink">DB_INIT_LOCK</a> 44or <a href="../api_reference/C/envopen.html#envopen_DB_INIT_CDB" class="olink">DB_INIT_CDB</a> flags were specified when the environment was 45created or joined, database operations will automatically perform all 46necessary locking operations for the application.</p> 47 <p>The following is a simple example of opening two databases within a 48database environment:</p> 49 <pre class="programlisting"> DB_ENV *dbenv; 50 DB *dbp1, *dbp2; 51 int ret; 52 53 dbenv = NULL; 54 dbp1 = dbp2 = NULL; 55 56 /* 57 * Create an environment and initialize it for additional error 58 * reporting. 59 */ 60 if ((ret = db_env_create(&dbenv, 0)) != 0) { 61 fprintf(errfp, "%s: %s\n", progname, db_strerror(ret)); 62 return (ret); 63 } 64 65 dbenv->set_errfile(dbenv, errfp); 66 dbenv->set_errpfx(dbenv, progname); 67 68 /* Open an environment with just a memory pool. */ 69 if ((ret = 70 dbenv->open(dbenv, home, DB_CREATE | DB_INIT_MPOOL, 0)) != 0) { 71 dbenv->err(dbenv, ret, "environment open: %s", home); 72 goto err; 73 } 74 75 /* Open database #1. */ 76 if ((ret = db_create(&dbp1, dbenv, 0)) != 0) { 77 dbenv->err(dbenv, ret, "database create"); 78 goto err; 79 } 80 if ((ret = dbp1->open(dbp1, 81 NULL, DATABASE1, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) { 82 dbenv->err(dbenv, ret, "DB->open: %s", DATABASE1); 83 goto err; 84 } 85 86 /* Open database #2. */ 87 if ((ret = db_create(&dbp2, dbenv, 0)) != 0) { 88 dbenv->err(dbenv, ret, "database create"); 89 goto err; 90 } 91 if ((ret = dbp2->open(dbp2, 92 NULL, DATABASE2, NULL, DB_HASH, DB_CREATE, 0664)) != 0) { 93 dbenv->err(dbenv, ret, "DB->open: %s", DATABASE2); 94 goto err; 95 } 96 97 return (0); 98 99err: if (dbp2 != NULL) 100 (void)dbp2->close(dbp2, 0); 101 if (dbp1 != NULL) 102 (void)dbp2->close(dbp1, 0); 103 (void)dbenv->close(dbenv, 0); 104 return (1); 105}</pre> 106 </div> 107 <div class="navfooter"> 108 <hr /> 109 <table width="100%" summary="Navigation footer"> 110 <tr> 111 <td width="40%" align="left"><a accesskey="p" href="env_create.html">Prev</a>��</td> 112 <td width="20%" align="center"> 113 <a accesskey="u" href="env.html">Up</a> 114 </td> 115 <td width="40%" align="right">��<a accesskey="n" href="env_error.html">Next</a></td> 116 </tr> 117 <tr> 118 <td width="40%" align="left" valign="top">Creating a database environment��</td> 119 <td width="20%" align="center"> 120 <a accesskey="h" href="index.html">Home</a> 121 </td> 122 <td width="40%" align="right" valign="top">��Error support</td> 123 </tr> 124 </table> 125 </div> 126 </body> 127</html> 128