I am running Perl 5.10 on Ubuntu 10.04 and using the Perl DBI module. I am trying to use the AND clause in the WHERE clause in SQL Query under Perl DBI. I am using the DBD :: CSV driver.
Please split test.csv below:
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 get 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's the output of hte;
0 rows
If I use Placeholders in my query instead of the actual values, as shown 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 will be an error as shown 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 the hte WEHRE clause, as shown below, then the script gives me the correct 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:
'1' '2' '4' 3 rows
So, in the bottom line and my problem, when I write the condition "and" in the "where" section, it does not work. I doubt that there is something wrong with my query syntax, but I still cannot figure it out. Any pointers or suggestions would be very helpful.
In addition, I have a constant thread on perlmonks for the same problem: http://www.perlmonks.org/?node_id=990214
Thanks.