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">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Replacing 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="DeleteEntryWCursor.html" title="Deleting Records Using Cursors" />
12 <link rel="next" href="cursorJavaUsage.html" title="Cursor Example" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">Replacing Records Using Cursors</th>
24 <td width="20%" align="left"><a accesskey="p" href="DeleteEntryWCursor.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 9. Using Cursors</th>
26 <td width="20%" align="right"> <a accesskey="n" href="cursorJavaUsage.html">Next</a></td>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
35 <h2 class="title" style="clear: both"><a id="ReplacingEntryWCursor"></a>Replacing Records Using Cursors</h2>
40 You replace the data for a database record by using
44 <code class="methodname">Cursor.putCurrent()</code>.
50 <a id="java_cursor9"></a>
51 <pre class="programlisting">import com.sleepycat.db.Cursor;
52 import com.sleepycat.db.Database;
53 import com.sleepycat.db.DatabaseEntry;
54 import com.sleepycat.db.LockMode;
55 import com.sleepycat.db.OperationStatus;
59 Database myDatabase = null;
62 // Database open omitted for brevity
64 // Create DatabaseEntry objects
65 // searchKey is some String.
66 DatabaseEntry theKey = new DatabaseEntry(searchKey.getBytes("UTF-8"));
67 DatabaseEntry theData = new DatabaseEntry();
69 // Open a cursor using a database handle
70 cursor = myDatabase.openCursor(null, null);
72 // Position the cursor. Ignoring the return value for clarity
73 OperationStatus retVal = cursor.getSearchKey(theKey, theData,
77 String replaceStr = "My replacement string";
78 DatabaseEntry replacementData =
79 new DatabaseEntry(replaceStr.getBytes("UTF-8"));
80 cursor.putCurrent(replacementData);
81 } catch (Exception e) {
82 // Exception handling goes here
84 // Make sure to close the cursor
88 Note that you cannot change a record's key using this method; the key
89 parameter is always ignored when you replace a record.
92 When replacing the data portion of a record, if you are replacing a
93 record that is a member of a sorted duplicates set, then the replacement
94 will be successful only if the new record sorts identically to the old
95 record. This means that if you are replacing a record that is a member
96 of a sorted duplicates set, and if you are using the default
97 lexicographic sort, then the replacement will fail due to violating the
98 sort order. However, if you
99 provide a custom sort routine that, for example, sorts based on just a
100 few bytes out of the data item, then potentially you can perform
101 a direct replacement and still not violate the restrictions described
105 <span>Under these circumstances, if</span>
107 you want to replace the data contained by a duplicate record,
109 and you are not using a custom sort routine, then
111 delete the record and create a new record with the desired key and data.
114 <div class="navfooter">
116 <table width="100%" summary="Navigation footer">
118 <td width="40%" align="left"><a accesskey="p" href="DeleteEntryWCursor.html">Prev</a> </td>
119 <td width="20%" align="center">
120 <a accesskey="u" href="Cursors.html">Up</a>
122 <td width="40%" align="right"> <a accesskey="n" href="cursorJavaUsage.html">Next</a></td>
125 <td width="40%" align="left" valign="top">Deleting Records Using Cursors </td>
126 <td width="20%" align="center">
127 <a accesskey="h" href="index.html">Home</a>
129 <td width="40%" align="right" valign="top"> Cursor Example</td>