How do I connect to an MS Access database using Perl?

走远了吗. 提交于 2019-11-29 11:27:36

Based on your connection string it looks like you are (a) on Win32 and (b) connecting to a database on your local machine. If I am correct why bother with ODBC when you can connect directly with Jet? Refer below:

#!/usr/bin/perl
use strict;use warnings;

use Win32::OLE;

my $DBFile  = qw( X:\Path\To\Your\Database.mdb ); # 
#Choose appropriate version 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 = "DELETE * FROM Test_Table";
$DB->Execute($SQLquery, 128); #128=DBFailOnError

I'm guessing the driver didn't match what you had for the DSN, or the other thing that causes problems is if you're mixing 64-bit Perl with a 32-bit ODBC driver, or 32-bit Perl with a 64-bit driver. The real problem is that error message, it's terribly vague -- you think maybe they could tell you whether the data source OR the driver was the problem? In a perfect world...

Anyway, that method you were trying does work if your DSN is correct, & if your Perl & ODBC driver are in the same bit family.

The driver reference in the DSN has to match exactly what's listed under Adminstrative Tools > Data Sources (ODBC) > Drivers tab. Mine is listed as Microsoft Access Driver (.mdb, .accdb) so that's slightly different from what you had. In Perl the line to connect is:

my $dbh = DBI->connect('dbi:ODBC:driver=Microsoft Access Driver (*.mdb, *.accdb);dbq=X:\Path\To\Your\Database.mdb')

More info on MS Access with Perl on Windows 7 is here.

I've successfully used connection strings with that format in the past, but that was for the old *.mdb format. It's possible that your ODBC driver doesn't support the newer *.accdb format in Access 2007.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!