4

I have one script in which I am connecting to database and trying to eexecute the sql query. My code is like this

use DBI;
$dbh = DBI->connect('Databasename', 'uid','pswd');
my $sth = $dbh->prepare ("select * from Tablename");
$sth->execute();
my @row_ary = $sth->hetshrow_array;
foreach $item (@row_ary)
{
print "$item\n";
}

when I am trying to execute this code I am getting the following error message

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

Can anybody please suggest me why I am getting this message. What is the configuration I need to do for executing sql queries.

Thanks

1
  • 1
    Seems to me that the error message is really very clear. You don't have 'dbi:<drivername>' at the start of your connection string. Perhaps you should try reading the documentation for DBI and the DBD that you are using. Commented Mar 28, 2013 at 9:25

1 Answer 1

8

DBI is flexible in that it supports multiple database servers (MySQL, Sybase, Oracle, etc). The first parameter you pass in to DBI->connect is a DSN (Data Source Name), not a database name. The format of the DSN is:

dbi:Driver:databasename

If your database is MySQL, you would use mysql for the driver:

dbi:mysql:databasename

Here's more info on DBI.

Sign up to request clarification or add additional context in comments.

1 Comment

You can find the name of the drivers by search for DBD on cpan. e.g. Postgresql is Pg because the module is DBD::Pg. You can find more info about connection to the specific database by looking in the appropriate DBD's docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.