我正在尝试使用psycopg
的参数化查询来删除一堆行。我的脚本有删除语句,如:
cur.executemany( "WITH remove_rows as (DELETE FROM users WHERE userid = %s RETURNING *), insert_rows as (INSERT INTO old.users SELECT * FROM remove_rows RETURNING *) SELECT count(*) from insert_rows;", (id,))
我得到的错误是:
Traceback (most recent call last):
File "removal.py", line 17, in <module>
cur.executemany( "WITH remove_rows as (DELETE FROM .users WHERE userid = %s RETURNING *), insert_rows as (INSERT INTO old.users SELECT * FROM remove_rows RETURNING *) SELECT count(*) from insert_rows;", (id,))
psycopg2.ProgrammingError: syntax error at or near "%"
LINE 1: ...ws as (DELETE FROM users WHERE userid = %s RETURNI...
当我从userid = %s
中移除空格并将其设为userid=%s
时,我收到了与错误column "s" does not exist
相同的错误。
我开始怀疑psycopg2
参数化是否不能处理CTE?
答案 0 :(得分:0)
executemany()
采用嵌套的序列参数,而不是一个。
将参数包装到另一个列表中,或者使用cur.execute()
代替只运行一次查询。