提问人:tab_philomath 提问时间:7/18/2023 最后编辑:tab_philomath 更新时间:7/18/2023 访问量:33
Psycopg2 未执行将表名传递给查询
Psycopg2 is not executing the passing of a table name to query
问:
我不知道出了什么问题。我能够使用相同的格式传递要删除的列的名称,但是当我尝试传递表名时,它不起作用。
错误为:psycopg2.errors.UndefinedColumn:列“delete_9”不存在。
try:
#pass the table name to get the columns
q = sql.SQL("SELECT column_name FROM information_schema.columns WHERE table_name = {};")
cur.execute(q.format(sql.Identifier('table1')))
columns = cur.fetchall()
except:
print("does not pass the string table")
但这确实有效:
for i in range(len(columns)):
col_name= columns[i][0]
query = sql.SQL( "UPDATE table1 SET {} = NULL WHERE {} = X;")
cur.execute(query.format(sql.Identifier(col_name),sql.Identifier(col_name)))
我正在传递一个表名来获取列。为什么它认为我在输入列?
答: 暂无答案
评论
sql.Identifier
SELECT * FROM {}
table_name
information_schema.columns
cur.execute("SELECT column_name FROM information_schema.columns WHERE table_name = %s;", "table1")
{}
... table_name = {}
%s