- 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()