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