home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Listing 29.2 Here You Open the Database and then Print Out Each Line as You Would Like to Display the Contents


open(FILE, “database.txt”);
@lines = <FILE>;
close(FILE);
foreach $line (@lines) {
$line =~ s/\n//g;
($name, $address, $city, $state) = split(/,/, $line);
print “$name - $address - $city - $state<br>\n”;
}
undef $lines;

It is quite possible that displaying the database in its entirety would not be visually appealing. Looking for one specific entry in a list of 200 records, for instance, would be daunting. The best solution would be to allow the visitor to enter a keyword and list only those entries that match the keyword. You can allow queries against specific fields or against the entire record.

As shown, flat file databases are handy and simple to use. Of course, using flat file databases is economical, as well, because they do not cost a penny. If your database is going to be relatively small, using flat file databases can provide a professional look and provide dynamic content without any expenses.

DBM Databases

Most UNIX systems have some kind of DBM database; in fact, I have yet to find a system that runs without one. DBM is a set of library routines that manages data files consisting of key and value pairs. The DBM routines control how users enter and retrieve information from the database. Although it is not the most powerful mechanism for storing information, using DBM is a faster method of retrieving information than using a flat file. Because most UNIX sites use one of the DBM libraries, the tools you need to store your information to a DBM database are readily available.

Almost as many flavors of the DBM libraries exist as those of UNIX systems. Although most of these libraries are not compatible with each other, all work basically the same way. This section explores each of the DBM flavors to give you a good understanding of their differences. Afterward, you’ll create an address book script, which should give you an idea of how DBM databases work.

A list follows of some of the most popular DBM libraries available:

  DBM—DBM stores the database in two files. The first has the extension .Pag and contains the bitmap. The second, which has the extension .Dir, contains the data.
  NDBM—NDBM is much like DBM with a few additional features; it was written to provide better storage and retrieval methods. Also, NDBM enables you to open many databases, unlike DBM, in which you are allowed to have only one database open within your script. Like DBM, NDBM stores its information in two files using the extensions .Pag and .Dir.
  SDBM—SDBM comes with the Perl archive, which has been ported to many platforms. Therefore, you can use DBM databases as long as a version of Perl exists for your computer. SDBM was written to match the functions provided with NDBM, so portability of code shouldn’t be a problem. Perl is available for most of the popular platforms, including Amiga, Macintosh, MS-DOS, and UNIX.
ON THE WEB
http://www.perl.com/perl/ For more information on SDBM and Perl, visit the Perl home page.

GDBM Version 1.7.1—GDBM is the GNU version of the DBM family of database routines.
GDBM also enables you to cache data, reducing the time that it takes to write to the database. The database has no size limit; its size depends completely on your system’s resources. GDBM database files have the extension .Db. Unlike DBM and NDBM, both of which use two files, GDBM uses only one file.
Berkeley db Version 1.85—The Berkeley db expands on the original DBM routines significantly. The Berkeley db uses hashed tables the same as the other DBM databases, but the library also can create databases based on a sorted balanced binary tree (BTREE) and store information with a record line number (RECNO). The method that you use depends completely on how you want to store and retrieve the information from a database. Berkeley’s db creates only one file, which has no extension.

If you can’t find a particular DBM database on your system, search the Web for DBM databases.

Writing to a DBM Database

Perl provides the capability to work with a DBM database as if it were an associative array. This enables you to manipulate a DBM database using Perl with very little difficulty. Include files for C exist for using DBM databases as well, making a DBM database with C simplistic (see the UNIX db main page). Unfortunately, the “how-to” of using DBM databases isn’t well documented, and trying to figure out how to use DBM databases can be a daunting task.

In actuality, using DBM databases is quite simple and much easier than using flat file databases. Adding an entry, for example, only requires a couple of lines in Perl. Perl provides various modules, depending on which DBM database you are using, and also supplies a module called AnyDBM_File.pm, which covers all the common DBM databases. The full documentation on AnyDBM can be found at http://www.perl.com/CPAN-local/doc/manual/html/lib/AnyDBM_File.html

DBM databases store information using a key and a value. The contents of each key cannot be repeated, although the value can. Therefore, you want to treat the contents of each key the same as if the key were a primary key in an SQL database. Because the key cannot be duplicated, you want to figure out what information you want to store would be unique. For a simple phone book, the email address would make a good unique identifier because some names are common (look in your phone book and see how many occurrences of “James Smith” you find).

For the value, you can store either one field or multiple fields using a delimiter, such as those used when accessing a flat file database. Using the email address as the key, you could store the name, work phone, and home phone in the value. The following is an example:

use AnyDBM_File;
$database=tie(%db, ‘NDBM_File’, “database”, O_RDWR|O_CREAT, 0666);
$db{‘mniles@itm.com’} = “Melissa Niles,555-5555,555-6666”;
untie %db;
undef $database;

As you can see, the key contains an email address, and the value contains the name and phone numbers—using a comma to delimit the record. With these few lines, the DBM database now contains a new entry.

Reading from a DBM Database

To retrieve information from a database, all you have to do is create a loop that reads the contents of the database and separates the value of each key at the colon. In your script, the following

while (($key,$value)= each(%db)) {

starts the loop that accomplishes this. Within the loop, the value of each key is split and assigned to the array part. When that is done, you can format the result in any manner you choose. In Listing 29.3, I have placed the name to be printed as part of a mailto: anchor, using each entry’s email address if it was entered.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.