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��9.��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��9.��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 44 </p> 45 <p>For example:</p> 46 <a id="java_cursor8"></a> 47 <pre class="programlisting">package db.GettingStarted; 48 49import com.sleepycat.db.Cursor; 50import com.sleepycat.db.Database; 51import com.sleepycat.db.DatabaseEntry; 52import com.sleepycat.db.LockMode; 53import com.sleepycat.db.OperationStatus; 54 55... 56 57Cursor cursor = null; 58Database myDatabase = null; 59try { 60 ... 61 // Database open omitted for brevity 62 ... 63 // Create DatabaseEntry objects 64 // searchKey is some String. 65 DatabaseEntry theKey = new DatabaseEntry(searchKey.getBytes("UTF-8")); 66 DatabaseEntry theData = new DatabaseEntry(); 67 68 // Open a cursor using a database handle 69 cursor = myDatabase.openCursor(null, null); 70 71 // Position the cursor. Ignoring the return value for clarity 72 OperationStatus retVal = cursor.getSearchKey(theKey, theData, 73 LockMode.DEFAULT); 74 75 // Count the number of records using the given key. If there is only 76 // one, delete that record. 77 if (cursor.count() == 1) { 78 System.out.println("Deleting " + 79 new String(theKey.getData(), "UTF-8") + 80 "|" + 81 new String(theData.getData(), "UTF-8")); 82 cursor.delete(); 83 } 84} catch (Exception e) { 85 // Exception handling goes here 86} finally { 87 // Make sure to close the cursor 88 cursor.close(); 89}</pre> 90 </div> 91 <div class="navfooter"> 92 <hr /> 93 <table width="100%" summary="Navigation footer"> 94 <tr> 95 <td width="40%" align="left"><a accesskey="p" href="PutEntryWCursor.html">Prev</a>��</td> 96 <td width="20%" align="center"> 97 <a accesskey="u" href="Cursors.html">Up</a> 98 </td> 99 <td width="40%" align="right">��<a accesskey="n" href="ReplacingEntryWCursor.html">Next</a></td> 100 </tr> 101 <tr> 102 <td width="40%" align="left" valign="top">Putting Records Using Cursors��</td> 103 <td width="20%" align="center"> 104 <a accesskey="h" href="index.html">Home</a> 105 </td> 106 <td width="40%" align="right" valign="top">��Replacing Records Using Cursors</td> 107 </tr> 108 </table> 109 </div> 110 </body> 111</html> 112