QSqlQuery UPDATE/INSERT DateTime with server's time (eg CURRENT_TIMESTAMP)

Posted by Skinniest Man on Stack Overflow See other posts from Stack Overflow or by Skinniest Man
Published on 2010-05-06T17:53:36Z Indexed on 2010/05/06 17:58 UTC
Read the original article Hit count: 237

Filed under:
|
|
|
|

I am using QSqlQuery to insert data into a MySQL database. Currently all I care about is getting this to work with MySQL, but ideally I'd like to keep this as platform-independent as possible.

What I'm after, in the context of MySQL, is to end up with code that effectively executes something like the following query:

UPDATE table SET time_field=CURRENT_TIMESTAMP() WHERE id='5'

The following code is what I have attempted, but it fails:

QSqlQuery query;
query.prepare("INSERT INTO table SET time_field=? WHERE id=?");
query.addBindValue("CURRENT_TIMESTAMP()");
query.addBindValue(5);
query.exec();

The error I get is: Incorrect datetime value: 'CURRENT_TIMESTAMP()' for column 'time_field' at row 1 QMYSQL3: Unable to execute statement. I am not surprised as I assume Qt is doing some type checking when it binds values.

I have dug through the Qt documentation as well as I know how, but I can't find anything in the API designed specifically for supporting MySQL's CURRENT_TIMESTAMP() function, or that of any other DBMS.

Any suggestions?

© Stack Overflow or respective owner

Related posts about qtsql

Related posts about mysql