通過使用DBI,用Perl可以很容易的連接到mysql數據庫:
代碼如下:
復制代碼 代碼如下:
#!/bin/perl
use DBI;
# Connect to target DB
my $dbh = DBI->connect("DBI:mysql:database=eygle;host=localhost","username","password", {'RaiseError' => 1});
# Insert one row
my $rows = $dbh->do("INSERT INTO test (id, name) VALUES (1, 'eygle')");
# query
my $sqr = $dbh->prepare("SELECT name FROM test");
$sqr->execute();
while(my $ref = $sqr->fetchrow_hashref()) {
print "$ref->{'name'}n";
}
$dbh->disconnect();
執行結果:
復制代碼 代碼如下:
[root@eygle ~]# perl test.cgi
eygle
以後很多統計數據可以直接通過Perl寫入Mysql數據庫,操作起來方便多了。