Tanktalus has asked for the wisdom of the Perl Monks concerning the following question:
I'm in the process of moving some of my stuff over from one Linux box to a newer box. During this transition, one of the scripts I use regularly does something like this (greatly simplified for posting purposes):
In this example code, lines 9 through 11 create a sample CSV data file - if your local time when you try this is not in the sample data, add it. The problem only exists in the update scenario, not the insert scenario, and I want to make as self-contained of an example as possible. The rest of the code is pretty much the same as what I'm using, but obviously doesn't work. I expect the file 'data' to look like this afterwards:#! /usr/bin/perl -w use strict; use DBI; use FindBin; my $new_total = 10; open my $fh, '>', $FindBin::Bin . '/data'; print $fh ("DATE,NUMBER\n2005-05-16,2\n2005-05-17,4\n"); close $fh; my $dbh = DBI->connect('dbi:CSV:f_dir=' . $FindBin::Bin . ";csv_eol=\n +") or warn "Can't connect to DBI"; my $total = $dbh->selectall_arrayref('select sum(number) from data')-> +[0][0]; if ($total||0 != $new_total) { require Time::localtime; my $lt = Time::localtime::localtime(time(); my $date = sprintf("%04d-%02d-%02d", $lt->year + 1900, $lt->mon + +1, $lt->mday); my $count = $dbh->selectall_arrayref('select count(*) from data wh +ere date = ?', {}, $date)->[0][0]; my $cur = $dbh->selectall_arrayref('select number from data where +date = ?', {}, $date); $cur = $cur->[0] while $cur and ref $cur; my $gained = $new_total - ($total||0); if ($count) { $cur += $gained; print "Updating today ($date) to be Number = $cur\n"; $dbh->do('update data set number = ? where date = ?', {}, $cur +, $date); } else { $cur = $gained; print "Inserting into today to be Number = $cur\n"; $dbh->do('insert into data (date,number) values(?,?)', {}, $da +te,$cur); } }
However, it actually looks like this:DATE,NUMBER 2005-05-16,2 2005-05-17,8
I'm using:DATE,NUMBER 2005-05-16,2 2005-05-17,2005-05-17
On another machine, the same command returns:$ perl -MDBD::CSV -e 'print "perl\t$]\n"; print $_,"\t",${$_."::VERSIO +N"},$/ foreach @ARGV' DBD::CSV SQL::Statement SQL::Parser Text::CSV_X +S SQL::Statement::Util perl 5.008005 DBD::CSV 0.22 SQL::Statement 1.14 SQL::Parser 1.13 Text::CSV_XS 0.23 SQL::Statement::Util -1, set by base.pm
and the update works fine. I suspect a problem in DBD::CSV, SQL::Statement, or SQL::Parser, but I'm not sure how to figure it out for sure. I suppose my workaround is to either find a way to uninstall these and install the older versions (which is kind of a pain), or to delete/reinsert rather than use update. Or maybe to get rid of the ?'s and put my number right in the string - which isn't that bad in my case, but if I'm really encountering a bug, I wouldn't want tossing ?'s out of one's repertoire to be the standard response...perl 5.008006 DBD::CSV 0.21 SQL::Statement 1.09 SQL::Parser 1.09 Text::CSV_XS 0.23 SQL::Statement::Util
If anyone has any other considerations, I'd be open to them. Otherwise, I'll submit this to the current maintainer of DBD::CSV (who will probably forward it on somewhere else, but that's as far as I understand this situation at the moment). Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBD::CSV, SQL::Statement, and UPDATE table statement
by jZed (Prior) on May 18, 2005 at 04:08 UTC | |
by Tanktalus (Canon) on May 18, 2005 at 21:23 UTC | |
by hartzell (Sexton) on Jul 07, 2005 at 05:03 UTC |