How to connect Access2007 by DBI in Perl

本文提供了一个使用Perl语言通过DBI和DBD::ODBC模块连接并查询Microsoft Access数据库的具体示例。示例中详细介绍了如何安装必要的Perl模块、设置数据源及执行基本的SQL查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

#The example runs on Windows platform.
#Before using DB, make sure DBI and DBD::ODBC packages are installed in advance.
#See PPM command in perl
#For more details, take perldoc DBI as reference.
#Note that the driver for Access2007 is a little different with previous versions.
# The DB suffix is *.accdb instead of *.mdb.
#If DBI and DBD-ODBC have not been installed in the system, first make sure the two modules been installed correctly.
# Run the command "ppm install DBI" and "ppm install DBD-ODBC" respectively.
#If you are behind the firewall, proxy server must be set so as to connect to Internet successfully.  set http_proxy=http://proxy.com/, then run "ppm install...".
#For more detail, type perldoc DBI as reference
use DBI;
 
my $dsn = "Provider=Microsoft.ACE.OLEDB.12.0;DSN=TestDSN";
#If is .mdb file. my $dsn="Provider=Microsoft.jet.OLEDB.4.0;DSN=TestDSN";
# my $dsn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C://TestDB.accdb";
my $username = "";
my $password = "";
my $dbh = DBI->connect("dbi:ODBC:$dsn", $username, $password) or die "Can not open the DB: $DBI::errstr";
 
my $sth = $dbh->prepare("select * from TABLE");
my $rv = $sth->execute or die "Can not execute the statement";
my $rec;
while($rec = $sth->fetchrow_arrayref)
{
    print "$rec->[0], $rec->[1]/n";
}
#my @rec
# while(@rec = $sth->fetchrow_array)
#{
#   print "@rec/n";
#}
#
$dbh->disconnect;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值