Introduction
The FTD (Flat Text Databases) is a term used to explain a plain text
file used to create a database. A database is a
series of tables, each table containing a rows and columns. Applications
of
databases are endless, one
example case would be an airline who needs to track flights, pilots,
passengers, fuel trucks, etc. Using a database they
could easily manage to track all of this information and know at any
time where a plane is, if it will have fuel when it
lands, how many passengers flew using first class and how many flew
second class, and all other critical information.
There are many
different types and sizes of databases. Small databases that only contain
one table (series of
records) could be something small like a password database that links
passwords to usernames. Larger databases, like the
airplane sample above, would have multiple crosslinked tables and could
grow to be very large databases in comparison to a
table of passwords and usernames. Smaller databases generally use a
simple format like the FTD, larger databases will
use formats like SQL (Structured Query Language) which allow far more
features and expandability than the FTD.
Advantages and Disadvantages
FTD's are used in situations where a database server such as MySQL is
too resource expensive and would generate far
too much
load overhead. An FTD uses very little resources when used for small
databases and is very easy to maintain.
Using an FTD for a large database would become very slow however. An
FTD is only useful for small, simple,
databases. In order to search and FTD you would need to individually
check each record, in order to make this very fast and
efficient it would take a lot of work - this work is made simple in
larger databases achieved through hashed searches done in
such packages as MySQL.
Implementation
In this demonstration there will be an html page that a visitor can
enter the title of the book they want and will be
given information on that book. Users will also be able to browse all
books in the database. The main file, ftd.pl,
demonstrates this.
To add an entry
to our database, we simply append data in the format of "bookname:bookdata"
to the flat text database.
To remove an entry, we remove the entry from the database.
Example Project: Bookstore
Get This Sample
View Live
Sample
#!/usr/bin/perl
#
TD July 1999
# ftd.pl
#
$datafile
= "./ftddata";
&parse_form; # Standard TD form parsing routine
if ($ENV{'REQUEST_METHOD'} ne "POST") {
if ($ENV{'QUERY_STRING'} eq "") {
&print_form;
} else {
&print_all;
}
}
else {
if ($FORM{'ACTION'} eq "Search") {
&read_data;
&print_html_head ("$FORM{'txtBook'}");
print "<H1>$FORM{'txtBook'}</H1>\n";
print "$BOOKS{$FORM{'txtBook'}}\n";
&print_html_tail;
} elsif ($FORM{'ACTION'} eq "Add") {
&add_data;
} else {
&print_html_head;
print "Action unknown!\n";
&print_html_tail;
}
}
sub
add_data {
$a = "No Title Entered!";
$b = "No Text Entered!";
$a = $FORM{'txtTitle'} unless ($FORM{'txtTitle'} eq "");
$b = $FORM{'txtDesc'} unless ($FORM{'txtDesc'} eq "");
$bu = 1;
open (IN, "$datafile");
@data = <IN>;
close (IN);
foreach (@data) {
($xa,$xb) = split(/:/,$_);
if ($a eq $xa) { $Bu = 0; }
}
if ($Bu == 1) {
`echo \"$a:$b\" >> ./$datafile`;
&print_html_head("Added Book");
print "Added Book\n";
&print_html_tail;
} else {
&print_html_head("Could not add book");
print "Could not add data, book by that name already exists!\n";
&print_html_tail;
}
}
sub print_all {
&print_html_head ("Show All");
open (IN, "$datafile");
@data = <IN>;
close (IN);
foreach (@data) {
($a,$b) = split(/:/,$_);
print "<P><H3>$a</H3>\n$b</P>\n\n";
}
&print_html_tail;
}
sub
read_data {
open (IN, "$datafile");
@data = <IN>;
close (IN);
foreach (@data) {
($a,$b) = split(/:/,$_);
$BOOKS{$a} = $b;
}
}
sub
parse_form {
read(STDIN, $buffer, $ENV{CONTENT_LENGTH});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $val) = split(/=/, $pair);
$val =~ tr/+/ /;
$val =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $val;
}
}
sub
print_form {
&print_html_head("FTD
Sample");
print <<EOF;
<H1>Look
up a book</H1>
<FORM
METHOD=POST ACTION="./ftd.pl">
Book: <SELECT NAME="txtBook">
EOF
open (IN, "$datafile");
@data = <IN>;
close (IN);
foreach (@data) {
($a,$b) = split(/:/,$_);
print "<OPTION>$a</OPTION>\n";
}
print <<EOF;
</SELECT><BR>
<INPUT TYPE="SUBMIT" NAME="ACTION" VALUE="Search">
</FORM>
<BR><BR><H1>Add a book</H1>
<FORM METHOD=POST ACTION="./ftd.pl">
Title: <INPUT TYPE=TEXT NAME="txtTitle"><BR>
Description: <BR><TEXTAREA NAME="txtDesc" COLS=35
ROWS=8></TEXTAREA><BR>
<INPUT TYPE="SUBMIT" VALUE="Add" NAME="ACTION">
</FORM>
<BR>
Or you can <A HREF="./ftd.pl?all">browse all books</A>.
EOF
&print_html_tail;
}
sub print_html_head {
print <<EOF;
Content-type: text/html
<HTML>
<HEAD><TITLE>$_[0]</TITLE></HEAD>
<BODY>
EOF
}
sub
print_html_tail {
print <<EOF;
</BODY>
</HTML>
EOF
}
|