Search code examples
mysqlperldbidbd

Why do I get an error when I try to use Perl's DBD::mysql?


I am trying to write data to a MySQL database using Perl. However, when I run my script I get the error below:

Can't locate loadable object for module DBD::mysql in @INC (@INC contains: C:/Perl/site/lib C:/Perl/lib .) at LargeLDAPSearch.pl line 10.

I do have these "use" statements and following code. This is just a small excerpt, because the script worked before I tried a DB connection:

use DBD::mysql;
use strict;
use warnings;
my $query_handle;

my ($platform,$database,$host,$port,$db_user,$pw) = ("mysql","results","localhost","3306","root","mysql123");

my $dsn = "dbi:$platform:$database:$host:$port";

my $connect = DBI->connect($dsn,$db_user,$pw) || die "Could not connect to database";

my $query_insert = "INSERT INTO " . $dbname . "(uid,status,lstpwdset,reset) VALUES (" . $strSAMA . "," . $strAcctControl . "," . $pwLS . "," . $reset . ")";

$query_handle = $connect->prepare($query_insert);
$query_handle->execute();

I have gone into my Perl folder on my local, and searched the lib file directory. In the /lib/ subfolder, I have two folders, a DBI and a DBD subfolder, and I have MySQL subfolder in DBD and also a DBD subfolder with a MySQL subfolder under DBI. /lib/DBD/mysql/ & `/lib/DBI/DBD/mysql/``

This could be the error, fodlers in two spots. I went to the CPAN website, and have tried the manual isntall steps, and i receive an install error of DBI regarding SQLLite.


Solution

  • Install DBI and DBD::mysql using

    C:\> ppm install DBI
    C:\> ppm install DBD::mysql

    You seem to be using ActivePerl, so use the facilities it provides you.

    DBD::mysql does come with documentation:

    From perl you activate the interface with the statement

    use DBI;
    

    After that you can connect to multiple MySQL database servers and send multiple queries to any of them via a simple object oriented interface. Two types of objects are available: database handles and statement handles. Perl returns a database handle to the connect method like so:

    my $dbh = DBI->connect("DBI:mysql:database=$db;host=$host",
                      $user, $password, {RaiseError => 1});