from pymysql import *
class MysqlHelper:
def __init__(self,host,db,user,passwd,port=3306,charset='utf8'):
self.host=host
self.port=port
self.db=db
self.user=user
self.passwd=passwd
self.charset=charset
def connect(self):
self.conn=connect(host=self.host,
port=self.port,
db=self.db,
user=self.user,
passwd=self.passwd)
self.cursor=self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def crud(self,sql,params):
try:
self.connect()
self.cursor.execute(sql,params)
self.conn.commit()
self.close()
print('ok')
except Exception as e:
print(e)
def all(self,sql):
try:
self.connect()
self.cursor.execute(sql)
result = self.cursor.fetchall()
self.close()
return result
except Exception as e:
print(e)
pymysql简单封装
最新推荐文章于 2023-03-09 17:11:47 发布