1
<!--$Id: curput.so,v 10.15 2001/05/19 14:51:30 bostic Exp $-->
2
<!--Copyright 1997-2001 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
6
<title>Berkeley DB Reference Guide: Storing records with a cursor</title>
7
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
8
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
11
<a name="2"><!--meow--></a><a name="3"><!--meow--></a>
12
<table width="100%"><tr valign=top>
13
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></h3></td>
14
<td align=right><a href="../../ref/am/curget.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am/curdel.html"><img src="../../images/next.gif" alt="Next"></a>
17
<h1 align=center>Storing records with a cursor</h1>
18
<p>The <a href="../../api_c/dbc_put.html">DBcursor->c_put</a> function is the standard interface for storing records into
19
the database with a cursor. In general, <a href="../../api_c/dbc_put.html">DBcursor->c_put</a> takes a key and
20
inserts the associated data into the database, at a location controlled
22
<p>There are several flags that you can set to customize storage:
24
<p><dt><a href="../../api_c/dbc_put.html#DB_AFTER">DB_AFTER</a><dd>Create a new record, immediately after the record to which the cursor
26
<p><dt><a href="../../api_c/dbc_put.html#DB_BEFORE">DB_BEFORE</a><dd>Create a new record, immediately before the record to which the cursor
28
<p><dt><a href="../../api_c/dbc_put.html#DB_CURRENT_PUT">DB_CURRENT</a><dd>Replace the data part of the record to which the cursor refers.
29
<p><dt><a href="../../api_c/dbc_put.html#DB_KEYFIRST">DB_KEYFIRST</a><dd>Create a new record as the first of the duplicate records for the
31
<p><dt><a href="../../api_c/dbc_put.html#DB_KEYLAST">DB_KEYLAST</a><dd>Create a new record, as the last of the duplicate records for the supplied
34
<p>In all cases, the cursor is repositioned by a <a href="../../api_c/dbc_put.html">DBcursor->c_put</a> operation
35
to point to the newly inserted key/data pair in the database.
36
<p>The following is a code example showing a cursor storing two data items
37
in a database that supports duplicate data items:
38
<p><blockquote><pre>int
47
* The DB handle for a Btree database supporting duplicate data
48
* items is the argument; acquire a cursor for the database.
50
if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
51
dbp->err(dbp, ret, "DB->cursor");
55
/* Initialize the key. */
56
memset(&key, 0, sizeof(key));
58
key.size = strlen(key.data) + 1;
60
/* Initialize the data to be the first of two duplicate records. */
61
memset(&data, 0, sizeof(data));
62
data.data = "new key's data: entry #1";
63
data.size = strlen(data.data) + 1;
65
/* Store the first of the two duplicate records. */
66
if ((ret = dbcp->c_put(dbcp, &key, &data, DB_KEYFIRST)) != 0)
67
dbp->err(dbp, ret, "DB->cursor");
69
/* Initialize the data to be the second of two duplicate records. */
70
data.data = "new key's data: entry #2";
71
data.size = strlen(data.data) + 1;
74
* Store the second of the two duplicate records. No duplicate
75
* record sort function has been specified, so we explicitly
76
* store the record as the last of the duplicate set.
78
if ((ret = dbcp->c_put(dbcp, &key, &data, DB_KEYLAST)) != 0)
79
dbp->err(dbp, ret, "DB->cursor");
81
err: if ((ret = dbcp->c_close(dbcp)) != 0)
82
dbp->err(dbp, ret, "DBcursor->close");
86
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/am/curget.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am/curdel.html"><img src="../../images/next.gif" alt="Next"></a>
88
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>