# load module
use DBI;
# connect
#$dbh = DBI->connect("DBI:mysql:$database:$hostname:$port",$user, $password);
#详见http://www.cnblogs.com/homezzm/archive/2011/07/22/2113618.html
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost", "root", "xxxxx", {'RaiseError' => 1});
# execute INSERT query
my $rows = $dbh->do("INSERT INTO users (id, username, country) VALUES (4, 'jay', 'CZ')");
print "$rows row(s) affected ";
# execute SELECT query
my $sth = $dbh->prepare("SELECT id, sn FROM snlist limit 1");
$sth->execute();
# iterate through resultset
# print values
while(my $ref = $sth->fetchrow_hashref()) {
	print "id: $ref->{id} ";
	print "sn: $ref->{sn} ";
	print "---------- \n";
}
# clean up
$dbh->disconnect();

标签: none

添加新评论