Jackie Kim
Jackie Kim

Reputation: 11

Error to use DBI::mySQL

I started learning coding two weeks ago without having any IT background so far. I have been stuck the problem mentioned in the title. I wrote the code as

#!/usr/bin/perl

use 5.010;
use strict;
use utf8;
use DBI;

my $dsn = 'DBI:mysql:database=DATABASENAME;host=HOSTNAME';
my $user = "USERNAME";
my $password = "PASSWORD";
my $dbh = DBI->connect($dsn,$user, $password, or die "cannot connect to MySQL: $DBI::errstr");

my $id=1;
my $query = "test";
my $email = "test";
my $sql_insert = " INSERT INTO TABLENAME (id,query, email) values ($id, $query,$email) ";
my $insert = $dbh ->prepare ($sql_insert);
$insert ->execute;
$insert -> finish;
$dbh -> disconnect;

However, I got the error message from command line.

Can't connect to data source 'PASSWORD' because I can't work out what driver to use (it doesn't seem t o contain a 'dbi:driver:' prefix and the DBI_DRIVER env var is not set) at DBItest.pl line 13.

I deleted some comments from the code so the sentence at the end mentioning line would be different.

Do you have any good idea to fix this problem? I'm trying to use mySQL on Freehostia.

Upvotes: 0

Views: 798

Answers (1)

AKHolland
AKHolland

Reputation: 4445

At a minimum, you have a typo here:

my $dbh = DBI->connect($dsn,$user, $password, or die "cannot connect to MySQL: $DBI::errstr");
                                            ^ HERE

It should be

my $dbh = DBI->connect($dsn,$user, $password) or die "cannot connect to MySQL: $DBI::errstr";

Also, always use warnings. You would have gotten a warning like so, pointing you right at the problem:

Useless use of private variable in void context at yourFile.pl line 11

Upvotes: 3

Related Questions