#!/usr/bin/env python #--*-- coding:utf-8 --*-- __author__ = 'Kevin' import MySQLdb as mdb import sys try: conn = mdb.connect('localhost','root','redhat','test') cursor = conn.cursor() cursor.execute("UPDATE Writers SET Name = %s WHERE Id = %s",("Leo Tolstoy","1")) cursor.execute("UPDATE Writers SET Name = %s WHERE Id = %s",("ROB POP","2")) cursor.execute("UPDATE Writers SET Name = %s WHERE Id = %s",("Leo POP","3")) conn.commit() cursor.close() conn.close() except mdb.Error,e: conn.rollback() print "Error %d:%s" % (e.args[0],e.args[1])
若是不存在Writers表,則會出現錯誤python
出現錯誤,觸發異常處理blog
3條語句的前兩條會自動變成沒有執行,結果不變utf-8
若是本代碼放入一個MyISAM引擎表,,前兩句會自動執行,第三句不會,若是是INNDB引擎,則都不會執行it