我尝试使用模板查询来构建我的 SQL 查询。这是示例代码:
unsigned int version = 2;
try {
// key_version is INT UNSIGNED
mysqlpp::Query query = conn->query("SELECT * FROM agentlist WHERE key_version != %0q");
mysqlpp::StoreQueryResult res = query.store(version);
// string type param also caused the same problem
// mysqlpp::StoreQueryResult res = query.store(std::to_string(version));
} catch (const exception &ex) {
// deal with exceptions
}
代码将转到catch
部分。 ex.what()
:
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 \'2\' at line 1
我认为这是由 SQLQueryParms
类型引起的,但教程和文档都没有显示任何解决此问题的方法。
顺便说一句,如何获取解析的查询字符串(替换模板参数)?我尝试了 query.str(version)
但它与 query.str()
相同。
最佳答案
我的错。忘记了一个重要的函数调用 query.parse()
。
现在一切正常了。
关于mysqlpp : SQL syntax error when using template queries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46767045/