2
我在Ubuntu 10.04上运行Perl 5.10并使用perl DBI模块。 我想在Perl DBI下的SQL Query的“WHERE”子句中使用“AND”条件。我正在使用DBD :: CSV驱动程序。Perl DBD :: CSV - SQL语法 - “AND”子句无法正常工作
请consoider以下test.csv:
OS,RELEASE,VERSION
Ubuntu,Warty,4
Ubuntu,Hoary,5
Ubuntu,Breezy,5
Fedora,Yarrow,1
Fedora,Tettnang,2
Fedora,Stentz,4
在这里我要检索的Fedora Stentz版本。 这里是我的代码:
#!/usr/bin/perl -w
use strict;
use DBI;
my $table = "test.csv";
my $dbh = DBI->connect ("dbi:CSV:") or die "Cannot connect to the CSV file: $DBI::errstr()";
$dbh->{RaiseError} = 1;
$dbh->{TraceLevel} = 0;
my $query = "select VERSION from $table where OS='Fedora' and RELEASE='Yarrow'";
my $sth = $dbh->prepare ($query);
$sth->execute();
$sth->dump_results();
$sth->finish();
$dbh->disconnect();
这里是hte输出;
0 rows
如果我在查询中使用占位符代替,如下实际值:
my $query = "select VERSION from $table where OS=? and RELEASE=?";
my $sth = $dbh->prepare ($query);
$sth->execute ('Fedora', 'Yarrow');
$sth->dump_results();
$sth->finish();
$dbh->disconnect();
则输出是错误的,如下:
DBD::CSV::st execute failed: You passed 2 parameters where 0 required [for Statement "select VERSION from test.csv where OS=? and RELEASE=?"] at count.pl line 14.
DBD::CSV::st execute failed: You passed 2 parameters where 0 required [for Statement "select VERSION from test.csv where OS=? and RELEASE=?"] at count.pl line 14.
但如果我只用在WEHRE子句中的一个条件如下,那么该脚本给我正确的输出:
my $query = "select VERSION from $table where OS=?";
my $sth = $dbh->prepare ($query);
$sth->execute ('Fedora');
$sth->dump_results();
$sth->finish();
$dbh->disconnect();
而且兴田输出为:
'1'
'2'
'4'
3 rows
因此,底线,我的问题是,当我写的“其中”条款的“和”的条件,这是行不通的。我怀疑我的查询语法有什么问题,但无法解决这个问题。任何指针或建议都会有很大的帮助。
另外,我对perlmonks对同一问题的正在执行的线程:http://www.perlmonks.org/?node_id=990214
感谢。
这太快捷了。我浪费了整整一天的工作时间,结果竟然是螺丝模块。根据你的建议,当我运行VersoinDump时,两个模块“SQL :: Statement”和“SQL :: Parser”表示“未知”,其他所有人都显示了一个版本号。所以我重新安装了“SQL :: Statement”并运行了VersionDump,并且显示了版本号。后来我的脚本也运行良好。非常感谢你的时间和帮助ikegami。我从未想过在我最疯狂的梦中,这是一个腐败的模块问题。谢谢 !!! – slayedbylucifer