如何用perl连接SQL Server
我知道有一个类似的问题: 连接到来自 Perl 的 SQL Server 2005 并执行 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 is not set) at script.pl line 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
我缺少什么?
I know there is a similar question: Connect to SQL Server 2005 from Perl and do a SELECT , but I tried the accepted answer and am unable to get it to work.
Assuming I have a db named test, and would love to do a select from mytable
(select id, name from mytable
)
Code is from the link above with updated 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;
This is what I got when running the script:
Can't connect to data source 'ODBC::database=test' because I can't work out what
driver to use (it doesn't seem to contain a 'dbi:driver:' prefix and the DBI_DR
IVER env var is not set) at script.pl line 9
Looks like the problem is in the dsn but I have no idea how to fix it (I am on sql 2005, active perl 5.10 and windows xp).
Edit:
I used the following code to verified whether ODBC is installed.
use DBI;
print join (", ", DBI->installed_versions);
Output:
It looks like ODBC is indeed in the list.
ADO, CSV, DBM, ExampleP, File, Gofer, ODBC, SQLite, Sponge, mysql
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚才在使用 SQLite 时遇到了同样的错误,看来你也做错了和我一样的事情。请注意第一个参数中的冒号数量 - 这是错误的格式:
第一个参数中实际上应该只有两个冒号,而不是两对冒号:
尽管问题已存在多年,但仍得到回答,因为它仍然 Google 中针对此特定错误消息的结果顶部
I got the same error with SQLite just now, and it looks like you did the same thing wrong as me. Note the number of colons in the first argument - this is the wrong format:
There should actually only be two colons, not two pairs of colons in the first argument:
Question answered despite its age because it's still top of the results in Google for this particular error message
尝试将 DSN 设置为类似以下内容:
如果这不起作用,请通过运行以下命令确保安装了 DBD::ODBC:
Try setting your DSN to something like:
If that doesn't work, ensure you have DBD::ODBC installed by running:
假设SQL server位于本地服务器,则如下连接即可:
Assume SQL server is located on local server, connection below can be right: