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>Replacing 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="DeleteEntryWCursor.html" title="Deleting Records Using Cursors" />
12    <link rel="next" href="CoreCursorUsage.html" title="Cursor Example" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Replacing Records Using Cursors</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="DeleteEntryWCursor.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="CoreCursorUsage.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="ReplacingEntryWCursor"></a>Replacing Records Using Cursors</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <p>
38        You replace the data for a database record by using
39        
40
41        
42
43        <span>
44            
45            <tt class="methodname">Dbc::put()</tt>
46            with the <tt class="literal">DB_CURRENT</tt> flag.
47        </span>
48            
49    </p>
50      <a id="cxx_cursor9"></a>
51      <pre class="programlisting">#include &lt;db_cxx.h&gt;
52#include &lt;string.h&gt;
53
54...
55
56Db my_database(NULL, 0);
57Dbc *cursorp;
58
59int ret;
60char *key1str = "My first string";
61char *replacement_data = "replace me";
62
63try {
64    // Database open omitted
65
66    // Get the cursor
67    my_database.cursor(NULL, &amp;cursorp, 0);
68
69    // Set up our DBTs 
70    Dbt key(key1str, strlen(key1str) + 1);
71    Dbt data;
72
73    // Position the cursor */
74    ret = cursorp-&gt;get(&amp;key, &amp;data, DB_SET);
75    if (ret == 0) {
76        data.set_data(replacement_data);
77        data.set_size(strlen(replacement_data) + 1);
78        cursorp-&gt;put(&amp;key, &amp;data, DB_CURRENT);
79    }
80} catch(DbException &amp;e) {
81        my_database.err(e.get_errno(), "Error!");
82} catch(std::exception &amp;e) {
83        my_database.errx("Error! %s", e.what());
84}
85
86// Cursors must be closed
87if (cursorp != NULL)
88    cursorp-&gt;close(); 
89
90my_database.close(0);</pre>
91      <p>
92        Note that you cannot change a record's key using this method; the key
93        parameter is always ignored when you replace a record.
94    </p>
95      <p>
96        When replacing the data portion of a record, if you are replacing a
97        record that is a member of a sorted duplicates set, then the replacement
98        will be successful only if the new record sorts identically to the old
99        record. This means that if you are replacing a record that is a member
100        of a sorted duplicates set, and if you are using the default
101        lexicographic sort, then the replacement will fail due to violating the
102        sort order. However, if you
103        provide a custom sort routine that, for example, sorts based on just a
104        few bytes out of the data item, then potentially you can perform
105        a direct replacement and still not violate the restrictions described
106        here.
107    </p>
108      <p>
109            <span>Under these circumstances, if</span>
110            
111        you want to replace the data contained by a duplicate record, 
112            <span>
113                and you are not using a custom sort routine, then
114            </span>
115        delete the record and create a new record with the desired key and data.
116    </p>
117    </div>
118    <div class="navfooter">
119      <hr />
120      <table width="100%" summary="Navigation footer">
121        <tr>
122          <td width="40%" align="left"><a accesskey="p" href="DeleteEntryWCursor.html">Prev</a> </td>
123          <td width="20%" align="center">
124            <a accesskey="u" href="Cursors.html">Up</a>
125          </td>
126          <td width="40%" align="right"> <a accesskey="n" href="CoreCursorUsage.html">Next</a></td>
127        </tr>
128        <tr>
129          <td width="40%" align="left" valign="top">Deleting Records Using Cursors </td>
130          <td width="20%" align="center">
131            <a accesskey="h" href="index.html">Home</a>
132          </td>
133          <td width="40%" align="right" valign="top"> Cursor Example</td>
134        </tr>
135      </table>
136    </div>
137  </body>
138</html>
139