Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Problems? Is your data what you think it is?
 
PerlMonks  

Problem with DBD::CSV

by Cristoforo (Chaplain)
on Apr 09, 2013 at 18:18 UTC ( #1027795=perlquestion: print w/ replies, xml ) Need Help??
Cristoforo has asked for the wisdom of the Perl Monks concerning the following question:

I am getting the following error message when trying to run my script.
C:\Old_Data\perlp>perl t7.pl Big DBD::CSV::st execute failed: Can't find table containing column named +'big' at C :/Perl64/site/lib/SQL/Statement/Operation.pm line 537 [for Statement " SELECT animal FROM animals WHERE size = Big "] at t7.pl line 18, <GEN1> line 1. Cannot execute: Can't find table containing column named 'big' at C:/P +erl64/site /lib/SQL/Statement/Operation.pm line 537 C:\Old_Data\perlp>
The script is:
#!/usr/bin/perl use strict; use warnings; #use 5.014; use DBI; my $dbh = DBI->connect(qq{DBI:CSV:}); $dbh->{'csv_tables'}->{'animals'} = { 'file' => 'o33.txt', 'col_names' => [qw/animal size/], 'sep_char' => ","}; my $sth = $dbh->prepare(qq{ SELECT animal FROM animals WHERE size = Big }); $sth->execute() or die "Cannot execute: " . $sth->errstr(); while(my @row = $sth->fetchrow_array) { print "@row\n"; } $sth->finish(); $dbh->disconnect(); __END__ o33.txt Dog,Big Dog,Medium Dog,Small Rabbit,Huge Rabbit,Tiny Rabbit,Middle Donkey,Massive Donkey,Little Donkey,Gigantic
I could run another program with the parameters set similiar to this one and it runs OK.

Thanks for any help you may provide.

Chris

Comment on Problem with DBD::CSV
Select or Download Code
Re: Problem with DBD::CSV
by choroba (Abbot) on Apr 09, 2013 at 18:22 UTC
    Missing quotes around Big?
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Problem with DBD::CSV
by kennethk (Monsignor) on Apr 09, 2013 at 18:26 UTC
    As choroba accurately points out, by missing quotes around Big, you are telling the SQL engine to return where the values in the columns named size and Big match, not where the value in column size is Big. That SQL would look like
    SELECT animal FROM animals WHERE size = 'Big'
    However, the better solution here is to use Placeholders and Bind Values. This way, the SQL engine handles the quoting and escaping for you, so you don't have to worry about what you are feeding it.
    my $sth = $dbh->prepare(qq{ SELECT animal FROM animals WHERE size = ? }); $sth->execute('Big') or die "Cannot execute: " . $sth->errstr();

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027795]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-04-19 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls