in reply to DBD::CSV - missing first row?

What version of DBD::CSV are you using? Here, with perl 5.8.6 and DBD::CSV 0.21, I don't get the error, like hossman. However, it may be something as simple as older versions requiring a leading "#" on the first row to identify it as such, e.g.,

#"field1";"field2";"field3";"field4";"field5";"field6";"field7";

Replies are listed 'Best First'.
Re^2: DBD::CSV - missing first row?
by Stenyj (Beadle) on May 08, 2005 at 06:12 UTC
    Thx for the feeback, but no luck so far.

    Running:
    DBI: 1.42
    DBD:CSV: 0.22
    Perl 5.6.1

    New code I copied & pasted to attempt to get it working:

    #!c:/apache/perl/bin/perl.exe -wT BEGIN { $| = 1; open (STDERR, ">&STDOUT"); print qq~Content-type: text/html\n\n~; } use CGI qw/:standard/; use strict; use Data::Dumper; use DBI; my $foo = new CGI; print $foo->header; # Connect to the database, (the directory containing our csv file(s)) my $dbh = DBI->connect("DBI:CSV:f_dir=.;csv_eol=\n;"); # Associate our csv file with the table name 'test2' $dbh->{'csv_tables'}->{'test2'} = { 'file' => 'test2.csv'}; # Output the name and contact field from each row my $sth = $dbh->prepare("SELECT * FROM test2 WHERE name LIKE 'G%'"); $sth->execute() or die "Can't execute the query: $sth->errstr"; while (my $row = $sth->fetchrow_hashref) { print("name = ", $row->{'Name'}, " contact = ", $row->{'Contact'} +. "\n"); } $sth->finish();

    CSV file that is being used for test2.csv:
    "Name","Address","Floors","Donated last year","Contact" "Charlotte French Cakes","1179 Glenhuntly Rd",1,"Y","John" "Glenhuntly Pharmacy","1181 Glenhuntly Rd",1,"Y","Paul" "Dick Wicks Magnetic Pain Relief","1183-1185 Glenhuntly Rd",1,"Y","Geo +rge" "Gilmour's Shoes","1187 Glenhuntly Rd",1,"Y","Ringo"

    The error I'm getting is:
    Content-Type: text/html; charset=ISO-8859-1 Execution ERROR: Missing f +irst row at c:/apache/Perl/site/lib/DBD/CSV.pm line 165, line 1. . DB +D::CSV::st fetchrow_hashref failed: Attempt to fetch row from a Non-S +ELECT statement [for statement ``SELECT * FROM test2 WHERE name LIKE +'G%''']) at /apache/htdocs/test2.cgi line 26.

    Any other suggestions? I appreciate your help/feedback guys...

    Stenyj
      Are you sure your testfile contains what you think? The only way I can reproduce the error is when test2.csv is present but empty.


      holli, /regexed monk/
        Yeah, the file is there & has the content I posted. I even just changed the script to attempt an open on the file, and return an error if it wasn't available:
        #!c:/apache/perl/bin/perl.exe -wT BEGIN { $| = 1; open (STDERR, ">&STDOUT"); print qq~Content-type: text/html\n\n~; } use CGI qw/:standard/; use strict; use Data::Dumper; use DBI; my $foo = new CGI; print $foo->header; # Connect to the database, (the directory containing our csv file(s)) my $dbh = DBI->connect("DBI:CSV:f_dir=.;csv_eol=\n;"); my $content = ''; open (FILE,"c:/apache/htdocs/test2.csv") || die "unable to open file"; while (<FILE>) { print $_; } close(FILE); # Associate our csv file with the table name 'test2' $dbh->{'csv_tables'}->{'test2'} = { 'file' => 'c:/apache/htdocs/test2. +csv'}; # Output the name and contact field from each row my $sth = $dbh->prepare("SELECT * FROM test2 WHERE name LIKE 'G%'"); $sth->execute() or die "Can't execute the query: $sth->errstr"; while (my $row = $sth->fetchrow_hashref) { print("name = ", $row->{'Name'}, " contact = ", $row->{'Contact'} +. "\n"); } $sth->finish();

        The file opens fine, it prints to the browser fine, but then is followed by the error about the missing first line etc.
        *shrug*
        Tackled this from a few different angles. Really wanted to use DBD::CSV vs. TEXT::CSV so that I would be able to do what I want with the data a LOT more efficiently since it's a script that I'm going to setup to run frequently on my server.
        I may just end up coding it with TEXT::CSV and then recode it if I ever get DBD::CSV working :-|

        Thx for the help all the same. I appreciate it!


        Stenyj