You will also see in this tutorial the basics of using DBI to perform queries, and display the results. Once you learn the basics you can use Perl to program the other SQL commands we saw briefly in our class on MySQL, and you should have learned in more details in the DB course. Please note that we have on reserve at the Library the book Programming the Perl DBI, the authoritative reference to DBI.
The installation in Windows relies in CPAN (Comprehensive Perl Archive Network) archives and PPM (Programmer's Package Manager) from ActiveState.
Once more at the PPM prompt type search DBI and you will see a variety of modules you can install, related to DBI. Install the main DBI module as shown here. The following script let you test if your installation is working fine:
#!/usr/bin/perl -w
use strict;
use DB_File;
my %database;
tie %database, 'DB_File', "createdb.dat" or die "Can't initialize database: $!\n";
untie %database;
exit;
If all went well a file createdb.dat will be created by the script.
Once more, at the ppm prompt type search DBD and see the variety of modules available. You are only interested in installing the driver for Mysql and at the ppm prompt type install DBD-Mysql and this is it.
#!/usr/bin/perl -w
use DBI;
#definition of variables
$db="MYTEST";
$host="localhost";
$user="root";
$password="rootpass"; # the root password
#connect to MySQL database
my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host",
$user,
$password)
or die "Can't connect to database: $DBI::errstr\n";
#disconnect from database
$dbh->disconnect or warn "Disconnection error: $DBI::errstr\n";
exit;
Lets see some basic concepts and a series of examples to illustrate them.
#!/usr/bin/perl -w use DBI; #definition of variables $db="MYTEST"; $host="localhost"; $user="root"; $password="rootpass"; #connect to MySQL database my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host", $user, $password) or die "Can't connect to database: $DBI::errstr\n"; #prepare the query my $sth = $dbh->prepare( " SELECT * FROM people"); #execute the query $sth->execute( ); ## Retrieve the results of a row of data and print print "\tQuery results:\n================================================\n"; while ( my @row = $sth->fetchrow_array( ) ) { print "@row\n"; } warn "Problem in retrieving results", $sth->errstr( ), "\n" if $sth->err( ); exit;Once you run the script you will see the following results:
[root@alaptop dbi]# ./queryall Query results: ================================================ 347 Al Bento 1420 N. Charles St, Baltimore, MD 21201 458 John Morris 1420 N. Charles St, Baltimore, MD 21201 569 Milt Jenkins 1420 N. Charles St, Baltimore, MD 21201
#!/usr/bin/perl -w
use DBI;
#definition of variables
$db="MYTEST";
$host="localhost";
$user="root";
$password="rootpass";
#connect to MySQL database
my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host",
$user,
$password)
or die "Can't connect to database: $DBI::errstr\n";
#prepare the query
my $sth = $dbh->prepare( "
SELECT name,address
FROM people");
#execute the query
$sth->execute( );
## Retrieve the results of a row of data and print
print "\tQuery results:\n================================================\n";
while ( ($name,$address) = $sth->fetchrow_array( ) ) {
print "Name: $name, Address: $address\n";
}
warn "Problem in retrieving results", $sth->errstr( ), "\n"
if $sth->err( );
exit;
Once you run the script you will see the following results:
[root@alaptop dbi]# ./querynames Query results: ================================================ Name: Al Bento, Address: 1420 N. Charles St, Baltimore, MD 21201 Name: John Morris, Address: 1420 N. Charles St, Baltimore, MD 21201 Name: Milt Jenkins, Address: 1420 N. Charles St, Baltimore, MD 21201
#!/usr/bin/perl -w use DBI; #definition of variables $db="MYTEST"; $host="localhost"; $user="root"; $password="rootpass"; #connect to MySQL database my $dbh = DBI->connect ("DBI:mysql:database=$db:host=$host", $user, $password) or die "Can't connect to database: $DBI::errstr\n"; #prepare the query my $sql = "SELECT * FROM people"; my $sth = $dbh->prepare( $sql); #execute the query $sth->execute( ); ## Retrieve the results of a row of data and print my ( $userid,$name,$address); $sth->bind_columns ( undef,\$userid, \$name, \$address ); print "\tQuery results:\n================================================\n"; while ( $sth->fetch( ) ) { print "Userid is $userid, the name is $name, and the address is $address\n"; } $sth->finish( ); $dbh->disconnect( ); exit;Once you run the script you will see the following results:
[root@alaptop dbi]# ./queryallfancy Query results: ================================================ Userid is 347, the name is Al Bento, and the address is 1420 N. Charles St, Baltimore, MD 21201 Userid is 458, the name is John Morris, and the address is 1420 N. Charles St, Baltimore, MD 21201 Userid is 569, the name is Milt Jenkins, and the address is 1420 N. Charles St, Baltimore, MD 21201
You are not limited to queries when using DBI. You can create and drop tables, insert, update, etc.
This page is maintained by Al Bento who can be reached at abento@ubmail.ubalt.edu. This page was last updated on October 28, 2002. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.