我在使用psycopg2中的WHERE $VARIABLE IN
子句时遇到了麻烦:
from app.commons.database import conn
from psycopg2 import sql
from psycopg2.extras import DictCursor
query = '''
SELECT
*
FROM
{}.{}
WHERE
{} in %s
'''.format(
sql.Identifier('information_schema'),sql.Identifier('tables'),sql.Identifier('table_schema')
)
data = (
'information_schema','pg_catalog'
)
with conn.cursor(cursor_factory=DictCursor) as cursor:
cursor.execute(query,data)
print(cursor.fetchall())
提高
TypeError:在字符串格式化期间并非所有参数都已转换
我已经看过关于同一主题的数百篇文章,并且压倒性的答案是:“在将数据作为cursor.execute
的第二个参数提交时,您需要使用元组”。我一直在这样做,但似乎仍然无法确定差距在哪里。