Python操作MySQL模擬銀行轉賬
今天在慕課網(wǎng)上學習了有關于python操作MySQL的相關知識,在此做些總結。python操作數(shù)據(jù)庫還是相對比較簡單的,由于python統(tǒng)一了各個數(shù)據(jù)庫的接口程序,也就是所謂的Python DB,所以無論使用何種數(shù)據(jù)可,都可以用統(tǒng)一的接口對數(shù)據(jù)庫進行操作。操作中主要涉及connection對象的操作和cursor的操作,前者主要是為了建立起python與數(shù)據(jù)庫的數(shù)據(jù)交換通道,后者則是訪問數(shù)據(jù)的游標,也可以理解為指針。數(shù)據(jù)庫的相關結構化語言在Python中均是以字符串的形式呈現(xiàn)的。另外注意rollback的重要性,一旦操作失敗,所有操作都要回滾到之前的狀態(tài),否則會發(fā)生錯誤。
另外,在編寫實例的時候,對于面向對象的編程思路又有了新的認識,自頂向下的程序編寫模式非常有利于拆分程序的功能,分而治之。面向對象的封裝性在此提醒的淋漓盡致!
代碼如下,在原有基礎上,我又增加了添加記錄的功能。
#coding=utf8
import MySQLdb
import sys
class TranseferMonet(object):
def __init__(self,conn):
self.conn = conn
def createNewUser(self,userID,money):
cursor = self.conn.cursor()
try:
sql = 'INSERT account VALUES(%s,%s)' %(str(userID),str(money))
cursor.execute(sql)
self.conn.commit()
except Exception as e:
self.conn.rollback()
raise e
def transferMoney(self,transeferID,recivierID,money):
try:
self.checkID(transeferID)
self.checkID(receiverID)
self.checkEnoughMoney(transferID,money)
self.subMoney(transferID,money)
self.addMoney(receiverID,money)
self.conn.commit()
except Exception as e:
self.conn.rollback()
raise e
def checkID(self,userID):
cursor = self.conn.cursor()
try:
sql = 'SELECT userID FROM account WHERE userID = %s' %str(userID)
cursor.execute(sql)
rs = cursor.fetchall()
if len(rs) != 1:
raise Exception("ID錯誤!")
finally:
cursor.close()
def checkEnoughMoney(self,transferID,money):
cursor = self.conn.cursor()
try:
sql = 'SELECT money FROM account WHERE userID = %s and money >= %s' %(str(transferID),str(money))
cursor.execute(sql)
rs = cursor.fetchall()
if len(rs) != 1:
raise Exception("余額不足!")
finally:
cursor.close()
def subMoney(self,transferID,money):
cursor = self.conn.cursor()
try:
sql = 'UPDATE account SET money = money-%s WHERE userID = %s' %(str(money),str(transferID))
cursor.execute(sql)
if cursor.rowcount != 1:
raise Exception('減款失??!')
finally:
cursor.close()
def addMoney(self,receiverID,money):
cursor = self.conn.cursor()
try:
sql = 'UPDATE account SET money = money+%s WHERE userID = %s' %(str(money),str(receiverID))
cursor.execute(sql)
if cursor.rowcount != 1:
raise Exception('加款失??!')
finally:
cursor.close()
if __name__=="__main__":
transferID = 2002
receiverID = 2001
money = 300
newID = 2003
newmoney = 900
conn = MySQLdb.connect(host = '127.0.0.1',port = 3306,user = 'root',passwd = '914767195',db = 'test',charset = 'utf8')
trMoney = TranseferMonet(conn)
try:
trMoney.transferMoney(transferID,receiverID,money)
except Exception as e:
print "轉賬錯誤"+str(e)
try:
trMoney.createNewUser(newID,newmoney)
except Exception as e:
print "創(chuàng)建用戶失??!"+str(e)
finally:
conn.close()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python實現(xiàn)簡單學生信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了python簡單的學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04
使用Python和OpenCV實現(xiàn)動態(tài)背景的畫中畫效果
這篇文章將通過一個詳細的Python腳本,使用OpenCV庫來為視頻添加動態(tài)背景,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-11-11
已安裝tensorflow-gpu,但keras無法使用GPU加速的解決
今天小編就為大家分享一篇已安裝tensorflow-gpu,但keras無法使用GPU加速的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
mvc框架打造筆記之wsgi協(xié)議的優(yōu)缺點以及接口實現(xiàn)
這篇文章主要給大家介紹了關于mvc框架打造筆記之wsgi協(xié)議的優(yōu)缺點以及接口實現(xiàn)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08
單步調試 step into/step out/step over 區(qū)
這篇文章主要介紹了單步調試 step into/step out/step over 區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Python使用xlrd和xlwt批量讀寫excel文件的示例代碼
這篇文章主要介紹了Python使用xlrd和xlwt批量讀寫excel文件,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03

