in reply to Using MySQL table's default values upon insert
You can handle it on the perl side, as others have suggested, or on the database side like this:
sub add_foo { my ($bar, $blah) = @_; my $sth = $dbh->prepare('INSERT INTO foo SET ' . "bar = isnull(?, 'default_bar'), " . "blah = isnull(?, 'default_blah')) " or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute($bar, $blah) or die "Couldn't execute statement: " . $sth->errstr; my $id = $dbh->last_insert_id(undef, undef, qw(foo id)) or die "no insert id?"; return $id; }
...roboticus
UPDATE: I changed the final quote on the line containing default_bar from a single quote to a double quote.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using MySQL table's default values upon insert
by Zettai (Acolyte) on Jan 30, 2010 at 20:25 UTC | |
by roboticus (Chancellor) on Jan 30, 2010 at 23:23 UTC |