我可以使用查询参数插入表名吗?

发布于 2025-02-12 08:23:03 字数 880 浏览 1 评论 0原文

我有一个SQL炼金术引擎,我尝试通过sqlalchemy.sql.text插入参数以防止SQL注入。

以下代码有效,其中我为条件和条件值编码变量。

from sqlalchemy import create_engine
from sqlalchemy.sql import text

db_engine = create_engine(...)

db_engine.execute(
         text(
             'SELECT * FROM table_name WHERE :condition_1 = :condition_1_value'), condition_1="name", condition_1_value="John"
         )
).fetchall()

但是,当我尝试为table_name的变量名称编码时,它会返回错误。

from sqlalchemy import create_engine
from sqlalchemy.sql import text

db_engine = create_engine(...)

db_engine.execute(
         text(
             'SELECT * FROM :table_name WHERE :condition_1 = :condition_1_value'), table_name="table_1", condition_1="name", condition_1_value="John"
         )
).fetchall()

有什么想法为什么这不起作用?

编辑: 我知道这与table_name不是字符串有关,但我不确定如何以其他方式进行操作。

I have an SQL Alchemy engine where I try to insert parameters via sqlalchemy.sql.text to protect against SQL injection.

The following code works, where I code variables for the condition and conditions values.

from sqlalchemy import create_engine
from sqlalchemy.sql import text

db_engine = create_engine(...)

db_engine.execute(
         text(
             'SELECT * FROM table_name WHERE :condition_1 = :condition_1_value'), condition_1="name", condition_1_value="John"
         )
).fetchall()

However, when I try to code the variable name for table_name, it returns an error.

from sqlalchemy import create_engine
from sqlalchemy.sql import text

db_engine = create_engine(...)

db_engine.execute(
         text(
             'SELECT * FROM :table_name WHERE :condition_1 = :condition_1_value'), table_name="table_1", condition_1="name", condition_1_value="John"
         )
).fetchall()

Any ideas why this does not work?

EDIT:
I know that it has something to do with the table_name not being a string, but I am not sure how to do it in another way.

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

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

发布评论

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

评论(1

只怪假的太真实 2025-02-19 08:23:03

有什么想法为什么这不起作用?

查询参数用于提供事物的 value (通常是列值),而不是事物的名称(表,列等)。我看过的每个数据库都这样工作。

因此,尽管无处不在的建议是动态SQL是一件“坏事”,但在某些情况下,这是必不可少的。这是其中之一。

table_name = "table_1"  # NOTE: Do not use untrusted input here!
stmt = text(f'SELECT * FROM "{table_name}" …')

另外,请检查您从尝试参数化列名的结果。您可能不会得到您的期望。

stmt = text("SELECT * FROM table_name WHERE :condition_1 = :condition_1_value")
db_engine.execute(stmt, dict(condition_1="name", condition_1_value="John"))

不会产生相当于

SELECT * FROM table_name WHERE name = 'John'

它的等效物将呈现等效

SELECT * FROM table_name WHERE 'name' = 'John'

且不会丢失错误,但是它也不会返回行,因为'name'='john'永远不会是正确的。

Any ideas why this does not work?

Query parameters are used to supply the values of things (usually column values), not the names of things (tables, columns, etc.). Every database I've seen works that way.

So, despite the ubiquitous advice that dynamic SQL is a "Bad Thing", there are certain cases where it is simply necessary. This is one of them.

table_name = "table_1"  # NOTE: Do not use untrusted input here!
stmt = text(f'SELECT * FROM "{table_name}" …')

Also, check the results you get from trying to parameterize a column name. You may not be getting what you expect.

stmt = text("SELECT * FROM table_name WHERE :condition_1 = :condition_1_value")
db_engine.execute(stmt, dict(condition_1="name", condition_1_value="John"))

will not produce the equivalent of

SELECT * FROM table_name WHERE name = 'John'

It will render the equivalent of

SELECT * FROM table_name WHERE 'name' = 'John'

and will not throw an error, but it will also return no rows because 'name' = 'John' will never be true.

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