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