2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB Reference Guide: Removing elements from a database</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<table width="100%"><tr valign=top>
13
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Simple Tutorial</dl></h3></td>
14
<td align=right><a href="../../ref/simple_tut/get.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/simple_tut/close.html"><img src="../../images/next.gif" alt="Next"></a>
17
<h1 align=center>Removing elements from a database</h1>
18
<p>The simplest way to remove elements from a database is the <a href="../../api_c/db_del.html">DB->del</a>
20
<p>The <a href="../../api_c/db_del.html">DB->del</a> interface takes four of the same five arguments that
21
the <a href="../../api_c/db_get.html">DB->get</a> and <a href="../../api_c/db_put.html">DB->put</a> interfaces take. The difference
22
is that there is no need to specify a data item, as the delete operation
23
is only interested in the key that you want to remove.
25
<p><dt>db<dd>The database handle returned by <a href="../../api_c/db_create.html">db_create</a>.
26
<p><dt>txnid<dd>A transaction ID.
27
In our simple case, we aren't expecting to recover the database after
28
application or system crash, so we aren't using transactions, and will
29
leave this argument unspecified.
30
<p><dt>key<dd>The key item for the key/data pair that we want to delete from the
32
<p><dt>flags<dd>Optional flags modifying the underlying behavior of the <a href="../../api_c/db_del.html">DB->del</a>
33
interface. There are currently no available flags for this interface,
34
so the flags argument should always be set to 0.
36
<p>Here's what the code to call <a href="../../api_c/db_del.html">DB->del</a> looks like:
37
<p><blockquote><pre>#include <sys/types.h>
38
#include <stdio.h>
41
#define DATABASE "access.db"
50
if ((ret = db_create(&dbp, NULL, 0)) != 0) {
51
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
54
if ((ret = dbp->open(dbp,
55
NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
56
dbp->err(dbp, ret, "%s", DATABASE);
60
memset(&key, 0, sizeof(key));
61
memset(&data, 0, sizeof(data));
63
key.size = sizeof("fruit");
65
data.size = sizeof("apple");
67
if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) == 0)
68
printf("db: %s: key stored.\n", (char *)key.data);
70
dbp->err(dbp, ret, "DB->put");
74
if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0)
75
printf("db: %s: key retrieved: data was %s.\n",
76
(char *)key.data, (char *)data.data);
78
dbp->err(dbp, ret, "DB->get");
81
<p><b> if ((ret = dbp->del(dbp, NULL, &key, 0)) == 0)
82
printf("db: %s: key was deleted.\n", (char *)key.data);
84
dbp->err(dbp, ret, "DB->del");
87
</b></pre></blockquote>
88
<p>After the <a href="../../api_c/db_del.html">DB->del</a> call returns, the entry to which the key
89
<b>fruit</b> refers has been removed from the database.
90
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/simple_tut/get.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/simple_tut/close.html"><img src="../../images/next.gif" alt="Next"></a>
92
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>