Sql psycopg2:(col1,col2)在我的列表中:ProgrammingError:syntax error at或near";数组";
我想通过psyopg2执行此sql:Sql psycopg2:(col1,col2)在我的列表中:ProgrammingError:syntax error at或near";数组";,sql,postgresql,psycopg2,Sql,Postgresql,Psycopg2,我想通过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;', [
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 vis psyopg2?如果传递的是元组而不是列表,它会起作用:
cursor.execute(
'select tablename, indexname from pg_indexes where (tablename, indexname) IN %s;', [
tuple([('tab1', 'col1'), ('tab2', 'col2')]),
])
不要问我,如果你传递了一个列表,它为什么会失败。正如你在错误消息中看到的,Pycopg将Python列表调整为Postgresql数组。元组适用于记录。