Gossamer Forum
Home : General : Perl Programming :

Database engine

Quote Reply
Database engine
Have any one got a database engine? I am trying to write a simple engine, that would take a 'database.txt' file and write the data to HTML file:

Code:

#!/usr/bin/perl

$html_file = "/usr/find/public_html/02/index.htm";
$database = "/usr/find/public_html/02/database.txt";

open(HTML, "$html_file");
open(DATABASE, "$database");
@datainfo = <DATABASE>;

foreach $database (@datainfo);
{
chop ($database);
($name, $tel) = split(/\|/, $database);
print HTML "<html><body>";
print HTML "$name, $tel <br>";
print HTML "</body></html>";
}
close(HTML);
close(DATABASE);

As I said, it's simple engine, but for some unknown reason this simple engine gives me '500' Frown

Any suggestions? Thank you in advance.


Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: Database engine In reply to
Come on guys, give me a hand here! Smile
I've tested, it prints, but for some reason it does not read the database:

Code:

#!/usr/bin/perl
#

open (HTML,">index.htm");

print HTML"<HTML><HEAD><TITLE>TEST</TITLE></HEAD><body>";

open (DATABASE,">database.txt");
@database_in=<DATABASE>;
foreach $_ (@database_in)
{
($name,$tel)=split(/\|/);
}

print HTML"$name <br> $tel<p>";
print HTML"</body></html>";

close (HTML);

print "Content-type: text/plain\n\n";
print "Done!";


What the hell am I doing wrong here? Smile


Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: Database engine In reply to
You have opened the database file for output. Changing:

open (DATABASE,">database.txt");

to:

open (DATABASE,"<database.txt");

should solve the problem.

Dan Smile
Quote Reply
Re: Database engine In reply to
Thenks for the reply, I've already found this misstake Smile plus I rewrote the all of the code again.
See it working at: http://find.virtualave.net/00/database.cgi

See the output: http://find.virtualave.net/00/

Pretty cooll? huh Smile

Regards,

Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: Database engine In reply to
Well, your script is simple enough for me to understand, and thats what I've been looking for, a very basic database script like that so I could build and learn.

I was trying to write a Viewer ( to read in the database and display the info ) and also to do an addition script.

I'm especially interested in an addition script. Could someone whip up a simple addition to add to a flat, pipe delimited db?
From form input, naturally Smile

Thanx in advance!

------------------
The Crowe crowe@darkspiral.com
http://www.darkspiral.com
Quote Reply
Re: Database engine In reply to
If you want to take a look at the code, then here it is:
http://find.virtualave.net/00/database.txt
How simple can it be? Smile

Regards,

Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: Database engine In reply to
What about a form parsing routine that allows you to add to the database via the web.

Your datbase script works great for info thats already in the database, but do you have a script that allows you to add to it?

I was working on one, but I can't seem to get the script to parse the info from the form. If I manually set the variables I want to output to the database.txt it works, but one its form input, nada Smile



------------------
The Crowe crowe@darkspiral.com
http://www.darkspiral.com
Quote Reply
Re: Database engine In reply to
Might want to have a look at:

http://webreview.com/wr/pub/Perl

which has some beginner articles for writing a simple database application..

Cheers,

Alex
Quote Reply
Re: Database engine In reply to
I don't think I know how to read the data from a form, but I'm sure that it's not so difficult.

We need something like:

Code:

$in{'fields0', 'fields1', 'fields2', 'fields3'};
open(TXT, ">database.txt");
print TXT "fields0|fields1|fields2|fields3\n";
close TXT;

But I'm not really sure about that $in{}
It's a good idea, for a beginner to write something like 'simple database'.

Any tips? Smile


Regards,

Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: Database engine In reply to
I would avoid using:

Code:
#Includes the library for parsing the form.
require "cgi-lib.pl";
#Calls the sub routine from cgi-lib.pl
&ReadParse;

but instead use:

use CGI;
$in = new CGI;

$usr = $in->param('username');
$eml = $in->param('email');
$url = $in->param('url');

instead. cgi-lib.pl is a pretty oudated module, and I'm not even sure if it's being developed any more.

Cheers,

Alex
Quote Reply
Re: Database engine In reply to
I did it Wink
I used cgi-lib.pl but I did it Wink

Thanx Alex for that url, its an awesome resource. Exactly the kind if thing I needed.

