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"> 4 <head> 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" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Chapter 4. Using Cursors</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="DbUsage.html">Prev</a> </td> 22 <th width="60%" align="center"> </th> 23 <td width="20%" align="right"> <a accesskey="n" href="Positioning.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="chapter" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title"><a id="Cursors"></a>Chapter 4. Using Cursors</h2> 33 </div> 34 </div> 35 </div> 36 <div class="toc"> 37 <p> 38 <b>Table of Contents</b> 39 </p> 40 <dl> 41 <dt> 42 <span class="sect1"> 43 <a href="Cursors.html#openCursor">Opening and Closing Cursors</a> 44 </span> 45 </dt> 46 <dt> 47 <span class="sect1"> 48 <a href="Positioning.html">Getting Records Using the Cursor</a> 49 </span> 50 </dt> 51 <dd> 52 <dl> 53 <dt> 54 <span class="sect2"> 55 <a href="Positioning.html#cursorsearch">Searching for Records</a> 56 </span> 57 </dt> 58 <dt> 59 <span class="sect2"> 60 <a href="Positioning.html#getdups">Working with Duplicate Records</a> 61 </span> 62 </dt> 63 </dl> 64 </dd> 65 <dt> 66 <span class="sect1"> 67 <a href="PutEntryWCursor.html">Putting Records Using Cursors</a> 68 </span> 69 </dt> 70 <dt> 71 <span class="sect1"> 72 <a href="DeleteEntryWCursor.html">Deleting Records Using Cursors</a> 73 </span> 74 </dt> 75 <dt> 76 <span class="sect1"> 77 <a href="ReplacingEntryWCursor.html">Replacing Records Using Cursors</a> 78 </span> 79 </dt> 80 <dt> 81 <span class="sect1"> 82 <a href="CoreCursorUsage.html">Cursor Example</a> 83 </span> 84 </dt> 85 </dl> 86 </div> 87 <p> 88 Cursors provide a mechanism by which you can iterate over the records in a 89 database. Using cursors, you can get, put, and delete database records. If 90 a database allows duplicate records, then cursors are 91 92 93 94 <span>the easiest way that you can access anything 95 other than the first record for a given key.</span> 96 </p> 97 <p> 98 This chapter introduces cursors. It explains how to open and close them, how 99 to use them to modify databases, and how to use them with duplicate records. 100 </p> 101 <div class="sect1" lang="en" xml:lang="en"> 102 <div class="titlepage"> 103 <div> 104 <div> 105 <h2 class="title" style="clear: both"><a id="openCursor"></a>Opening and Closing Cursors</h2> 106 </div> 107 </div> 108 </div> 109 <p> 110 Cursors are managed using the 111 <span><code class="classname">DBC</code> structure.</span> 112 113 To use a cursor, you must open it using the 114 <code class="methodname">DB->cursor()</code> 115 116 method. 117 </p> 118 <p>For example:</p> 119 <a id="c_cursor1"></a> 120 <pre class="programlisting">#include <db.h> 121 122... 123 124DB *my_database; 125DBC *cursorp; 126 127/* Database open omitted for clarity */ 128 129/* Get a cursor */ 130my_database->cursor(my_database, NULL, &cursorp, 0); </pre> 131 <p> 132 When you are done with the cursor, you should close it. To close a 133 cursor, call the 134 <code class="methodname">DBC->close()</code> 135 136 method. Note that closing your database while cursors are still opened 137 within the scope of the DB handle, especially if those cursors are 138 writing to the database, can have unpredictable results. Always 139 close your cursors before closing your database. 140 </p> 141 <a id="c_cursor2"></a> 142 <pre class="programlisting">#include <db.h> 143 144... 145 146DB *my_database; 147DBC *cursorp; 148 149/* Database and cursor open omitted for clarity */ 150 151if (cursorp != NULL) 152 cursorp->close(cursorp); 153 154if (my_database != NULL) 155 my_database->close(my_database, 0); </pre> 156 </div> 157 </div> 158 <div class="navfooter"> 159 <hr /> 160 <table width="100%" summary="Navigation footer"> 161 <tr> 162 <td width="40%" align="left"><a accesskey="p" href="DbUsage.html">Prev</a> </td> 163 <td width="20%" align="center"> </td> 164 <td width="40%" align="right"> <a accesskey="n" href="Positioning.html">Next</a></td> 165 </tr> 166 <tr> 167 <td width="40%" align="left" valign="top">Database Usage Example </td> 168 <td width="20%" align="center"> 169 <a accesskey="h" href="index.html">Home</a> 170 </td> 171 <td width="40%" align="right" valign="top"> Getting Records Using the Cursor</td> 172 </tr> 173 </table> 174 </div> 175 </body> 176</html> 177