如何从字典中选择多行(executemany select)
我正在使用 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])
I'm using Python and its MySQLdb module,
is it possible to do a "selectmany"-like from a tuple/dictionary/list in the condition
something like this:
cursor.executemany("""SELECT * FROM customers WHERE name= %(name)s""",[d.__dict__ for d in data])
selected_rows = cursor.fecthall()
doing a delete/update/insert works fine with this method :
cursor.executemany("""UPDATE customers SET item = %(item)s WHERE name = %(name)s""",[d.__dict__ for d in data])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用“WHERE IN”sql 语法,例如,
You could use the "WHERE IN" sql syntax, for example,
我还没有使用
executemany
方法,但我想知道它是否用于 SELECT。 怎么样?不如
插入一个包含
data
字典键的元组I haven't used the
executemany
method yet, but I wonder if it's meant to be used for SELECTs. What aboutinstead of
and inserting a tuple containing the keys of your
data
dictionaries?executemany
大部分用于插入或更新。不太适合查询 data,根据我的经验,它只会返回最后一组参数。executemany
for the most part is for insertion or update. Doesn't really work well for querying data, from my experience it will only return the last set of parameters.