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