I am running Perl 5.10 on Ubuntu 10.04 and using perl DBI module. I am trying to use the "AND" condition in "WHERE" clause in SQL Query under Perl DBI. I am using the DBD::CSV driver.
Please consoider below test.csv:
OS,RELEASE,VERSION
Ubuntu,Warty,4
Ubuntu,Hoary,5
Ubuntu,Breezy,5
Fedora,Yarrow,1
Fedora,Tettnang,2
Fedora,Stentz,4
Here I want to retrieve the VERSION for Fedora Stentz. Here is my code:
#!/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();
Here is hte output;
0 rows
If I use Placeholders in my query instead of the actual values as below:
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();
then the output is an error as below:
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.
But if i use only one condition in hte WEHRE clause as below, then the script gives me the right output:
my $query = "select VERSION from $table where OS=?";
my $sth = $dbh->prepare ($query);
$sth->execute ('Fedora');
$sth->dump_results();
$sth->finish();
$dbh->disconnect();
And hte output is:
'1'
'2'
'4'
3 rows
so, the bottom line and my issue is, when I write the "and" condition in "where" clause, it is not working. I doubt that there is something wrong with my query syntax but not able to figure that out yet. Any pointers or suggestion would be of great help.
Also, I have an ongoing thread on perlmonks for the same issue: http://www.perlmonks.org/?node_id=990214
Thanks.