【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. mysql

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


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 sql

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: ubuntu

  • 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

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

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: app

  • 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】 ide

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. ui

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

【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.