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 <span><code class="methodname">DBC->del()</code>.</span> 45 46 47 </span> 48 </p> 49 <p>For example:</p> 50 <a id="c_cursor8"></a> 51 <pre class="programlisting">#include <db.h> 52#include <string.h> 53 54... 55 56DB *dbp; 57DBC *cursorp; 58DBT key, data; 59char *key1str = "My first string"; 60int ret; 61 62/* Database open omitted */ 63 64/* Initialize our DBTs. */ 65memset(&key, 0, sizeof(DBT)); 66memset(&data, 0, sizeof(DBT)); 67 68/* Set up our DBTs */ 69key.data = key1str; 70key.size = strlen(key1str) + 1; 71 72/* Get the cursor */ 73dbp->cursor(dbp, NULL, &cursorp, 0); 74 75/* Iterate over the database, deleting each record in turn. */ 76while ((ret = cursorp->get(cursorp, &key, 77 &data, DB_SET)) == 0) { 78 cursorp->del(cursorp, 0); 79} 80 81/* Cursors must be closed */ 82if (cursorp != NULL) 83 cursorp->close(cursorp); 84 85if (dbp != NULL) 86 dbp->close(dbp, 0);</pre> 87 </div> 88 <div class="navfooter"> 89 <hr /> 90 <table width="100%" summary="Navigation footer"> 91 <tr> 92 <td width="40%" align="left"><a accesskey="p" href="PutEntryWCursor.html">Prev</a> </td> 93 <td width="20%" align="center"> 94 <a accesskey="u" href="Cursors.html">Up</a> 95 </td> 96 <td width="40%" align="right"> <a accesskey="n" href="ReplacingEntryWCursor.html">Next</a></td> 97 </tr> 98 <tr> 99 <td width="40%" align="left" valign="top">Putting Records Using Cursors </td> 100 <td width="20%" align="center"> 101 <a accesskey="h" href="index.html">Home</a> 102 </td> 103 <td width="40%" align="right" valign="top"> Replacing Records Using Cursors</td> 104 </tr> 105 </table> 106 </div> 107 </body> 108</html> 109