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.


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
[stevenlm87]: Needs help. I am taking an online class for perl and is using Padre. How can I get a list from the input in this code? print "What are your favorite hobbies? "; my $hobbies = <STDIN>; chomp $hobbies; print "Your favorite hobbies are '$hobbies'\n";
[toolic]: what does your input look like?

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-02-23 19:52 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (351 votes). Check out past polls.