MySQL select语句的QSlQuery失败

时间:2012-04-24 09:41:32

标签: mysql qt

我的Qt应用程序中使用了以下MySQL查询:

query.prepare(QString("SELECT productName, value, COUNT(value), SUM(value)"
                  "FROM tblmystock"
                  "WHERE time>=\'%1\' AND time<=\'%2\'"
                  "GROUP BY productName, value").arg(startDateTime, endDateTime));

我收到以下错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near'>='2012-04-24 00:00:00' AND time<='2012-04-24 23:59:59:59' GROUP BY productName' at line 1 QMYSQL: Unable to execute query

即使我尝试使用bindValues,它也会失败并出现同样的错误。

但是当我在MySQL提示符下执行相同的查询时,它可以正常工作:

mysql> SELECT productName, value, COUNT(value), SUM(value) FROM tblmystock WHERE time>='2012-04-24 00:00:00' AND time<='2012-04-25 23:59:59' GROUP BY productName, value;

有人知道解决方法吗?

1 个答案:

答案 0 :(得分:1)

time是保留关键字。使用反引号逃脱它。
您也可以使用between代替<=>=

WHERE `time` between '2012-04-24 00:00:00' AND '2012-04-25 23:59:59'