如何使用元组作为查询参数之一将 pandas read_sql 从 psycopg2 迁移到 sqlalchemy

How to migrate pandas read_sql from psycopg2 to sqlalchemy with a tuple as one of the query params

在 pandas=1.4.0 中,它会发出关于不在 read_sql 中直接使用 psycopg2 而是使用 sqlalchemy 的警告。在尝试进行此类迁移时,我无法解决如何将 tuple 作为查询参数之一传递的问题。例如,这目前有效:

import pandas as pd
import psycopg2

read_sql(
    "SELECT * from news where id in %s",
    psycopg2.connect("dbname=mydatabase"),
    params=[(1, 2, 3),],
)

正在尝试将此迁移到 sqlalchemy,如下所示:

import pandas as pd
read_sql(
    "SELECT * from news where id in %s",
    "postgresql://localhost/mydatabase",
    params=[(1, 2, 3),],
)

结果

...snipped...
  File "/opt/miniconda3/envs/prod/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1802, in _execute_context
    self.dialect.do_execute(
  File "/opt/miniconda3/envs/prod/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
    cursor.execute(statement, parameters)
TypeError: not all arguments converted during string formatting

那么如何在 pandas read_sql 中将 tuple 作为 params 参数传递?

用 SQLAlchemy 包装您的查询 text object, use named parameters 并将参数值作为字典传递:

import pandas as pd
from sqlalchemy import text
read_sql(
    text("SELECT * from news where id in :ids"),
    "postgresql://localhost/mydatabase",
    params={'id': (1, 2, 3),},
)