Here is what I did.

Code:
#!/usr/bin/perl
#This script adds to the database file
#


#Includes the library for parsing the form.
require "cgi-lib.pl";

#Calls the sub routine from cgi-lib.pl
&ReadParse;

#assigns the input from the form to variables
#for use with this script.
$usr=$in{'username'};
$eml=$in{'email'};
$url=$in{'url'};

#Formats the variables for printing to the database.
$joinline = join '|', $usr,$eml,$url;


#Writes the joined line to the databse.
open (DATABASE,">>database.txt");
print DATABASE "$joinline\n";
close(DATABASE);


#tells you your done as in Pasha's script Smile
print "Content-type: text/plain\n\n";
print "Done!";

I will be adding a search/replace/delete/modify over the
next few days. As I do, I'll post them here Wink

------------------
The Crowe crowe@darkspiral.com
http://www.darkspiral.com
Quote Reply
Re: Database engine In reply to
Hey Alex, I didn't know that! Smile
I was also trying to do this job using 'cgi_lib.pl'.

Currenly I am trying to make my simple database searchable. I already can search it, but I am having problems to brake the reluts on multiple pages when I get too many results.

But again, there's nothing impossible Smile


Regards,

Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: Database engine In reply to
Here's some dirty code, that I use to search the database:

Code:

#!/usr/bin/perl

require "cgi-lib.pl";

$database='/home/find/public_html/cgi-bin/00.db';
if (&ReadParse())
{
print &PrintHeader;
open (DATABASE,$database) &#0124; &#0124; &CgiDie("Search Script encountered an error!","Can't open database file ($database): $!");
@database_in=<DATABASE>;
foreach $_ (@database_in)
{
($dir,$filename,$size,$keyword,$description)=split(/\|/);
$temp="$keyword";
if ($temp =~ /$in{'searchkeys'}/i)
{
$output=$output."<tr><td><a href=\"$dir\">$dir</a></td><td><A HREF=\"$dir$filename\">$filename</A></td><td>$size<BR></td><td>$description</td></tr>\n";
}
}
print "<html><head><title>Search Results</title></head>\n";
print "<body><!--VirtualAvenueBanner--><br>\n";
print "<!--#exec cgi=\"/cgi-bin/header.cgi\" -->\n";
print "<b>Search for <i>$in{'searchkeys'}</i> returned:</b><P>\n\n";
if ($output eq "")
{
print "I am sorry, but right now <b>$in{'searchkeys'}</b> is not available.\n";
}
else
{
print "<table border width=\"100%\"><tr><td><b>Location</b></td><td><b>File</b></td><td><b>S ize</b></td><td><b>Description</b></td></tr>";
print $output;
print "</table>";
}
print "<!--#exec cgi=\"/cgi-bin/footer.cgi\" --></body></html>";
}
else
{
print &PrintHeader;
print "<html><head><title>Find! File Search</title></head>\n";
print "<body><!--#exec cgi=\"/cgi-bin/header.cgi\" -->\n";
print "<h1>Error!</h1> Please report this error to the webmaster!\n";
print "<!--#exec cgi=\"/cgi-bin/footer.cgi\" --></body></html>\n";
}

But again, I use 'cgi-lib.pl'.

"... can't live with 'em, can't kill 'em" Wink


Regards,

Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: Database engine In reply to
Pasha,

Alex posted http://webreview.com/wr/pub/Perl and it really is awesome. That page has a database script broken down int o every lesson you'd need. Starting with how to open a file. How to post to the file using a form using cgi-lib.pl, then he moves on to CGI.pm , which Alex posted about using and it works great!

The url also has a Search , Modify, Delete and Add routine all using CGI.pm with line by line instructions on what each peice of code does. I stopped using cgi-lib.pl and am using the CGI.pm thanx to Alex and his link. That link is EXACTLY what I've been looking for.

Alex: If you have any more gold mines like that, please post the urls. I have "Programming Perl" and "Perl Cookbook" by O'Reilly and they are both great, but If you have some web sources that have code like web reference, I'd love to take a look at them.



------------------
The Crowe crowe@darkspiral.com
http://www.darkspiral.com
Quote Reply
Re: Database engine In reply to
You might find http://wdvl.internet.com/Authoring/DB/Intro/ interesting too..

Cheers,

Alex