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

I'm 99.9% sure this is a bug in DBD::CSV please corroborate! I've spent now 4hrs tracking this down, <notice> this is as compact as I could get it.</notice> This thing is absolutely driving me crazy.
#!/usr/bin/perl use strict; no warnings; use DBD::CSV; my $file = shift; my $dbh = DBI->connect('DBI:CSV:f_dir=.') or die "Cannot connect $!"; $dbh->{'csv_tables'}{'current_table'} = { file => $file, quote_char => '~', eol=> "\r\n" }; my $sth = $dbh->prepare(q{SELECT * FROM current_table}); $sth->execute(); while ( defined ( my @row = $sth->fetchrow_array ) ) { if ( $sth->errstr() ) { print $sth->errstr() and die }; } print $sth->errstr();
And it seems to crash on this data:
~Style ID~,~Hist Style ID~,~Model ID~,~Model Year~,~Sequence~,~Style C +ode~,~Full Style Code~,~Style Name~,~True Base Price~,~Invoice~ 282198, 2007700803 ,16139 ,2007 ,1 ,~ZMB67~ + ,~ZMB67~ ,~2dr Conv~ ,~Y~ ,21319.80

(program run with ./program file.csv)
Error Message as follows:
DBD::CSV::st fetchrow_array failed: Attempt to fetch row from a Non-SE +LECT statement [for Statement "SELEC + T * FROM current_table"] at ./col_proc.pl line 12. Died at ./col_proc.pl line 12. evan@dealermade:/var/chrome/NVDv2_US_EN$ ./col_proc.pl ./problem.txt DBD::CSV::st fetchrow_array failed: Attempt to fetch row from a Non-SE +LECT statement [for Statement "SELECT * FROM current_table"] at ./col +_proc.pl line 12. Died at ./col_proc.pl line 12.

As a last note, the whitespace in the csv are to show that there is no real funny business going on there. Thanks in advance monks. From a later post.
From a later post: synopsis of thread. BUG FOUND When this is the data file it works fine!!! Data File:
~Style Name~ ~5dr Crew Cab 130~
Deduction: The bug is DBD::CSV breaking if there is an occurance of a double quotes within a non-standard quote character of '~', (so what is the point of using quote_char) ?
Still no resolution


Evan Carroll
www.EvanCarroll.com

Replies are listed 'Best First'.
Re: A bug in DBD::CSV?
by diotalevi (Canon) on Apr 12, 2006 at 22:37 UTC

    It appears you're checking errstr more often than you should. Your code works just fine if you just test the results of the fetches. I'm sure that if a result came back undefined or empty you'd want to check errstr then. This sounds like doing die $! if $! which fails for the exact same reason - if you haven't just done a system operation then you don't know that $! will be false.

    In preference to doing manual testing of of success/failure, it's much easier to ask DBI to throw exceptions for you: DBI->connect( 'DBI:CSV:f_dir=.', undef, undef,{ RaiseError => 1, ShowErrorStatement => 1 } ).

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      The problem isn't just the error, The error is the symptom of the problem. I was altered to this because the rows below this problem were not read at all. I will supply more details tomorrow.


      Evan Carroll
      www.EvanCarroll.com

        I copied your exact code and data into files. It worked great as long as I didn't check for the failure text when there wasn't a failure.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: A bug in DBD::CSV?
by cosimo (Hermit) on Apr 13, 2006 at 13:00 UTC

    Hi Evan,
    Change:

    while( defined ( my @row = $sth->fetchrow_array ) ) {
    to:
    while( my @row = $sth->fetchrow_array ) {
    and an endless loop problem is fixed.
    I think the other problem is your peculiar choice of quote_char.
    I found that specifying the general quote char on DBI connection options makes the error go away. As you probably know, this is a general setting, not per table/csv handle.
    my $dbh = DBI->connect('DBI:CSV:f_dir=.;csv_quote_char=~;') or die .. +.
    I didn't have time to investigate on this one, though it would be interesting to do.
Re: A bug in DBD::CSV?
by EvanCarroll (Chaplain) on Apr 13, 2006 at 16:23 UTC

    EASIER BUG DEMONSTRATION

    Perl Script
    #!/usr/bin/perl use strict; no warnings; use DBI; my $file = shift; my $dbh = DBI->connect('DBI:CSV:f_dir=.;') or die "Cannot connect $!"; $dbh->{'csv_tables'}{'current_table'} = { file => $file, quote_char => '~' }; my $sth = $dbh->prepare(q{SELECT * FROM current_table}); $sth->execute(); while ( my @row = $sth->fetchrow_array ) { print @row; print "\n"; }


    Data File:
    ~Style Name~ ~5dr Crew Cab 130" WB 2WD LS~
    BUG FOUND When this is the data file it works fine!!!
    Data File:
    ~Style Name~ ~5dr Crew Cab 130~
    Deduction: The bug is DBD::CSV breaking if there is an occurance of a double quotes within a non-standard quote character of '~', (so what is the point of using quote_char) ?


    Evan Carroll
    www.EvanCarroll.com
A reply falls below the community's threshold of quality. You may see it by logging in.