Greetings!
I have been maintaining a Perl/CGI application that tries to do
database stuff without using a database, instead working with
cumbersome comma-delimited text files. I would dearly love to convert
it to use a real live database. The application is hosted on a Unix
box, but I am developing it under WinXP Pro. MySQL is available in
both places. Can somebody point me to a step-by-step guide for using
DBI and MySQL under WinXP for somebody who has never done it before?
Thank you very much!
Rob Richardson
Here's a snippet of code I used for a Sybase database. The way I used it
(possibly not the most efficient, but it's what I could find at the time)
required that you knew at least how to form your own queries. If you
can't manage that, best do some more reading.
This should be enough to get you started. Remember, 'perldoc' is your
friend!
And to preface, printError() was a function I wrote to let the script
die nicely (since this was used in a CGI fashion). Anyway, here goes:
-----------------------------------------------------
$dbname = "some.server.somewhere.com";
$dbuser = "AUser";
$dbpass = "user";
$dbdriv = "Sybase";
$dbtable= "messy_table";
use DBI;
<snip>
sub getDB_FID {
my @gFID, @rowData;
my $dbh, $query, $data, $i;
$dbh = DBI->connect("dbi:Sybase:server=$dbname;database=MES", $dbuser, $dbpass) or printError("Can't access DB");
$query = "select distinct Fermentor_ID from $dbtable;";
$data = $dbh->prepare($query) or printError("Can't execute prepare statement!");
$data->execute or printError("Can't execute statement!<BR>\n ".$data->errstr);
$i=0;
while (@rowData = $data->fetchrow_array) {
push(@gFID, $rowData[0];
}
return @gFID;
}
........
Basically, here's what's going on:
DBI->connect(params) Creates a connection to the database at the server
$query Text string for DB query
$dbh->prepare($query) Module loads query
$dbh->execute Module sends query to DB
$data->fetchrow_array One array of data per result returned
(IOW, call this until you've gotten all results)
For this particular function, I only cared
about the first column of data
+-----------------------------------------------------------------------+
Trey Waters (e-mail address removed)
Experience is the worst teacher.
It always gives the test first and the instruction afterward.
+-----------------------------------------------------------------------+