当前位置:首页 > 数据库 > SQlite

c – 我可以在sqlite3中使用表名参数吗?

我有一些奇怪的感觉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语句以包含表名?

解决方法:

Ooookay,应该更彻底地看待SO.

回答:
SQLite Parameters – Not allowing tablename as parameter
Variable table name in sqlite

它们适用于Python,但我猜这同样适用于C.

TL;博士:

您不能将表名作为参数传递.
如果有任何人在SQLite文档中有一个链接,我已经确认了这一点,我很乐意接受答案.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!