MySQLdb - mysql数据库

写在前面:
本文将crazyant在慕课网上的课程代码贴出来,已标出出处,还希望作者理解:

$ pip install MySQL-pyhton
作者采用自顶向下的设计思路展开:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 28 19:04:32 2016

@author: root
"""
import MySQLdb
import sys

class TransferMoney(object):
    def __init__(self,conn):
        self.conn = conn
        
    def check_account_available(self,accountId):
        cursor = self.conn.cursor()
        try:            
            sql = "select * from account where id = %s" % accountId
            cursor.execute(sql)
            print "check_account_available:"+sql
            rs = cursor.fetchall()
            if len(rs) != 1:
                raise Exception("account %s is not exist!" % accountId)
        finally:
            cursor.close()
        
    def has_enough_money(self,accountId,money):
        cursor = self.conn.cursor()
        try:            
            sql = "select * from account where id = %s and money>%s" % (accountId,money)
            cursor.execute(sql)
            print "has_enough_money:"+sql
            rs = cursor.fetchall()
            if len(rs) != 1:
                raise Exception("account %s does not have enough money!" % accountId)
        finally:
            cursor.close()
            
    def reduce_money(self,accountId,money):
        cursor = self.conn.cursor()
        try:            
            sql = "update account set money = money - %s where id = %s" % (money,accountId)
            cursor.execute(sql)
            print "reduce_money:"+sql
            if cursor.rowcount != 1:
                raise Exception("account %s reduce momey, failed!" % accountId)
        finally:
            cursor.close()
            
    def add_money(self,accountId,money):
        cursor = self.conn.cursor()
        try:            
            sql = "update account set money = money + %s where id = %s" % (money,accountId)
            cursor.execute(sql)
            print "add_money:"+sql
            if cursor.rowcount != 1:
                raise Exception("account %s add momey, failed!" % accountId)
        finally:
            cursor.close()
    
    def transfer(self,source_id,target_id,money):
        try:
            self.check_account_available(source_id)
            self.check_account_available(target_id)
            self.has_enough_money(source_id,money)
            self.reduce_money(source_id,money)
            self.add_money(target_id,money)
            self.conn.commit()
        except Exception as e:
            self.conn.rollback()
            raise e

if __name__ == "__main__":
    source_id = sys.argv[1]
    target_id = sys.argv[2]
    money = sys.argv[3]
    
    conn = MySQLdb.Connect(
                       host = '127.0.0.1',
                       port = 3306,
                       user = 'root',
                       passwd = '1234aaaa',
                       db = 'ngs_web'
                       )
    tr_money = TransferMoney(conn)
    
    try:
        tr_money.transfer(source_id,target_id,money)
    except Exception as e:
        print "error is:"+str(e)
    finally:
        conn.close()

你可能感兴趣的:(MySQLdb - mysql数据库)