in reply to Re: DBD::CSV Update
in thread DBD::CSV Update

The skip_rows option will skip valid CSV-rows (and will croak otherwise). Though the hint is good, I think that it doesn't currently do as required by the OP: it skips CSV lines after the header.

When not passing the col_names like you did, it will skip the count of lines as passed like this after reading the header line, so in given example, that would still result in just 3 fields. That obviously needs a new option that would skip lines before the header line in whatever format, so a CSV file like this:

This file holds the report for sales over August 2013 id,count,description,price FAIL,,Placeholder,0.00 \N,,, 10D2,4,Drill 9mm,0.80 11E1,1,Festool DRC 18/4.515.15

could be opened and correctly dealt with with code like:

#!/usr/bin/perl use 5.20.0; use warnings; use DBI; my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_dir => ".", csv_auto_diag => 1, }); $dbh->{csv_tables}{stock} = { file => "stock.csv", skip_rows => 2, skip_before => 3, # nonexisting option # look ma, no col_names }; my $sth = $dbh->prepare ("select * from stock"); $sth->execute; while (my $row = $sth->fetch) { say "@$row"; } $dbh->do ("update foo set price = 420 where id = '11E1'"); $dbh->disconnect;

The new option however has no control over the format of the lines. I'll consider adding a new option for this.


Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^3: DBD::CSV Update
by soonix (Chancellor) on Jun 01, 2015 at 10:37 UTC
    Would it be a sensible suggestion to use a byte offset (as in seek) for this option?
    That would make it possible to have binary data in that "header".

      Sounds like a recipe for failure. How do you think Joe Programmer will check the exact amount of *bytes* to the real data? Think UTF-8 and JPEG.

      As an alternative, one could think of a start-of-data-tag.

      Both however will only make life for DBD::CSV harder, as it has to put the same data back on the same place on updates. I'm still not convinced this would all be worth the trouble.


      Enjoy, Have FUN! H.Merijn

        Don't know about Joe, but I'd fire up a hex editor/viewer to look at the position :-)

        The thought leading to my suggestion was something with an SFX-like header, where the byte count would be known in advance. However, that was just a theoretical idea, so …