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