1<!--$Id: curput.so,v 10.18 2003/10/18 19:15:52 bostic Exp $--> 2<!--Copyright (c) 1997,2008 Oracle. All rights reserved.--> 3<!--See the file LICENSE for redistribution information.--> 4<html> 5<head> 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,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++"> 9</head> 10<body bgcolor=white> 11<a name="2"><!--meow--></a><a name="3"><!--meow--></a> 12<table width="100%"><tr valign=top> 13<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></b></td> 14<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> 15</td></tr></table> 16<p align=center><b>Storing records with a cursor</b></p> 17<p>The <a href="/api_c/dbc_put.html">DBcursor->put</a> method stores records into the database using a cursor. In 18general, <a href="/api_c/dbc_put.html">DBcursor->put</a> takes a key and inserts the associated data 19into the database, at a location controlled by a specified flag.</p> 20<p>There are several flags that you can set to customize storage:</p> 21<br> 22<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 23refers.</ul> 24<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 25refers.</ul> 26<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> 27<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 28supplied key.</ul> 29<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 30key.</ul> 31<br> 32<p>In all cases, the cursor is repositioned by a <a href="/api_c/dbc_put.html">DBcursor->put</a> operation 33to point to the newly inserted key/data pair in the database.</p> 34<p>The following is a code example showing a cursor storing two data items 35in a database that supports duplicate data items:</p> 36<blockquote><pre>int 37store(dbp) 38 DB *dbp; 39{ 40 DBC *dbcp; 41 DBT key, data; 42 int ret; 43<p> 44 /* 45 * The DB handle for a Btree database supporting duplicate data 46 * items is the argument; acquire a cursor for the database. 47 */ 48 if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) { 49 dbp->err(dbp, ret, "DB->cursor"); 50 goto err; 51 } 52<p> 53 /* Initialize the key. */ 54 memset(&key, 0, sizeof(key)); 55 key.data = "new key"; 56 key.size = strlen(key.data) + 1; 57<p> 58 /* Initialize the data to be the first of two duplicate records. */ 59 memset(&data, 0, sizeof(data)); 60 data.data = "new key's data: entry #1"; 61 data.size = strlen(data.data) + 1; 62<p> 63 /* Store the first of the two duplicate records. */ 64 if ((ret = dbcp->c_put(dbcp, &key, &data, DB_KEYFIRST)) != 0) 65 dbp->err(dbp, ret, "DB->cursor"); 66<p> 67 /* Initialize the data to be the second of two duplicate records. */ 68 data.data = "new key's data: entry #2"; 69 data.size = strlen(data.data) + 1; 70<p> 71 /* 72 * Store the second of the two duplicate records. No duplicate 73 * record sort function has been specified, so we explicitly 74 * store the record as the last of the duplicate set. 75 */ 76 if ((ret = dbcp->c_put(dbcp, &key, &data, DB_KEYLAST)) != 0) 77 dbp->err(dbp, ret, "DB->cursor"); 78<p> 79err: if ((ret = dbcp->c_close(dbcp)) != 0) 80 dbp->err(dbp, ret, "DBcursor->close"); 81<p> 82 return (0); 83}</pre></blockquote> 84<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> 85</td></tr></table> 86<p><font size=1>Copyright (c) 1996,2008 Oracle. All rights reserved.</font> 87</body> 88</html> 89