从 Perl 连接到 SQL Server 2005 并执行 SELECT [英] Connect to SQL Server 2005 from Perl and do a SELECT

查看:36
本文介绍了从 Perl 连接到 SQL Server 2005 并执行 SELECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Perl 脚本对 SQL Server 2005 执行 SELECT?

How do I do a SELECT on a SQL Server 2005 from a Perl script?

推荐答案

您将需要使用 DBI,并且您可能最好使用来自 (CPAN).如果您不了解 DBI,那么您需要阅读相关内容.有一本书(Programming the Perl DBI)很旧但仍然有效.

You will need to use DBI and you are probably best using the DBD::ODBC provider from (CPAN). If you don't know about DBI, then you need to read up about that. There's a book (Programming the Perl DBI) which is old but still valid.

然后是这样的:

use strict;
use warnings;
use DBI;

# Insert your DSN's name here.
my $dsn = 'DSN NAME HERE'

# 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;

这篇关于从 Perl 连接到 SQL Server 2005 并执行 SELECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