我有一些奇怪的感觉abour sqlite3参数,我想透露给你 .
这是我的查询和失败消息:
#query
'SELECT id FROM ? WHERE key = ? AND (userid = '0' OR userid = ?) ORDER BY userid DESC LIMIT 1;'
#error message, fails when calling sqlite3_prepare()
error: 'near "?": syntax error'
在我的代码中它看起来像:
// Query is a helper class, at creation it does an sqlite3_preprare()
Query q("SELECT id FROM ? WHERE key = ? AND (userid = 0 OR userid = ?) ORDER BY userid DESC LIMIT 1;");
// bind arguments
q.bindString(1, _db_name.c_str() ); // class member, the table name
q.bindString(2, key.c_str()); // function argument (std::string)
q.bindInt (3, currentID); // function argument (int)
q.execute();
我觉得我不能使用sqlite参数作为表名,但我找不到Sqlite3 C API中的确认 .
你知道我的查询有什么问题吗?
在准备查询之前,是否必须预先处理我的SQL语句以包含表名?
2 回答
Ooookay,应该更彻底地看待SO .
回答:
它们适用于Python,但我猜这同样适用于C.
tl;dr :
您不能将表名作为参数传递 .
如果有人在SQLite文档中有一个链接,我已经确认了这一点,我很乐意接受答案 .
我知道这已经超级旧了,但由于你的查询只是一个字符串,你总是可以在C中附加这样的表名:
或在目标-C中: