Sophie

Sophie

distrib > Mandriva > 2007.0 > x86_64 > media > main-release > by-pkgid > a4c98df40e78f6c892308fd6841f950a > files > 758

lib64db4.2-devel-4.2.52-11mdv2007.0.x86_64.rpm

<!--$Id: curput.so,v 10.18 2003/10/18 19:15:52 bostic Exp $-->
<!--Copyright 1997-2003 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="../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>
<h3 align=center>Storing records with a cursor</h3>
<p>The <a href="../../api_c/dbc_put.html">DBcursor-&gt;c_put</a> method stores records into the database using a cursor.  In
general, <a href="../../api_c/dbc_put.html">DBcursor-&gt;c_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>
<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-&gt;c_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><a href="../../sleepycat/legal.html">Copyright (c) 1996-2003</a> <a href="http://www.sleepycat.com">Sleepycat Software, Inc.</a> - All rights reserved.</font>
</body>
</html>