Mu class python Operations database 2 example of bank transfer

Source: Internet
Author: User
Tags rowcount

CREATE TABLE' account ' (' Acctid ' )int( One)DEFAULT NULLCOMMENT'Account ID',  ` Money`int( One)DEFAULT NULLCOMMENT'Balance') ENGINE=InnoDBDEFAULTCHARSET=Utf8

1 #Coding:utf82 3 ImportMySQLdb4 ImportSYS5 6 classTransferMoney (object):7     """docstring for TransferMoney"""8     def __init__(SELF, conn):9Self.conn =ConnTen  One     defcheck_acct_avaiable (self,acctid): Acursor =self.conn.cursor () -         Try: -sql ="SELECT * from account where Acctid =%s"%Acctid the cursor.execute (SQL) -             Print "check_acct_avaiable:"+SQL -rs =Cursor.fetchall () -             ifLen (rs)! = 1: +                 RaiseException ("account%s does not exist"%Acctid) -         finally: + cursor.close () A  at     defHas_enough_money (Self,acctid,money): -cursor =self.conn.cursor () -         Try: -sql ="SELECT * from account where Acctid =%s and money >%s"%(Acctid,money) - cursor.execute (SQL) -             Print "Has_enough_money:"+SQL inrs =Cursor.fetchall () -             ifLen (rs)! = 1: to                 RaiseException ("account%s does not have enough money"%Acctid) +         finally: - cursor.close () the  *     defReduce_money (Self,acctid,money): $cursor =self.conn.cursor ()Panax Notoginseng         Try: -sql ="Update account Set money = Money-%s where Acctid =%s"%(Money,acctid) the cursor.execute (SQL) +             Print "Reduce_money:"+SQL A             ifCursor.rowcount! = 1: the                 RaiseException ("account%s failed to reduce payment"%Acctid) +         finally: - cursor.close () $  $     defAdd_money (Self,acctid,money): -cursor =self.conn.cursor () -         Try: thesql ="Update account Set Money = Money +%s where Acctid =%s"%(Money,acctid) - cursor.execute (SQL)Wuyi             Print "Add_money:"+SQL the             ifCursor.rowcount! = 1: -                 RaiseException ("Account%s bonus failed"%Acctid) Wu         finally: - cursor.close () About  $     defTransfer (Self,source_acctid,target_acctid,money): -         Try: - self.check_acct_avaiable (Source_acctid) - self.check_acct_avaiable (Target_acctid) A Self.has_enough_money (Source_acctid,money) + Self.reduce_money (Source_acctid,money) the Self.add_money (Target_acctid,money) - Self.conn.commit () $         exceptException, E: the Self.conn.rollback () the             Raisee the  the          -          in  the if __name__=="__main__": theSource_acctid = sys.argv[1] AboutTarget_acctid = sys.argv[2] theMoney = Sys.argv[3] the  theconn =MySQLdb.connect ( +Host ='127.0.0.1', -Port = 3306, theuser ='njczy2010',Bayipasswd ='57040516', thedb ='Czy', theCharSet ='UTF8' -     ) -  theTr_money =TransferMoney (conn) the  the     Try: the Tr_money.transfer (Source_acctid,target_acctid,money) -     exceptException as E: the         Print "problems occurred"+Str (e) the     finally: theConn.close ()

Correct situation:

Check_acct_avaiable:Select *  fromAccountwhereAcctid=  Onecheck_acct_avaiable:Select *  fromAccountwhereAcctid=  AHas_enough_money:Select *  fromAccountwhereAcctid=  One  and  Money >  -Reduce_money:UpdateAccountSet  Money =  Money -  - whereAcctid=  OneAdd_money:UpdateAccountSet  Money =  Money +  - whereAcctid=  A

Error condition:

Check_acct_avaiable:Select *  fromAccountwhereAcctid=  Onecheck_acct_avaiable:Select *  fromAccountwhereAcctid=  AHas_enough_money:Select *  fromAccountwhereAcctid=  One  and  Money >  -There is a problem account 11 not enough money

Mu class python Operations database 2 example of bank transfer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.