In my application I have a sub that read the user input and creates a hash ref that includes only the fields with a non empty value, then I build the insert.
my $record_ref = { bar => 'barbar', }; # Check if ID field is present, add it, if not #if ( ! exists $record_ref->{id} ) { # $record_ref->{id} = undef; #} # Not needed for mysql. # Transform hash into two arrays my @fields = keys %{$record_ref}; my @values = map { $record_ref->{$_} } @fields; # code stolen from the IBPerl module examples ;) my $sql = "INSERT INTO foo (" . join( ',', @fields ) . ') VALUES (' . ( '?,' x $#fields ) . "?)"; print "sql=", $sql, "\n";
Update:
This is (also) a solution based on mysql's default mechanism, that is general enough to work with other rdbms's.
Update 2: Commented out the code regarding the ID column
In reply to Re: Using MySQL table's default values upon insert
by stefbv
in thread Using MySQL table's default values upon insert
by Zettai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |