2

我在 Ubuntu 10.04 上运行 Perl 5.10 并使用 perl DBI 模块。我正在尝试在 Perl DBI 下的 SQL 查询中的“WHERE”子句中使用“AND”条件。我正在使用 DBD::CSV 驱动程序。

请考虑下面的 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 的 VERSION。这是我的代码:

#!/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.

但是,如果我在 hte WEHRE 子句中只使用一个条件,如下所示,那么脚本会给我正确的输出:

my $query = "select VERSION from $table where OS=?";
my $sth = $dbh->prepare ($query);
$sth->execute ('Fedora');
$sth->dump_results();
$sth->finish();
$dbh->disconnect();

hte 输出为:

'1'
'2'
'4'
3 rows

所以,底线和我的问题是,当我在“where”子句中写“and”条件时,它不起作用。我怀疑我的查询语法有问题,但还不能弄清楚。任何指示或建议都会有很大帮助。

另外,我在 perlmonks 上有一个关于同一问题的持续线程:http ://www.perlmonks.org/?node_id=990214

谢谢。

4

1 回答 1

3

你所有的片段对我来说都很好。确保您的模块是最新的。从Devel::VersionDump(退出时调用):

Perl version: v5.16.0 on linux (/home/eric/usr/perlbrew/perls/5.16.0t/bin/perl)
AutoLoader                      -     5.72
Carp                            -     1.26
Clone                           -     0.31
Config                          -  Unknown
Cwd                             -  3.39_02
DBD::CSV                        -     0.36
DBD::File                       -     0.40
DBI                             -    1.620
DBI::DBD::SqlEngine             -     0.03
DBI::SQL::Nano                  - 1.014600
Data::Dumper                    - 2.135_06
Devel::VersionDump              -     0.02
DynaLoader                      -     1.14
Errno                           -     1.15
Exporter                        -     5.66
Exporter::Heavy                 -     5.66
Fcntl                           -     1.11
File::Basename                  -     2.84
File::Spec                      -  3.39_02
File::Spec::Unix                -  3.39_02
IO                              -  1.25_06
IO::File                        -     1.16
IO::Handle                      -     1.33
IO::Seekable                    -      1.1
List::Util                      -     1.23
Params::Util                    -     1.07
SQL::Dialects::AnyData          -     1.33
SQL::Dialects::Role             -     1.33
SQL::Eval                       -     1.33
SQL::Parser                     -     1.33
SQL::Statement                  -     1.33
SQL::Statement::Function        -     1.33
SQL::Statement::Functions       -     1.33
SQL::Statement::Operation       -     1.33
SQL::Statement::Placeholder     -     1.33
SQL::Statement::RAM             -     1.33
SQL::Statement::Term            -     1.33
SQL::Statement::TermFactory     -     1.33
SQL::Statement::Util            -     1.33
Scalar::Util                    -     1.23
SelectSaver                     -     1.02
Symbol                          -     1.07
Text::CSV_XS                    -     0.91
Tie::Hash                       -     1.04
XSLoader                        -     0.16
base                            -     2.18
bytes                           -     1.04
constant                        -     1.23
overload                        -     1.18
overloading                     -     0.02
sort                            -     2.01
strict                          -     1.07
unicore::Heavy.pl               -  Unknown
unicore::lib::Perl::Word.pl     -  Unknown
unicore::lib::Perl::_PerlIDS.pl -  Unknown
utf8                            -     1.09
utf8_heavy.pl                   -  Unknown
vars                            -     1.02
warnings                        -     1.13
warnings::register              -     1.02
于 2012-08-28T17:13:40.017 回答