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="DbCXXUsage.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="DbCXXUsage.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 112 <span><code class="classname">Dbc</code> class.</span> 113 To use a cursor, you must open it using the 114 115 <code class="methodname">Db::cursor()</code> 116 method. 117 </p> 118 <p>For example:</p> 119 <a id="cxx_cursor1"></a> 120 <pre class="programlisting">#include <db_cxx.h> 121 122... 123 124Dbc *cursorp; 125Db my_database(NULL, 0); 126 127// Database open omitted for clarity 128 129// Get a cursor 130my_database.cursor(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 135 <code class="methodname">Dbc::close()</code> 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="cxx_cursor2"></a> 142 <pre class="programlisting">#include <db_cxx.h> 143 144... 145 146Dbc *cursorp; 147Db my_database(NULL, 0); 148 149// Database and cursor open omitted for clarity 150 151if (cursorp != NULL) 152 cursorp->close(); 153 154my_database.close(0);</pre> 155 </div> 156 </div> 157 <div class="navfooter"> 158 <hr /> 159 <table width="100%" summary="Navigation footer"> 160 <tr> 161 <td width="40%" align="left"><a accesskey="p" href="DbCXXUsage.html">Prev</a>��</td> 162 <td width="20%" align="center">��</td> 163 <td width="40%" align="right">��<a accesskey="n" href="Positioning.html">Next</a></td> 164 </tr> 165 <tr> 166 <td width="40%" align="left" valign="top">Database Usage Example��</td> 167 <td width="20%" align="center"> 168 <a accesskey="h" href="index.html">Home</a> 169 </td> 170 <td width="40%" align="right" valign="top">��Getting Records Using the Cursor</td> 171 </tr> 172 </table> 173 </div> 174 </body> 175</html> 176