# -*- coding: utf8 -*-
import MySQLdb
import sys
reload(sys)
sys.setdefaultencoding('utf8')
class TransferMoney(object):
def __init__(self,conn):
self.conn = conn
def tranfer(self,toAccount,fromAccount,money):
self.check_acc_available(toAccount)
self.check_acc_available(fromAccount)
self.check_money_enough(fromAccount,money)
self.reduce_money(fromAccount,money)
self. add_money(toAccount,money)
print u"转账成功"
def check_acc_available(self,account):
try:
cursor = self.conn.cursor()
sql = "select * from user where name='%s'"%account
cursor.execute(sql)
if cursor.rowcount!=1:
raise Exception("帐号%s不存在"%account)
finally:
cursor.close()
def check_money_enough(self,account,money):
try:
cursor = self.conn.cursor()
sql = "select money from user where name='%s'"%account
cursor.execute(sql)
account_money = cursor.fetchone()[0]
if account_money 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4759/viewspace-2798797/ ,如需转载,请注明出处,否则将追究法律责任。