问题
I am tring to retrive data from a table in access. The code is
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $DBFile = qw(C:test\INSTRUCTIONS.mdb );
my $dbh = DBI->connect("dbi:ODBC:driver=microsoft access driver (*.mdb);dbq=$DBFile",'','') or die("cannot connect to DB");
my $SQLquery = "select * FROM IndemDate";
$dbh->Execute($SQLquery);
This is the error i recieve
DBI connect('driver=microsoft access driver (*.mdb);dbq=C:test\INSTRUCTIONS.mdb','',...) failed: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (SQL-IM002) at C:/Test/connectaccess.pl line 9.
cannot connect to DB at C:/Test/connectaccess.pl line 9.
can someone help me rectify my error. Is there any driver I've missed to install.
回答1:
As indicated in the comments to the question, the Perl script was originally running as a 64-bit process. Therefore the older Microsoft "Jet" ODBC driver
Microsoft Access Driver (*.mdb)
was not available. Only 32-bit processes can use the older Jet driver. If you must run your Perl script as a 64-bit process then you will have to download and install the 64-bit version of the newer Microsoft Access Database Engine (a.k.a. "ACE") from here and then use the driver name
Microsoft Access Driver (*.mdb, *.accdb)
Or, you could run your Perl script as a 32-bit process and use the older Jet driver.
Edit re: comment
Since you have 32-bit Access 2007 installed you already have a 32-bit version of the ACE driver on the machine. That effectively eliminates the option to install the 64-bit version of the ACE driver because the 64-bit ACE installer will abort if it finds 32-bit Office components on the machine. (There is apparently a way to force the second install but it is reported to break Office in some circumstances and is definitely not supported.)
So your options can be revised to:
Run the Perl script as a 32-bit process, or
Remove 32-bit Access 2007 and install a 64-bit version of Access, then run the Perl script as a 64-bit process using the 64-bit ACE driver.
回答2:
Try to use Jet
:
#!/usr/bin/perl -w
use strict;
use warnings;
use Win32::OLE;
my $DBFile = qw( C:\test\INSTRUCTIONS.mdb );
# choose the appropriate versione of Jet for your system
my $Jet = Win32::OLE->CreateObject('DAO.DBEngine.36') or die "Can't create Jet database engine.";
my $DB = $Jet->OpenDatabase( $DBFile );
my $SQLquery = "select * FROM IndemDate";
$DB->Execute($SQLquery, 128); #128=DBFailOnError
[source: here, also have a look here]
回答3:
For a script running as a 32-bit process (in my case, 32-bit Strawberry Perl), the following code works for me:
#!perl
use strict;
use warnings;
use DBI;
print 'bits: ' . (8 * (length pack 'P', -1)) . "\n\n";
my $DBFile = q(C:\Users\Public\mdbTest.mdb);
my $dbh = DBI->connect("dbi:ODBC:Driver={Microsoft Access Driver (*.mdb)};DBQ=$DBFile",'','') or die("cannot connect to DB");
my $SQLquery = "SELECT * FROM Members";
my $sth = $dbh->prepare($SQLquery);
my $rc = $sth->execute;
while (my $href = $sth->fetchrow_hashref) {
print "memberID: " . $$href{"memberID"} . "\n";
print "memberName: " . $$href{"memberName"} . "\n";
print "\n";
}
来源:https://stackoverflow.com/questions/21675186/connect-perl-to-ms-access