




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第PythonMySQL數(shù)據(jù)庫(kù)基本操作及項(xiàng)目示例詳解#連接數(shù)據(jù)庫(kù)
db=pymysql.connect(host='localhost',user='root',password='1234',charset='utf8')
cursor=db.cursor()
#創(chuàng)建bank庫(kù)
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)
##插入數(shù)據(jù)
#insert_sql='''
#insertintoaccountvalues('001','123456',1000.00),('002','456789',5000.00)
#'''
#cursor.execute(insert_sql)
#mit()
##查詢所有數(shù)據(jù)
#cursor.execute('select*fromaccount')
#all=cursor.fetchall()
#foriinall:
#print(i)
#輸入賬號(hào)和密碼
z=input("請(qǐng)輸入賬號(hào):")
m=input("請(qǐng)輸入密碼:")
#從account表中進(jìn)行賬號(hào)和密碼的匹配
cursor.execute('select*fromaccountwhereaccount_id=%sandaccount_passwd=%s',(z,m))
#如果找到,則登錄成功
ifcursor.fetchall():
print('登錄成功')
else:
print('登錄失敗')
exceptExceptionase:
print(e)
finally:
cursor.close()
db.close()
1、進(jìn)行初始化操作
importpymysql
#創(chuàng)建bank庫(kù)
CREATE_SCHEMA_SQL='''
createschemabankcharsetutf8;
#創(chuàng)建account表
CREATE_TABLE_SQL='''
createtableaccount(
account_idvarchar(20)NOTNULL,
account_passwdchar(6)NOTNULL,
#decimal用于保存精確數(shù)字的類(lèi)型,decimal(10,2)表示總位數(shù)最大為12位,其中整數(shù)10位,小數(shù)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()
#不讓別人調(diào)用
if__name__=="__main__":
init()
2、登錄檢查,并選擇操作
importpymysql
#定義全局變量為空
DB=None
#創(chuàng)建Account類(lèi)
classAccount():
#傳入?yún)?shù)
def__init__(self,account_id,account_passwd):
self.account_id=account_id
self.account_passwd=account_passwd
#登錄檢查
defcheck_account(self):
cursor=DB.cursor()
try:
#把輸入賬號(hào)和密碼進(jìn)行匹配(函數(shù)體內(nèi)部傳入?yún)?shù)用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("錯(cuò)誤原因:",e)
finally:
cursor.close()
#查詢余額
#defquery_money
#取錢(qián)
#defreduce_money
#存錢(qián)
#defadd_money
defmain():
#定義全局變量
globalDB
#連接bank庫(kù)
DB=pymysql.connect(host="localhost",user="root",passwd="1234",database="bank")
cursor=DB.cursor()
#輸入賬號(hào)和密碼
from_account_id=input("請(qǐng)輸入賬號(hào):")
from_account_passwd=input("請(qǐng)輸入密碼:")
#輸入的參數(shù)傳入給Account類(lèi),并創(chuàng)建account對(duì)象
account=Account(from_account_id,from_account_passwd)
#調(diào)用check_account方法,進(jìn)行登錄檢查
ifaccount.check_account():
choose=input("請(qǐng)輸入操作:\n1、查詢余額\n2、取錢(qián)\n3、存錢(qián)\n4、取卡\n")
#當(dāng)輸入不等于4的時(shí)候執(zhí)行,等于4則退出
whilechoose!="4":
#查詢
ifchoose=="1":
print("111")
#取錢(qián)
elifchoose=="2":
print("222")
#存錢(qián)
elifchoose=="3":
print("333")
#上面操作完成之后,繼續(xù)輸入其他操作
choose=input("請(qǐng)輸入操作:\n1、查詢余額\n2、取錢(qián)\n3、存錢(qián)\n4、取卡\n")
else:
print("謝謝使用!")
else:
print("賬號(hào)或密碼錯(cuò)誤")
DB.close()
main()
3、加入查詢功能
存在銀行里的錢(qián)可能會(huì)產(chǎn)生利息,所以需要考慮余額為小數(shù)的問(wèn)題,需要用到decimal庫(kù)
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("錯(cuò)誤",e)
finally:
cursor.close()
#查詢余額
defquery_money(self):
cursor=DB.cursor()
try:
#匹配賬號(hào)密碼,并返回money
SQL="selectmoneyfromaccountwhereaccount_id=%sandaccount_passwd=%s"%(self.account_id,self.account_passwd)
cursor.execute(SQL)
money=cursor.fetchone()[0]
#如果賬戶有錢(qián)就返回金額,沒(méi)錢(qián)返回0.00
ifmoney:
#返回值為decimal類(lèi)型,quantize函數(shù)進(jìn)行四舍五入,'0.00'表示保留兩位小數(shù)
returnstr(money.quantize(decimal.Decimal('0.00')))
else:
return0.00
exceptExceptionase:
print("錯(cuò)誤原因",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("請(qǐng)輸入賬號(hào):")
from_account_passwd=input("請(qǐng)輸入密碼:")
account=Account(from_account_id,from_account_passwd)
ifaccount.check_account():
choose=input("請(qǐng)輸入操作:\n1、查詢余額\n2、取錢(qián)\n3、存錢(qián)\n4、取卡\n")
whilechoose!="4":
#查詢
ifchoose=="1":
#調(diào)用query_money方法
print("您的余額是%s元"%account.query_money())
#取錢(qián)
elifchoose=="2":
print("222")
#存錢(qián)
elifchoose=="3":
print("333")
choose=input("請(qǐng)輸入操作:\n1、查詢余額\n2、取錢(qián)\n3、存錢(qián)\n4、取卡\n")
else:
print("謝謝使用")
else:
print("賬號(hào)或密碼錯(cuò)誤")
DB.close()
main()
4、加入取錢(qián)功能
取錢(qián)存錢(qián)要用update來(lái)執(zhí)行數(shù)據(jù)庫(kù),還要注意取錢(qián)需要考慮余額是否充足的問(wèn)題
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("錯(cuò)誤",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("錯(cuò)誤原因",e)
finally:
cursor.close()
#取錢(qián)(注意傳入money參數(shù))
defreduce_money(self,money):
cursor=DB.cursor()
try:
#先調(diào)用query_money方法,查詢余額
has_money=self.query_money()
#所取金額小于余額則執(zhí)行(注意類(lèi)型轉(zhuǎn)換)
ifdecimal.Decimal(money)=decimal.Decimal(has_money):
#進(jìn)行數(shù)據(jù)更新操作
SQL="updateaccountsetmoney=money-%swhereaccount_id=%sandaccount_passwd=%s"%(money,self.account_id,self.account_passwd)
cursor.execute(SQL)
#rowcount進(jìn)行行計(jì)數(shù),行數(shù)為1則將數(shù)據(jù)提交給數(shù)據(jù)庫(kù)
ifcursor.rowcount==1:
DB.commit()
returnTrue
else:
#rollback數(shù)據(jù)庫(kù)回滾,行數(shù)不為1則不執(zhí)行
DB.rollback()
returnFalse
else:
print("余額不足")
exceptExceptionase:
print("錯(cuò)誤原因",e)
finally:
cursor.close()
#存錢(qián)
#defadd_money
defmain():
globalDB
DB=pymysql.connect(host="localhost",user="root",passwd="1234",charset="utf8",database="bank")
cursor=DB.cursor()
from_account_id=input("請(qǐng)輸入賬號(hào):")
from_account_passwd=input("請(qǐng)輸入密碼:")
account=Account(from_account_id,from_account_passwd)
ifaccount.check_account():
choose=input("請(qǐng)輸入操作:\n1、查詢余額\n2、取錢(qián)\n3、存錢(qián)\n4、取卡\n")
whilechoose!="4":
#查詢
ifchoose=="1":
print("您的余額是%s元"%account.query_money())
#取錢(qián)
elifchoose=="2":
#先查詢余額,再輸入取款金額,防止取款金額大于余額
money=input("您的余額是%s元,請(qǐng)輸入取款金額"%account.query_money())
#調(diào)用reduce_money方法,money不為空則取款成功
ifaccount.reduce_money(money):
print("取款成功,您的余額還有%s元"%account.query_money())
else:
print("取款失??!")
#存錢(qián)
elifchoose=="3":
print("333")
choose=input("請(qǐng)輸入操作:\n1、查詢余額\n2、取錢(qián)\n3、存錢(qián)\n4、取卡\n")
else:
print("謝謝使用!")
else:
print("賬號(hào)或密碼錯(cuò)誤")
DB.close()
main()
5、加入存錢(qián)功能
存錢(qián)功能和取錢(qián)功能相似,而且不需要考慮余額的問(wèn)題,至此已完善當(dāng)前所有功能
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("錯(cuò)誤",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("錯(cuò)誤原因",e)
finally:
cursor.close()
#取錢(qián)
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("錯(cuò)誤原因",e)
finally:
cursor.close()
#存錢(qián)
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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 美術(shù)課件兒童樂(lè)園
- 美術(shù)生班會(huì)課課件
- 幼兒園交通事故應(yīng)急預(yù)案
- 企業(yè)信息安全管理體系認(rèn)證
- 建筑工程起重機(jī)械安全監(jiān)督管理規(guī)定
- 電力工程施工安全管控措施
- 建筑安全體驗(yàn)館建設(shè)方案
- 醫(yī)院開(kāi)展安全生產(chǎn)月活動(dòng)
- 2025年咖啡連鎖經(jīng)營(yíng)項(xiàng)目規(guī)劃申請(qǐng)報(bào)告模板
- 2025至2030全球及中國(guó)移動(dòng)錢(qián)包行業(yè)項(xiàng)目調(diào)研及市場(chǎng)前景預(yù)測(cè)評(píng)估報(bào)告
- 2025年全國(guó)低壓電工作業(yè)證(復(fù)審)考試練習(xí)題庫(kù)(600題)附答案
- 軟件開(kāi)發(fā)文檔-電子政務(wù)云服務(wù)平臺(tái)系統(tǒng)招標(biāo)文件范本
- 抖音本地生活服務(wù)方案
- PET考試培訓(xùn)課件
- 新修河堤河道治理新建攔水壩生態(tài)修復(fù)主體工程施工方案及關(guān)鍵性技術(shù)措
- 2022年寧夏回族自治區(qū)7月普通高中學(xué)業(yè)水平測(cè)試生物試卷會(huì)考試題及答案
- 水利工程項(xiàng)目管理全過(guò)程流程圖
- 婚紗攝影市場(chǎng)分析與前景預(yù)測(cè)
- 敦煌藝術(shù)在小學(xué)美術(shù)教學(xué)中的融入與教學(xué)實(shí)踐
- 舞蹈治療研究-深度研究
- 2026年日歷表全年表(含農(nóng)歷、周數(shù)、節(jié)假日及調(diào)休-A4紙可直接打印)-
評(píng)論
0/150
提交評(píng)論