sqlite3中表名可以使用参数吗?

发布于 2024-11-04 15:08:41 字数 929 浏览 0 评论 0原文

我对 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 parameters 作为表名,但我无法在Sqlite3 C API

你知道我的查询有什么问题吗?
在准备查询之前,我是否必须预处理 SQL 语句以包含表名?

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();

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一指流沙 2024-11-11 15:08:41

哦,好吧,应该更彻底地研究一下。

答案:
- SQLite 参数 - 不允许表名作为参数
- sqlite 中的变量表名称

它们适用于 Python,但我想这同样适用对于C++。

tl;dr

您不能将表名称作为参数传递。
如果有人在 SQLite 文档中提供了我对此进行确认的链接,我将很乐意接受答案。

Ooookay, should have looked more thoroughly on SO.

Answers:
- SQLite Parameters - Not allowing tablename as parameter
- Variable table name in sqlite

They are meant for Python, but I guess the same applies for C++.

tl;dr:

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.

郁金香雨 2024-11-11 15:08:41

我知道这已经非常旧了,但是由于您的查询只是一个字符串,您始终可以在 C++ 中附加表名称:

std::string queryString = "SELECT id FROM " + std::string(_db_name);

或在 Objective-C 中:

[@"SELECT id FROM " stringByAppendingString:_db_name];

I know this is super old already but since your query is just a string you can always append the table name like this in C++:

std::string queryString = "SELECT id FROM " + std::string(_db_name);

or in objective-C:

[@"SELECT id FROM " stringByAppendingString:_db_name];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文