Gui*_*i13 15 c c++ sqlite parameters
我有一些奇怪的感觉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'
Run Code Online (Sandbox Code Playgroud)
在我的代码中它看起来像:
// 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();
Run Code Online (Sandbox Code Playgroud)
我觉得我不能使用sqlite 参数作为表名,但我在Sqlite3 C API中找不到确认.
你知道我的查询有什么问题吗?在准备查询之前,
是否必须预先处理我的SQL语句以包含表名?
Gui*_*i13 12
Ooookay,应该更彻底地看待SO.
答案:
- SQLite参数 - 不允许tablename作为参数
- sqlite中的变量表名
它们适用于Python,但我想同样适用于C++.
tl;博士:
您不能将表名作为参数传递.
如果有人在SQLite文档中有一个链接,我已经确认了这一点,我很乐意接受答案.
小智 5
我知道这已经非常旧了,但是由于您的查询只是一个字符串,您始终可以在 C++ 中附加表名称,如下所示:
std::string queryString = "SELECT id FROM " + std::string(_db_name);
Run Code Online (Sandbox Code Playgroud)
或者在 Objective-C 中:
[@"SELECT id FROM " stringByAppendingString:_db_name];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12204 次 |
| 最近记录: |