Can I use parameters for the table name in sqlite3?(我可以在 sqlite3 中使用表名的参数吗?)
问题描述
我对 sqlite3 参数有一些奇怪的感觉,我想向您公开.
I'm having some strange feeling abour sqlite3 parameters that I would like to expose to you.
这是我的查询和失败消息:
This is my query and the fail message :
#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'
在我的代码中它看起来像:
In my code it looks like:
// 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.
I have the feeling that I can't use sqlite parameters for the table name, but I can't find the confirmation in the Sqlite3 C API.
你知道我的查询有什么问题吗?
在准备查询之前,我是否必须预处理我的 SQL 语句以包含表名?
Do you know what's wrong with my query?
Do I have to pre-process my SQL statement to include the table name before preparing the query?
推荐答案
Ooookay,应该更彻底地了解 SO.
Ooookay, should have looked more thoroughly on SO.
答案:
- SQLite 参数 - 不允许表名作为参数
- sqlite中的变量表名
它们适用于 Python,但我想这同样适用于 C++.
They are meant for Python, but I guess the same applies for C++.
tl;dr:
您不能将表名作为参数传递.
如果有人在 SQLite 文档中有我对此进行确认的链接,我很乐意接受答案.
You can't pass the table name as a parameter.
If anyone have a link in the SQLite documentation where I have the confirmation of this, I'll gladly accept the answer.
这篇关于我可以在 sqlite3 中使用表名的参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以在 sqlite3 中使用表名的参数吗?


- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- SQL 临时表问题 2022-01-01