$dbh->do ("CREATE TABLE ScoreCard
( id integer PRIMARY KEY AUTOINCREMENT,
Url varchar(80) DEFAULT '',
Score integer DEFAULT 0,
Club varchar(30) DEFAULT ''
);
");
####
$score //= 0; #set to zero if undef
####
#!/usr/bin/perl
use strict;
use warnings;
$|=1; #turn STDIO buffering off
use Data::Dump qw(pp);
use DBI;
my $dbfile = "./monk_test.sqlite";
unlink $dbfile if -e $dbfile;
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","",{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 $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], [2, undef], [3, ""], [4, 333]]