wendallsan
04-28-2006, 09:04 PM
Hi all,
I'm very new to Perl, and am tasked with developing a CGI calendar application with a tight deadline. I am learning how to use the DBI and DBD::CSV modules to prototype the project and have come up with a couple questions that I haven't found answers to in my collection of O'reilly books. If you have any info on these, I would love to hear from you.
1st, how do I check to see if a table exists? My only idea here was to try to do a "select all" SQL query against the table and see if anything at all came back or if an error got thrown. I put together some code to run a query against the table 'nothing' (which doesn't exist). Here's some code:
# CHECK TO SEE IF A TABLE EXISTS (THIS SHOULD FAIL)
eval{
my $sth = $DBH->prepare("SELECT * FROM nothing;");
$sth->execute() or die ("Can't execute: " . $DBH->errorstr());
};
if ($@){
print $CGI->p("there was an error preparing to select from table 'nothing'");
}
else
{
print $CGI->p("no error in preparing to select from table 'nothing'");
}
When this is run, it executes without error (outputs the "no error in preparing to select from table 'nothing'" bit), but I also get the following message output to my page:
SQL ERROR: Bad table or column name 'nothing;' has chars not alphanumeric or underscore!
Is this SQL error not fatal, and that's why eval is not catching it?
Next question, if I try to create a table that already exists, how to I check for this and handle it? Code:
# CHECK TO SEE IF THE EVENTS TABLE EXISTS
eval{
$sth = $DBH->prepare(qq`
create table events
(
id int,
title char,
eventDescription char,
link1URL char,
link1Description char,
link2URL char,
link2Description char,
link3URL char,
link3Description char,
date char,
eventDate char,
time char,
locationDescription char,
locationDirections char,
locationLinkURL char,
locationLinkDescription char,
expireDate char,
archiveTo int
)
`);
$sth->execute() or die ("Cannot execute: ". $DBH->errorstr());
} ;
# IF THERE WAS AN ERROR IN THE EVAL ABOVE, HANDLE IT
if ($@)
{
print $CGI->p("There was an error creating: " . $@);
}
else
{
print $CGI->p("create operation completed successfully");
}
Again, this code executes fine (no error thrown, no error message sent to eval). The "create operation completed successfully" bit makes it's way to the browser. But I also get the message:
Execution ERROR: Cannot create table events: Already exists at /usr/local/lib/perl5/site_perl/5.8.5/sun4-solaris/DBD/File.pm line 564. called from /web5/DSB-TESTING/cgi-bin/dsbcalendar3.cgi at 55.
So once again, is this error non-fatal, and is that why the eval statement isn't handling it?
Thanks again for your time and if you have any answers, I would love to hear them.
thanks,
Dan Steeby
I'm very new to Perl, and am tasked with developing a CGI calendar application with a tight deadline. I am learning how to use the DBI and DBD::CSV modules to prototype the project and have come up with a couple questions that I haven't found answers to in my collection of O'reilly books. If you have any info on these, I would love to hear from you.
1st, how do I check to see if a table exists? My only idea here was to try to do a "select all" SQL query against the table and see if anything at all came back or if an error got thrown. I put together some code to run a query against the table 'nothing' (which doesn't exist). Here's some code:
# CHECK TO SEE IF A TABLE EXISTS (THIS SHOULD FAIL)
eval{
my $sth = $DBH->prepare("SELECT * FROM nothing;");
$sth->execute() or die ("Can't execute: " . $DBH->errorstr());
};
if ($@){
print $CGI->p("there was an error preparing to select from table 'nothing'");
}
else
{
print $CGI->p("no error in preparing to select from table 'nothing'");
}
When this is run, it executes without error (outputs the "no error in preparing to select from table 'nothing'" bit), but I also get the following message output to my page:
SQL ERROR: Bad table or column name 'nothing;' has chars not alphanumeric or underscore!
Is this SQL error not fatal, and that's why eval is not catching it?
Next question, if I try to create a table that already exists, how to I check for this and handle it? Code:
# CHECK TO SEE IF THE EVENTS TABLE EXISTS
eval{
$sth = $DBH->prepare(qq`
create table events
(
id int,
title char,
eventDescription char,
link1URL char,
link1Description char,
link2URL char,
link2Description char,
link3URL char,
link3Description char,
date char,
eventDate char,
time char,
locationDescription char,
locationDirections char,
locationLinkURL char,
locationLinkDescription char,
expireDate char,
archiveTo int
)
`);
$sth->execute() or die ("Cannot execute: ". $DBH->errorstr());
} ;
# IF THERE WAS AN ERROR IN THE EVAL ABOVE, HANDLE IT
if ($@)
{
print $CGI->p("There was an error creating: " . $@);
}
else
{
print $CGI->p("create operation completed successfully");
}
Again, this code executes fine (no error thrown, no error message sent to eval). The "create operation completed successfully" bit makes it's way to the browser. But I also get the message:
Execution ERROR: Cannot create table events: Already exists at /usr/local/lib/perl5/site_perl/5.8.5/sun4-solaris/DBD/File.pm line 564. called from /web5/DSB-TESTING/cgi-bin/dsbcalendar3.cgi at 55.
So once again, is this error non-fatal, and is that why the eval statement isn't handling it?
Thanks again for your time and if you have any answers, I would love to hear them.
thanks,
Dan Steeby