Home >Database >Mysql Tutorial >使用Perl连接Mysql数据库_MySQL

使用Perl连接Mysql数据库_MySQL

WBOY
WBOYOriginal
2016-06-01 13:52:411053browse

网站后台数据库转为Mysql,现在使用Perl连接数据库就方便多了。

通过DBI,Perl可以很容易的连接到数据库:

#!/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'}";
}

$dbh->disconnect();

执行结果:

[root@eygle ~]# perl test.cgi
eygle

以后很多统计数据可以直接通过Perl写入Mysql数据库,操作起来方便多了。
看来这次迁移是值得的:)

-The End-

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn