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

#!/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更新,并且输出将为

我应该如何同时更新多行?

回答:

可能您正在寻找cursor.executemany。

cur.executemany("UPDATE Writers SET Name = %s WHERE Id = %s ",

[("new_value" , "3"),("new_value" , "6")])

以上是 如何在python中使用单个MySQL查询更新多行? 的全部内容, 来源链接: utcz.com/qa/397782.html

回到顶部