|
Linux's Witness Ministry
|
#!/usr/bin/perl -w
#cxe- Created by Chris Edwards
#cxe- Tue Mar 16 12:39:05 PST 2004
#cxe- This program is a simple test of how one might go about accessing a
#cxe- database managed by MySQL using Perl and, specifically, the DBI API.
#cxe- Cross your fingers that it's actually installed.
use DBI;
#cxe- The DBI can understand many databases depending on what drivers are present.
my @drivers = DBI->available_drivers;
#print "Available drivers:@drivers\n";
my $driver_OK=0;
foreach $driver(@drivers) { if ($driver=~/mysql/i) {$driver_OK=1;} }
if ($driver_OK eq 1) { print "MySQL Driver is available here.\n"; }
else {print "Oh! MySQL driver is missing."; die;}
#cxe- Information about the exisiting database.
$data_source="DBI:mysql:geometry:localhost";
$username="chris";
$password="CnffJbeq"; $password=~tr/a-zA-Z/n-za-mN-ZA-M/;
#cxe- CONNECT to the database server.
my $db=DBI->connect($data_source, $username, $password) or die;
print "$data_source is connected as:$db\n";
#cxe- PREPARE a statement for future use.
my $statement_handle=$db->prepare("SELECT * FROM pointlist");
#cxe- EXECUTE the statement on the database.
$rows_affected = $statement_handle->execute();
print "Number of rows affected:$rows_affected\n";
#cxe- FETCH the results by row to view them.
while (@results=$statement_handle->fetchrow_array() ) {
print "Here are some results:@results\n"; }
#cxe- FINISH to flush any operations.
$statement_handle->finish();
#cxe- DISCONNECT is also good housekeeping.
$db->disconnect() or die;
print "$data_source is disconnected.\n";
#cxe- This doesn't seem to work. I wonder why.
#my @sources = DBI->data_sources('mysql');
#print "Sources:@sources\n";
#cxe- This does work.
@sources = $db->func(_ListDBs);
print "Sources:@sources\n";
my @tables = $db->tables();
print "Tables:@tables\n";
Typical output from this program might be:
MySQL Driver is available here. DBI:mysql:geometry:localhost is connected as:îDBI::db=HASH(0x81d5284) Number of rows affected:6 Here are some results:1 0 0 0 Here are some results:2 68 0 0 Here are some results:3 103.5 0 0 Here are some results:4 110 0 0 Here are some results:5 110 165 0 Here are some results:6 165 0 0 DBI:mysql:geometry:localhost is disconnected. Sources:geometry mysql test Tables:attributetypelist pointlist
| Return to Program Examples |
| This page was created with only free, open-source, publicly licensed software. This page was designed to be viewed with any browser on any system. |
| Chris X. Edwards ~ January 2003 |