QSqlQuery UPDATE/INSERT DateTime 与服务器时间(例如 CURRENT_TIMESTAMP)
我正在使用 QSqlQuery 将数据插入 MySQL 数据库。目前我关心的只是让它与 MySQL 一起工作,但理想情况下我希望尽可能保持它与平台无关。
在 MySQL 的上下文中,我所追求的是最终得到有效执行类似以下查询的代码:
UPDATE table SET time_field=CURRENT_TIMESTAMP() WHERE id='5'
以下代码是我尝试过的,但它失败了:
QSqlQuery query;
query.prepare("INSERT INTO table SET time_field=? WHERE id=?");
query.addBindValue("CURRENT_TIMESTAMP()");
query.addBindValue(5);
query.exec();
我得到的错误是:不正确日期时间值:第 1 行“time_field”列的“CURRENT_TIMESTAMP()”QMYSQL3:无法执行语句
。我并不感到惊讶,因为我认为 Qt 在绑定值时会进行一些类型检查。
我已经深入研究了 Qt 文档以及我知道的方法,但我在 API 中找不到任何专门为支持 MySQL 的 CURRENT_TIMESTAMP() 函数或任何其他 DBMS 函数而设计的内容。
有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我这里没有要测试的 SQL 服务器,但 addBindValue() 绑定了 Qt 的数据类型。您应该将时间戳函数直接放入查询中
应该可以解决问题。
I have no SQL server to test here, but addBindValue() binds Qt's data types. You should put the timestamp function direct into the query
should do the trick.
我没有找到获取数据库时间命令的方法。我认为它会在 QSqlDriver 中,因为它特定于数据库。
我能想到的唯一方法:
编辑:事实上,我不确定 CURRENT_TIMESTAMP() 是特定于数据库的......
I don't see a way to get the database time command. I think it would be in QSqlDriver as it is specific to the database.
The only way I can think of :
Edit : In fact, I'm not sure CURRENT_TIMESTAMP() is DB specific ...