码迷,mamicode.com
首页 > 数据库 > 详细

python-MySQLdb-练习

时间:2015-10-06 06:55:43      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:

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

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()

  

 

python-MySQLdb-练习

标签:

原文地址:http://www.cnblogs.com/juedui0769/p/4856616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!