简体   繁体   English

将perl连接到ms访问

[英]Connect perl to ms access

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. 有没有我错过安装的驱动程序。

As indicated in the comments to the question, the Perl script was originally running as a 64-bit process. 正如对问题的评论中所指出的,Perl脚本最初是作为64位进程运行的。 Therefore the older Microsoft "Jet" ODBC driver 因此较旧的Microsoft“Jet”ODBC驱动程序

Microsoft Access Driver (*.mdb)

was not available. 没有。 Only 32-bit processes can use the older Jet driver. 只有32位进程才能使用旧的Jet驱动程序。 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 (aka "ACE") from here and then use the driver name 如果必须以64位进程运行Perl脚本,则必须从此处下载并安装较新的Microsoft Access数据库引擎(又称“ACE”)的64位版本,然后使用驱动程序名称

Microsoft Access Driver (*.mdb, *.accdb)

Or, you could run your Perl script as a 32-bit process and use the older Jet driver. 或者,您可以将Perl脚本作为32位进程运行并使用旧的Jet驱动程序。

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. 由于您安装了32位Access 2007,因此您已在计算机上安装了32位版本的ACE驱动程序。 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. 这有效地消除了安装64位版本ACE驱动程序的选项,因为如果在计算机上找到32位Office组件,64位ACE安装程序将中止。 (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.) (显然有一种方法可以强制进行第二次安装,但据报道在某些情况下会破坏Office,但绝对支持。)

So your options can be revised to: 因此,您的选项可以修改为:

  1. Run the Perl script as a 32-bit process, or 运行Perl脚本作为32位进程,或

  2. 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. 删除32位Access 2007并安装64位版本的Access,然后使用64位ACE驱动程序将Perl脚本作为64位进程运行。

Try to use Jet : 尝试使用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 ] [来源: 在这里 ,也看看这里 ]

For a script running as a 32-bit process (in my case, 32-bit Strawberry Perl), the following code works for me: 对于作为32位进程运行的脚本(在我的例子中,32位Strawberry Perl),以下代码适用于我:

#!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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM