




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第PythonMySQL数据库基本操作及项目示例详解#连接数据库
db=pymysql.connect(host='localhost',user='root',password='1234',charset='utf8')
cursor=db.cursor()
#创建bank库
cursor.execute('createdatabasebankcharsetutf8;')
cursor.execute('usebank;')
##创建表
#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
#创建bank库
CREATE_SCHEMA_SQL='''
createschemabankcharsetutf8;
#创建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;
#创建银行账户
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
#创建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类,并创建account对象
account=Account(from_account_id,from_account_passwd)
#调用check_account方法,进行登录检查
ifaccount.check_account():
choose=input("请输入操作:\n1、查询余额\n2、取钱\n3、存钱\n4、取卡\n")
#当输入不等于4的时候执行,等于4则退出
whilechoose!="4":
#查询
ifchoose=="1":
print("111")
#取钱
elifchoose=="2":
print("222")
#存钱
elifchoose=="3":
print("333")
#上面操作完成之后,继续输入其他操作
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来执行数据库,还要注意取钱需要考虑余额是否充足的问题
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()
#所取金额小于余额则执行(注意类型转换)
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则不执行
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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论