• python-MySQLdb-练习


    看完视频,自己练习一遍. 还是遇到问题,不过最终还是解决了.贴上完成的代码.

    CREATE TABLE `NewTable` (
    `acctid`  int(11) NOT NULL AUTO_INCREMENT COMMENT '账户ID' ,
    `money`  int(11) NULL DEFAULT NULL COMMENT '余额' ,
    PRIMARY KEY (`acctid`)
    )
    ENGINE=InnoDB
    DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
    AUTO_INCREMENT=1
    ROW_FORMAT=COMPACT
    ;
    

      

    # -*- coding:utf-8 -*-
    '''
    Created on 2015年10月6日
    
    @author: WXG
    '''
    import MySQLdb
    
    class TranslateAccount(object):
        def __init__(self, conn):
            self.conn = conn
        
        
        def checkAccount(self, acctid):
            cursor = self.conn.cursor()
            try:
                sql = "select * from account where acctid = %s" % acctid
                print "sql for checkAccount:" + sql
                cursor.execute(sql)
                rs = cursor.fetchall()
                if len(rs) < 1 :
                    raise Exception("不存在此账号%s" % acctid)
            finally:
                cursor.close()
                
        
        def checkEnoughMoney(self, acctid, money):
            cursor = self.conn.cursor()
            try:
                sql = "select * from account where acctid = %s and money > %s" % (acctid, money)
                print "sql for checkEnoughMoney:" + sql
                cursor.execute(sql)
                rs = cursor.fetchall()
                if len(rs) < 1 :
                    raise Exception("此账号%s没有足够的余额" % acctid)
            finally:
                cursor.close()
        
        
        def reduceMoney(self, acctid, money):
            cursor = self.conn.cursor()
            try:
                sql = "update account set money = money-%s where acctid = %s" % (money,acctid)
                print "sql for reduceMoney:" + sql
                cursor.execute(sql)
                if cursor.rowcount != 1 :
                    raise Exception("此账号%s减款失败!" % acctid)
            finally:
                cursor.close()
        
        
        def addMoney(self, acctid, money):
            cursor = self.conn.cursor()
            try:
                sql = "update account set money = money+%s where acctid = %s" % (money,acctid)
                print "sql for addMoney:" + sql
                cursor.execute(sql)
                if cursor.rowcount != 1 :
                    raise Exception("此账号%s加款失败!" % acctid)
            finally:
                cursor.close()
        
        
        def translate(self, source_acctid, target_acctid, money):
            try:
                self.checkAccount(source_acctid)
                self.checkAccount(target_acctid)
                self.checkEnoughMoney(source_acctid, money)
                self.reduceMoney(source_acctid, money)
                self.addMoney(target_acctid, money)
                self.conn.commit()
            except Exception as e:
                self.conn.rollback()
                print "遇到异常%s,执行回滚!" % e
    
    
    if __name__ == "__main__":
        source_acctid = raw_input("Source Account ID:")
        target_acctid = raw_input("Target Account ID:")
        money = raw_input("Translate Money:")
        conn = MySQLdb.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='blog', charset='utf8')
        try:
            transAccount = TranslateAccount(conn)
            transAccount.translate(source_acctid, target_acctid, money)
        finally:
            conn.close()
    

      

  • 相关阅读:
    iOS 应用内付费(IAP)开发步骤
    国内银行CNAPS CODE 查询 苹果开发者,应用内购,需要填写税务相关信息必须的
    untiy 插件工具: 游戏中 策划数据Excel 导出到项目中
    大陆 Google play 开发者注册(2016)
    unity UGUI动态字体显示模糊
    Vue--webpack实时重新加载
    Vue--webpack打包css、image资源
    Vue--webpack打包js文件
    Vue--axios
    Vue--生命周期
  • 原文地址:https://www.cnblogs.com/juedui0769/p/4856616.html
Copyright © 2020-2023  润新知