Question

I am trying to connect to access 2007 database using perl. Below is the code that I tried executing

use DBI;

$path='T:\a\abc.accdb';
$datasource = "driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=$path";
$dbh = DBI->connect("dbi:ODBC:$datasource", '', '') || die "Error connecting: $!";
$query="select name from receiver";
$exe=$dbh->prepare($query) or die "cannot prepare";
$exe->execute or die "cannot execute";

$a=$exe->fetchall_arrayref();
foreach $aa(@{$a})
{
print @$aa;
}  

but the execution fails with the below error

DBI connect('driver={Microsoft Access Driver(*.mdb,*.accdb)};DBQ=T:\a\abc.accdb','',...)failed: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at test.pl line 5. Error connecting: at test.pl line 5.

can someone please guide me how to resolve this issue.

Was it helpful?

Solution

Your driver name is missing a space. ODBC is very fussy about that. Try

$datasource = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=$path";

Also, NAME is a reserved word in Access SQL so you may need to use

$query="select [name] from receiver";

OTHER TIPS

My experience with DBI:ODBC is somewhat dated but I would suggest the following:

  • Check the driver names in the ODBC datasource setup and make sure that you have the spelling right. (spaces and all) - Maybe set up a DSN and use that instead - more reliable
  • Check that the file exists (I don't think that is the problem from the error message)
  • Capitalization on the Driver/Dbq (not likely but we are dealing with MS)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top