#coding:utf-8
import pymysql
class conn():
def __init__(self):
self.conn= pymysql.connect(host='localhost',port=3306,user='root',password='',database='wenhuafan',charset='utf8')
self.cursor=self.conn.cursor()
def __del__(self):
self.cursor.close()
self.conn.close()
class_name = self.__class__.__name__
# print(class_name, "销毁")
def exists(self,sql):
try:
# 开启事务;
self.conn.begin()
self.cursor.execute(sql)
data =self.cursor.fetchall()
if not data:
return False
else:
return True
except Exception as e:
# 若有异常就回滚;
self.conn.rollback()
print(e)
return False
def select(self,sql):
try:
# 开启事务;
self.conn.begin()
self.cursor.execute(sql)
data =self.cursor.fetchall()
if not data:
return False
else:
return data
except Exception as e:
# 若有异常就回滚;
self.conn.rollback()
print(e)
return False
def execute(self,sql):
try:
# 开启事务;
self.conn.begin()
self.cursor.execute(sql)
# 提交事务;
self.conn.commit()
print('数据成功入库!')
except Exception as e:
# 若有异常就回滚;
self.conn.rollback()
print(e)