1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>Db::put()</title> 7 <link rel="stylesheet" href="apiReference.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> 9 <link rel="start" href="index.html" title="Berkeley DB C++ API Reference" /> 10 <link rel="up" href="db.html" title="Chapter 2. The Db Handle" /> 11 <link rel="prev" href="dbopen.html" title="Db::open()" /> 12 <link rel="next" href="dbremove.html" title="Db::remove()" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Db::put()</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="dbopen.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 2. 23 The Db Handle 24 </th> 25 <td width="20%" align="right"> <a accesskey="n" href="dbremove.html">Next</a></td> 26 </tr> 27 </table> 28 <hr /> 29 </div> 30 <div class="sect1" lang="en" xml:lang="en"> 31 <div class="titlepage"> 32 <div> 33 <div> 34 <h2 class="title" style="clear: both"><a id="dbput"></a>Db::put()</h2> 35 </div> 36 </div> 37 </div> 38 <pre class="programlisting">#include <db_cxx.h> 39 40int 41Db::put(DbTxn *txnid, Dbt *key, Dbt *data, u_int32_t flags);</pre> 42 <p> 43 The <code class="methodname">Db::put()</code> method stores key/data pairs in the database. The 44 default behavior of the <code class="methodname">Db::put()</code> function is to enter the new 45 key/data pair, replacing any previously existing key if duplicates are 46 disallowed, or adding a duplicate data item if duplicates are allowed. 47 If the database supports duplicates, the <code class="methodname">Db::put()</code> method adds the 48 new data value at the end of the duplicate set. If the database 49 supports sorted duplicates, the new data value is inserted at the 50 correct sorted location. 51 </p> 52 <p> 53 Unless otherwise specified, the <code class="methodname">Db::put()</code> <span> 54 55 <span> 56 method either returns a non-zero error value or throws an 57 exception that encapsulates a non-zero error value on 58 failure, and returns 0 on success. 59 </span> 60 </span> 61 </p> 62 <div class="sect2" lang="en" xml:lang="en"> 63 <div class="titlepage"> 64 <div> 65 <div> 66 <h3 class="title"><a id="id1640671"></a>Parameters</h3> 67 </div> 68 </div> 69 </div> 70 <div class="sect3" lang="en" xml:lang="en"> 71 <div class="titlepage"> 72 <div> 73 <div> 74 <h4 class="title"><a id="id1640979"></a>flags</h4> 75 </div> 76 </div> 77 </div> 78 <p> 79 The <span class="bold"><strong>flags</strong></span> parameter must be set to 0 80 or one of the following values: 81 </p> 82 <div class="itemizedlist"> 83 <ul type="disc"> 84 <li> 85 <p><a id="dbput_DB_APPEND"></a> 86 <code class="literal">DB_APPEND</code> 87 </p> 88 <p> 89 Append the key/data pair to the end of the database. For the 90 DB_APPEND flag to be specified, the underlying database must be a 91 Queue or Recno database. The record number allocated to the record is 92 returned in the specified <span class="bold"><strong>key</strong></span>. 93 </p> 94 <p> 95 There is a minor behavioral difference between the Recno 96 and Queue access methods for the DB_APPEND flag. If a 97 transaction enclosing a 98 <code class="methodname">Db::put()</code> operation with the 99 DB_APPEND flag aborts, the record number may be 100 reallocated in a subsequent <code class="literal">DB_APPEND</code> 101 operation if you are using the Recno access method, but it 102 will not be reallocated if you are using the Queue access 103 method. 104 </p> 105 </li> 106 <li> 107 <p><a id="put_DB_NODUPDATA"></a> 108 <code class="literal">DB_NODUPDATA</code> 109 </p> 110 <p> 111 In the case of the Btree and Hash access methods, enter the new 112 key/data pair only if it does not already appear in the database. 113 </p> 114 <p> 115 The DB_NODUPDATA flag may only be specified if the underlying database 116 has been configured to support sorted duplicates. The DB_NODUPDATA 117 flag may not be specified to the Queue or Recno access methods. 118 </p> 119 <p> 120 The <code class="methodname">Db::put()</code> method will return 121 <a class="xref" href="dbcput.html#dbcput_DB_KEYEXIST" title="DB_KEYEXIST">DB_KEYEXIST</a> 122 if DB_NODUPDATA is set and the key/data pair already appears in the 123 database. 124 </p> 125 </li> 126 <li> 127 <p><a id="put_DB_NOOVERWRITE"></a> 128 <code class="literal">DB_NOOVERWRITE</code> 129 </p> 130 <p> 131 Enter the new key/data pair only if the key does not already appear in 132 the database. The <code class="methodname">Db::put()</code> method call with the DB_NOOVERWRITE flag 133 set will fail if the key already exists in the database, even if the 134 database supports duplicates. 135 </p> 136 <p> 137 The <code class="methodname">Db::put()</code> method will return 138 <a class="xref" href="dbcput.html#dbcput_DB_KEYEXIST" title="DB_KEYEXIST">DB_KEYEXIST</a> 139 if DB_NOOVERWRITE is set and the key already appears in the database. 140 </p> 141 <p> 142 143 This enforcement of uniqueness of keys applies only to the primary key. The 144 behavior of insertions into secondary databases is not affected by the 145 DB_NOOVERWRITE flag. In particular, the insertion of a record that would result in 146 the creation of a duplicate key in a secondary database that allows duplicates would 147 not be prevented by the use of this flag. 148 </p> 149 </li> 150 <li> 151 <p><a id="put_DB_MULTIPLE"></a> 152 <code class="literal">DB_MULTIPLE</code> 153 </p> 154 <p> 155 Put multiple data items using keys from the buffer to which the <span class="bold"><strong>key</strong></span> parameter refers and data values from the buffer to 156 which the <span class="bold"><strong>data</strong></span> parameter refers. 157 </p> 158 <p> 159 To put records in bulk with the btree or hash access methods, construct bulk buffers 160 in the <span class="bold"><strong>key</strong></span> and 161 <span class="bold"><strong>data</strong></span> <a class="link" href="dbt.html" title="Chapter 4. The Dbt Handle">Dbt</a> using 162 <a class="xref" href="dbmultipledatabuilder.html" title="DbMultipleDataBuilder">DbMultipleDataBuilder</a>. To 163 put records in bulk with the recno or queue access methods, construct bulk buffers 164 in the <span class="bold"><strong>data</strong></span> <a class="link" href="dbt.html" title="Chapter 4. The Dbt Handle">Dbt</a> 165 as before, but construct the <span class="bold"><strong>key</strong></span> 166 <a class="link" href="dbt.html" title="Chapter 4. The Dbt Handle">Dbt</a> using 167 <a class="xref" href="dbmultiplerecnodatabuilder.html" title="DbMultipleRecnoDataBuilder">DbMultipleRecnoDataBuilder</a> 168 with a data size of zero. 169 </p> 170 <p> 171 A successful bulk operation is logically equivalent to a loop through each key/data 172 pair, performing a <a class="xref" href="dbput.html" title="Db::put()">Db::put()</a> for each one. 173 </p> 174 <p> 175 See <a class="xref" href="dbt.html#dbtlist" title="DBT and Bulk Operations">DBT and Bulk Operations</a> for more information on 176 working with bulk updates. 177 </p> 178 <p> 179 The <code class="literal">DB_MULTIPLE</code> flag may only be used alone, or with the 180 <code class="literal">DB_OVERWRITE_DUP</code> option. 181 </p> 182 </li> 183 <li> 184 <p><a id="put_DB_MULTIPLE_KEY"></a> 185 <code class="literal">DB_MULTIPLE_KEY</code> 186 </p> 187 <p> 188 Put multiple data items using keys and data from the buffer to which the 189 <span class="bold"><strong>key</strong></span> parameter refers. 190 </p> 191 <p> 192 To put records in bulk with the btree or hash access methods, construct a bulk 193 buffer in the <span class="bold"><strong>key</strong></span> 194 <a class="link" href="dbt.html" title="Chapter 4. The Dbt Handle">Dbt</a> using 195 <a class="xref" href="dbmultiplekeydatabuilder.html" title="DbMultipleKeyDataBuilder">DbMultipleKeyDataBuilder</a>. 196 To put records in bulk with the recno or queue access methods, construct a bulk 197 buffer in the <span class="bold"><strong>key</strong></span> 198 <a class="link" href="dbt.html" title="Chapter 4. The Dbt Handle">Dbt</a> using 199 <a class="xref" href="dbmultiplerecnodatabuilder.html" title="DbMultipleRecnoDataBuilder">DbMultipleRecnoDataBuilder</a>. 200 </p> 201 <p> 202 See <a class="xref" href="dbt.html#dbtlist" title="DBT and Bulk Operations">DBT and Bulk Operations</a> for more information on 203 working with bulk updates. 204 </p> 205 <p> 206 The <code class="literal">DB_MULTIPLE_KEY</code> flag may only be used alone, or with the 207 <code class="literal">DB_OVERWRITE_DUP</code> option. 208 </p> 209 </li> 210 <li> 211 <p><a id="put_DB_OVERWRITE_DUP"></a> 212 <code class="literal">DB_OVERWRITE_DUP</code> 213 </p> 214 <p> 215 Ignore duplicate records when overwriting records in a database configured for 216 sorted duplicates. 217 </p> 218 <p> 219 Normally, if a database is configured for sorted duplicates, an attempt to 220 put a record that compares identically to a record already existing in the 221 database will fail. Using this flag causes the put to silently proceed, without 222 failure. 223 </p> 224 <p> 225 This flag is extremely useful when performing bulk puts (using the 226 <code class="literal">DB_MULTIPLE</code> or <code class="literal">DB_MULTIPLE_KEY</code> flags). 227 Depending on the number of records you are writing to the database with a bulk 228 put, you may not want the operation to fail in the event that a 229 duplicate record is encountered. Using this flag along with the 230 <code class="literal">DB_MULTIPLE</code> or <code class="literal">DB_MULTIPLE_KEY</code> flags 231 allows the bulk put to complete, even if a duplicate record is encountered. 232 233 </p> 234 <p> 235 This flag is also useful if you are using a custom comparison function that compares only 236 part of the data portion of a record. In this case, two records can compare 237 equally when, in fact, they are not equal. This flag allows the put to 238 complete, even if your custom comparison routine claims the two records are 239 equal. 240 </p> 241 </li> 242 </ul> 243 </div> 244 </div> 245 <div class="sect3" lang="en" xml:lang="en"> 246 <div class="titlepage"> 247 <div> 248 <div> 249 <h4 class="title"><a id="id1641306"></a>data</h4> 250 </div> 251 </div> 252 </div> 253 <p> 254 The data <a class="link" href="dbt.html" title="Chapter 4. The Dbt Handle">Dbt</a> operated on. 255 </p> 256 </div> 257 <div class="sect3" lang="en" xml:lang="en"> 258 <div class="titlepage"> 259 <div> 260 <div> 261 <h4 class="title"><a id="id1641050"></a>key</h4> 262 </div> 263 </div> 264 </div> 265 <p> 266 The key <a class="link" href="dbt.html" title="Chapter 4. The Dbt Handle">Dbt</a> operated on. 267 </p> 268 </div> 269 <div class="sect3" lang="en" xml:lang="en"> 270 <div class="titlepage"> 271 <div> 272 <div> 273 <h4 class="title"><a id="id1641464"></a>txnid</h4> 274 </div> 275 </div> 276 </div> 277 <p> 278 If the operation is part of an application-specified transaction, the 279 <span class="bold"><strong>txnid</strong></span> parameter is a transaction 280 handle returned from <a class="xref" href="txnbegin.html" title="DbEnv::txn_begin()">DbEnv::txn_begin()</a>; if the 281 operation is part of a Berkeley DB Concurrent Data Store group, the 282 <span class="bold"><strong>txnid</strong></span> parameter is a handle returned 283 from <a class="xref" href="envcdsgroup_begin.html" title="DbEnv::cdsgroup_begin()">DbEnv::cdsgroup_begin()</a>; 284 otherwise NULL. If no transaction handle is specified, but the 285 operation occurs in a transactional database, the operation will be 286 implicitly transaction protected. 287 </p> 288 </div> 289 </div> 290 <div class="sect2" lang="en" xml:lang="en"> 291 <div class="titlepage"> 292 <div> 293 <div> 294 <h3 class="title"><a id="id1641532"></a>Errors</h3> 295 </div> 296 </div> 297 </div> 298 <p> 299 The <code class="methodname">Db::put()</code> <span> 300 301 <span> 302 method may fail and throw a <a class="link" href="dbexception.html" title="Chapter 6. The DbException Class">DbException</a> 303 exception, encapsulating one of the following non-zero errors, or return one 304 of the following non-zero errors: 305 </span> 306 </span> 307 </p> 308 <div class="sect3" lang="en" xml:lang="en"> 309 <div class="titlepage"> 310 <div> 311 <div> 312 <h4 class="title"><a id="id1641275"></a>DB_FOREIGN_CONFLICT</h4> 313 </div> 314 </div> 315 </div> 316 <p> 317 A <a class="link" href="dbassociate_foreign.html" title="Db::associate_foreign()">foreign key constraint violation</a> 318 has occurred. This can be caused by one of two things: 319 </p> 320 <div class="orderedlist"> 321 <ol type="1"> 322 <li> 323 <p> 324 An attempt was made to add a record to a 325 constrained database, and the key used for that 326 record does not exist in the foreign key 327 database. 328 </p> 329 </li> 330 <li> 331 <p> 332 <a class="link" href="dbassociate_foreign.html#associate_foreign_DB_FOREIGN_ABORT">DB_FOREIGN_ABORT</a> 333 was declared for a foreign key database, and then 334 subsequently a record was deleted from the 335 foreign key database without first removing it 336 from the constrained secondary database. 337 </p> 338 </li> 339 </ol> 340 </div> 341 </div> 342 <div class="sect3" lang="en" xml:lang="en"> 343 <div class="titlepage"> 344 <div> 345 <div> 346 <h4 class="title"><a id="id1640951"></a><span>DbDeadlockException or </span>DB_LOCK_DEADLOCK</h4> 347 </div> 348 </div> 349 </div> 350 <p> 351 A transactional database environment operation was selected to resolve 352 a deadlock. 353 </p> 354 <p> 355 <a class="xref" href="dbdeadlock.html" title="DbDeadlockException">DbDeadlockException</a> is thrown if 356 your Berkeley DB API is configured to throw exceptions. 357 Otherwise, <code class="literal">DB_LOCK_DEADLOCK</code> is returned. 358 </p> 359 </div> 360 <div class="sect3" lang="en" xml:lang="en"> 361 <div class="titlepage"> 362 <div> 363 <div> 364 <h4 class="title"><a id="id1641727"></a><span>DbLockNotGrantedException or </span>DB_LOCK_NOTGRANTED</h4> 365 </div> 366 </div> 367 </div> 368 <p> 369 A Berkeley DB Concurrent Data Store database environment configured 370 for lock timeouts was unable to grant a lock in the allowed time. 371 </p> 372 <p> 373 <a class="xref" href="dblocknotgranted.html" title="DbLockNotGrantedException">DbLockNotGrantedException</a> is thrown if 374 your Berkeley DB API is configured to throw exceptions. 375 Otherwise, <code class="literal">DB_LOCK_NOTGRANTED</code> is returned. 376 </p> 377 </div> 378 <div class="sect3" lang="en" xml:lang="en"> 379 <div class="titlepage"> 380 <div> 381 <div> 382 <h4 class="title"><a id="id1640646"></a><span>DbRepHandleDeadException or</span> DB_REP_HANDLE_DEAD</h4> 383 </div> 384 </div> 385 </div> 386 <p> 387 When a client synchronizes with the master, it is possible for committed 388 transactions to be rolled back. This invalidates all the database and cursor 389 handles opened in the replication environment. Once this occurs, an attempt to use 390 such a handle will 391 <span> 392 throw a <a class="xref" href="dbrephandledead.html" title="DbRepHandleDeadException">DbRepHandleDeadException</a> (if 393 your application is configured to throw exceptions), or 394 </span> 395 return <code class="literal">DB_REP_HANDLE_DEAD</code>. 396 The application will need to discard the handle and open a new one in order to 397 continue processing. 398 </p> 399 </div> 400 <a id="dbput_DB_REP_LOCKOUT"></a> 401 <div class="sect3" lang="en" xml:lang="en"> 402 <div class="titlepage"> 403 <div> 404 <div> 405 <h4 class="title"><a id="id1641683"></a><span>DbDeadlockException or </span>DB_REP_LOCKOUT</h4> 406 </div> 407 </div> 408 </div> 409 <p> 410 The operation was blocked by client/master synchronization. 411 </p> 412 <p> 413 <a class="xref" href="dbdeadlock.html" title="DbDeadlockException">DbDeadlockException</a> is thrown if 414 your Berkeley DB API is configured to throw exceptions. 415 Otherwise, <code class="literal">DB_REP_LOCKOUT</code> is returned. 416 </p> 417 </div> 418 <div class="sect3" lang="en" xml:lang="en"> 419 <div class="titlepage"> 420 <div> 421 <div> 422 <h4 class="title"><a id="id1641697"></a>EACCES</h4> 423 </div> 424 </div> 425 </div> 426 <p> 427 An attempt was made to modify a read-only database. 428 </p> 429 </div> 430 <div class="sect3" lang="en" xml:lang="en"> 431 <div class="titlepage"> 432 <div> 433 <div> 434 <h4 class="title"><a id="id1641771"></a>EINVAL</h4> 435 </div> 436 </div> 437 </div> 438 <p> 439 If a record number of 0 was specified; an attempt was made to add a 440 record to a fixed-length database that was too large to fit; an 441 attempt was made to do a partial put; an attempt was made to add a 442 record to a secondary index; or if an invalid flag value or parameter 443 was specified. 444 </p> 445 </div> 446 <div class="sect3" lang="en" xml:lang="en"> 447 <div class="titlepage"> 448 <div> 449 <div> 450 <h4 class="title"><a id="id1641578"></a>ENOSPC</h4> 451 </div> 452 </div> 453 </div> 454 <p> 455 A btree exceeded the maximum btree depth (255). 456 </p> 457 </div> 458 </div> 459 <div class="sect2" lang="en" xml:lang="en"> 460 <div class="titlepage"> 461 <div> 462 <div> 463 <h3 class="title"><a id="id1641164"></a>Class</h3> 464 </div> 465 </div> 466 </div> 467 <p> 468 <a class="link" href="db.html" title="Chapter 2. The Db Handle">Db</a> 469 </p> 470 </div> 471 <div class="sect2" lang="en" xml:lang="en"> 472 <div class="titlepage"> 473 <div> 474 <div> 475 <h3 class="title"><a id="id1641154"></a>See Also</h3> 476 </div> 477 </div> 478 </div> 479 <p> 480 <a class="xref" href="db.html#dblist" title="Database and Related Methods">Database and Related Methods</a> 481 </p> 482 </div> 483 </div> 484 <div class="navfooter"> 485 <hr /> 486 <table width="100%" summary="Navigation footer"> 487 <tr> 488 <td width="40%" align="left"><a accesskey="p" href="dbopen.html">Prev</a> </td> 489 <td width="20%" align="center"> 490 <a accesskey="u" href="db.html">Up</a> 491 </td> 492 <td width="40%" align="right"> <a accesskey="n" href="dbremove.html">Next</a></td> 493 </tr> 494 <tr> 495 <td width="40%" align="left" valign="top">Db::open() </td> 496 <td width="20%" align="center"> 497 <a accesskey="h" href="index.html">Home</a> 498 </td> 499 <td width="40%" align="right" valign="top"> Db::remove()</td> 500 </tr> 501 </table> 502 </div> 503 </body> 504</html> 505