如何用perl连接SQL Server

发布于 2024-08-15 14:01:00 字数 1449 浏览 6 评论 0原文

我知道有一个类似的问题: 连接到来自 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 技术交流群。

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

发布评论

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

评论(3

一人独醉 2024-08-22 14:01:00

我刚才在使用 SQLite 时遇到了同样的错误,看来你也做错了和我一样的事情。请注意第一个参数中的冒号数量 - 这是错误的格式:

my $db = DBI->connect('DBI::SQLite::dbname=testing.db', '', '', {RaiseError => 1, AutoCommit => 1});

第一个参数中实际上应该只有两个冒号,而不是两对冒号:

my $db = DBI->connect('DBI:SQLite:dbname=testing.db', '', '', {RaiseError => 1, AutoCommit => 1});

尽管问题已存在多年,但仍得到回答,因为它仍然 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:

my $db = DBI->connect('DBI::SQLite::dbname=testing.db', '', '', {RaiseError => 1, AutoCommit => 1});

There should actually only be two colons, not two pairs of colons in the first argument:

my $db = DBI->connect('DBI:SQLite:dbname=testing.db', '', '', {RaiseError => 1, AutoCommit => 1});

Question answered despite its age because it's still top of the results in Google for this particular error message

温柔少女心 2024-08-22 14:01:00

尝试将 DSN 设置为类似以下内容:

my $dbh = DBI->connect("dbi:ODBC:test", 'username', 'password')

如果这不起作用,请通过运行以下命令确保安装了 DBD::ODBC:

perl -MDBI -e 'DBI->installed_versions;'

Try setting your DSN to something like:

my $dbh = DBI->connect("dbi:ODBC:test", 'username', 'password')

If that doesn't work, ensure you have DBD::ODBC installed by running:

perl -MDBI -e 'DBI->installed_versions;'
混浊又暗下来 2024-08-22 14:01:00

假设SQL server位于本地服务器,则如下连接即可:

my $DSN = "driver={SQL Server};Server=127.0.0.1;Database=test;UID=sa;PWD=123456";
my $dbh = DBI->connect("dbi:ODBC:$DSN");

Assume SQL server is located on local server, connection below can be right:

my $DSN = "driver={SQL Server};Server=127.0.0.1;Database=test;UID=sa;PWD=123456";
my $dbh = DBI->connect("dbi:ODBC:$DSN");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文