1
<!--$Id: del.so,v 10.23 2001/05/23 21:03:32 bostic Exp $-->
2
<!--Copyright 1997-2001 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
6
<title>Berkeley DB Reference Guide: Removing elements from a database</title>
7
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
8
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
11
<table width="100%"><tr valign=top>
12
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Simple Tutorial</dl></h3></td>
13
<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>
16
<h1 align=center>Removing elements from a database</h1>
17
<p>The simplest way to remove elements from a database is the <a href="../../api_c/db_del.html">DB->del</a>
19
<p>The <a href="../../api_c/db_del.html">DB->del</a> interface takes four of the same five arguments that
20
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
21
is that there is no need to specify a data item, as the delete operation
22
is only interested in the key that you want to remove.
24
<p><dt>db<dd>The database handle returned by <a href="../../api_c/db_create.html">db_create</a>.
25
<p><dt>txnid<dd>A transaction ID.
26
In our simple case, we aren't expecting to recover the database after
27
application or system crash, so we aren't using transactions, and will
28
leave this argument unspecified.
29
<p><dt>key<dd>The key item for the key/data pair that we want to delete from the
31
<p><dt>flags<dd>Optional flags modifying the underlying behavior of the <a href="../../api_c/db_del.html">DB->del</a>
32
interface. There are currently no available flags for this interface,
33
so the flags argument should always be set to 0.
35
<p>Here's what the code to call <a href="../../api_c/db_del.html">DB->del</a> looks like:
36
<p><blockquote><pre>#include <sys/types.h>
37
#include <stdio.h>
40
#define DATABASE "access.db"
49
if ((ret = db_create(&dbp, NULL, 0)) != 0) {
50
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
53
if ((ret = dbp->open(
54
dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
55
dbp->err(dbp, ret, "%s", DATABASE);
59
memset(&key, 0, sizeof(key));
60
memset(&data, 0, sizeof(data));
62
key.size = sizeof("fruit");
64
data.size = sizeof("apple");
66
if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) == 0)
67
printf("db: %s: key stored.\n", (char *)key.data);
69
dbp->err(dbp, ret, "DB->put");
73
if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0)
74
printf("db: %s: key retrieved: data was %s.\n",
75
(char *)key.data, (char *)data.data);
77
dbp->err(dbp, ret, "DB->get");
80
<p><b> if ((ret = dbp->del(dbp, NULL, &key, 0)) == 0)
81
printf("db: %s: key was deleted.\n", (char *)key.data);
83
dbp->err(dbp, ret, "DB->del");
86
</b></pre></blockquote>
87
<p>After the <a href="../../api_c/db_del.html">DB->del</a> call returns, the entry to which the key
88
<b>fruit</b> refers has been removed from the database.
89
<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>
91
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>