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

 2023-09-05 阅读 33 评论 0

摘要:2019独角兽企业重金招聘Python工程师标准>>> 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 isDBD::mysql. On Deb

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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/perluse 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

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/1478.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息