-
python3连接MySQL数据库
- import pymysql
-
- class MYSQL:
-
- def __init__(self,host,user,pwd,dbname):
- self.host = host
- self.user = user
- self.pwd = pwd
- self.dbname = dbname
-
-
-
- def getCursor(self):
-
-
- self.db = pymysql.connect(self.host,self.user,self.pwd,self.dbname)
-
-
- cur = self.db.cursor()
-
-
- return cur
-
-
- def queryOperation(self,sql):
-
-
- cur = self.getCursor()
-
-
- cur.execute(sql)
-
-
- row = cur.rowcount
-
-
-
-
- dataList = cur.fetchall()
-
-
- cur.close()
-
-
- self.db.close()
-
-
- return dataList,row
-
-
- def deleteOperation(self,sql):
-
-
- cur = self.getCursor()
- try:
-
- cur.execute(sql)
-
-
- self.db.commit()
-
- except Exception as e:
- print(e)
-
-
- self.db.rollback()
-
-
- cur.close()
-
-
- self.db.close()
-
-
- def updateOperation(self,sql):
- cur = self.getCursor()
- try:
- cur.execute(sql)
- self.db.commit()
- except Exception as e:
- print(e)
- self.db.rollback()
-
- cur.close()
- self.db.close()
-
-
- def insertOperation(self,sql):
-
- cur = self.getCursor()
- try:
- cur.execute(sql)
- self.db.commit()
- except Exception as e:
- print(e)
- self.db.rollback()
-
- cur.close()
- self.db.close()
-
相关阅读:
日月
硕人
式微
芣苡
樛木
兔罝
绿衣
汉广
小星
惠子相梁
-
原文地址:https://www.cnblogs.com/meng-wei-zhi/p/8214745.html
Copyright © 2020-2023
润新知