import pymysql from Utils.config_handler import base_config class SqlHandler: def __init__(self,address,port,password,account,database = 'willdesk'): self.address = address self.port = port self.password = password self.account = account self.database = database self.conn = pymysql.connect(host=address,port=port,user=account,password=password,database=database,cursorclass=pymysql.cursors.DictCursor,connect_timeout=60,read_timeout=120) self.cursor = self.conn.cursor() def select_one_value(self,sql): self.cursor.execute(sql) result = self.cursor.fetchone() return result def select_many_value(self, sql): self.cursor.execute(sql) result = self.cursor.fetchall() return result def execute_sql(self,sql): try: self.cursor.execute(sql) self.conn.commit() except Exception: self.conn.rollback() def close_db(self): print('已关闭') self.cursor.close() self.conn.close() test_env_conn = SqlHandler(address=base_config.get_value('mysql','address'),port=int(base_config.get_value('mysql','port')),account=base_config.get_value('mysql','account'),password=base_config.get_value('mysql','password'))