PythonMySQL數(shù)據(jù)庫基本操作及項目示例詳解_第1頁
PythonMySQL數(shù)據(jù)庫基本操作及項目示例詳解_第2頁
PythonMySQL數(shù)據(jù)庫基本操作及項目示例詳解_第3頁
PythonMySQL數(shù)據(jù)庫基本操作及項目示例詳解_第4頁
PythonMySQL數(shù)據(jù)庫基本操作及項目示例詳解_第5頁
已閱讀5頁,還剩13頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第PythonMySQL數(shù)據(jù)庫基本操作及項目示例詳解#連接數(shù)據(jù)庫

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)

##插入數(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)

#輸入賬號和密碼

z=input("請輸入賬號:")

m=input("請輸入密碼:")

#從account表中進(jìn)行賬號和密碼的匹配

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庫

CREATE_SCHEMA_SQL='''

createschemabankcharsetutf8;

#創(chuàng)建account表

CREATE_TABLE_SQL='''

createtableaccount(

account_idvarchar(20)NOTNULL,

account_passwdchar(6)NOTNULL,

#decimal用于保存精確數(shù)字的類型,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類

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:

#把輸入賬號和密碼進(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("錯誤原因:",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("請輸入密碼:")

#輸入的參數(shù)傳入給Account類,并創(chuàng)建account對象

account=Account(from_account_id,from_account_passwd)

#調(diào)用check_account方法,進(jìn)行登錄檢查

ifaccount.check_account():

choose=input("請輸入操作:\n1、查詢余額\n2、取錢\n3、存錢\n4、取卡\n")

#當(dāng)輸入不等于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、加入查詢功能

存在銀行里的錢可能會產(chǎn)生利息,所以需要考慮余額為小數(shù)的問題,需要用到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函數(shù)進(jìn)行四舍五入,'0.00'表示保留兩位小數(shù)

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":

#調(diào)用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í)行數(shù)據(jù)庫,還要注意取錢需要考慮余額是否充足的問題

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參數(shù))

defreduce_money(self,money):

cursor=DB.cursor()

try:

#先調(diào)用query_money方法,查詢余額

has_money=self.query_money()

#所取金額小于余額則執(zhí)行(注意類型轉(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)行行計數(shù),行數(shù)為1則將數(shù)據(jù)提交給數(shù)據(jù)庫

ifcursor.rowcount==1:

DB.commit()

returnTrue

else:

#rollback數(shù)據(jù)庫回滾,行數(shù)不為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())

#調(diào)用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、加入存錢功能

存錢功能和取錢功能相似,而且不需要考慮余額的問題,至此已完善當(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("錯誤",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)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論