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 <tt class="methodname">DBC->put()</tt> 45 46 with the <tt class="literal">DB_CURRENT</tt> flag. 47 </span> 48 49 </p> 50 <a id="c_cursor9"></a> 51 <pre class="programlisting">#include <db.h> 52#include <string.h> 53 54... 55 56DB *dbp; 57DBC *cursorp; 58DBT key, data; 59char *key1str = "My first string"; 60char *replacement_data = "replace me"; 61int ret; 62 63/* Initialize our DBTs. */ 64memset(&key, 0, sizeof(DBT)); 65memset(&data, 0, sizeof(DBT)); 66 67/* Set up our DBTs */ 68key.data = key1str; 69key.size = strlen(key1str) + 1; 70 71/* Database open omitted */ 72 73/* Get the cursor */ 74dbp->cursor(dbp, NULL, &cursorp, 0); 75 76/* Position the cursor */ 77ret = cursorp->get(cursorp, &key, &data, DB_SET); 78if (ret == 0) { 79 data.data = replacement_data; 80 data.size = strlen(replacement_data) + 1; 81 cursorp->put(cursorp, &key, &data, DB_CURRENT); 82} 83 84/* Cursors must be closed */ 85if (cursorp != NULL) 86 cursorp->close(cursorp); 87 88if (dbp != NULL) 89 dbp->close(dbp, 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