gpt4 book ai didi

python - 如何在 python 中使用单个 MySQL 查询更新多行?

转载 作者:太空狗 更新时间:2023-10-29 21:16:01 26 4
gpt4 key购买 nike

#!/usr/bin/python

# -*- coding: utf-8 -*-
import MySQLdb as mdb

con = mdb.connect('localhost', 'root', 'root', 'kuis')

with con:

cur = con.cursor()
cur.execute("UPDATE Writers SET Name = %s WHERE Id = %s ",
("new_value" , "3"))
print "Number of rows updated:", cur.rowcount


使用上面的代码,数据库 kuis 中表 Writers 的第三行的值被更新为 new_value 并且输出将是 Number od rows updated: 1
如何同时更新多行?

最佳答案

可能您正在寻找 cursor.executemany .

cur.executemany("UPDATE Writers SET Name = %s WHERE Id = %s ",
[("new_value" , "3"),("new_value" , "6")])

关于python - 如何在 python 中使用单个 MySQL 查询更新多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27743447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com