#coding:utf-8import mysqldb #调用MySQL数据库模块conn =mysqldb.connect ( Host= ' ... ', #输入数据库的IP地址 port=3306, # Enter the database using port user= ' ... ', #输入数据库用户账号 passwd= ' ... ', #输入数据库用户密码 db= ' ... ', #输入数据库名称 charset= ' UTF8 ' #输入数据库编码 ) #转账函数def transfer (s_id,d_id,num): conn.autocommit (False) a=id_avi (s_ ID) #调用账号确认函数 b=id_avi (d_id) c=money_avi (s_id,num) # Call Balance Confirmation function if a and b and c: money_reduce (s_id,num) #调用减钱函数 money_add (d_id,num) #调用加钱函数 conn.commit () print u ' transfer success !' else: print u ' Transfer failed! ' conn.rollback () #存钱函数def add_in (id,num): a=id_avi (ID) if&nbsP;a: money_add (id,num) #调用加钱函数 conn.commit () else: Print u ' account%s does not exist, please check! ' %id conn.rollback () #取钱函数def reduce_out (id,num): a=id_avi (ID) b=money_avi (id,num) if a and b: money_reduce (id,num) #调用减钱函数 conn.commit () else: print u ' failed to withdraw money! Please check! ' conn.rollback () #账号确认函数def id_avi (ID): curr=conn.cursor () sql= ' select * from money where uid=%s '%id curr.execute (SQL) row=curr.rowcount if row==1: print u ' account%s confirmed success! ' %id return true else: print u ' account%s confirmation failed! ' %id return false curr.close () #账户余额确认函数def money_avi (id,num): curr=conn.cursor () sql= ' select money from money where uid=%s '%id curr.execute (SQL) n= Curr.fetchone ()  NN=N[0] IF NN>=NUM: &NBSp;print u ' account%s balance is enough! ' %id return true else: print u ' account%s balance is low! ' %id return false curr.close () #减钱函数def money_reduce (id,num): curr= Conn.cursor () sql= ' update money set money=money-%s where uid=%s '% (num,id) Curr.execute (SQL) print u ' turn out confirmation! ' curr.close () #加钱函数def money_add (id,num): curr=conn.cursor () sql= ' update money set money=money+%s where uid=%s '% (num,id) curr.execute (SQL) print u ' transfer to confirm! ' curr.close () #实例: Reduce_out (1,2000) transfer (2,1,1999)
Using Python to implement bank transfer functions