zoukankan      html  css  js  c++  java
  • mysql for python,银行转账模拟

    学习中, 本人为初学者。勿喷。

    #-*- coding:utf-8 -*-
    import MySQLdb
    class Tranferaccount(object):
        def __init__(self,sqlcon):
            self.sqlcon = sqlcon
        def account_check_avaiable(self,accid):
            cursor = self.sqlcon.cursor()
            sql_str = "select * from account where accid = %s "% accid
            try:
                print 'account_check_avaiable:%s'%sql_str
                cursor.execute(sql_str)
                rs = cursor.fetchall()
                if len(rs)!=1:
                    raise Exception('此账号%s不存在'%accid) 
            finally:
                cursor.close()
        def account_enough_money(self,accid,money):
            cursor = self.sqlcon.cursor()
            sql_str = "select * from account where accid = %s and money >%s "% (accid,money)
            try:
                print 'account_enough_money:%s'%sql_str
                cursor.execute(sql_str)
                rs = cursor.fetchall()
                if len(rs)!=1:
                    raise Exception('此账号%s余额不足'%accid)
            finally:
                cursor.close()
        def account_reduce_money(self,accid,money):
            cursor = self.sqlcon.cursor()
            sql_str = "update account set money = money-%s where accid = %s "% (money,accid)
            try:
                print 'account_reduce_money:%s'%sql_str
                cursor.execute(sql_str)
                if cursor.rowcount != 1:
                    raise Exception('此账号%s减款失败'%accidd)
            finally:
                cursor.close()
        def account_add_money(self,accid,money):
            cursor = self.sqlcon.cursor()
            sql_str = "update account set money = money+%s where accid = %s "% (money,accid)
            try:
                print 'account_add_money:%s'%sql_str
                cursor.execute(sql_str)
                if cursor.rowcount != 1:
                    raise Exception('此账号%s加款失败'%accid)
            finally:
                cursor.close()                                        
        def tranfer(self,source_accid,dest_accid,money):
            try:
                self.account_check_avaiable(source_accid)
                self.account_check_avaiable(dest_accid)
                self.account_enough_money(source_accid,money)
                self.account_reduce_money(source_accid,money)
                self.account_add_money(dest_accid,money)
                self.sqlcon.commit()
            except Exception as e:
                self.sqlcon.rollback()
                raise e
    if __name__ == "__main__":
        source_accid = 11
        dest_accid = 12
        money = 100
        conn = MySQLdb.connect(host='127.0.0.1',user='root',db = 'liunx',passwd='root',port=3306,charset='utf8')
        tr_money = Tranferaccount(conn)
        try:
            tr_money.tranfer(source_accid,dest_accid,money)
        except Exception as e:
            print '出现问题:%s'% e
        finally:  
            conn.close()
  • 相关阅读:
    windows常用快捷键
    清华为什么被称为“水木清华”
    CTP程序化系统开发(C++ && PHP)
    PHP判断SESSION过期的方法
    Linux 下 ---ThinkPHP 图片上传提示:上传根目录不存在!请尝试手动创建
    &#65279导致页面顶部空白一行解决方法 【】
    Code笔记 之:ajax诡异的错误-请求status为200
    PHP之:序列化和反序列化-serialize()和unserialize()
    Linux之:Ubuntu速学笔记(1)
    C语言归并排序(合并排序)算法及代码
  • 原文地址:https://www.cnblogs.com/nx520zj/p/5897053.html
Copyright © 2011-2022 走看看