如何使用 Perl 连接到 MS Access 数据库?

发布于 2024-08-09 16:42:06 字数 426 浏览 3 评论 0原文

我的本地计算机上有一个 .accdb 文件,我正在尝试连接到它并从数据库中的 3 个表中读取一些数据。如何使用 Perl 建立连接?

到目前为止,我已经为 MS Access 收集了这么多内容,但我收到错误消息,说我没有使用正确的驱动程序。有什么想法吗?

my $msaccess_dbh = DBI->connect(
    'dbi:ODBC:driver=microsoft access driver (*.accdb);' .
    'dbq=C:\path\to\database\databasefile.accdb'
);

谢谢!

编辑:只是为了澄清,我在这里没有真正的要求。我只需要从这个 MS Access DB 中进行 2 或 3 个选择,然后我就完成了。因此,任何有关连接和选择的帮助都会很棒。再次感谢。

I have a .accdb file on my local machine and I am trying to connect to it and read some data from 3 tables within the DB. How do I establish the connection using Perl?

So far I have scraped together this much for MS Access, but I am getting errors saying that I am not using the correct driver. Any ideas?

my $msaccess_dbh = DBI->connect(
    'dbi:ODBC:driver=microsoft access driver (*.accdb);' .
    'dbq=C:\path\to\database\databasefile.accdb'
);

Thanks!

EDIT: Just to clarify, I have no real requirements here. I just need to do 2 or 3 selections from this MS Access DB, and then I will be done with it. So any help with connecting and selecting would be great. Thanks again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和影子一齐双人舞 2024-08-16 16:42:06

根据您的连接字符串,您似乎 (a) 在 Win32 上并且 (b) 连接到本地计算机上的数据库。如果我是正确的,当您可以直接与 Jet 连接时,为什么还要麻烦使用 ODBC?参考如下:

#!/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

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
愚人国度 2024-08-16 16:42:06

我猜驱动程序与您的 DSN 驱动程序不匹配,或者导致问题的另一件事是,如果您将 64 位 Perl 与 32 位 ODBC 驱动程序混合使用,或者将 32 位 Perl 与 64 位混合使用,位驱动程序。真正的问题是错误消息,它非常模糊 - 你认为也许他们可以告诉你数据源还是驱动程序是问题所在?在完美的世界中...

无论如何,如果您的 DSN 正确,那么您尝试的方法确实有效,并且您的 DSN 正确。如果你的 Perl 和ODBC 驱动程序属于同一位系列。

DSN 中的驱动程序参考必须与管理工具 > 下列出的内容完全匹配。 数据源(ODBC) > 驱动程序选项卡。我的被​​列为Microsoft Access 驱动程序(.mdb、.accdb),因此与您的略有不同。在 Perl 中,要连接的行是:

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

有关 Windows 7 上使用 Perl 的 MS Access 位于此处

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.

雨后咖啡店 2024-08-16 16:42:06

我过去曾成功使用过该格式的连接字符串,但那是针对旧的 *.mdb 格式。您的 ODBC 驱动程序可能不支持 Access 2007 中较新的 *.accdb 格式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文