【问题标题】:How to select many rows from a dictionary (executemany select)如何从字典中选择多行(executemany select)
【发布时间】:2010-12-26 11:00:27
【问题描述】:

我正在使用 Python 及其 MySQLdb 模块, 是否可以从条件中的元组/字典/列表中执行类似“selectmany”的操作

类似这样的:

cursor.executemany("""SELECT * FROM customers WHERE name= %(name)s""",[d.__dict__ for d in data])

selected_rows = cursor.fecthall()

使用此方法进行删除/更新/插入可以正常工作:

cursor.executemany("""UPDATE customers SET item = %(item)s WHERE name = %(name)s""",[d.__dict__ for d in data])

【问题讨论】:

    标签: python select mysql executemany


    【解决方案1】:

    您可以使用“WHERE IN” sql 语法,例如,

    SELECT * FROM customers WHERE name IN ('john', 'mary', 'jane');
    

    【讨论】:

    • 大规模呢?批处理查询是唯一的解决方案吗?
    【解决方案2】:

    我还没有使用executemany 方法,但我想知道它是否适用于 SELECT。怎么样

    ... where name in (...)
    

    而不是

    ... where name = ...
    

    并插入一个包含 data 字典键的元组?

    【讨论】:

      【解决方案3】:

      executemany 大部分用于插入或更新。查询data 并不能很好地工作,根据我的经验,它只会返回最后一组参数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-15
        • 1970-01-01
        • 2020-06-07
        • 2013-09-16
        • 2016-09-06
        相关资源
        最近更新 更多