1<!--$Id: open.so,v 10.19 2003/10/18 19:16:01 bostic Exp $-->
2<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
3<!--See the file LICENSE for redistribution information.-->
4<html>
5<head>
6<title>Berkeley DB Reference Guide: Opening databases within the environment</title>
7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
9</head>
10<body bgcolor=white>
11<table width="100%"><tr valign=top>
12<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Environment</dl></b></td>
13<td align=right><a href="/env/create.html"><img src="/images/prev.gif" alt="Prev"></a><a href="/toc.html"><img src="/images/ref.gif" alt="Ref"></a><a href="/env/error.html"><img src="/images/next.gif" alt="Next"></a>
14</td></tr></table>
15<p align=center><b>Opening databases within the environment</b></p>
16<p>Once the environment has been created, database handles may be created
17and then opened within the environment.  This is done by calling the
18<a href="/api_c/db_class.html">db_create</a> function and specifying the appropriate environment
19as an argument.</p>
20<p>File naming, database operations, and error handling will all be done as
21specified for the environment.  For example, if the <a href="/api_c/env_open.html#DB_INIT_LOCK">DB_INIT_LOCK</a>
22or <a href="/api_c/env_open.html#DB_INIT_CDB">DB_INIT_CDB</a> flags were specified when the environment was
23created or joined, database operations will automatically perform all
24necessary locking operations for the application.</p>
25<p>The following is a simple example of opening two databases within a
26database environment:</p>
27<blockquote><pre>	DB_ENV *dbenv;
28	DB *dbp1, *dbp2;
29	int ret;
30<p>
31	dbenv = NULL;
32	dbp1 = dbp2 = NULL;
33<p>
34	/*
35	 * Create an environment and initialize it for additional error
36	 * reporting.
37	 */
38	if ((ret = db_env_create(&dbenv, 0)) != 0) {
39		fprintf(errfp, "%s: %s\n", progname, db_strerror(ret));
40		return (ret);
41	}
42<p>
43	dbenv-&gt;set_errfile(dbenv, errfp);
44	dbenv-&gt;set_errpfx(dbenv, progname);
45<p>
46	/* Open an environment with just a memory pool. */
47	if ((ret =
48	    dbenv-&gt;open(dbenv, home, DB_CREATE | DB_INIT_MPOOL, 0)) != 0) {
49		dbenv-&gt;err(dbenv, ret, "environment open: %s", home);
50		goto err;
51	}
52<p>
53	/* Open database #1. */
54	if ((ret = db_create(&dbp1, dbenv, 0)) != 0) {
55		dbenv-&gt;err(dbenv, ret, "database create");
56		goto err;
57	}
58	if ((ret = dbp1-&gt;open(dbp1,
59	    NULL, DATABASE1, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
60		dbenv-&gt;err(dbenv, ret, "DB-&gt;open: %s", DATABASE1);
61		goto err;
62	}
63<p>
64	/* Open database #2. */
65	if ((ret = db_create(&dbp2, dbenv, 0)) != 0) {
66		dbenv-&gt;err(dbenv, ret, "database create");
67		goto err;
68	}
69	if ((ret = dbp2-&gt;open(dbp2,
70	    NULL, DATABASE2, NULL, DB_HASH, DB_CREATE, 0664)) != 0) {
71		dbenv-&gt;err(dbenv, ret, "DB-&gt;open: %s", DATABASE2);
72		goto err;
73	}
74<p>
75	return (0);
76<p>
77err:	if (dbp2 != NULL)
78		(void)dbp2-&gt;close(dbp2, 0);
79	if (dbp1 != NULL)
80		(void)dbp2-&gt;close(dbp1, 0);
81	(void)dbenv-&gt;close(dbenv, 0);
82	return (1);
83}</pre></blockquote>
84<table width="100%"><tr><td><br></td><td align=right><a href="/env/create.html"><img src="/images/prev.gif" alt="Prev"></a><a href="/toc.html"><img src="/images/ref.gif" alt="Ref"></a><a href="/env/error.html"><img src="/images/next.gif" alt="Next"></a>
85</td></tr></table>
86<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
87</body>
88</html>
89