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>Deleting Records 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="Cursors.html" title="Chapter��4.��Using Cursors" /> 11 <link rel="prev" href="PutEntryWCursor.html" title="Putting Records Using Cursors" /> 12 <link rel="next" href="ReplacingEntryWCursor.html" title="Replacing Records Using Cursors" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Deleting Records Using Cursors</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="PutEntryWCursor.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��4.��Using Cursors</th> 23 <td width="20%" align="right">��<a accesskey="n" href="ReplacingEntryWCursor.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="DeleteEntryWCursor"></a>Deleting Records Using Cursors</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 38 To delete a record using a cursor, simply position the cursor to the 39 record that you want to delete and then call 40 41 42 43 <span> 44 45 <span><code class="methodname">Dbc::del()</code>.</span> 46 47 </span> 48 </p> 49 <p>For example:</p> 50 <a id="cxx_cursor8"></a> 51 <pre class="programlisting">#include <db_cxx.h> 52#include <string.h> 53 54... 55 56char *key1str = "My first string"; 57Db my_database(NULL, 0); 58Dbc *cursorp; 59 60try { 61 // Database open omitted 62 63 // Get the cursor 64 my_database.cursor(NULL, &cursorp, 0); 65 66 // Set up our DBTs 67 Dbt key(key1str, strlen(key1str) + 1); 68 Dbt data; 69 70 // Iterate over the database, deleting each record in turn. 71 int ret; 72 while ((ret = cursorp->get(&key, &data, 73 DB_SET)) == 0) { 74 cursorp->del(0); 75 } 76 77} catch(DbException &e) { 78 my_database.err(e.get_errno(), "Error!"); 79} catch(std::exception &e) { 80 my_database.errx("Error! %s", e.what()); 81} 82 83// Cursors must be closed 84if (cursorp != NULL) 85 cursorp->close(); 86 87my_database.close(0);</pre> 88 </div> 89 <div class="navfooter"> 90 <hr /> 91 <table width="100%" summary="Navigation footer"> 92 <tr> 93 <td width="40%" align="left"><a accesskey="p" href="PutEntryWCursor.html">Prev</a>��</td> 94 <td width="20%" align="center"> 95 <a accesskey="u" href="Cursors.html">Up</a> 96 </td> 97 <td width="40%" align="right">��<a accesskey="n" href="ReplacingEntryWCursor.html">Next</a></td> 98 </tr> 99 <tr> 100 <td width="40%" align="left" valign="top">Putting Records Using Cursors��</td> 101 <td width="20%" align="center"> 102 <a accesskey="h" href="index.html">Home</a> 103 </td> 104 <td width="40%" align="right" valign="top">��Replacing Records Using Cursors</td> 105 </tr> 106 </table> 107 </div> 108 </body> 109</html> 110