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


Creating and Using Flat File Databases

Flat file databases are about the easiest databases you can create. To create a small ASCII text database, you need nothing more than a language with which to program and a text editor.

A flat file database consists mainly of lines of text, where each line is its own entry. If you have more than one field for each record, the records are usually separated by a delimiter. No special technique is used to index the database. Therefore, flat file databases usually are relatively small (about 1,000–2,000 records). The larger the database, the longer it takes to perform queries to it.

In a flat file database, each record is contained on its own line. How many fields are in each record is completely up to you, but they are usually separated by some sort of delimiter. Often commas or tabs are used to delimit a record. A record containing, for example, a name, email address, home phone, work phone, and country may look like this:

Melissa Niles,mniles@itm.com,555-5555,555-5566,USA

It doesn’t matter which delimiter you use, but you want to ensure that the delimiter (in this case, a comma) isn’t going to be used anywhere in each field. An address may contain commas and can present problems when retrieving information; for example:

Melissa Niles,555 E. Lane, Suite 600,Anywhere, WA.,99999

In the example, the street address would become two separate fields when it was meant to be one. You would either have to use a different delimiter, or you would have to have your script catch any use of the delimiter when receiving information to add to the database. The latter can be easily accomplished using Perl, with something like

$incoming{‘address’} =~ s/,/ /g;   #Replace any commas with a space

No matter what delimiter you use, it is always good practice to check the information coming in to ensure that it will comply with how you have your database structured.

You can build your database using any text editor or spreadsheet that will export a delimited text file (such as Microsoft Excel). After the database has been created, you will need to add, remove, and browse information within the database.

Adding Information

In a flat file database, nothing exists to check and ensure that the data you are receiving is the data that you are expecting. We already talked about checking to ensure that the new information doesn’t contain the delimiter you are using. When writing your script, you will also have to ensure that you are getting what you need. If you are expecting a name, then you might want to ensure that you are not receiving an email address. A database is only as good as the information contained in it. Incorrect information can create problems.

Adding information to a flat file database is quite easy, no matter which programming language you use. The basic idea is that you want to append a new record to the end of the database. Rarely do you find that you have to place a new record somewhere in the middle of the database. If you would like to display your listing in a numerical or alphabetical order, then you can easily sort the contents of the database any time you read from the database.

Using Perl, only a few lines of code are needed to add a new entry to a flat file database. If you need to store a name, address, city, and state, this could be easily accomplished with the following:

open(FILE, “>>database.txt”);
print FILE “$name|$address|$city|$state\n”;
close(FILE);

With Perl, you open the file using “>>”, which appends the information to whatever already exists. Next, using the FILE filehandle, you add information using a pipe (|) for the delimiter, and then you finish your entry by printing a newline.


NOTE:  Although Perl was used in the example, you can easily perform the same operation using any other programming language. The functions may work differently, but the programming concept is the same. Perl is used here because it is easy to read and to follow and because Perl is widely available for various platforms.

Of course, you will want to ensure that you were able to successfully open the database and that the program produced an error if you could not.

Removing Information

Although adding information is crucial to any database application, so is the capability to remove any old information from the database. No matter which programming language is used, removing information is relatively easy. The basic idea is to read the database into an array and then print the database back out to the file, excluding the records that you do not wish to keep.

Again, using Perl, it takes only a few lines of code to accomplish this task. If $remove_key contained the string “smith,” then Listing 29.1 would remove any instance of “smith.”

Listing 29.1 Search Through a Database and Remove a Record


open(FILE, “database.txt”); @lines = <FILE>; close(FILE);
open(FILE, “>database.txt”);
foreach $line (@lines) {
  print FILE “$line” unless $line =~ /$remove_key/i; }
close(FILE);

If you had a database containing information about people and needed to remove an entry with the name “John Smith,” then you will have to ensure that your script will only remove the “John Smith” that you no longer want. Often, you would find in this instance that more than one person has the same name. The following code stores the text “smith” in the variable $remove_key. Next, we write everything back into the database unless any record matches “smith”:

$remove_key = “smith”;
print FILE “$line” unless $name =~ /$remove_key/i;

This could remove any entry such as “John Smith,” “Jill Smithy,” and “Jennifer Wilsmithmire.” When deleting information from a database, you need to be as specific as possible so that you don’t remove any records that you want to keep.

Browsing the Database

Now that you have the capability to add and remove entries to the database, you need to create a script that will enable you to display the contents of your database.

If the database is expected to be small in size, the easiest thing to do is to simply display all entries, formatting each record as you deem fit. With Perl, you read the database into an array and then print each line, as shown in Listing 29.2. Each field is provided within the record by using a comma as the delimiter.


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.