connect to MySQL from Perl using DBI

Hi,
I want to connect perl with the mysql to select and so on but the connection don't work

code

#!/usr/bin/perl 
BEGIN {
# PERL MODULES WE WILL BE USING
use DBI;
$dbh = DBI->connect('DBI:mysql:C:\Program Files\MySQL\MySQL Server 5.0\data\db1','','pass') or die $DBI::errstr;}
 
# (insert query examples here...)
$sth = $dbh->prepare('SELECT gene_name FROM protein_names );
$sth->execute();
$result = $sth->fetchrow_hashref();
print "Value returned: $result->{description}\n";
end{
$dbh->disconnect();
}

I want from this code to get all gene_name in the table and put them in a hash table.

Note:

 
$dbh = DBI->connect('DBI:mysql:C:\Program Files\MySQL\MySQL Server 5.0\data\db1','','pass') or die $DBI::errstr;

here i defined the path to the db file and pass is the password i set to the mysql when i treat with and i didn't use a user name for this database

Thanks in advance

Hi,

try to change the path you're using to the database with the actual database name in mysql.

Change:

$dbh = DBI->connect('DBI:mysql:C:\Program Files\MySQL\MySQL Server 5.0\data\db1','','pass') or die $DBI::errstr;}

to:

$dbh = DBI->connect('DBI:mysql:<db name>','','pass') or die $DBI::errstr;}

I'm new to Perl but one problem that i can see with your syntax is that this part, 'DBI:mysql:C:\Program Files ...', could make perl believe that the name of your database is 'C' since it uses ':' as a seperator in the DBI connectionstring.