通过dbi使用perl连接mysql数据库的方法
通过使用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数据库,操作起来方便多了。
相关推荐
边城客栈学无止境 2020-07-05
Walter的学习笔记 2020-07-04
A宇 2020-06-14
边城客栈学无止境 2020-06-10
邓博学习笔记 2020-06-03
davidliu00 2020-05-26
ShiShuo 2020-05-16
Aggressivesnail 2020-05-10
ShiShuo 2020-04-26
hanxingwang00 2020-04-22
davidliu00 2020-03-06
ShiShuo 2020-03-06
ShiShuo 2020-03-05
Aggressivesnail 2020-02-28
aaLiweipeng 2020-02-01
amberom 2020-01-16
Walter的学习笔记 2020-01-06