【perl】Connect to a MySQL using the Perl DBI module

The DBI module itself does not have the ability to communicate with any specific DBMS: for that it is necessary to install the appropriate back-end module, which in the case of MySQL is DBD::mysql.

On Debian-based systems (including Ubuntu) the package that provides this module is libdbd-mysql-perl:


apt-get install libdbd-mysql-perl
and on Redhat-based systems it is perl-DBD-mysql :


yum install perl-DBD-mysql
The connection to the database is opened using the function connect . It returns a connection handle, which is needed when making subsequent calls to the DBI module:

my $dbh = DBI->connect('dbi:mysql:database=finance;host=db.example.com','user','xyzzy',{AutoCommit=>1,RaiseError=>1,PrintError=>0});


#!/usr/bin/perl

use DBI;

my $dbh = DBI->connect('dbi:mysql:dbname=finance;host=db.example.com','user','xyzzy',{AutoCommit=>1,RaiseError=>1,PrintError=>0});
print "2+2=",$dbh->selectrow_array("SELECT 2+2"),"\n";

【error】Can't locate DBI.pm

Can't locate DBI.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at ./example.pl line 3.
BEGIN failed--compilation aborted at ./example.pl line 3.

could indicate that:

  • the DBI module (and therefore probably the DBD module) has not been installed, or
  • the module is not on the include path (@INC).

【Error】Can't locate DBD/mysql.pm

[windows]ppm install DBD::mysql (重新安装,要先删除已安装的) 

install_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at (eval 3) line 3.
Perhaps the DBD::mysql perl module hasn't been fully installed,
or perhaps the capitalisation of 'mysql' isn't right.
Available drivers: DBM, ExampleP, File, Gofer, Proxy, Sponge.
 at ./example.pl line 5

indicates that the DBI module has been successfully loaded, and that some DBD modules are available (listed on the penultimate line), but that the specific DBD module needed to access MySQL cannot be found. Possible explanations are that:

  • the required DBD module has not been installed (see above),
  • the module is not on the include path (see above, but unlikely if the DBI module was loaded), or
  • the name of the data source passed to DBI::connect was incorrect.

【Error】

install_driver(mysql) failed: Global symbol "$VERSION" requires explicit package name at /mtkoss/perl/5.14.2-ubuntu-12.04/x86_64/lib/site_perl/5.14.2/x86_64-linux-thread-multi/DBD/mysql.pm line 6.

Compilation failed in require at (eval 311) line 3.

【error】

DBD::mysql initialisation failed: Can't locate object method "driver" via package "DBD::mysql" at /mtkoss/perl/5.14.2-ubuntu-12.04/x86_64/lib/site_perl/5.14.2/x86_64-linux-thread-multi/DBI.pm line 821.
Perhaps the capitalisation of DBD 'mysql' isn't right. at /proj/srv_kereadmin/keyword_script/module/keyword_mysql.pm line 44.



转载于:https://my.oschina.net/u/347414/blog/416250

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值