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