__MagicPower
Do one thing and do it best!

导航

 
#!/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表,则会出现错误

出现错误,触发异常处理

3条语句的前两条会自动变成没有执行,结果不变

如果本代码放入一个MyISAM引擎表,,前两句会自动执行,第三句不会,如果是INNDB引擎,则都不会执行

posted on 2016-01-05 16:31  __MagicPower  阅读(254)  评论(0编辑  收藏  举报