Hello all.

I am having problems with DBI:CSV. I keep getting the following error:

"SQL ERROR: Bad table or column name 'col2 + col3' has chars not alphanumeric or underscore!
DBD::CSV::db prepare failed: SQL ERROR: Bad table or column name 'col2 + col3' has chars not alphanumeric or underscore!
for Statement "SELECT AVG(col2 + col3) FROM mydata.csv" at test_script.pl line 11."

I have the following code to query a csv file.

#!/usr/bin/perl use DBI; use strict; use warnings; ## Set up connection to csv data file as a database my $dbh = DBI->connect("dbi:CSV:f_dir=./", "", "", {AutoCommit => 1, RaiseError => 1}); my $sth = $dbh->prepare("SELECT AVG(col2 + col3) FROM mydata.csv"); $sth->execute(); my $value = $sth->fetchrow_array; foreach (@value){ print $_."\n"; } $sth->finish(); $dbh->disconnect();
This works fine:
my $sth = $dbh->prepare("SELECT AVG(col3) FROM mydata.csv");
So does this:
my $sth = $dbh->prepare("SELECT (col2 + col3) FROM mydata.csv");
Just not this:
my $sth = $dbh->prepare("SELECT AVG(col2 + col3) FROM mydata.csv");
The mydata.csv file has the following in it:
col1,col2,col3,col4 575,480,192,5 388,485,194,5 379,447,210,5 504,465,215,0.8 356,439,219,0.7 481,435,225,0.6
Any idea what is going wrong? Any help would be appreciated.


In reply to DBI:CSV SQL parsing problem by rlewisuk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.