Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Problem with DBD::CSV

by Cristoforo (Deacon)
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 Big DBD::CSV::st execute failed: Can't find table containing column named +'big' at C :/Perl64/site/lib/SQL/Statement/ line 537 [for Statement " SELECT animal FROM animals WHERE size = Big "] at line 18, <GEN1> line 1. Cannot execute: Can't find table containing column named 'big' at C:/P +erl64/site /lib/SQL/Statement/ 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.


Comment on Problem with DBD::CSV
Select or Download Code
Replies are listed 'Best First'.
Re: Problem with DBD::CSV
by kennethk (Abbot) 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.

Re: Problem with DBD::CSV
by choroba (Chancellor) on Apr 09, 2013 at 18:22 UTC
    Missing quotes around Big?
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2016-02-10 11:46 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (345 votes), past polls