Heh, how long this thread has gone on ... so, to distinguish null from empty string in CSV, you could to

my $csv = qq{foo,,bar,''};

And write your code so that the second item in that list is NULL, while the fourth is a blank string. Unfortunately I don't know if there's a standard for that or not. I suppose I should try both in DBD::CSV and see what pops up. (update down the road)

as promised here's some code ... I think the upshot is that DBD::CSV doesn't distinguish between blank strings and undef values, but I can't tell because the code isn't working ATM. posted for educational purposes only.

#!/usr/bin/perl -w use DBI; use strict; my $db = DBI->connect("DBI:CSV:f_dir=/home/arturo/testing") or die "Cannot connect: $DBI::errstr\n"; =pod # used to create the table my $sth = $db->prepare("CREATE TABLE foo (id INTEGER, firstname VARCHAR(32), lastname VARCHAR(32) )" +); $sth->execute; $sth = $db->prepare("INSERT INTO foo (id, firstname, lastname) VALUES +(?,?,?)"); $sth->execute(1, 'Charo', ''); $sth->execute(2, 'Wynonna', undef); $sth->execute(3, undef, 'Cher'); $sth->execute(4, '', "Sting"); # note: Charo has a blank last name, Wynonna has an undef one # Cher has undef first name, Sting has blank first name =cut my $sth = $db->prepare("SELECT id, firstname, lastname FROM foo") or die "Cannot prepare: $DBI::errstr\n"; # next line currently causes an error on my system # (fresh installs of all the modules on which DBD::CSV depends) # UPDATE the answer, of course, is to execute it first # /me LARTs himself $sth->execute(); while ( my ($id, $first, $last) = $sth->fetchrow_array() ) { print "First name '$first' is " . ($first eq '' && defined $first) ? "blank" : "undefine +d"; print "Second name '$last' is " . ($last eq '' && defined $last ) ? "blank" : "undefined +"; } $db->disconnect;

As I mentioned, looking at the file that got created, I don't see a difference between blank and undef, EXCEPT in the first two cases: the "Charo" line has a trailing comma, the "Wynonna" line does not. And thanks to busunsl for pointing out I forgot to execute my statement (sigh, it's not the best of days for me when it comes to details).

Philosophy can be made out of anything. Or less -- Jerry A. Fodor


In reply to (arturo) Re:(6)Null fields by arturo
in thread Null fields by quietone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.