【问题标题】:In clause query on Python (pymysql and mysql.connector) [duplicate]Python上的子句查询(pymysql和mysql.connector)[重复]
【发布时间】:2014-07-21 08:00:25
【问题描述】:

我想使用这个查询:

select x from y where z in ("a", "b", "c")

我有 pymysql 和 mysql.connector for MySQL with Python。

这适用于 pymysql:

args = ["a", "b", "c"]
db = cur.execute('select x from y where z in %s',(tuple(args),))

但它不适用于 mysql.connector。它给了我错误,例如 'MySQLConverter' 对象没有属性 '_tuple_to_mysql'

pymysql 有一个很好的解决方案,但我无法在 mysql.connector 上意识到这一点。你能帮帮我吗?

【问题讨论】:

  • 还有SQLAlchemy WHERE IN single value (raw SQL)。我将您链接到的问题的答案使用? 作为占位符,只需使用%s 作为SQL 参数占位符。此评论中的第二个链接生成:i0等命名占位符;更复杂,这里可能不需要。

标签: python mysql sql pymysql


【解决方案1】:

怎么样

db = cur.execute('select x from y where z in (%s,%s,%s)',tuple(args))

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2020-11-05
    • 2020-05-05
    • 2013-03-04
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2014-07-09
    相关资源
    最近更新 更多