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

I swear this was working on another 'puter ... so I just made sure I updated DBD::CSV and SQL::Statement (both were already up to date according to 'cpan -i $module'). Perhaps someone can tell me where I'm going wrong. Here's what I have as a pared down example (which was originally in a Template Toolkit plugin). First, the data, which I put in a file called "mytable":

SORTORDER,POSITION,NAME,EMAIL 1,"First Position","Tanktalus","tanktalus -at- gmail.com"
Second, the code:
use DBI; use SQL::Statement; use DBD::CSV; use strict; use warnings; my $dbh; my $csv_dir = '.'; sub _dbh { $dbh ||= DBI->connect(qq{DBI:CSV:f_dir=$csv_dir;csv_eol=\n}, undef +, undef, {RaiseError => 1}); $dbh; } sub getme { #_dbh->{TraceLevel}=10; my $f = _dbh->selectall_arrayref(qq{select name from mytable where + position = ?}, {}, 'First Position'); $f = $f->[0] while ref $f; $f; } sub getme2 { #_dbh->{TraceLevel}=10; my $f = _dbh->selectall_arrayref(qq{select name from mytable where + sortorder = ?}, {}, 1); $f = $f->[0] while ref $f; $f; } print "SQL::Statement's version: ", $SQL::Statement::VERSION, $/; print "DBD::CSV's version: ", $DBD::CSV::VERSION, $/; print getme,$/; print getme2,$/;
Next, the output:
$ perl ./q.pl SQL::Statement's version: 1.15 DBD::CSV's version: 0.22 Use of uninitialized value in print at ./q.pl line 32. Tanktalus
And, finally, the expected output:
$ perl ./q.pl SQL::Statement's version: 1.15 DBD::CSV's version: 0.22 Tanktalus Tanktalus
So, selecting by comparing against the sort order (number) is working fine to pull out a string. But selecting against a string seems to be failing. I've tried a few variations without any luck - using "like ?" and 'F%', not using placeholders, etc. Since getme2 works, I have to assume that I'm at least calling stuff mostly correctly - it can find the CSV file, for example.

As you can tell, I've even tried the tracing, but I can't read a lick of it, so it didn't help me.

Thanks,

Update: Nevermind. I suppose "POSITION" is a keyword that has been (relatively) recently added to SQL::Statement's repertoire or something. Because it's not being parsed as a column header, but as something else. Changing it to "posit" works fine. The one thing I hadn't varied ...

Replies are listed 'Best First'.
Re: DBD::CSV oddity? (solved)
by jZed (Prior) on Mar 20, 2006 at 20:01 UTC
    Yes, POSITION(substring,string) is now a built-in function (see SQL::Statement::Functions so it can't be used as an identifier. I should make function names reserved words so that a warning is issued when you attmept to name a column with the name of a function. Sorry for the difficulties.

    update If you have no use for the POSITION() function and you want a column named POSITION, you can probably issue "DROP FUNCTION position" and then use it as a column name. You can do this with most keywords too - "DROP KEYWORD foo" although not with structurally critical keywords like FROM and WHERE.