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">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Cursor Example</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
10 <link rel="up" href="Cursors.html" title="Chapter 4. Using Cursors" />
11 <link rel="prev" href="ReplacingEntryWCursor.html" title="Replacing Records Using Cursors" />
12 <link rel="next" href="indexes.html" title="Chapter 5. Secondary Databases" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">Cursor Example</th>
24 <td width="20%" align="left"><a accesskey="p" href="ReplacingEntryWCursor.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 4. Using Cursors</th>
26 <td width="20%" align="right"> <a accesskey="n" href="indexes.html">Next</a></td>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
35 <h2 class="title" style="clear: both"><a id="CoreCursorUsage"></a>Cursor Example</h2>
42 <a class="xref" href="DbCXXUsage.html" title="Database Usage Example">Database Usage Example</a>
44 application that loaded two databases with
45 vendor and inventory information. In this example, we will write an
46 application to display all of the items in the inventory database. As a
47 part of showing any given inventory item, we will look up the vendor who
48 can provide the item and show the vendor's contact information.
51 Specifically, the <code class="classname">example_database_read</code>
52 application does the following:
54 <div class="orderedlist">
58 Opens the the inventory and vendor databases
59 that were created by our <code class="classname">example_database_load</code>
62 <a class="xref" href="DbCXXUsage.html#exampledbload-cxx" title="Example 3.3 example_database_load">example_database_load</a>
63 for information on how that
64 application creates the databases and writes data to them.
68 <p>Obtains a cursor from the inventory database.</p>
72 Steps through the inventory database, displaying
73 each record as it goes.
78 Gets the name of the vendor for that inventory item from the
84 Uses the vendor name to look up the vendor record in the vendor
89 <p>Displays the vendor record.</p>
94 Remember that you can find the complete implementation of this application
97 <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_cxx/getting_started</pre>
99 where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you
100 placed your DB distribution.
102 <div class="example">
103 <a id="CoreEIR-cxx"></a>
105 <b>Example 4.1 example_database_read</b>
107 <div class="example-contents">
109 To begin, we include the necessary header files and perform our
110 forward declarations. We also write our <code class="function">usage()</code>
113 <a id="cxx_cursor10"></a>
114 <pre class="programlisting">// File: example_database_read.cpp
115 #include <iostream>
116 #include <fstream>
117 #include <cstdlib>
120 #include "gettingStartedCommon.hpp"
122 // Forward declarations
123 int show_all_records(MyDb &inventoryDB, MyDb &vendorDB);
124 int show_vendor(MyDb &vendorDB, const char *vendor); </pre>
126 Next we write our <code class="function">main()</code> function. Note that it is
127 somewhat unnecessarily complicated here because we will be extending it
128 in the next chapter to perform inventory item lookups.
130 <a id="cxx_cursor11"></a>
131 <pre class="programlisting">// Displays all inventory items and the associated vendor record.
133 main (int argc, char *argv[])
135 // Initialize the path to the database files
136 std::string databaseHome("./");
139 std::string vDbName("vendordb.db");
140 std::string iDbName("inventorydb.db");
142 // Parse the command line arguments
143 // Omitted for brevity
147 // Open all databases.
148 MyDb inventoryDB(databaseHome, iDbName);
149 MyDb vendorDB(databaseHome, vDbName);
151 show_all_records(inventoryDB, vendorDB);
152 } catch(DbException &e) {
153 std::cerr << "Error reading databases. " << std::endl;
154 std::cerr << e.what() << std::endl;
155 return(e.get_errno());
156 } catch(std::exception &e) {
157 std::cerr << "Error reading databases. " << std::endl;
158 std::cerr << e.what() << std::endl;
165 Next we need to write the <code class="function">show_all_records()</code>
166 function. This function displays all
167 of the inventory records found in the inventory database. Once it shows
168 the inventory record, it retrieves the vendor's name from that record
169 and uses it to look up and display the appropriate vendor record:
171 <a id="cxx_cursor12"></a>
172 <pre class="programlisting">// Shows all the records in the inventory database.
173 // For each inventory record shown, the appropriate
174 // vendor record is also displayed.
176 show_all_records(MyDb &inventoryDB, MyDb &vendorDB)
178 // Get a cursor to the inventory db
181 inventoryDB.getDb().cursor(NULL, &cursorp, 0);
183 // Iterate over the inventory database, from the first record
184 // to the last, displaying each in turn
187 while ((ret = cursorp->get(&key, &data, DB_NEXT)) == 0 )
189 InventoryData inventoryItem(data.get_data());
190 inventoryItem.show();
192 show_vendor(vendorDB, inventoryItem.getVendor().c_str());
194 } catch(DbException &e) {
195 inventoryDB.getDb().err(e.get_errno(),
196 "Error in show_all_records");
199 } catch(std::exception &e) {
208 Note that the <code class="classname">InventoryData</code> class that we use here
210 <a class="xref" href="DbCXXUsage.html#InventoryData" title="Example 3.2 InventoryData Class">InventoryData Class</a>.
213 Having displayed the inventory record, we now want to display the
214 vendor record corresponding to this record.
215 In this case we do not need to use a
216 cursor to display the vendor record. Using a cursor here complicates our
217 code slightly for no good gain. Instead, we simply perform a
218 <code class="function">get()</code> directly against the vendor database.
220 <a id="cxx_cursor13"></a>
221 <pre class="programlisting">// Shows a vendor record. Each vendor record is an instance of
222 // a vendor structure. See loadVendorDB() in
223 // example_database_load for how this structure was originally
224 // put into the database.
226 show_vendor(MyDb &vendorDB, const char *vendor)
232 // Set the search key to the vendor's name
233 // vendor is explicitly cast to char * to stop a compiler
235 Dbt key((char *)vendor, strlen(vendor) + 1);
237 // Make sure we use the memory we set aside for the VENDOR
238 // structure rather than the memory that DB allocates.
239 // Some systems may require structures to be aligned in memory
240 // in a specific way, and DB may not get it right.
242 data.set_data(&my_vendor);
243 data.set_ulen(sizeof(VENDOR));
244 data.set_flags(DB_DBT_USERMEM);
247 vendorDB.getDb().get(NULL, &key, &data, 0);
248 std::cout << " " << my_vendor.street << "\n"
249 << " " << my_vendor.city << ", "
250 << my_vendor.state << "\n"
251 << " " << my_vendor.zipcode << "\n"
252 << " " << my_vendor.phone_number << "\n"
253 << " Contact: " << my_vendor.sales_rep << "\n"
254 << " " << my_vendor.sales_rep_phone
257 } catch(DbException &e) {
258 vendorDB.getDb().err(e.get_errno(), "Error in show_vendor");
260 } catch(std::exception &e) {
267 <br class="example-break" />
269 That completes the implementation of
270 <code class="classname">example_database_read()</code>. In the next chapter, we
271 will extend this application to make use of a secondary database so that
272 we can query the inventory database for a specific inventory item.
275 <div class="navfooter">
277 <table width="100%" summary="Navigation footer">
279 <td width="40%" align="left"><a accesskey="p" href="ReplacingEntryWCursor.html">Prev</a> </td>
280 <td width="20%" align="center">
281 <a accesskey="u" href="Cursors.html">Up</a>
283 <td width="40%" align="right"> <a accesskey="n" href="indexes.html">Next</a></td>
286 <td width="40%" align="left" valign="top">Replacing Records Using Cursors </td>
287 <td width="20%" align="center">
288 <a accesskey="h" href="index.html">Home</a>
290 <td width="40%" align="right" valign="top"> Chapter 5. Secondary Databases</td>