mysql.connector 将列表传递给 WHERE IN 子句 - python

问题描述

这是一个示例 sql 查询,它使用 WHERE/IN 子句来选择匹配多个条件的多行。 例如,

SELECT * FROM table WHERE id IN ('AAA','BBB',CCC')
and date < '2021-06-28';

我想在 python 中将 ('AAA',CCC')2021-06-28 作为参数 id_listas_of_date 传递:

query = '''SELECT * FROM table WHERE id IN %(id_list)s
and date < date(%(as_of_date)s);'''

我已设法与 sqlalchemy 一起使用:

engine = sqlalchemy.create_engine('MysqL://xxxx')
dataframe = pandas.read_sql(query,engine,params=query_params)

其中 query_params 是带有 dict 字符串和 ID 列表的 as_of_date

{
        "as_of_date": "2021-06-28,"id_list": ['AAA',......]
    }

然而,我不知道如何使用 TLSv1.2 强制 ssl 连接到 sqlalchemy,所以我决定改为使用 engine = MysqL.connector.connect() 但如果我这样做:

engine = MysqL.connector.connect(xxxxx)
cursor = engine.cursor()
cursor.execute(query,query_param)

这会给我错误

_MysqL_connector.MysqLInterfaceError: Python type tuple cannot be converted

我尝试将 id_list 参数转换为元组,但它仍然无法正常工作并给出错误 _MysqL_connector.MysqLInterfaceError: Python type tuple cannot be converted。 有人可以帮忙吗?谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)