http://www.perlmonks.org?node_id=648991

rlewisuk has asked for the wisdom of the Perl Monks concerning the following question:

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.