import pymysql
from common.do_config import cfg
import logging
class HandleMysql:
def __init__(self):
self.conn = pymysql.connect(host = cfg.get_values('db','host'),
user = cfg.get_values('db','user'),
password = cfg.get_values('db','password'),
database = cfg.get_values('db','database'),
port = cfg.get_int('db','port'),
charset = cfg.get_values('db','charset'),
cursorclass =pymysql.cursors.DictCursor)
self.cur = self.conn.cursor()
def query_sql(self,sql,args=None,is_all=False):
"""
查询sql
:param sql:
:param args:
:param is_all:
:return:
"""
self.cur.execute(sql,args)
self.conn.commit()
if is_all:
return self.cur.fetchall()
else:
return self.cur.fetchone()
def insert_update_delete(self,sqls):
"""
增删改
:param sql:
:return:
"""
try:
for i in sqls:
self.cur.execute(i)
except Exception as e:
print("执行增删改有错,错误是{},需要回滚".format(e))
self.conn.rollback()
# 增删改操作有误时回滚操作
else:
self.conn.commit()
print("事务提交成功")