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