一、基礎知識
1、MySQL-python的安裝
下載,然后 pip install 安裝包
2、python編寫通用數據庫程序的API規范
(1)、數據庫連接對象 connection,建立python客戶端與數據庫的網絡連接,創建方法為 MySQLdb.Connect(參數)
? ? ?參數有六個: ? ? host(MySQL服務器地址,一般本地為127.0.0.1)
? ? ? ? ? ? ? ? ? ? ? ? ? ? port(MySQL服務器端口號)
? ? ? ? ? ? ? ? ? ? ? ? ? ? user(用戶名)
? ? ? ? ? ? ? ? ? ? ? ? ? ? passwd(密碼)
? ? ? ? ? ? ? ? ? ? ? ? ? ? db(數據庫名稱)
? ? ? ? ? ? ? ? ? ? ? ? ? ? charset(連接編碼)
? ? ?connection的方法: cursor()使用該連接并返回游標
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? commit()提交當前事務
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? rollback()回滾當前事務
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? close()關閉連接
(2)、數據庫游標對象cursor,用于執行查詢和獲取結果
? ? 方法:execute(op[,args])執行一個數據庫查詢 和 命令
? ? ? ? ? ? ? fetchone()取得結果集的下一行
? ? ? ? ? ? ? fetchmany(size)獲取結果集的下幾行
? ? ? ? ? ? ? fetchall()獲取結果集中剩下的所有行
? ? ? ? ? ? ? rowcount 最近一次execute返回數據的行數或影響行數
? ? ? ? ? ? ? close()關閉游標對象
connection與cursor:connection相當于python與MySQL之間的路,而cursor相當于路上的運輸車來傳送命令與結果。
? ? ? ? ? ? ? ? ? ? ? ?
3、簡單命令:
select ?查詢數據:sql="select * from 表名 所查項目"
insert ?插入數據:sql=“insert into 表名 ?所插項目”
update 更改數據: sql=“updata 表名 set ?所改項目 ”
delete 刪除數據: sql=“delete from 表名 所刪項目”
where也是sql命令的關鍵存在,通常是 ?where 表頭=列名 來定位那一列
4、事務
訪問和更新數據庫的一個程序執行單元,所執行的命令,都可以稱為事務
具有原子性,一致性,隔離性,持久性
事務執行:
? ? ?conn.commit() 正常結束事務
? ? ?conn.rollback() 異常結束事務,對事務進行回滾,若程序執行單元中的連續的操作在進行中出錯,之前的操作還原。
簡單操作過程: ? ?開始?→?創建connection?→獲取cursor?→?程序執行單元?→?關閉cursor?→?關閉connection?→?結束
二、模擬銀行轉賬系統代碼
[python]?view plaincopy
?? import?sys?? import?MySQLdb?? ?? ''? ? ?? ?? class?Trans_for_Money(object):?? ?? ?????? ????def?__init__(self,conn):?? ????????self.conn?=?conn?? ?? ?????? ????def?check_acct_available(self,source_acctid):?? ?? ?????????? ????????cursor=self.conn.cursor()?? ????????try:?? ?????????????? ????????????sql="select?*?from?tr_money?where?acctid=%s"?%source_acctid?? ?????????????? ????????????cursor.execute(sql)?? ?????????????? ????????????print?"check_acct_available:"?+?sql?? ?????????????? ????????????result=cursor.fetchall()?? ????????????if?len(result)!=1:?? ????????????????raise?Exception("賬號%s不存在"?%source_acctid)?? ????????finally:?? ?????????????? ????????????cursor.close()?? ?? ?????? ????def?has_enough_money(self,source_acctid,money):?? ?? ????????cursor=self.conn.cursor()?? ????????try:?? ????????????sql="select?*?from?tr_money?where?acctid=%s?and?money>%s"?%(source_acctid,money)?? ????????????cursor.execute(sql)?? ????????????print?"has_enough_money:"?+?sql?? ????????????result=cursor.fetchall()?? ????????????if?len(result)!=1:?? ????????????????raise?Exception("賬號%s余額不足"?%source_acctid)?? ????????finally:?? ????????????cursor.close()?? ?? ?????? ????def?reduce_money(self,source_acctid,money):?? ?? ????????cursor=self.conn.cursor()?? ????????try:?? ?????????????? ????????????sql="update?tr_money?set?money=money-%s?where?acctid=%s"?%(money,source_acctid)?? ????????????cursor.execute(sql)?? ????????????print?"reduce_money:"?+?sql?? ?????????????? ????????????if?cursor.rowcount!=1:?? ????????????????raise?Exception("賬號%s減款失敗"?%source_acctid)?? ????????finally:?? ????????????cursor.close()?? ?? ?? ?????? ????def?add_money(self,target_acctid,money):?? ?? ????????cursor=self.conn.cursor()?? ????????try:?? ????????????sql="update?tr_money?set?money=money+%s?where?acctid?=%s"?%(money,target_acctid)?? ????????????cursor.execute(sql)?? ????????????print?"add_money:"?+?sql?? ?? ????????????if?cursor.rowcount!=1:?? ????????????????raise?Exception("賬號%s收款失敗"?%target_acctid)?? ????????finally:?? ????????????cursor.close()?? ?? ?????? ????def?trans_for(self,source_acctid,target_acctid,money):?? ????????try:?? ????????????self.check_acct_available(source_acctid)?? ????????????self.check_acct_available(target_acctid)?? ????????????self.has_enough_money(source_acctid,money)?? ????????????self.reduce_money(source_acctid,money)?? ????????????self.add_money(target_acctid,money)?? ?????????????? ????????????self.conn.commit()?? ????????except?Exception?as?e:?? ?????????????? ????????????self.conn.rollback()?? ????????????raise?e?? ?? ?? if?__name__=="__main__":?? ?? ?????? ?????? ?????? ?? ?????? ????conn?=?MySQLdb.Connect(?? ????????????????????????????host='127.0.0.1',?? ????????????????????????????port=3306,?? ????????????????????????????user='root',?? ????????????????????????????passwd='12345678',?? ????????????????????????????db='tt',?? ????????????????????????????charset='utf8'?? ??????????????????????????)?? ?? ?????? ????source_acctid=raw_input("請輸入減款人:?")?? ????target_acctid=raw_input("請輸入收款人:?")?? ????money=raw_input("請輸入轉款數:?")?? ?? ?????? ????tr_money=Trans_for_Money(conn)?? ????try:?? ????????tr_money.trans_for(source_acctid,target_acctid,money)?? ????except?Exception?as?e:?? ????????print"出現問題:"+str(e)?? ????finally:?? ????????conn.close()?? ??????????
三、問題解決
[python]?view plaincopy
??
1、sys.argv[ ] ?
因為教學視頻中用的IDE是MyEclipse,最后用run.Configuration 輸入參數,而我用的是pycharm,表示笨的找不到還是其實它沒有!
所以選擇用raw_input() 在執行過程中輸入參數
其實有去了解sys.argv[ ],但還是懂不太清楚。
2、 ?mysql_exceptions.IntegrityError: (1062, "Duplicate entry '7' for key 'PRIMARY'")
這個錯誤表示你所要插入的數據已經存在,最好去觀察一下數據庫的數據與自己的程序操作是否有矛盾
3、MySql 建表或輸入數值時出錯:1170-BLOB/TEXT column‘name’used in key specification without a key length
錯誤信息為BLOB或者TEXT字段使用了未指定鍵值長度的鍵
解決方法:設置其他為主鍵 ?或 ? 將數據形式改為varchar
具體解釋網址:http://myhblog1989.blog.163.com/blog/static/183225376201110875818884/
4、TypeError: 'post' is an invalid keyword argument for this function
錯誤原因:TypeError:?“post”是這個函數的無效參數
這個問題錯的很無語,一時腦子進水把 “port”=3306 寫成了“post”=‘3306’
5、1054, "Unknown column 'acctid' in 'where clause'
錯誤原因:在where子句中找不到“acctid”列
呵呵,上個錯誤腦子進的水沒排出來,把表頭寫錯了…………
6、另外,還有一個錯誤是手動輸入的減款,收款人設為字母或漢字時找不到
可能是我代碼或數據庫建表時的設定問題,表示在字符轉換和數據庫這方面還是小白一枚,繼續奮斗吧!
7、MySQL數據庫的啟動
計算機?→ 右鍵?→ 管理?→ 服務和應用程序?→ 服務?→ 找到MySQL?→ 右鍵啟動
四、具體執行顯示
1、數據庫 tr_money 表的初始狀態
2、代碼執行,輸入減款人,收款人,轉款數額
3、執行,結果出現代碼中特意 print 的操作進程顯示
4、數據庫 tr_money 表執行后狀態
總結
以上是生活随笔為你收集整理的python操作MySQL 模拟简单银行转账操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。