ITPub博客

首页 > IT职业 > IT职场 > 【Python操作MySQL数据库】4-2 银行转账实例(源码)

【Python操作MySQL数据库】4-2 银行转账实例(源码)

IT职场 作者:smanzheng 时间:2021-09-09 09:02:09 0 删除 编辑
# -*- 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/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2019-04-25

  • 博文量
    115
  • 访问量
    1240