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 <span><tt class="methodname">DBC->del()</tt>.</span> 46 47 48 </span> 49 </p> 50 <p>For example:</p> 51 <a id="c_cursor8"></a> 52 <pre class="programlisting">#include <db.h> 53#include <string.h> 54 55... 56 57DB *dbp; 58DBC *cursorp; 59DBT key, data; 60char *key1str = "My first string"; 61int ret; 62 63/* Database open omitted */ 64 65/* Initialize our DBTs. */ 66memset(&key, 0, sizeof(DBT)); 67memset(&data, 0, sizeof(DBT)); 68 69/* Set up our DBTs */ 70key.data = key1str; 71key.size = strlen(key1str) + 1; 72 73/* Get the cursor */ 74dbp->cursor(dbp, NULL, &cursorp, 0); 75 76/* Iterate over the database, deleting each record in turn. */ 77while ((ret = cursorp->get(cursorp, &key, 78 &data, DB_SET)) == 0) { 79 cursorp->del(cursorp, 0); 80} 81 82/* Cursors must be closed */ 83if (cursorp != NULL) 84 cursorp->close(cursorp); 85 86if (dbp != NULL) 87 dbp->close(dbp, 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