Sep 22, 2008

perl use DBI connect to mysql

#!/usr/bin/perl
# MySQL installation verification.
# Open database 'sedb', then just exit.
# No errors should occur.

use strict;
use Socket;
#use DBI();
use DBI;
my $host="127.0.0.1";
my $db="sec";
# Connect to the database.
#my $dbh = DBI->connect("DBI:mysql:database=sec;host=127.0.0.1","root", "ashley",{'RaiseError' => 1});
print "access database--$db\n";
my $dbh = DBI->connect("DBI:mysql:$db;$host","root", "ashley",{'RaiseError' => 1});
print "drop table if exist cmdb\n";
$dbh->do("drop table if exists cmdb");
print "Create table cmdb\n";
$dbh->do("create table cmdb(auto int(5) unsigned not null)");
print "insert into data\n";
for(my $i=1;$i< 10;$i++)
{
$dbh->do("insert into cmdb(auto) values($i)");
}
print "select data from cmdb table\n";
my $sth=$dbh->prepare("select * from cmdb");
$sth->execute() or die $sth->errstr;
my $i=0;
while((my $row=$sth->fetchrow_arrayref))
{
print $row->[0],"\n";
}
exit;

0 意見: