If you specify a column in your INSERT statement, it seems at least SQLite (but I guess SQL) won't use the default. I've added a third column, which is not mentioned in the INSERT statement and it always picks up the default value:

#!/usr/bin/perl use strict; use warnings; $|=1; #turn STDIO buffering off use Data::Dump qw(pp); use DBI; my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:","","",{RaiseError +=> 1}) or die "Couldn't connect to database: " . DBI->errstr; $dbh->do ("CREATE TABLE ScoreCard ( id INTEGER PRIMARY KEY AUTOINCREMENT, Score INTEGER DEFAULT 11, My_Info VARCHAR DEFAULT '<none>' ); "); my $insert_row = $dbh->prepare("INSERT INTO ScoreCard(Score) + VALUES(?)"); # desired goal: get Score in the DB to be 11 if its not specified. $insert_row->execute(); #row 1 Score=undef $insert_row->execute(undef); #row 2 Score=undef $insert_row->execute(''); #row 3 Score=null string $insert_row->execute(333); #row 4 Score=333 my $get_all_rows = $dbh->prepare("SELECT * FROM ScoreCard"); $get_all_rows->execute(); my $aref = $get_all_rows->fetchall_arrayref; print "all rows="; pp $aref; __END__ all rows=[ [1, undef, "<none>"], [2, undef, "<none>"], [3, "", "<none>"], [4, 333, "<none>"], ]

In reply to Re^2: DBI isn't using default row values by Corion
in thread DBI isn't using default row values by exceltior

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.