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
|
<!--$Id: curput.html,v 1.1.1.1 2003/11/20 22:14:08 toshok Exp $-->
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
<!--All rights reserved.-->
<!--See the file LICENSE for redistribution information.-->
<html>
<head>
<title>Berkeley DB Reference Guide: Storing records with a cursor</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
</head>
<body bgcolor=white>
<a name="2"><!--meow--></a><a name="3"><!--meow--></a>
<table width="100%"><tr valign=top>
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></h3></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>
</td></tr></table>
<p>
<h1 align=center>Storing records with a cursor</h1>
<p>The <a href="../../api_c/dbc_put.html">DBcursor->c_put</a> method is the standard interface for storing records into
the database with a cursor. In general, <a href="../../api_c/dbc_put.html">DBcursor->c_put</a> takes a key and
inserts the associated data into the database, at a location controlled
by a specified flag.
<p>There are several flags that you can set to customize storage:
<p><dl compact>
<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
refers.
<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
refers.
<p><dt><a href="../../api_c/dbc_get.html#DB_CURRENT">DB_CURRENT</a><dd>Replace the data part of the record to which the cursor refers.
<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
supplied key.
<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
key.
</dl>
<p>In all cases, the cursor is repositioned by a <a href="../../api_c/dbc_put.html">DBcursor->c_put</a> operation
to point to the newly inserted key/data pair in the database.
<p>The following is a code example showing a cursor storing two data items
in a database that supports duplicate data items:
<p><blockquote><pre>int
store(dbp)
DB *dbp;
{
DBC *dbcp;
DBT key, data;
int ret;
<p>
/*
* The DB handle for a Btree database supporting duplicate data
* items is the argument; acquire a cursor for the database.
*/
if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
dbp->err(dbp, ret, "DB->cursor");
goto err;
}
<p>
/* Initialize the key. */
memset(&key, 0, sizeof(key));
key.data = "new key";
key.size = strlen(key.data) + 1;
<p>
/* Initialize the data to be the first of two duplicate records. */
memset(&data, 0, sizeof(data));
data.data = "new key's data: entry #1";
data.size = strlen(data.data) + 1;
<p>
/* Store the first of the two duplicate records. */
if ((ret = dbcp->c_put(dbcp, &key, &data, DB_KEYFIRST)) != 0)
dbp->err(dbp, ret, "DB->cursor");
<p>
/* Initialize the data to be the second of two duplicate records. */
data.data = "new key's data: entry #2";
data.size = strlen(data.data) + 1;
<p>
/*
* Store the second of the two duplicate records. No duplicate
* record sort function has been specified, so we explicitly
* store the record as the last of the duplicate set.
*/
if ((ret = dbcp->c_put(dbcp, &key, &data, DB_KEYLAST)) != 0)
dbp->err(dbp, ret, "DB->cursor");
<p>
err: if ((ret = dbcp->c_close(dbcp)) != 0)
dbp->err(dbp, ret, "DBcursor->close");
<p>
return (0);
}</pre></blockquote>
<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>
</td></tr></table>
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>
</body>
</html>
|