1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Secondary Database Example</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
<link rel="up" href="indexes.html" title="Chapter 5. Secondary Databases" />
<link rel="prev" href="joins.html" title="Database Joins" />
<link rel="next" href="dbconfig.html" title="Chapter 6. Database Configuration" />
</head>
<body>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Secondary Database Example</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="joins.html">Prev</a> </td>
<th width="60%" align="center">Chapter 5. Secondary Databases</th>
<td width="20%" align="right"> <a accesskey="n" href="dbconfig.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="coreindexusage"></a>Secondary Database Example</h2>
</div>
</div>
</div>
<div class="toc">
<dl>
<dt>
<span class="sect2">
<a href="coreindexusage.html#edlWIndexes">Secondary Databases with example_database_load</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="coreindexusage.html#edrWIndexes">Secondary Databases with example_database_read</a>
</span>
</dt>
</dl>
</div>
<p>
In previous chapters in this book, we built applications that load
and display several DB databases. In this example, we will extend those
examples to use secondary databases. Specifically:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
In
<a class="xref" href="DbUsage.html" title="Database Usage Example">Database Usage Example</a>
we built an application that can open and load data into several databases.
In <a class="xref" href="coreindexusage.html#edlWIndexes" title="Secondary Databases with example_database_load">Secondary Databases with example_database_load</a> we will extend
that application to also open a secondary database for the purpose
of indexing inventory item names.
</p>
</li>
<li>
<p>
In <a class="xref" href="CoreCursorUsage.html" title="Cursor Example">Cursor Example</a> we
built an application to display our inventory database (and related
vendor information). In
<a class="xref" href="coreindexusage.html#edrWIndexes" title="Secondary Databases with example_database_read">Secondary Databases with example_database_read</a>
we will extend that application to
show inventory records based on the index we cause to be loaded using
<code class="function">example_database_load</code>.
</p>
</li>
</ul>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="edlWIndexes"></a>Secondary Databases with example_database_load</h3>
</div>
</div>
</div>
<p>
<code class="function">example_database_load</code> uses several utility
functions to open and close its databases. In order to cause
<code class="function">example_database_load</code> to maintain an index of
inventory item names, all we really need to do is update the utility
functions to:
</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>
Create a new database to be used as a secondary database.
</p>
</li>
<li>
<p>
Associate our new database to the inventory primary
database.
</p>
</li>
<li>
<p>
Close the secondary database when we close the rest of our
databases.
</p>
</li>
</ol>
</div>
<p>
We also need a function that can create our secondary keys for us.
</p>
<p>
Because DB maintains secondary databases for us; once this work
is done we need not make any other changes to <code class="function">example_database_load</code>.
<span>Therefore, we can limit
all our work to the code found in <code class="filename">gettingstarted_common.h</code>
and <code class="filename">gettingstarted_common.c</code>.
</span>
</p>
<p>
Remember that you can find the complete implementation of these functions
in:
</p>
<pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_c/getting_started</pre>
<p>
where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you
placed your DB distribution.
</p>
<p>
To begin, we need to update the <code class="literal">stock_dbs</code>
structure to accommodate the additional database. We defined this
structure in <code class="filename">gettingstarted_common.h</code>. We can
limit our update to this file to just that structure definition:
</p>
<p>
Remember that new code is in <strong class="userinput"><code>bold</code></strong>.
</p>
<a id="c_index10"></a>
<pre class="programlisting">/* file: gettingstarted_common.h */
#include <db.h>
typedef struct stock_dbs {
DB *inventory_dbp; /* Database containing inventory information */
DB *vendor_dbp; /* Database containing vendor information */
<strong class="userinput"><code>DB *itemname_sdbp; /* Index based on the item name index */</code></strong>
char *db_home_dir; /* Directory containing the database files */
<strong class="userinput"><code>char *itemname_db_name; /* Itemname secondary database */</code></strong>
char *inventory_db_name; /* Name of the inventory database */
char *vendor_db_name; /* Name of the vendor database */
} STOCK_DBS;
/* Function prototypes */
int databases_setup(STOCK_DBS *, const char *, FILE *);
int databases_close(STOCK_DBS *);
void initialize_stockdbs(STOCK_DBS *);
int open_database(DB **, const char *, const char *, FILE *, <strong class="userinput"><code>int</code></strong>);
void set_db_filenames(STOCK_DBS *my_stock);
</pre>
<p>
Because we updated our stock_dbs structure, we need to update our
stock_dbs utility functions
(<a class="xref" href="CoreDbUsage.html#stock-db-functions" title="Example 2.2 The stock_db Utility Functions">The stock_db Utility Functions</a>)
accordingly. The updates are trivial and so we won't show them here
in the interest of space. You can find their complete implementation
in the <code class="filename">gettingstarted_common.c</code> file
accompanying this example in your DB distribution.
</p>
<p>
More importantly, however, we need to go to
<code class="filename">gettingstarted_common.c</code>
and create our secondary key extractor function. When we
store our inventory items, we place the item name in the buffer
immediately after a <code class="literal">float</code> and an
<code class="literal">int</code>, so retrieving the string from the buffer is
fairly easy to do:
</p>
<a id="c_index11"></a>
<pre class="programlisting">/* file: gettingstarted_common.c */
#include "gettingstarted_common.h"
<strong class="userinput"><code>/*
* Used to extract an inventory item's name from an
* inventory database record. This function is used to create
* keys for secondary database records.
*/
int
get_item_name(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey)
{
int offset;
/*
* First, obtain the buffer location where we placed the
* item's name. In this example, the item's name is located
* in the primary data. It is the first string in the
* buffer after the price (a float) and the quantity (an int).
*
* See load_inventory_database() in example_database_load.c
* for how we marshalled the inventory information into the
* data DBT.
*/
offset = sizeof(float) + sizeof(int);
/* Check to make sure there's data */
if (pdata->size < offset)
return (-1); /* Returning non-zero means that the
* secondary record is not created/updated.
*/
/* Now set the secondary key's data to be the item name */
memset(skey, 0, sizeof(DBT));
skey->data = pdata->data + offset;
skey->size = strlen(skey->data) + 1;
return (0);
}</code></strong> </pre>
<p>
Having completed that function, we need to update
<code class="function">set_db_filenames()</code> and
<code class="function">initialize_stockdbs()</code> to handle the
new secondary databases that our application will now use.
These functions were originally introduced in
<a class="xref" href="CoreDbUsage.html#stock-db-functions" title="Example 2.2 The stock_db Utility Functions">The stock_db Utility Functions</a>.
</p>
<a id="c_index11.1"></a>
<pre class="programlisting">
/* Initializes the STOCK_DBS struct.*/
void
initialize_stockdbs(STOCK_DBS *my_stock)
{
my_stock->db_home_dir = DEFAULT_HOMEDIR;
my_stock->inventory_dbp = NULL;
my_stock->vendor_dbp = NULL;
<strong class="userinput"><code>my_stock->itemname_sdbp = NULL;</code></strong>
my_stock->inventory_db_name = NULL;
my_stock->vendor_db_name = NULL;
<strong class="userinput"><code>my_stock->itemname_db_name = NULL;</code></strong>
}
/* Identify all the files that will hold our databases. */
void
set_db_filenames(STOCK_DBS *my_stock)
{
size_t size;
/* Create the Inventory DB file name */
size = strlen(my_stock->db_home_dir) + strlen(INVENTORYDB) + 1;
my_stock->inventory_db_name = malloc(size);
snprintf(my_stock->inventory_db_name, size, "%s%s",
my_stock->db_home_dir, INVENTORYDB);
/* Create the Vendor DB file name */
size = strlen(my_stock->db_home_dir) + strlen(VENDORDB) + 1;
my_stock->vendor_db_name = malloc(size);
snprintf(my_stock->vendor_db_name, size, "%s%s",
my_stock->db_home_dir, VENDORDB);
<strong class="userinput"><code>/* Create the itemname DB file name */
size = strlen(my_stock->db_home_dir) + strlen(ITEMNAMEDB) + 1;
my_stock->itemname_db_name = malloc(size);
snprintf(my_stock->itemname_db_name, size, "%s%s",
my_stock->db_home_dir, ITEMNAMEDB);</code></strong>
} </pre>
<p>
We also need to update the
<code class="function">open_database()</code> (as described in
<a class="xref" href="CoreDbUsage.html#open-db" title="Example 2.3 open_database() Function">open_database() Function</a>)
to take special actions if we are
opening a secondary database. Unlike our primary databases, we want to
support sorted duplicates for our secondary database. This is because we
are indexing based on an item's name, and item names are
shared by multiple inventory records. As a result every key the secondary
database (an item name) will be used by multiple records (pointers to
records in our primary database). We allow this by configuring our
secondary database to support duplicate records. Further, because
BTrees perform best when their records are sorted, we go ahead and
configure our secondary database for sorted duplicates.
</p>
<p>
To do this, we add a parameter to the function that indicates whether we are
opening a secondary database, and we add in the few lines of code
necessary to set the sorted duplicates flags.
</p>
<a id="c_index12"></a>
<pre class="programlisting">/* Opens a database */
int
open_database(DB **dbpp, /* The DB handle that we are opening */
const char *file_name, /* The file in which the db lives */
const char *program_name, /* Name of the program calling this
* function */
FILE *error_file_pointer,
<strong class="userinput"><code>int is_secondary</code></strong>)
{
DB *dbp; /* For convenience */
u_int32_t open_flags;
int ret;
/* Initialize the DB handle */
ret = db_create(&dbp, NULL, 0);
if (ret != 0) {
fprintf(error_file_pointer, "%s: %s\n", program_name,
db_strerror(ret));
return (ret);
}
/* Point to the memory malloc'd by db_create() */
*dbpp = dbp;
/* Set up error handling for this database */
dbp->set_errfile(dbp, error_file_pointer);
dbp->set_errpfx(dbp, program_name);
<strong class="userinput"><code>/*
* If this is a secondary database, then we want to allow
* sorted duplicates.
*/
if (is_secondary) {
ret = dbp->set_flags(dbp, DB_DUPSORT);
if (ret != 0) {
dbp->err(dbp, ret, "Attempt to set DUPSORT flag failed.",
file_name);
return (ret);
}
}</code></strong>
/* Set the open flags */
open_flags = DB_CREATE;
/* Now open the database */
ret = dbp->open(dbp, /* Pointer to the database */
NULL, /* Txn pointer */
file_name, /* File name */
NULL, /* Logical db name (unneeded) */
DB_BTREE, /* Database type (using btree) */
open_flags, /* Open flags */
0); /* File mode. Using defaults */
if (ret != 0) {
dbp->err(dbp, ret, "Database '%s' open failed.", file_name);
return (ret);
}
return (ret);
} </pre>
<p>
That done, we can now update <code class="function">databases_setup()</code>
(see <a class="xref" href="CoreDbUsage.html#databasesetup" title="Example 2.4 The databases_setup() Function">The databases_setup() Function</a>) to create
and open our secondary database. To do this, we have to add a flag to
each call to <code class="function">open_database()</code> that indicates whether
the database is a secondary. We also have to associate our secondary
database with the inventory database (the primary).
</p>
<p>
Note that we do not anywhere in this example show the definition of
<code class="literal">PRIMARY_DB</code> and <code class="literal">SECONDARY_DB</code>. See
<code class="filename">gettingstarted_common.h</code> in your DB examples
directory for those definitions (they are just <code class="literal">0</code> and
<code class="literal">1</code>, respectively).
</p>
<a id="c_index13"></a>
<pre class="programlisting">/* opens all databases */
int
databases_setup(STOCK_DBS *my_stock, const char *program_name,
FILE *error_file_pointer)
{
int ret;
/* Open the vendor database */
ret = open_database(&(my_stock->vendor_dbp),
my_stock->vendor_db_name,
program_name, error_file_pointer,
<strong class="userinput"><code>PRIMARY_DB</code></strong>);
if (ret != 0)
/*
* Error reporting is handled in open_database() so just return
* the return code here.
*/
return (ret);
/* Open the inventory database */
ret = open_database(&(my_stock->inventory_dbp),
my_stock->inventory_db_name,
program_name, error_file_pointer,
<strong class="userinput"><code>PRIMARY_DB</code></strong>);
if (ret != 0)
/*
* Error reporting is handled in open_database() so just return
* the return code here.
*/
return (ret);
<strong class="userinput"><code>/*
* Open the itemname secondary database. This is used to
* index the product names found in the inventory
* database.
*/
ret = open_database(&(my_stock->itemname_sdbp),
my_stock->itemname_db_name,
program_name, error_file_pointer,
SECONDARY_DB);
if (ret != 0)
/*
* Error reporting is handled in open_database() so just return
* the return code here.
*/
return (ret);
/*
* Associate the itemname db with its primary db
* (inventory db).
*/
my_stock->inventory_dbp->associate(
my_stock->inventory_dbp, /* Primary db */
NULL, /* txn id */
my_stock->itemname_sdbp, /* Secondary db */
get_item_name, /* Secondary key extractor */
0); /* Flags */
</code></strong>
printf("databases opened successfully\n");
return (0);
}</pre>
<p>
Finally, we need to update <code class="function">databases_close()</code>
(<a class="xref" href="CoreDbUsage.html#database_close" title="Example 2.5 The databases_close() Function">The databases_close() Function</a>)
to close our
new secondary database. Note that we are careful to close the secondary
before the primary, even though the database close routine is single
threaded.
</p>
<a id="c_index14"></a>
<pre class="programlisting">/* Closes all the databases and secondary databases. */
int
databases_close(STOCK_DBS *my_stock)
{
int ret;
/*
* Note that closing a database automatically flushes its cached data
* to disk, so no sync is required here.
*/
<strong class="userinput"><code>if (my_stock->itemname_sdbp != NULL) {
ret = my_stock->itemname_sdbp->close(my_stock->itemname_sdbp, 0);
if (ret != 0)
fprintf(stderr, "Itemname database close failed: %s\n",
db_strerror(ret));
}</code></strong>
if (my_stock->inventory_dbp != NULL) {
ret = my_stock->inventory_dbp->close(my_stock->inventory_dbp, 0);
if (ret != 0)
fprintf(stderr, "Inventory database close failed: %s\n",
db_strerror(ret));
}
if (my_stock->vendor_dbp != NULL) {
ret = my_stock->vendor_dbp->close(my_stock->vendor_dbp, 0);
if (ret != 0)
fprintf(stderr, "Vendor database close failed: %s\n",
db_strerror(ret));
}
printf("databases closed.\n");
return (0);
} </pre>
<p>
And the implementation changes slightly to take advantage of the new
boolean. Note that to save space, we just show the constructor where the
code actually changes:
</p>
<p>
That completes our update to <code class="function">example_database_load</code>.
Now when this program is called, it will automatically index inventory
items based on their names. We can then query for those items using the
new index. We show how to do that in the next section.
</p>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="edrWIndexes"></a>Secondary Databases with example_database_read</h3>
</div>
</div>
</div>
<p>
In <a class="xref" href="CoreCursorUsage.html" title="Cursor Example">Cursor Example</a> we
wrote an application that displays every inventory item in the
Inventory database. In this section, we will update that example to
allow us to search for and display an inventory item given a
specific name. To do this, we will make use of the secondary
database that <code class="function">example_database_load</code> now
creates.
</p>
<p>
Because we manage all our database open and close activities in
<code class="function">databases_setup()</code> and
<code class="function">databases_close()</code>,
the update to <code class="function">example_database_read</code> is
relatively modest. We need only add a command line parameter on
which we can specify the item name, and we will need a new function
in which we will perform the query and display the results.
</p>
<p>
To begin, we add a single forward declaration to the application,
and update our usage function slightly:
</p>
<a id="c_index15"></a>
<pre class="programlisting">/* File: example_database_read.c */
/* gettingstarted_common.h includes db.h for us */
#include "gettingstarted_common.h"
/* Forward declarations */
char * show_inventory_item(void *);
int show_all_records(STOCK_DBS *);
<strong class="userinput"><code>int show_records(STOCK_DBS *, char *);</code></strong>
int show_vendor_record(char *, DB *); </pre>
<p>
Next, we update <code class="function">main()</code> to
accept the new command line switch.
We also need a new variable to contain the item's name.
</p>
<a id="c_index16"></a>
<pre class="programlisting">/*
<strong class="userinput"><code> * Searches for a inventory item based on that item's name. The search is
* performed using the item name secondary database. Displays all
* inventory items that use the specified name, as well as the vendor
* associated with that inventory item.
*
* If no item name is provided, then all inventory items are displayed.</code></strong>
*/
int
main(int argc, char *argv[])
{
STOCK_DBS my_stock;
int ret;
<strong class="userinput"><code>char *itemname</code></strong>;
/* Initialize the STOCK_DBS struct */
initialize_stockdbs(&my_stock);
<strong class="userinput"><code>itemname = NULL;</code></strong>
/*
* Parse the command line arguments here and determine
* the location of the database files <strong class="userinput"><code>as well as the
* inventory item we want displayed, if any.</code></strong> This step is
* omitted for brevity.
*/
/*
* Identify the files that will hold our databases
* This function uses information obtained from the
* command line to identify the directory in which
* the database files reside.
*/
set_db_filenames(&my_stock);
/* Open all databases */
ret = databases_setup(&my_stock, "example_database_read", stderr);
if (ret != 0) {
fprintf(stderr, "Error opening databases\n");
databases_close(&my_stock);
return (ret);
}</pre>
<p>
The final update to the <code class="function">main()</code> entails a little bit
of logic to determine whether we want to display all available inventory
items, or just the ones that match a name provided on the
<code class="literal">-i</code> command line parameter.
</p>
<a id="c_index17"></a>
<pre class="programlisting"> /*
* Show either a single item or all items, depending
* on whether itemname is set to a value.
*/
<strong class="userinput"><code>if (itemname == NULL)
ret = show_all_records(&my_stock);
else
ret = show_records(&my_stock, itemname);</code></strong>
/* Close our databases */
databases_close(&my_stock);
return (ret);
} </pre>
<p>
The only other thing that we need to add to the application is the
implementation of the
<code class="function">show_records()</code>
function.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
In the interest of space, we refrain from showing the other
functions used by this application. For their implementation, please
see <a class="xref" href="CoreCursorUsage.html" title="Cursor Example">Cursor Example</a>.
Alternatively, you can see the entire implementation of this
application
in:
</p>
<pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_c/getting_started</pre>
<p>
where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you
placed your DB distribution.
</p>
</div>
<a id="c_index18"></a>
<pre class="programlisting">/*
* Search for an inventory item given its name (using the inventory item
* secondary database) and display that record and any duplicates that may
* exist.
*/
int
show_records(STOCK_DBS *my_stock, char *itemname)
{
DBC *itemname_cursorp;
DBT key, data;
char *the_vendor;
int ret, exit_value;
/* Initialize our DBTs. */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
/* Get a cursor to the itemname db */
my_stock->itemname_sdbp->cursor(my_stock->itemname_sdbp, 0,
&itemname_cursorp, 0);
/*
* Get the search key. This is the name on the inventory
* record that we want to examine.
*/
key.data = itemname;
key.size = strlen(itemname) + 1;
/*
* Position our cursor to the first record in the secondary
* database that has the appropriate key.
*/
exit_value = 0;
ret = itemname_cursorp->get(itemname_cursorp, &key, &data, DB_SET);
if (!ret) {
do {
/*
* Show the inventory record and the vendor responsible
* for this inventory item.
*/
the_vendor = show_inventory_item(data.data);
ret = show_vendor_record(the_vendor, my_stock->vendor_dbp);
if (ret) {
exit_value = ret;
break;
}
/*
* Our secondary allows duplicates, so we need to loop over
* the next duplicate records and show them all. This is done
* because an inventory item's name is not a unique value.
*/
} while(itemname_cursorp->get(itemname_cursorp, &key, &data,
DB_NEXT_DUP) == 0);
} else {
printf("No records found for '%s'\n", itemname);
}
/* Close the cursor */
itemname_cursorp->close(itemname_cursorp);
return (exit_value);
} </pre>
<p>
This completes our update to
<code class="classname">example_inventory_read</code>. Using this update, you
can now search for and show all inventory items that match a particular
name. For example:
</p>
<pre class="programlisting"> example_inventory_read -i "Zulu Nut"</pre>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="joins.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="indexes.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="dbconfig.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Database Joins </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Chapter 6. Database Configuration</td>
</tr>
</table>
</div>
</body>
</html>
|