|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
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 DatabasesMost 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, youll 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:
If you cant find a particular DBM database on your system, search the Web for DBM databases. Writing to a DBM DatabasePerl 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 isnt 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 numbersusing a comma to delimit the record. With these few lines, the DBM database now contains a new entry. Reading from a DBM DatabaseTo 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 entrys email address if it was entered.
|
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. |