How do I set up DBI?

R

Rob Richardson

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
 
A

A. Sinan Unur

(e-mail address removed) (Rob Richardson) wrote in
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?

Fire up a command line: Start -> Run, type cmd, press "Enter"
Type ppm and press enter at the command prompt
Type s DBI and press enter at the ppm prompt

You will get a list of packages with DBI in them. One or more of them will
look like this:

69. DBI [1.43] Database independent interface for Perl

Find the one with the most recent version number, then type i 69 and press
enter. (Replace 69 with the appropriate number in the list you get). Then,
using a similar procedure, find the DBD's you want and install them. Please
do not respond to this before you read the relevant ppm documentation.
(Hint: It is in your computer. It seems like you did not even look in the
ActiveState Perl folder in your start menu before posting.

Sinan.
 
T

Trey Waters

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.
+-----------------------------------------------------------------------+
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top