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