#-*- coding:utf-8 -*- """ author:leetao """ import MySQLdb conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8") cursor = conn.cursor() sql = "update t_user set name=%s where id = 1" param = ("lt") cursor.execute(sql,param)
cursor.close() conn.commit() conn.close()
同样的执行完脚本,然后查看修改是否发生效果
修改成功,cool~~
查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#-*- coding:utf-8 -*- """ author:leetao """ import MySQLdb conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8") cursor = conn.cursor() sql = "select * from t_user where id = 1" cursor.execute(sql) for rows in cursor.fetchall(): for row in rows: print row
cursor.close() conn.commit() conn.close()
执行脚本,控制台成功输出如下结果:
删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#-*- coding:utf-8 -*- """ author:leetao """ import MySQLdb conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8") cursor = conn.cursor() sql = "delete from t_user where id = 1" cursor.execute(sql)