import pymysql
class SQLHepler:
def __init__(self, USER, PASSWORD, DATABASE, HOST='127.0.0.1', CHARSET='utf8'):
self.HOST = HOST
self.USER = USER
self.PASSWORD = PASSWORD
self.DATABASE = DATABASE
self.CHARSET = CHARSET
def get_conn(self):
conn = pymysql.connect(host=self.HOST, user=self.USER, password=self.PASSWORD, db=self.DATABASE, charset=self.CHARSET)
cur = conn.cursor()
return conn, cur
'''
功能:单向操作,主要用于(增加,删除,修改)
参数:安全的sql语句
'''
def get_excute_Non_query(self, sql):
conn, cur = self.get_conn()
try:
cur.execute(sql)
conn.commit()
except:
print('查询失败')
conn.rollback()
conn.close()
'''
功能:查询数据库中数据
参数:安全的sql语句
'''
def get_data_query(self, sql):
conn, cur = self.get_conn()
data = None
try:
cur.execute(sql)
data = cur.fetchall()
except:
print('查询失败')
conn.close()
return data