#!/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 我应该如何同时更新多行?
可能您正在寻找cursor.executemany。
cur.executemany("UPDATE Writers SET Name = %s WHERE Id = %s ", [("new_value" , "3"),("new_value" , "6")])