【发布时间】:2022-01-10 19:39:02
【问题描述】:
我正在尝试对 SQL 使用字符串格式。 但是在传递变量时,这些变量会用引号插入并破坏语法。
示例
这里我试图将表名传递给函数。
def see_results(cur, table):
print("complete")
cur.execute(''' SELECT * from %s ''', (table,))
results = cur.fetchall()
print(results)
问题
如果我将"temp_yellow_pages" 作为参数传递,则生成的查询是:''' SELECT * from "temp_yellow_pages" '''。
这打破了。
我想不出不使用" 的变量table 分配任何东西的方法,因为query = temp_yellow_pages 也会中断。
【问题讨论】:
-
您不能将表名作为查询参数传递。您必须将其插入到实际的查询字符串中。
-
根据您使用的库,您可能拥有比
f"SELECT * from {table}"更安全的东西。 -
如果我将
table设置为table = "employee -- drop table employee"或其他一些基于字符串 concat 的“这里有龙”的 sql 示例怎么办? -
psycopg2,例如,提供cur.execute(psycopg2.sql.SQL("select * from {}").format(sql.Identifier(table)))。注意这不是str.format,而是一个知道SQL语法的SQL.format方法。 -
谢谢大家。我认为 f-string 文字可能是唯一的方法,但有兴趣看看是否有其他常见的解决方案。