1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
<! "@(#)close.so 10.4 (Sleepycat) 10/20/98">
<!Copyright 1997, 1998 by Sleepycat Software, Inc. All rights reserved.>
<html>
<body bgcolor=white>
<head>
<title>Berkeley DB Reference Guide: Simple Tutorial</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btr
ee,hash,hashing,transaction,transactions,locking,logging,access method,access me
thods,java,C,C++">
</head>
<h3>Berkeley DB Reference Guide: Simple Tutorial</h3>
<p>
<h1 align=center>Closing a database</h1>
<p>
The only other operation that we need for our simple example is closing
the database. Again, this interface is accessed through a function
pointer that is an element of the database handle returned by
<a href="../../api_c/Db/open.html">db_open</a>.
<p>
It is necessary that the database be closed. The most important reason
for this is that Berkeley DB runs on top of an underlying buffer cache. If
the the database is never closed, changes that you have made to the
database may never make it out to disk, because they are still in the
cache. As the close function, by default, flushes the cache, closing
the database will update the on-disk information.
<p>
The <a href="../../api_c/Db/close.html">DB->close</a> interface takes two arguments:
<dl compact>
<p><dt>db<dd>The database handle returned by <a href="../../api_c/Db/open.html">db_open</a>.
<p><dt>flags<dd>Optional flags modifying the underlying behavior of the <a href="../../api_c/Db/close.html">DB->close</a>
interface.
</dl>
<p>
Here's what the code to call <a href="../../api_c/Db/close.html">DB->close</a> looks like:
<p><ul><pre>
#include <sys/types.h>
#include <stdio.h>
#include <db.h>
<p>
#define DATABASE "access.db"
<p>
int
main()
{
extern int errno;
DB *dbp;
DBT key, data;
<p>
if ((errno = db_open(DATABASE,
DB_BTREE, DB_CREATE, 0664, NULL, NULL, &dbp)) != 0) {
fprintf(stderr, "db: %s: %s\n", DATABASE, strerror(errno));
exit (1);
}
<p>
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
key.data = "fruit";
key.size = sizeof("fruit");
data.data = "apple";
data.size = sizeof("apple");
<p>
switch (errno = dbp->put(dbp, NULL, &key, &data, 0)) {
case 0:
printf("db: %s: key stored.\n", (char *)key.data);
break;
default:
fprintf(stderr, "db: put: %s\n", strerror(errno));
exit (1);
}
<p>
switch (errno = dbp->get(dbp, NULL, &key, &data, 0)) {
case 0:
printf("db: %s: key retrieved: data was %s.\n",
(char *)key.data, (char *)data.data);
break;
case DB_NOTFOUND:
printf("db: %s: key not found.\n", (char *)key.data);
break;
default:
fprintf(stderr, "db: get: %s\n", strerror(errno));
exit (1);
}
<p>
switch (errno = dbp->del(dbp, NULL, &key, 0)) {
case 0:
printf("db: %s: key was deleted.\n", (char *)key.data);
break;
default:
fprintf(stderr, "db: del: %s\n", strerror(errno));
exit (1);
}
<p><a name="startcode"><b>
switch (errno = dbp->close(dbp, 0)) {
case 0:
printf("db: database closed.\n");
break;
default:
fprintf(stderr, "db: get: %s\n", strerror(errno));
exit (1);
}
<p>
exit(0);
}
</b>
</pre></ul><p>
<p>
<a href="../../ref/simple_tut/del.html"><img src="../../images/prev.gif"></a>
<a href="../../ref/toc.html"><img src="../../images/toc.gif"></a>
<a href="../../ref/am/ops.html"><img src="../../images/next.gif"></a>
</tt>
</body>
</html>
|