用python操作mysql数据库(之“更新”操作)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import MySQLdb

#建立连接
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1qaz#EDC',db='test_db')
cur = conn.cursor()

#对数据进行操作
sql = "update user set name=%s where id=7" #定义sql语句,用于修改ID为7的name字段
params = ("zhongjw",)   #修改为"zhongjw"

cur.execute(sql,params)
conn.commit()   #提交请求

#关闭数据库连接
cur.close()
conn.close()