
面向对象数据库
药房工作制度-天吊
2023年3月18日发(作者:拼音拼读练习)⾯向对象式python代码连接mysql数据库
importpymysql
simportDictCursor
frompymysqlimportError
"""
以⾯向对象的⽅式整理⼀下python链接数据库
"""
classPython_Mysql(object):
def__init__(self):
=t_db()
defconnect_db(self):
#创建链接
try:
conn=t(
host="127.0.0.1",
user="root",
password="root",
database="my_news",
port=3306,
cursorclass=DictCursor
)
returnconn
exceptErrorase:
print(e)
defclose_conn(self):
#关闭链接
try:
notNone:
()
print("数据库关闭成功")
exceptExceptionase:
print("数据库关闭失败")
defget_one(self):
#sql="select*fromuser;"
sql="select*fromuserwherename=%s;"#防⽌sql的注⼊
()ascursor:
e(sql,("lili"))
result=ne()#查询⼀条数据
##result=ll()#查询所有的数据
#result=any(3)#查询多个的数据
returnresult
defassert_one(self):
sql='insertintouser(name,pwd)VALUES("lili","12345");'
()ascursor:
e(sql)
()
print("插⼊数据完成")
defupdate_one(self):
sql='UPDATEusersetname="l78ili"whereid=7;'
()ascursor:
e(sql)
()
print("插⼊数据完成")
defshiwu(self):
sql1='UPDATEusersetname="l9ili"whereid=7;'
sql2='UPDATEusersetname="lli"whereid=8;'
()ascursor:
()ascursor:
()
e(sql1)
e(sql2)
()
()
print("插⼊数据完成")
defmain():
pymysql_1=Python_Mysql()
result=pymysql_()
#关闭链接
pymysql__conn()
if__name__=="__main__":
main()
--来⾃盘超级会员V2的分享
hi,这是我⽤⽹盘分享的内容~复制这段内容打开「百度⽹盘」APP即可获取
提取码:s99d