




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第PythonMySQL數據庫基本操作及項目示例詳解#連接數據庫
db=pymysql.connect(host='localhost',user='root',password='1234',charset='utf8')
cursor=db.cursor()
#創(chuàng)建bank庫
cursor.execute('createdatabasebankcharsetutf8;')
cursor.execute('usebank;')
##創(chuàng)建表
#sql='''createtableaccount(
#account_idvarchar(20)NOTNULL,
#account_passwdchar(6)NOTNULL,
#moneydecimal(10,2),
#primarykey(account_id)
#);'''
#cursor.execute(sql)
##插入數據
#insert_sql='''
#insertintoaccountvalues('001','123456',1000.00),('002','456789',5000.00)
#'''
#cursor.execute(insert_sql)
#mit()
##查詢所有數據
#cursor.execute('select*fromaccount')
#all=cursor.fetchall()
#foriinall:
#print(i)
#輸入賬號和密碼
z=input("請輸入賬號:")
m=input("請輸入密碼:")
#從account表中進行賬號和密碼的匹配
cursor.execute('select*fromaccountwhereaccount_id=%sandaccount_passwd=%s',(z,m))
#如果找到,則登錄成功
ifcursor.fetchall():
print('登錄成功')
else:
print('登錄失敗')
exceptExceptionase:
print(e)
finally:
cursor.close()
db.close()
1、進行初始化操作
importpymysql
#創(chuàng)建bank庫
CREATE_SCHEMA_SQL='''
createschemabankcharsetutf8;
#創(chuàng)建account表
CREATE_TABLE_SQL='''
createtableaccount(
account_idvarchar(20)NOTNULL,
account_passwdchar(6)NOTNULL,
#decimal用于保存精確數字的類型,decimal(10,2)表示總位數最大為12位,其中整數10位,小數2位
moneydecimal(10,2),
primarykey(account_id)
)defaultcharset=utf8;
#創(chuàng)建銀行賬戶
CREATE_ACCOUNT_SQL='''
insertintoaccountvalues('001','123456',1000.00),('002','456789',5000.00);
#初始化
definit():
try:
DB=pymysql.connect(host='localhost',user='root',password='1234',charset='utf8')
cursor1=DB.cursor()
cursor1.execute(CREATE_SCHEMA_SQL)
DB=pymysql.connect(host='localhost',user='root',password='1234',charset='utf8',database='bank')
cursor2=DB.cursor()
cursor2.execute(CREATE_TABLE_SQL)
cursor2.execute(CREATE_ACCOUNT_SQL)
DB.commit()
print('初始化成功')
exceptExceptionase:
print('初始化失敗',e)
finally:
cursor1.close()
cursor2.close()
DB.close()
#不讓別人調用
if__name__=="__main__":
init()
2、登錄檢查,并選擇操作
importpymysql
#定義全局變量為空
DB=None
#創(chuàng)建Account類
classAccount():
#傳入參數
def__init__(self,account_id,account_passwd):
self.account_id=account_id
self.account_passwd=account_passwd
#登錄檢查
defcheck_account(self):
cursor=DB.cursor()
try:
#把輸入賬號和密碼進行匹配(函數體內部傳入參數用self.)
SQL="select*fromaccountwhereaccount_id=%sandaccount_passwd=%s"%(self.account_id,self.account_passwd)
cursor.execute(SQL)
#匹配成功返回True,失敗返回False
ifcursor.fetchall():
returnTrue
else:
returnFalse
exceptExceptionase:
print("錯誤原因:",e)
finally:
cursor.close()
#查詢余額
#defquery_money
#取錢
#defreduce_money
#存錢
#defadd_money
defmain():
#定義全局變量
globalDB
#連接bank庫
DB=pymysql.connect(host="localhost",user="root",passwd="1234",database="bank")
cursor=DB.cursor()
#輸入賬號和密碼
from_account_id=input("請輸入賬號:")
from_account_passwd=input("請輸入密碼:")
#輸入的參數傳入給Account類,并創(chuàng)建account對象
account=Account(from_account_id,from_account_passwd)
#調用check_account方法,進行登錄檢查
ifaccount.check_account():
choose=input("請輸入操作:\n1、查詢余額\n2、取錢\n3、存錢\n4、取卡\n")
#當輸入不等于4的時候執(zhí)行,等于4則退出
whilechoose!="4":
#查詢
ifchoose=="1":
print("111")
#取錢
elifchoose=="2":
print("222")
#存錢
elifchoose=="3":
print("333")
#上面操作完成之后,繼續(xù)輸入其他操作
choose=input("請輸入操作:\n1、查詢余額\n2、取錢\n3、存錢\n4、取卡\n")
else:
print("謝謝使用!")
else:
print("賬號或密碼錯誤")
DB.close()
main()
3、加入查詢功能
存在銀行里的錢可能會產生利息,所以需要考慮余額為小數的問題,需要用到decimal庫
importpymysql
#引入decimal模塊
importdecimal
DB=None
classAccount():
def__init__(self,account_id,account_passwd):
self.account_id=account_id
self.account_passwd=account_passwd
#登錄檢查
defcheck_account(self):
cursor=DB.cursor()
try:
SQL="select*fromaccountwhereaccount_id=%sandaccount_passwd=%s"%(self.account_id,self.account_passwd)
cursor.execute(SQL)
ifcursor.fetchall():
returnTrue
else:
returnFalse
exceptExceptionase:
print("錯誤",e)
finally:
cursor.close()
#查詢余額
defquery_money(self):
cursor=DB.cursor()
try:
#匹配賬號密碼,并返回money
SQL="selectmoneyfromaccountwhereaccount_id=%sandaccount_passwd=%s"%(self.account_id,self.account_passwd)
cursor.execute(SQL)
money=cursor.fetchone()[0]
#如果賬戶有錢就返回金額,沒錢返回0.00
ifmoney:
#返回值為decimal類型,quantize函數進行四舍五入,'0.00'表示保留兩位小數
returnstr(money.quantize(decimal.Decimal('0.00')))
else:
return0.00
exceptExceptionase:
print("錯誤原因",e)
finally:
cursor.close()
defmain():
globalDB
DB=pymysql.connect(host="localhost",user="root",passwd="1234",charset="utf8",database="bank")
cursor=DB.cursor()
from_account_id=input("請輸入賬號:")
from_account_passwd=input("請輸入密碼:")
account=Account(from_account_id,from_account_passwd)
ifaccount.check_account():
choose=input("請輸入操作:\n1、查詢余額\n2、取錢\n3、存錢\n4、取卡\n")
whilechoose!="4":
#查詢
ifchoose=="1":
#調用query_money方法
print("您的余額是%s元"%account.query_money())
#取錢
elifchoose=="2":
print("222")
#存錢
elifchoose=="3":
print("333")
choose=input("請輸入操作:\n1、查詢余額\n2、取錢\n3、存錢\n4、取卡\n")
else:
print("謝謝使用")
else:
print("賬號或密碼錯誤")
DB.close()
main()
4、加入取錢功能
取錢存錢要用update來執(zhí)行數據庫,還要注意取錢需要考慮余額是否充足的問題
importpymysql
importdecimal
DB=None
classAccount():
def__init__(self,account_id,account_passwd):
self.account_id=account_id
self.account_passwd=account_passwd
#登錄檢查
defcheck_account(self):
cursor=DB.cursor()
try:
SQL="select*fromaccountwhereaccount_id=%sandaccount_passwd=%s"%(self.account_id,self.account_passwd)
cursor.execute(SQL)
ifcursor.fetchall():
returnTrue
else:
returnFalse
exceptExceptionase:
print("錯誤",e)
finally:
cursor.close()
#查詢余額
defquery_money(self):
cursor=DB.cursor()
try:
SQL="selectmoneyfromaccountwhereaccount_id=%sandaccount_passwd=%s"%(self.account_id,self.account_passwd)
cursor.execute(SQL)
money=cursor.fetchone()[0]
ifmoney:
returnstr(money.quantize(decimal.Decimal('0.00')))
else:
return0.00
exceptExceptionase:
print("錯誤原因",e)
finally:
cursor.close()
#取錢(注意傳入money參數)
defreduce_money(self,money):
cursor=DB.cursor()
try:
#先調用query_money方法,查詢余額
has_money=self.query_money()
#所取金額小于余額則執(zhí)行(注意類型轉換)
ifdecimal.Decimal(money)=decimal.Decimal(has_money):
#進行數據更新操作
SQL="updateaccountsetmoney=money-%swhereaccount_id=%sandaccount_passwd=%s"%(money,self.account_id,self.account_passwd)
cursor.execute(SQL)
#rowcount進行行計數,行數為1則將數據提交給數據庫
ifcursor.rowcount==1:
DB.commit()
returnTrue
else:
#rollback數據庫回滾,行數不為1則不執(zhí)行
DB.rollback()
returnFalse
else:
print("余額不足")
exceptExceptionase:
print("錯誤原因",e)
finally:
cursor.close()
#存錢
#defadd_money
defmain():
globalDB
DB=pymysql.connect(host="localhost",user="root",passwd="1234",charset="utf8",database="bank")
cursor=DB.cursor()
from_account_id=input("請輸入賬號:")
from_account_passwd=input("請輸入密碼:")
account=Account(from_account_id,from_account_passwd)
ifaccount.check_account():
choose=input("請輸入操作:\n1、查詢余額\n2、取錢\n3、存錢\n4、取卡\n")
whilechoose!="4":
#查詢
ifchoose=="1":
print("您的余額是%s元"%account.query_money())
#取錢
elifchoose=="2":
#先查詢余額,再輸入取款金額,防止取款金額大于余額
money=input("您的余額是%s元,請輸入取款金額"%account.query_money())
#調用reduce_money方法,money不為空則取款成功
ifaccount.reduce_money(money):
print("取款成功,您的余額還有%s元"%account.query_money())
else:
print("取款失敗!")
#存錢
elifchoose=="3":
print("333")
choose=input("請輸入操作:\n1、查詢余額\n2、取錢\n3、存錢\n4、取卡\n")
else:
print("謝謝使用!")
else:
print("賬號或密碼錯誤")
DB.close()
main()
5、加入存錢功能
存錢功能和取錢功能相似,而且不需要考慮余額的問題,至此已完善當前所有功能
importpymysql
importdecimal
DB=None
classAccount():
def__init__(self,account_id,account_passwd):
self.account_id=account_id
self.account_passwd=account_passwd
#登錄檢查
defcheck_account(self):
cursor=DB.cursor()
try:
SQL="select*fromaccountwhereaccount_id=%sandaccount_passwd=%s"%(self.account_id,self.account_passwd)
cursor.execute(SQL)
ifcursor.fetchall():
returnTrue
else:
returnFalse
exceptExceptionase:
print("錯誤",e)
finally:
cursor.close()
#查詢余額
defquery_money(self):
cursor=DB.cursor()
try:
SQL="selectmoneyfromaccountwhereaccount_id=%sandaccount_passwd=%s"%(self.account_id,self.account_passwd)
cursor.execute(SQL)
money=cursor.fetchone()[0]
ifmoney:
returnstr(money.quantize(decimal.Decimal('0.00')))
else:
return0.00
exceptExceptionase:
print("錯誤原因",e)
finally:
cursor.close()
#取錢
defreduce_money(self,money):
cursor=DB.cursor()
try:
has_money=self.query_money()
ifdecimal.Decimal(money)=decimal.Decimal(has_money):
SQL="updateaccountsetmoney=money-%swhereaccount_id=%sandaccount_passwd=%s"%(money,self.account_id,self.account_passwd)
cursor.execute(SQL)
ifcursor.rowcount==1:
DB.commit()
returnTrue
else:
DB.rollback()
returnFalse
else:
print("余額不足")
exceptExceptionase:
print("錯誤原因",e)
finally:
cursor.close()
#存錢
defadd_money(self,money):
cursor=DB.cursor()
try:
SQL="updateaccountsetmoney=money+%swhereaccount_id=%sandaccount_passwd=%s"%(money,self.account_id,self.account_passwd)
cursor.execute(SQL)
ifcursor.rowcount==1:
DB.commit()
returnTrue
else:
DB.rollback()
returnFalse
exceptExceptionase:
DB.rollback()
pri
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 車庫做倉庫合同協(xié)議
- 辯論賽協(xié)議合同協(xié)議
- 轉租合同定金合同協(xié)議
- 轉讓加油站合同協(xié)議
- 車輛運營補貼合同協(xié)議
- 車床維修外包合同協(xié)議
- 毀林調解協(xié)議書
- 油船建造協(xié)議書
- 個人擔保定金合同書
- 個性化農業(yè)技術服務合同
- 大班科學《神奇的洞洞》課件
- 公司工作交接清單表格
- 季節(jié)性防雷防汛防臺風安全檢查表
- 歸檔文件目錄
- 2022年四川省綿陽市中考英語試題及參考答案
- 防疫小組人員名單
- NYT 393-綠色食品 農藥使用準則
- 偏心塊振動式土壤夯實機的結構設計說明
- 主題班會《堅定信念--放飛理想》
- 實驗幼兒園大三班一周活動計劃表
- 緬懷申齊創(chuàng)始人——吳齊南先生
評論
0/150
提交評論