in reply to Want DBI to load empty strings as NULL

Look how simple life is ... with placeholders!

I'm far from an expert when it comes to DB and DBI, but my impression is that most of the 'simplicity' associated with the use of placeholders stems from avoidance of the screaming horrors you will experience in the midst of an SQL injection attack. Almost any syntactic cost would be justified to avoid these terrors, and
    $sth->execute( map {$_ ? $_ : undef} @seen_values );
seems quite cheap.

Replies are listed 'Best First'.
Re^2: Want DBI to load empty strings as NULL
by ikegami (Patriarch) on Feb 14, 2010 at 16:32 UTC
    That will also convert the string 0 into undef. Fix:
    $sth->execute( map { defined($_) && length($_) ? $_ : undef } @seen_values );
    or
    $sth->execute( map { no warnings 'uninitialized'; length($_) ? $_ : undef } @seen_values );

      YES DO THIS

      If map is unfamiliar territory, just look up some examples of using map and grep functions on lists (like your list of arguments for execute()).

      Also - don't add single-quotes to your values - they will be added automatically (if necessary) for you.