小编典典

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

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更新,并且输出将为 Number od rows更新:1
我应该如何同时更新多行?


阅读 375

收藏
2020-05-17

共1个答案

小编典典

可能您正在寻找cursor.executemany

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