2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB: Db::put</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<a name="2"><!--meow--></a>
13
<table width="100%"><tr valign=top>
18
<a href="../api_cxx/c_index.html"><img src="../images/api.gif" alt="API"></a><a href="../reftoc.html"><img src="../images/ref.gif" alt="Ref"></a>
23
#include <db_cxx.h>
26
Db::put(DbTxn *txnid, Dbt *key, Dbt *data, u_int32_t flags);
29
<p>The Db::put method stores key/data pairs in the database. The default
30
behavior of the Db::put function is to enter the new key/data
31
pair, replacing any previously existing key if duplicates are disallowed,
32
or adding a duplicate data item if duplicates are allowed. If the database
33
supports duplicates, the Db::put method adds the new data value at the
34
end of the duplicate set. If the database supports sorted duplicates,
35
the new data value is inserted at the correct sorted location.
36
<p>If the operation is to be transaction-protected (other than by specifying
37
the DB_AUTO_COMMIT flag), the <b>txnid</b> parameter is a
38
transaction handle returned from <a href="../api_cxx/txn_begin.html">DbEnv::txn_begin</a>; otherwise, NULL.
39
<p>The <b>flags</b> value must be set to 0 or
40
one of the following values:
42
<p><dt><a name="DB_APPEND">DB_APPEND</a><dd>Append the key/data pair to the end of the database. For the
43
DB_APPEND flag to be specified, the underlying database must be
44
a Queue or Recno database. The record number allocated to the record is
45
returned in the specified <b>key</b>.
46
<p>There is a minor behavioral difference between the Recno and Queue access
47
methods for the DB_APPEND flag. If a transaction enclosing a
48
Db::put operation with the DB_APPEND flag aborts, the
49
record number may be decremented (and later reallocated by a subsequent
50
DB_APPEND operation) by the Recno access method, but will not be
51
decremented or reallocated by the Queue access method.
52
<p><dt><a name="DB_NODUPDATA">DB_NODUPDATA</a><dd>In the case of the Btree and Hash access methods, enter the new key/data
53
pair only if it does not already appear in the database. If the
54
key/data pair already appears in the database, <a href="../api_cxx/dbc_put.html#DB_KEYEXIST">DB_KEYEXIST</a> is
55
returned. The DB_NODUPDATA flag may only be specified if the
56
underlying database has been configured to support sorted duplicates.
57
<p>The DB_NODUPDATA flag may not be specified to the Queue or Recno
59
<p><dt><a name="DB_NOOVERWRITE">DB_NOOVERWRITE</a><dd>Enter the new key/data pair only if the key does not already appear in
60
the database. If the key already appears in the database,
61
<a href="../api_cxx/dbc_put.html#DB_KEYEXIST">DB_KEYEXIST</a> is returned. Even if the database allows duplicates,
62
a call to Db::put with the DB_NOOVERWRITE flag set will
63
fail if the key already exists in the database.
65
<p>In addition, the following flag may be set by
66
bitwise inclusively <b>OR</b>'ing it into the <b>flags</b> parameter:
68
<p><dt><a name="DB_AUTO_COMMIT">DB_AUTO_COMMIT</a><dd>Enclose the Db::put call within a transaction. If the call succeeds,
69
changes made by the operation will be recoverable. If the call fails,
70
the operation will have made no changes.
73
Otherwise, the Db::put method either returns a non-zero error value or throws an exception that
74
encapsulates a non-zero error value on failure, and returns 0 on success.
76
<p>The Db::put method may fail and throw an exception or return a non-zero error for the following conditions:
78
<p><dt>EACCES<dd>An attempt was made to modify a read-only database.
81
<p><dt>EINVAL<dd>An invalid flag value or parameter was specified.
82
<p>A record number of 0 was specified.
83
<p>An attempt was made to add a record to a fixed-length database that was too
85
<p>An attempt was made to do a partial put.
86
<p>An attempt was made to add a record to a secondary index.
89
<p><dt>ENOSPC<dd>A btree exceeded the maximum btree depth (255).
91
<p>If the operation was selected to resolve a deadlock, the
92
Db::put method will fail and
93
and either return <a href="../ref/program/errorret.html#DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a> or
94
throw a <a href="../api_cxx/deadlock_class.html">DbDeadlockException</a> exception.
95
<p>The Db::put method may fail and throw an exception or return a non-zero error for errors specified for other Berkeley DB and C library or system methods.
96
If a catastrophic error has occurred, the Db::put method may fail and
97
either return <a href="../ref/program/errorret.html#DB_RUNRECOVERY">DB_RUNRECOVERY</a> or throw a
98
<a href="../api_cxx/runrec_class.html">DbRunRecoveryException</a>,
99
in which case all subsequent Berkeley DB calls will fail in the same way.
101
<a href="../api_cxx/db_class.html">Db</a>
103
<a href="../api_cxx/db_list.html">Databases and Related Methods</a>
105
<table width="100%"><tr><td><br></td><td align=right>
106
<a href="../api_cxx/c_index.html"><img src="../images/api.gif" alt="API"></a><a href="../reftoc.html"><img src="../images/ref.gif" alt="Ref"></a>
108
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>