curput.html   [plain text]


<!--$Id: curput.so,v 10.18 2003/10/18 19:15:52 bostic Exp $-->
<!--Copyright (c) 1997,2008 Oracle.  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,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><b><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></b></td>
<td align=right><a href="../am/curget.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am/curdel.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p align=center><b>Storing records with a cursor</b></p>
<p>The <a href="../../api_c/dbc_put.html">DBcursor-&gt;put</a> method stores records into the database using a cursor.  In
general, <a href="../../api_c/dbc_put.html">DBcursor-&gt;put</a> takes a key and inserts the associated data
into the database, at a location controlled by a specified flag.</p>
<p>There are several flags that you can set to customize storage:</p>
<br>
<b><a href="../../api_c/dbc_put.html#DB_AFTER">DB_AFTER</a></b><ul compact><li>Create a new record, immediately after the record to which the cursor
refers.</ul>
<b><a href="../../api_c/dbc_put.html#DB_BEFORE">DB_BEFORE</a></b><ul compact><li>Create a new record, immediately before the record to which the cursor
refers.</ul>
<b><a href="../../api_c/dbc_get.html#DB_CURRENT">DB_CURRENT</a></b><ul compact><li>Replace the data part of the record to which the cursor refers.</ul>
<b><a href="../../api_c/dbc_put.html#DB_KEYFIRST">DB_KEYFIRST</a></b><ul compact><li>Create a new record as the first of the duplicate records for the
supplied key.</ul>
<b><a href="../../api_c/dbc_put.html#DB_KEYLAST">DB_KEYLAST</a></b><ul compact><li>Create a new record, as the last of the duplicate records for the supplied
key.</ul>
<br>
<p>In all cases, the cursor is repositioned by a <a href="../../api_c/dbc_put.html">DBcursor-&gt;put</a> operation
to point to the newly inserted key/data pair in the database.</p>
<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-&gt;cursor(dbp, NULL, &dbcp, 0)) != 0) {
		dbp-&gt;err(dbp, ret, "DB-&gt;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-&gt;c_put(dbcp, &key, &data, DB_KEYFIRST)) != 0)
		dbp-&gt;err(dbp, ret, "DB-&gt;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-&gt;c_put(dbcp, &key, &data, DB_KEYLAST)) != 0)
		dbp-&gt;err(dbp, ret, "DB-&gt;cursor");
<p>
err:	if ((ret = dbcp-&gt;c_close(dbcp)) != 0)
		dbp-&gt;err(dbp, ret, "DBcursor-&gt;close");
<p>
	return (0);
}</pre></blockquote>
<table width="100%"><tr><td><br></td><td align=right><a href="../am/curget.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am/curdel.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
</body>
</html>