#!/bin/perl
# load module
use DBI;
# connect
my $dbh = DBI->connect("DBI:mysql:database=db2;host=localhost", "joe", "guessme", {'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 username, country FROM users");
$sth->execute();
# iterate through resultset
# print values
while(my $ref = $sth->fetchrow_hashref()) {
print "User: $ref-> ";
print "Country: $ref-> ";
print "---------- ";
}
# clean up
$dbh->disconnect();
本文转自yifangyou 51CTO博客,原文链接:http://blog.51cto.com/yifangyou/603505,如需转载请自行联系原作者