本文实例讲述了Python事务操作实现方法。分享给大家供大家参考,具体如下:
#coding=utf-8
import sys
import MySQLdb
class TransferMoney(object):
def __init__(self,conn):
self.conn = conn
#检查账户是否合法
def check_acct_avaiable(self,acctid):
cursor = self.conn.cursor()
try:
sql = "select * from account where acctid=%s" % acctid
cursor.execute(sql)
print "check account:" + sql
rs = cursor.fetchall()
if len(rs) != 1:
raise Exception("account %s illega" % acctid)
finally:
cursor.close()
#检查是否有足够的钱
def has_enough_money(self,acctid,money):
cursor = self.conn.cursor()
try:
sql = "select * from account where acctid=%s and money > %s" % (acctid,money)
cursor.execute(sql)
print "has enou