我知道有一个类似的问题:Connect to SQL Server 2005 from Perl and do a SELECT,但我尝试了接受的答案,但无法让它工作。
假设我有一个名为 test 的数据库,并且很想从 mytable ( select id, name from mytable
)中进行选择
代码来自上面带有更新 dsn 的链接:
use strict;
use warnings;
use DBI;
# Insert your DSN's name here.
my $dsn = 'database=test'
# Change username and password to something more meaningful
my $dbh = DBI->connect("DBI::ODBC::$dsn", 'username', 'password')
# Prepare your sql statement (perldoc DBI for much more info).
my $sth = $dbh->prepare('select id, name from mytable');
# Execute the statement.
if ($sth->execute)
{
# This will keep returning until you run out of rows.
while (my $row = $sth->fetchrow_hashref)
{
print "ID = $row->{id}, Name = $row->{name}\n";
}
}
# Done. Close the connection.
$dbh->disconnect;
这是我在运行脚本时得到的结果:无法连接到数据源 'ODBC::database=test' 因为我无法确定要使用的驱动程序(它似乎不包含 'dbi:driver: ' 前缀并且未设置 DBI_DR IVER env var)在 script.pl 第 9 行
看起来问题出在 dsn 中,但我不知道如何解决(我使用的是 sql 2005,活动 perl 5.10 和 windows xp)。
编辑:我使用以下代码验证是否安装了 ODBC。使用 DBI;
print join (", ", DBI->installed_versions);
输出:看起来 ODBC 确实在列表中。
ADO, CSV, DBM, ExampleP, File, Gofer, ODBC, SQLite, Sponge, mysql
我错过了什么?