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>Putting 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="Positioning.html" title="Getting Records Using the Cursor" /> 12 <link rel="next" href="DeleteEntryWCursor.html" title="Deleting Records Using Cursors" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Putting Records Using Cursors</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="Positioning.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="DeleteEntryWCursor.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="PutEntryWCursor"></a>Putting Records Using Cursors</h2> 33 </div> 34 </div> 35 <div></div> 36 </div> 37 <p> 38 You can use cursors to put records into the database. DB's behavior 39 when putting records into the database differs depending on the flags 40 that you use when writing the record, on the access method that you are 41 using, and on whether your database supports sorted duplicates. 42 </p> 43 <p> 44 Note that when putting records to the database using a cursor, the 45 cursor is positioned at the record you inserted. 46 </p> 47 <p> 48 You use 49 <tt class="methodname">DBC->put()</tt> 50 51 52 to put (write) records to the database. You can use the following flags 53 with this method: 54 </p> 55 <div class="itemizedlist"> 56 <ul type="disc"> 57 <li> 58 <p> 59 60 <tt class="literal">DB_NODUPDATA</tt> 61 </p> 62 <p> 63 If the provided key already exists 64 in the database, then this method returns 65 <tt class="literal">DB_KEYEXIST</tt>. 66 </p> 67 <p> 68 If the key does not exist, then the order that the record is put into the database 69 is determined by the 70 71 <span> 72 insertion order in use by the database. If a comparison 73 function has been provided to the database, the record is 74 inserted in its sorted location. Otherwise (assuming BTree), 75 lexicographical sorting is used, with 76 shorter items collating before longer items. 77 </span> 78 </p> 79 <p> 80 This flag can only be used for the BTree and Hash access methods, 81 and only if the database has been configured to support sorted 82 duplicate data items (<tt class="literal">DB_DUPSORT</tt> was specified at 83 database creation time). 84 </p> 85 <p> 86 This flag cannot be used with the Queue or Recno access methods. 87 </p> 88 <p> 89 For more information on duplicate records, see 90 <a href="btree.html#duplicateRecords">Allowing Duplicate Records</a>. 91 </p> 92 </li> 93 <li> 94 <p> 95 96 <tt class="literal">DB_KEYFIRST</tt> 97 </p> 98 <p> 99 For databases that do not support duplicates, this method behaves 100 101 <span> 102 exactly the same as if a default insertion was performed. 103 </span> 104 If the database supports duplicate records, 105 106 <span> 107 and a duplicate sort function has been specified, the 108 inserted data item is added in its sorted location. If 109 the key already exists in the database and no duplicate 110 sort function has been specified, the inserted data item 111 is added as the first of the data items for that key. 112 </span> 113 </p> 114 </li> 115 <li> 116 <p> 117 118 <tt class="literal">DB_KEYLAST</tt> 119 </p> 120 <p> 121 Behaves exactly as if 122 <tt class="literal">DB_KEYFIRST</tt> 123 124 was used, except that if the key already exists in the database and no 125 duplicate sort function has been specified, the 126 inserted data item is added as the last of the data 127 items for that key. 128 </p> 129 </li> 130 </ul> 131 </div> 132 <p>For example:</p> 133 <a id="c_cursor7"></a> 134 <pre class="programlisting">#include <db.h> 135#include <string.h> 136 137... 138 139DB *dbp; 140DBC *cursorp; 141DBT data1, data2, data3; 142DBT key1, key2; 143char *key1str = "My first string"; 144char *data1str = "My first data"; 145char *key2str = "A second string"; 146char *data2str = "My second data"; 147char *data3str = "My third data"; 148int ret; 149 150/* Set up our DBTs */ 151key1.data = key1str; 152key1.size = strlen(key1str) + 1; 153data1.data = data1str; 154data1.size = strlen(data1str) + 1; 155 156key2.data = key2str; 157key2.size = strlen(key2str) + 1; 158data2.data = data2str; 159data2.size = strlen(data2str) + 1; 160data3.data = data3str; 161data3.size = strlen(data3str) + 1; 162 163/* Database open omitted */ 164 165/* Get the cursor */ 166dbp->cursor(dbp, NULL, &cursorp, 0); 167 168/* 169 * Assuming an empty database, this first put places 170 * "My first string"/"My first data" in the first 171 * position in the database 172 */ 173ret = cursorp->put(cursorp, &key1, 174 &data1, DB_KEYFIRST); 175 176/* 177 * This put places "A second string"/"My second data" in the 178 * the database according to its key sorts against the key 179 * used for the currently existing database record. Most likely 180 * this record would appear first in the database. 181 */ 182ret = cursorp->put(cursorp, &key2, 183 &data2, DB_KEYFIRST); /* Added according to sort order */ 184 185/* 186 * If duplicates are not allowed, the currently existing record that 187 * uses "key2" is overwritten with the data provided on this put. 188 * That is, the record "A second string"/"My second data" becomes 189 * "A second string"/"My third data" 190 * 191 * If duplicates are allowed, then "My third data" is placed in the 192 * duplicates list according to how it sorts against "My second data". 193 */ 194ret = cursorp->put(cursorp, &key2, 195 &data3, DB_KEYFIRST); /* If duplicates are not allowed, record 196 * is overwritten with new data. Otherwise, 197 * the record is added to the beginning of 198 * the duplicates list. 199 */ </pre> 200 </div> 201 <div class="navfooter"> 202 <hr /> 203 <table width="100%" summary="Navigation footer"> 204 <tr> 205 <td width="40%" align="left"><a accesskey="p" href="Positioning.html">Prev</a> </td> 206 <td width="20%" align="center"> 207 <a accesskey="u" href="Cursors.html">Up</a> 208 </td> 209 <td width="40%" align="right"> <a accesskey="n" href="DeleteEntryWCursor.html">Next</a></td> 210 </tr> 211 <tr> 212 <td width="40%" align="left" valign="top">Getting Records Using the Cursor </td> 213 <td width="20%" align="center"> 214 <a accesskey="h" href="index.html">Home</a> 215 </td> 216 <td width="40%" align="right" valign="top"> Deleting Records Using Cursors</td> 217 </tr> 218 </table> 219 </div> 220 </body> 221</html> 222