CURRENT_TIMESTAMP for MySQL in Perl DBI

software development

#1

OK, I think that VALUES(,) is neat and safe from SQL Injection. Now, I want to INSERT a record with a timestamp:

$query = q{
INSERT INTO rejectedTrades
values (?, ?, ?) } ;

$statementHandle = $databaseHandle->prepare($query) ;

error handling omitted

my $didExecute = $statementHandle->execute(NULL, CURRENT_TIMESTAMP, $serialNumberReceived) ;

Everything works except the CURRENT_TIMESTAMP. I have also tried in the above line: CURRENT_TIMESTAMP(), NULL, and DEFAULT (with the column’s DEFAULT set to CURRENT_TIMESTAMP).

Result: I get either an error or 0000-00-00 00:00:00.

Is there a trick to getting a CURRENT_TIMESTAMP via DBI? It works fine if I INSERT from the command line.

Thanks again,

Jerry


#2

You can’t pass function names as parameters. They’ll get auto quoted, as you’ve just found out.

You’ll have to hardcode that yourself:

$query = q{
INSERT INTO rejectedTrades
values (?, CURRENT_TIMESTAMP, ?) } ;

$statementHandle = $databaseHandle->prepare($query) ;

error handling omitted

my $didExecute = $statementHandle->execute(NULL, $serialNumberReceived) ;


yerba# rm -rf /etc
yerba#