Skip Menu |

This queue is for tickets about the DBD-CSV CPAN distribution.

Report information
The Basics
Id: 80253
Status: rejected
Priority: 0/
Queue: DBD-CSV

People
Owner: Nobody in particular
Requestors: BENBOOTH [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: DBD::CSV skips first row of file even when col_names is given
DBD::CSV is skipping the first row of the file, even when I set col_names attribute to []. I expected the first row to be included since the file does not have a header row and I set col_names. I attached a test case that demonstrates the issue.
Subject: dont_skip_first_row.t
#!/usr/bin/env perl use Test::Simple tests=>1; use DBI; use File::Temp; use File::Basename qw(basename); use DBD::CSV; use Text::CSV; use strict; use warnings; my $tmp = File::Temp->new(DIR=>'.'); $tmp->unlink_on_destroy(1); print $tmp "$_\n" for 1..5; close $tmp; my %tab=(sep_char=>"\t",quote_char=>undef,escape_char=>"\\",allow_loose_escapes=>1); my $dbh = DBI->connect("dbi:CSV:", undef, undef, { RaiseError=>1, PrintError=>1, map { ("csv_$_"=>$tab{$_}) } keys %tab, }); my $sth = $dbh->prepare("select * from ".basename($tmp->filename)); $dbh->{csv_tables}{seq}{col_names} = []; $sth->execute; my $ctr = 0; while (my $row=$sth->fetchrow_arrayref) { $ctr++; } ok($ctr == 5);
On Wed Oct 17 21:19:11 2012, BENBOOTH wrote: Show quoted text
> DBD::CSV is skipping the first row of the file, even when I set > col_names attribute to []. I > expected the first row to be included since the file does not have a > header row and I set > col_names. > > I attached a test case that demonstrates the issue.
Your example contains several mistakes: 1) You must give the column names for the table before preparing the statement 2) you must tell DBD::CSV how to use the column names for table seq in queries against table eval { basename($tmp->filename) }. I'd recommend to use csv_tables => { seq => { f_file => $tmp->filename, col_names => [qw(col1 .. coln)] } } in connection attributes ;) Note: There must be at least one column name, tables without columns aren't supported. Unnamed columns read from file are discarded in later states of SQL processing. Best regards, Jens
Subject: test.pl
#!/usr/bin/env perl use Test::Simple tests=>1; use DBI; use File::Temp; use File::Basename qw(basename); use DBD::CSV; use Text::CSV; use strict; use warnings; my $tmp = File::Temp->new(DIR=>'.'); $tmp->unlink_on_destroy(1); print $tmp "$_\n" for 1..5; close $tmp; my %tab=(sep_char=>"\t",quote_char=>undef,escape_char=>"\\",allow_loose_escapes=>1); my $dbh = DBI->connect("dbi:CSV:", undef, undef, { RaiseError=>1, PrintError=>1, map { ("csv_$_"=>$tab{$_}) } keys %tab, }); $dbh->{csv_tables}{seq}{col_names} = [qw(foo)]; $dbh->{csv_tables}{seq}{f_file} = basename($tmp->filename); my $sth = $dbh->prepare("select * from seq"); $sth->execute; my $ctr = 0; while (my $row=$sth->fetchrow_arrayref) { $ctr++; } ok($ctr == 5);
CC: BENBOOTH [...] cpan.org
Subject: Re: [rt.cpan.org #80253] DBD::CSV skips first row of file even when col_names is given
Date: Thu, 18 Oct 2012 11:35:30 -0700
To: bug-DBD-CSV [...] rt.cpan.org
From: Ben Booth <benwbooth [...] gmail.com>
OK, cool, thanks for the information. This will really help me with my scripts. Thanks, Ben On Oct 18, 2012, at 12:02 AM, "Jens Rehsack via RT" <bug-DBD-CSV@rt.cpan.org> wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=80253 > > > On Wed Oct 17 21:19:11 2012, BENBOOTH wrote:
>> DBD::CSV is skipping the first row of the file, even when I set >> col_names attribute to []. I >> expected the first row to be included since the file does not have a >> header row and I set >> col_names. >> >> I attached a test case that demonstrates the issue.
> > Your example contains several mistakes: > 1) You must give the column names for the table before preparing the > statement > 2) you must tell DBD::CSV how to use the column names for table seq in > queries against table eval { basename($tmp->filename) }. > > I'd recommend to use csv_tables => { seq => { f_file => $tmp->filename, > col_names => [qw(col1 .. coln)] } } in connection attributes ;) > > Note: There must be at least one column name, tables without columns > aren't supported. Unnamed columns read from file are discarded in later > states of SQL processing. > > Best regards, > Jens > <test.pl>