1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Chapter 4. Using Cursors</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
10 <link rel="up" href="index.html" title="Getting Started with Berkeley DB" />
11 <link rel="prev" href="DbUsage.html" title="Database Usage Example" />
12 <link rel="next" href="Positioning.html" title="Getting Records Using the Cursor" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">Chapter 4. Using Cursors</th>
24 <td width="20%" align="left"><a accesskey="p" href="DbUsage.html">Prev</a> </td>
25 <th width="60%" align="center"> </th>
26 <td width="20%" align="right"> <a accesskey="n" href="Positioning.html">Next</a></td>
31 <div class="chapter" lang="en" xml:lang="en">
32 <div class="titlepage">
35 <h2 class="title"><a id="Cursors"></a>Chapter 4. Using Cursors</h2>
41 <b>Table of Contents</b>
46 <a href="Cursors.html#openCursor">Opening and Closing Cursors</a>
51 <a href="Positioning.html">Getting Records Using the Cursor</a>
58 <a href="Positioning.html#cursorsearch">Searching for Records</a>
63 <a href="Positioning.html#getdups">Working with Duplicate Records</a>
70 <a href="PutEntryWCursor.html">Putting Records Using Cursors</a>
75 <a href="DeleteEntryWCursor.html">Deleting Records Using Cursors</a>
80 <a href="ReplacingEntryWCursor.html">Replacing Records Using Cursors</a>
85 <a href="CoreCursorUsage.html">Cursor Example</a>
91 Cursors provide a mechanism by which you can iterate over the records in a
92 database. Using cursors, you can get, put, and delete database records. If
93 a database allows duplicate records, then cursors are
97 <span>the easiest way that you can access anything
98 other than the first record for a given key.</span>
101 This chapter introduces cursors. It explains how to open and close them, how
102 to use them to modify databases, and how to use them with duplicate records.
104 <div class="sect1" lang="en" xml:lang="en">
105 <div class="titlepage">
108 <h2 class="title" style="clear: both"><a id="openCursor"></a>Opening and Closing Cursors</h2>
113 Cursors are managed using the
114 <span><code class="classname">DBC</code> structure.</span>
116 To use a cursor, you must open it using the
117 <code class="methodname">DB->cursor()</code>
122 <a id="c_cursor1"></a>
123 <pre class="programlisting">#include <db.h>
130 /* Database open omitted for clarity */
133 my_database->cursor(my_database, NULL, &cursorp, 0); </pre>
135 When you are done with the cursor, you should close it. To close a
137 <code class="methodname">DBC->close()</code>
139 method. Note that closing your database while cursors are still opened
140 within the scope of the DB handle, especially if those cursors are
141 writing to the database, can have unpredictable results.
142 It is recommended that you
143 close all cursor handles after their use to ensure concurrency and to release resources such as page locks.
145 <a id="c_cursor2"></a>
146 <pre class="programlisting">#include <db.h>
153 /* Database and cursor open omitted for clarity */
156 cursorp->close(cursorp);
158 if (my_database != NULL)
159 my_database->close(my_database, 0); </pre>
162 <div class="navfooter">
164 <table width="100%" summary="Navigation footer">
166 <td width="40%" align="left"><a accesskey="p" href="DbUsage.html">Prev</a> </td>
167 <td width="20%" align="center"> </td>
168 <td width="40%" align="right"> <a accesskey="n" href="Positioning.html">Next</a></td>
171 <td width="40%" align="left" valign="top">Database Usage Example </td>
172 <td width="20%" align="center">
173 <a accesskey="h" href="index.html">Home</a>
175 <td width="40%" align="right" valign="top"> Getting Records Using the Cursor</td>