1 #coding=utf-8 2 import MySQLdb 3 conn = MySQLdb.Connect(host = '127.0.0.1',port=3306,user='root',passwd='',db='test',charset='utf8') 4 cursor = conn.cursor() 5 6 try: 7 #插入数据 8 sql_insert = "insert into orders(order_id,product_id) values (777,777)" 9 cursor.execute(sql_insert)10 print cursor.rowcount11 12 #修改数据13 sql_update = "update orders set order_id = 99999 where id=12"14 cursor.execute(sql_update)15 print cursor.rowcount16 17 #删除数据18 sql_delete = "delete from orders where id = 3"19 cursor.execute(sql_delete)20 print cursor.rowcount21 22 #提交23 conn.commit()24 except Exception as e:25 print e26 conn.rollback()27 28 #关闭指针和连接29 cursor.close()30 conn.close()