从Perl文件中的mssql中选择语句

时间:2016-03-29 13:45:48

标签: perl

简单地说,我使用Perl从mssql数据库中选择一个值并打印出来。

这是我的代码:

my $serverName = "Alaa";
    my $database="blasti2b2";
    my $user="i2b2blast";
    my $pass="demouser";

    my $dsn  = "Provider=sqloledb;Trusted Connection=yes;";
    $dsn .= "Server=$serverName;Database=$database";
    my $dbh = DBI->connect("dbi:ADO:$dsn",$user,$pass,{ RaiseError => 1, AutoCommit => 1}) || die "Database connection not made: $DBI::errstr";

    my $sql = $dbh->prepare("SELECT DatabaseVersion FROM BlastDatabaseDim WHERE DatabaseName = $db_name");
    $sql->execute();
    my @row
    @row = $sql->fetchrow_array
    my $DatabaseVersion= $row[0];
    print "$DatabaseVersion\n";

我在$ DatabaseVersion,$ db_name和@row。

中有错误

请问有什么正确的方法吗?

+

如何检查Null值? 我试试:

if ($DatabaseVersion eq undef)
if ($DatabaseVersion == undef)
if ($DatabaseVersion eq '')
if ($DatabaseVersion eq "")

所有人都给我一个错误。 感谢

1 个答案:

答案 0 :(得分:0)

您至少应该更改这些行:

    my $sql = $dbh->prepare("SELECT DatabaseVersion FROM BlastDatabaseDim WHERE DatabaseName = ?");
    $sql->execute($db_name);
    my @row = $sql->fetchrow_array();