<!--$Id: curget.so,v 10.20 2003/10/18 19:15:52 bostic Exp $--> <!--Copyright (c) 1997,2007 Oracle. All rights reserved.--> <!--See the file LICENSE for redistribution information.--> <html> <head> <title>Berkeley DB Reference Guide: Retrieving records with a cursor</title> <meta name="description" content="Berkeley DB: An embedded database programmatic toolkit."> <meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++"> </head> <body bgcolor=white> <a name="2"><!--meow--></a><a name="3"><!--meow--></a> <table width="100%"><tr valign=top> <td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></h3></td> <td align=right><a href="../am/cursor.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am/curput.html"><img src="../../images/next.gif" alt="Next"></a> </td></tr></table> <p> <h3 align=center>Retrieving records with a cursor</h3> <p>The <a href="../../api_c/dbc_get.html">DBcursor->get</a> method retrieves records from the database using a cursor. The <a href="../../api_c/dbc_get.html">DBcursor->get</a> method takes a flag which controls how the cursor is positioned within the database and returns the key/data item associated with that positioning. Similar to <a href="../../api_c/db_get.html">DB->get</a>, <a href="../../api_c/dbc_get.html">DBcursor->get</a> may also take a supplied key and retrieve the data associated with that key from the database. There are several flags that you can set to customize retrieval.</p> <h3>Cursor position flags</h3> <dl compact> <dt><a href="../../api_c/dbc_get.html#DB_FIRST">DB_FIRST</a>, <a href="../../api_c/dbc_get.html#DB_LAST">DB_LAST</a><dd>Return the first (last) record in the database. <dt><a href="../../api_c/dbc_get.html#DB_NEXT">DB_NEXT</a>, <a href="../../api_c/dbc_get.html#DB_PREV">DB_PREV</a><dd>Return the next (previous) record in the database. <dt><a href="../../api_c/dbc_get.html#DB_NEXT_DUP">DB_NEXT_DUP</a><dd>Return the next record in the database, if it is a duplicate data item for the current key. <dt><a href="../../api_c/dbc_get.html#DB_NEXT_NODUP">DB_NEXT_NODUP</a>, <a href="../../api_c/dbc_get.html#DB_PREV_NODUP">DB_PREV_NODUP</a><dd>Return the next (previous) record in the database that is not a duplicate data item for the current key. <dt><a href="../../api_c/dbc_get.html#DB_CURRENT">DB_CURRENT</a><dd>Return the record from the database to which the cursor currently refers. </dl> <h3>Retrieving specific key/data pairs</h3> <dl compact> <dt><a href="../../api_c/dbc_get.html#DB_SET">DB_SET</a><dd>Return the record from the database that matches the supplied key. In the case of duplicates the first duplicate is returned and the cursor is positioned at the beginning of the duplicate list. The user can then traverse the duplicate entries for the key. <dt><a href="../../api_c/dbc_get.html#DB_SET_RANGE">DB_SET_RANGE</a><dd>Return the smallest record in the database greater than or equal to the supplied key. This functionality permits partial key matches and range searches in the Btree access method. <dt><a href="../../api_c/db_get.html#DB_GET_BOTH">DB_GET_BOTH</a><dd>Return the record from the database that matches both the supplied key and data items. This is particularly useful when there are large numbers of duplicate records for a key, as it allows the cursor to easily be positioned at the correct place for traversal of some part of a large set of duplicate records. <dt><a href="../../api_c/db_get.html#DB_GET_BOTH_RANGE">DB_GET_BOTH_RANGE</a><dd>Return the smallest record in the database greater than or equal to the supplied key and data items. </dl> <h3>Retrieving based on record numbers</h3> <dl compact> <dt><a href="../../api_c/db_get.html#DB_SET_RECNO">DB_SET_RECNO</a><dd>If the underlying database is a Btree, and was configured so that it is possible to search it by logical record number, retrieve a specific record based on a record number argument. <dt><a href="../../api_c/dbc_get.html#DB_GET_RECNO">DB_GET_RECNO</a><dd>If the underlying database is a Btree, and was configured so that it is possible to search it by logical record number, return the record number for the record to which the cursor refers. </dl> <h3>Special-purpose flags</h3> <dl compact> <dt><a href="../../api_c/db_get.html#DB_CONSUME">DB_CONSUME</a><dd>Read-and-delete: the first record (the head) of the queue is returned and deleted. The underlying database must be a Queue. <dt><a href="../../api_c/dbc_get.html#DB_RMW">DB_RMW</a><dd>Read-modify-write: acquire write locks instead of read locks during retrieval. This can enhance performance in threaded applications by reducing the chance of deadlock. </dl> <p>In all cases, the cursor is repositioned by a <a href="../../api_c/dbc_get.html">DBcursor->get</a> operation to point to the newly-returned key/data pair in the database.</p> <p>The following is a code example showing a cursor walking through a database and displaying the records it contains to the standard output:</p> <blockquote><pre>int display(database) char *database; { DB *dbp; DBC *dbcp; DBT key, data; int close_db, close_dbc, ret; <p> close_db = close_dbc = 0; <p> /* Open the database. */ if ((ret = db_create(&dbp, NULL, 0)) != 0) { fprintf(stderr, "%s: db_create: %s\n", progname, db_strerror(ret)); return (1); } close_db = 1; <p> /* Turn on additional error output. */ dbp->set_errfile(dbp, stderr); dbp->set_errpfx(dbp, progname); <p> /* Open the database. */ if ((ret = dbp->open(dbp, NULL, database, NULL, DB_UNKNOWN, DB_RDONLY, 0)) != 0) { dbp->err(dbp, ret, "%s: DB->open", database); goto err; } <p> /* Acquire a cursor for the database. */ if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) { dbp->err(dbp, ret, "DB->cursor"); goto err; } close_dbc = 1; <p> /* Initialize the key/data return pair. */ memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); <p> /* Walk through the database and print out the key/data pairs. */ while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0) printf("%.*s : %.*s\n", (int)key.size, (char *)key.data, (int)data.size, (char *)data.data); if (ret != DB_NOTFOUND) { dbp->err(dbp, ret, "DBcursor->get"); goto err; } <p> err: if (close_dbc && (ret = dbcp->c_close(dbcp)) != 0) dbp->err(dbp, ret, "DBcursor->close"); if (close_db && (ret = dbp->close(dbp, 0)) != 0) fprintf(stderr, "%s: DB->close: %s\n", progname, db_strerror(ret)); return (0); }</pre></blockquote> <table width="100%"><tr><td><br></td><td align=right><a href="../am/cursor.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am/curput.html"><img src="../../images/next.gif" alt="Next"></a> </td></tr></table> <p><font size=1>Copyright (c) 1996,2007 Oracle. All rights reserved.</font> </body> </html>