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>Reading and Writing Database Records</title> 7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 9 <link rel="home" href="index.html" title="Getting Started with Berkeley DB" /> 10 <link rel="up" href="DBEntry.html" title="Chapter��3.��Database Records" /> 11 <link rel="previous" href="DBEntry.html" title="Chapter��3.��Database Records" /> 12 <link rel="next" href="DbCXXUsage.html" title="Database Usage Example" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Reading and Writing Database Records</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="DBEntry.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��3.��Database Records</th> 23 <td width="20%" align="right">��<a accesskey="n" href="DbCXXUsage.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="usingDbt"></a>Reading and Writing Database Records</h2> 33 </div> 34 </div> 35 <div></div> 36 </div> 37 <p> 38 When reading and writing database records, be aware that there are some 39 slight differences in behavior depending on whether your database supports duplicate 40 records. Two or more database records are considered to be duplicates of 41 one another if they share the same key. The collection of records 42 sharing the same key are called a <span class="emphasis"><em>duplicates set.</em></span> 43 44 <span> 45 In DB, a given key is stored only once for a single duplicates set. 46 </span> 47 </p> 48 <p> 49 By default, DB databases do 50 not support duplicate records. Where duplicate records are supported, 51 cursors (see below) are <span>typically</span> used 52 to access all of the records in the duplicates set. 53 </p> 54 <p> 55 DB provides two basic mechanisms for the storage and retrieval of database 56 key/data pairs: 57 </p> 58 <div class="itemizedlist"> 59 <ul type="disc"> 60 <li> 61 <p> 62 The 63 64 65 <tt class="methodname">Db::put()</tt> 66 and 67 68 69 <tt class="methodname">Db::get()</tt> 70 methods provide the easiest access for all non-duplicate records in the database. 71 These methods are described in this section. 72 </p> 73 </li> 74 <li> 75 <p>Cursors provide several methods for putting and getting database 76 records. Cursors and their database access methods are described in 77 <a href="Cursors.html">Using Cursors</a>.</p> 78 </li> 79 </ul> 80 </div> 81 <div class="sect2" lang="en" xml:lang="en"> 82 <div class="titlepage"> 83 <div> 84 <div> 85 <h3 class="title"><a id="databaseWrite"></a>Writing Records to the Database</h3> 86 </div> 87 </div> 88 <div></div> 89 </div> 90 <p> 91 Records are stored in the database using whatever organization is 92 required by the access method that you have selected. In some cases (such as 93 BTree), records are stored in a sort order that you may want to define 94 (see <a href="btree.html#comparators">Setting Comparison Functions</a> for more information). 95 </p> 96 <p> 97 In any case, the mechanics of putting and getting database records do not 98 change once you have selected your access method, configured your 99 sorting routines (if any), and opened your database. From your 100 code's perspective, a simple database put and get is largely the 101 same no matter what access method you are using. 102 </p> 103 <p> 104 You use 105 106 <tt class="methodname">Db::put()</tt> 107 to put, or write, a database record. This method requires you to provide 108 the record's key and data in the form of a pair of 109 110 <span><tt class="methodname">Dbt</tt> objects.</span> 111 You can also provide one or more flags that control DB's behavior 112 for the database write. 113 </p> 114 <p> 115 Of the flags available to this method, <tt class="literal">DB_NOOVERWRITE</tt> 116 may be interesting to you. This flag disallows overwriting (replacing) 117 an existing record in the database. If the provided key already exists 118 in the database, then this method returns <tt class="literal">DB_KEYEXIST</tt> even if 119 the database supports duplicates. 120 </p> 121 <p> 122 For example: 123 </p> 124 <a id="cxx_dbt3"></a> 125 <pre class="programlisting">#include <db_cxx.h> 126#include <string.h> 127 128... 129 130char *description = "Grocery bill."; 131float money = 122.45; 132 133Db my_database(NULL, 0); 134// Database open omitted for clarity 135 136Dbt key(&money, sizeof(float)); 137Dbt data(description, strlen(description) + 1); 138 139int ret = my_database.put(NULL, &key, &data, DB_NOOVERWRITE); 140if (ret == DB_KEYEXIST) { 141 my_database.err(ret, "Put failed because key %f already exists", money); 142}</pre> 143 </div> 144 <div class="sect2" lang="en" xml:lang="en"> 145 <div class="titlepage"> 146 <div> 147 <div> 148 <h3 class="title"><a id="CoreDatabaseRead"></a>Getting Records from the Database</h3> 149 </div> 150 </div> 151 <div></div> 152 </div> 153 <p> 154 You can use the 155 156 <tt class="methodname">Db::get()</tt> 157 method to retrieve database records. Note that if your 158 database supports duplicate records, then by default this method will only 159 return the first record in a duplicate set. For this reason, if your 160 database supports duplicates, the common solution is to use a cursor to retrieve 161 records from it. Cursors are described in <a href="Cursors.html">Using Cursors</a>. 162 </p> 163 <p> 164 (You can also retrieve a set of duplicate records using a bulk get. 165 To do this, you use the <tt class="literal">DB_MULTIPLE</tt> flag on the 166 call to 167 168 <span><tt class="methodname">Db::get()</tt>.</span> 169 170 For more information, see the DB Programmer's Reference Guide). 171 </p> 172 <p> 173 By default, 174 175 <tt class="methodname">Db::get()</tt> 176 returns the first record found whose key matches the key 177 provide on the call to this method. If your database supports 178 duplicate records, you can change this behavior slightly by supplying 179 the <tt class="literal">DB_GET_BOTH</tt> flag. This flag causes 180 181 <tt class="methodname">DB::get()</tt> 182 to return the first record that matches the provided key and data. 183 </p> 184 <p> 185 If the specified key and/or data does not exist in the database, this 186 method returns <tt class="literal">DB_NOTFOUND</tt>. 187 </p> 188 <a id="cxx_dbt4"></a> 189 <pre class="programlisting">#include <db_cxx.h> 190#include <string.h> 191 192... 193 194float money; 195char description[DESCRIPTION_SIZE + 1]; 196 197Db my_database(NULL, 0); 198// Database open omitted for clarity 199 200money = 122.45; 201 202Dbt key, data; 203 204key.set_data(&money); 205key.set_size(sizeof(float)); 206 207data.set_data(description); 208data.set_ulen(DESCRIPTION_SIZE + 1); 209data.set_flags(DB_DBT_USERMEM); 210 211my_database.get(NULL, &key, &data, 0); 212 213// Description is set into the memory that we supplied. </pre> 214 <p> 215 Note that in this example, the 216 <tt class="literal">data.size</tt> 217 field would be automatically set to the size of the retrieved data. 218 </p> 219 </div> 220 <div class="sect2" lang="en" xml:lang="en"> 221 <div class="titlepage"> 222 <div> 223 <div> 224 <h3 class="title"><a id="recordDelete"></a>Deleting Records</h3> 225 </div> 226 </div> 227 <div></div> 228 </div> 229 <p> 230 231 You can use the 232 233 234 <tt class="methodname">Db::del()</tt> 235 method to delete a record from the database. If your database supports 236 duplicate records, then all records associated with the provided key are 237 deleted. To delete just one record from a list of duplicates, use a 238 cursor. Cursors are described in <a href="Cursors.html">Using Cursors</a>. 239 240 </p> 241 <p> 242 You can also delete every record in the database by using 243 244 245 <tt class="methodname">Db::truncate().</tt> 246 </p> 247 <p>For example:</p> 248 <a id="cxx_dbt5"></a> 249 <pre class="programlisting">#include <db_cxx.h> 250 251... 252 253Db my_database(NULL, 0); 254// Database open omitted for clarity 255 256float money = 122.45; 257Dbt key(&money, sizeof(float)); 258 259my_database.del(NULL, &key, 0);</pre> 260 </div> 261 <div class="sect2" lang="en" xml:lang="en"> 262 <div class="titlepage"> 263 <div> 264 <div> 265 <h3 class="title"><a id="datapersist"></a>Data Persistence</h3> 266 </div> 267 </div> 268 <div></div> 269 </div> 270 <p> 271 When you perform a database modification, your modification is made 272 in the in-memory cache. This means that your data modifications 273 are not necessarily flushed to disk, and so your data may not appear 274 in the database after an application restart. 275 </p> 276 <p> 277 Note that as a normal part of closing a database, its cache is 278 written to disk. However, in the event of an application or system 279 failure, there is no guarantee that your databases will close 280 cleanly. In this event, it is possible for you to lose data. Under 281 extremely rare circumstances, it is also possible for you to 282 experience database corruption. 283 </p> 284 <p> 285 Therefore, if you care if your data is durable across system 286 failures, and to guard against the rare possibility of 287 database corruption, you should use transactions to protect your 288 database modifications. Every time you commit a transaction, DB 289 ensures that the data will not be lost due to application or 290 system failure. Transaction usage is described in the 291 292 293 294 295 <span> 296 <i class="citetitle">Berkeley DB Getting Started with Transaction Processing</i> guide. 297 </span> 298 </p> 299 <p> 300 If you do not want to use transactions, then the assumption is that 301 your data is of a nature that it need not exist the next time your 302 application starts. You may want this if, for example, you are using 303 DB to cache data relevant only to the current application 304 runtime. 305 </p> 306 <p> 307 If, however, you are not using transactions for some reason and you 308 still want some guarantee that your database modifications are 309 persistent, then you should periodically 310 311 312 <span>call <tt class="methodname">Db::sync()</tt>.</span> 313 Syncs cause any dirty entries in the in-memory cache and the 314 operating system's file cache to be written to disk. As 315 such, they are quite expensive and you should use them sparingly. 316 </p> 317 <p> 318 Remember that by default a sync is performed any time a non-transactional 319 database is closed cleanly. (You can override this behavior by 320 specifying 321 <tt class="literal">DB_NOSYNC</tt> 322 323 on the call to 324 325 <span><tt class="methodname">Db::close()</tt>.)</span> 326 327 328 That said, you can manually run a sync by calling 329 330 331 <tt class="methodname">Db::sync().</tt> 332 333 334 </p> 335 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> 336 <h3 class="title">Note</h3> 337 <p> 338 If your application or system crashes and you are not using 339 transactions, then you should either discard and recreate your 340 databases, or verify them. You can verify a database using 341 342 <span>Db::verify().</span> 343 344 If your databases do not verify cleanly, use the 345 <span><b class="command">db_dump</b></span> command to salvage as much of the 346 database as is possible. Use either the <tt class="literal">-R</tt> or 347 <tt class="literal">-r</tt> command line options to control how 348 aggressive <span><b class="command">db_dump</b></span> should be when salvaging 349 your databases. 350 </p> 351 </div> 352 </div> 353 </div> 354 <div class="navfooter"> 355 <hr /> 356 <table width="100%" summary="Navigation footer"> 357 <tr> 358 <td width="40%" align="left"><a accesskey="p" href="DBEntry.html">Prev</a>��</td> 359 <td width="20%" align="center"> 360 <a accesskey="u" href="DBEntry.html">Up</a> 361 </td> 362 <td width="40%" align="right">��<a accesskey="n" href="DbCXXUsage.html">Next</a></td> 363 </tr> 364 <tr> 365 <td width="40%" align="left" valign="top">Chapter��3.��Database Records��</td> 366 <td width="20%" align="center"> 367 <a accesskey="h" href="index.html">Home</a> 368 </td> 369 <td width="40%" align="right" valign="top">��Database Usage Example</td> 370 </tr> 371 </table> 372 </div> 373 </body> 374</html> 375