psycopg2.ProgrammingError:语法错误在“%”或附近

时间:2014-12-23 15:57:14

标签: python postgresql psycopg2 psycopg

我正在尝试使用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?

1 个答案:

答案 0 :(得分:0)

executemany()采用嵌套的序列参数,而不是一个。

将参数包装到另一个列表中,或者使用cur.execute()代替只运行一次查询。