summaryrefslogtreecommitdiff
path: root/bdb/docs/ref/simple_tut/get.html
blob: 697aa8f511c13877df6de60309f496b37ae226d7 (plain)
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
<!--$Id: get.so,v 10.23 2000/12/14 21:42:18 bostic Exp $-->
<!--Copyright 1997, 1998, 1999, 2000 by Sleepycat Software, Inc.-->
<!--All rights reserved.-->
<html>
<head>
<title>Berkeley DB Reference Guide: Retrieving elements from a database</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
</head>
<body bgcolor=white>
<table><tr valign=top>
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Simple Tutorial</dl></h3></td>
<td width="1%"><a href="../../ref/simple_tut/put.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../ref/toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/simple_tut/del.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h1 align=center>Retrieving elements from a database</h1>
<p>The simplest way to retrieve elements from a database is the
<a href="../../api_c/db_get.html">DB-&gt;get</a> interface.
<p>The <a href="../../api_c/db_get.html">DB-&gt;get</a> interface takes the same five arguments that the
<a href="../../api_c/db_put.html">DB-&gt;put</a> interface takes:
<p><dl compact>
<p><dt>db<dd>The database handle returned by <a href="../../api_c/db_create.html">db_create</a>.
<p><dt>txnid<dd>A transaction ID.  In our simple case, we aren't expecting to recover
the database after application or system crash, so we aren't using
transactions, and will leave this argument NULL.
<p><dt>key<dd>The key item for the key/data pair that we want to retrieve from the
database.
<p><dt>data<dd>The data item for the key/data pair that we want to retrieve from the
database.
<p><dt>flags<dd>Optional flags modifying the underlying behavior of the <a href="../../api_c/db_get.html">DB-&gt;get</a>
interface.
</dl>
<p>Here's what the code to call <a href="../../api_c/db_get.html">DB-&gt;get</a> looks like:
<p><blockquote><pre>#include &lt;sys/types.h&gt;
#include &lt;stdio.h&gt;
#include &lt;db.h&gt;
<p>
#define	DATABASE "access.db"
<p>
int
main()
{
	DB *dbp;
	DBT key, data;
	int ret;
<p>
	if ((ret = db_create(&dbp, NULL, 0)) != 0) {
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
		exit (1);
	}
	if ((ret = dbp-&gt;open(
	    dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
		dbp-&gt;err(dbp, ret, "%s", DATABASE);
		goto err;
	}
<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>
	if ((ret = dbp-&gt;put(dbp, NULL, &key, &data, 0)) == 0)
		printf("db: %s: key stored.\n", (char *)key.data);
	else {
		dbp-&gt;err(dbp, ret, "DB-&gt;put");
		goto err;
	}
<p><b>	if ((ret = dbp-&gt;get(dbp, NULL, &key, &data, 0)) == 0)
		printf("db: %s: key retrieved: data was %s.\n",
		    (char *)key.data, (char *)data.data);
	else {
		dbp-&gt;err(dbp, ret, "DB-&gt;get");
		goto err;
	}
</b></pre></blockquote>
<p>It is not usually necessary to clear the <a href="../../api_c/dbt.html">DBT</a> structures passed
to the Berkeley DB functions between calls.  This is not always true, when
some of the less commonly used flags for <a href="../../api_c/dbt.html">DBT</a> structures are
used.  The <a href="../../api_c/dbt.html">DBT</a> manual page specified the details of those cases.
<p>It is possible, of course, to distinguish between system errors and the
key/data pair simply not existing in the database.  There are three
standard returns from <a href="../../api_c/db_get.html">DB-&gt;get</a>:
<p><ol>
<p><li>The call might be successful and the key found, in which case the return
value will be 0.
<li>The call might be successful, but the key not found, in which case the
return value will be <a href="../../ref/program/errorret.html#DB_NOTFOUND">DB_NOTFOUND</a>.
<li>The call might not be successful, in which case the return value will
be a system error.
</ol>
<table><tr><td><br></td><td width="1%"><a href="../../ref/simple_tut/put.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../ref/toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/simple_tut/del.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>
</body>
</html>