我想通过psyopg2执行这个sql:
select indexname from pg_indexes where (tablename, indexname) in (
('tab1', 'index1'),
('tab2', 'index2')
);
以下是代码:
cursor.execute(
'select tablename, indexname from pg_indexes where (tablename, indexname) IN %s;', [
[('tab1', 'col1'), ('tab2', 'col2')],
])
我得到了这个例外:
ProgrammingError: syntax error at or near "ARRAY"
LINE 1: ...e from pg_indexes where (tablename, indexname) IN ARRAY[('ta...
如何将元组列表传递给PostgreSQL和psyopg2?
答案 0 :(得分:8)
如果您传递一个元组而不是一个列表,它可以工作:
cursor.execute(
'select tablename, indexname from pg_indexes where (tablename, indexname) IN %s;', [
tuple([('tab1', 'col1'), ('tab2', 'col2')]),
])
如果你通过一个清单,不要问我失败的原因。