in reply to Re: DBI isn't using default row values
in thread DBI isn't using default row values
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>"], ]
|
|---|