Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql psycopg2:(col1,col2)在我的列表中:ProgrammingError:syntax error at或near";数组";_Sql_Postgresql_Psycopg2 - Fatal编程技术网

Sql psycopg2:(col1,col2)在我的列表中:ProgrammingError:syntax error at或near";数组";

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;', [

我想通过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 vis psyopg2?

如果传递的是元组而不是列表,它会起作用:

cursor.execute(
'select tablename, indexname from pg_indexes where (tablename, indexname) IN %s;', [
    tuple([('tab1', 'col1'), ('tab2', 'col2')]),
               ])

不要问我,如果你传递了一个列表,它为什么会失败。

正如你在错误消息中看到的,Pycopg将Python列表调整为Postgresql数组。元组适用于记录。