*I;Berkeley DB Reference Guide: Simple Tutorial[P5

Berkeley DB Reference Guide: Simple Tutorial



9

Retrieving elements from a database



<The simplest way to retrieve elements from a database is the\DB->get interface. This interface is accessed through<a function pointer that is an element of the database handle;returned by db_open.

bThe DB->get interface takes the same five arguments that the>DB->put interface takes:

\

db
The database handle returned by db_open.!

txnid
A transaction ID.EIn our simple case, we aren't expecting to recover the database afterFapplication or system crash, so we aren't using transactions, and will leave this argument unspecified.R

key
The key item for the key/data pair that we want to retrieve from the database.T

data
The data item for the key/data pair that we want to retrieve from the database.u

flags
Optional flags modifying the underlying behavior of the DB->get interface.


VHere's what the code to call DB->get looks like:



FNote that we do not have to clear the structures that we're passing tocDB->get again, we can simply use the ones that were passed to;the DB->put function.

@We also have to add a new possible error condition, DB_NOTFOUND.dThis is because there are three possible returns from DB->get:

    E
  1. The call might be successful and the key found, in which case theerror return will be 0.B
  2. The call might be successful, but the key not found, in0which case the error return will be DB_NOTFOUND.F
  3. Or, the call might not be successful, in which case a system errorwill be returned.


MAMÿÿ