from traceback import format_exc import pymysql as MySQLdb class MySQLUtils(object): def get_db_conn(self, host, user, password, db, log_instance=None, charset='utf8', port=3306): """ this function is able to get mysql connection :param host: mysql host :param user: mysql username :param password: mysql password :param db: mysql db name :param log_instance: log instance, default None :param charset: charset, default utf-8 :param port: mysql port, default 3306 :return: mysql connection """ try: conn = MySQLdb.connect(host=host, user=user, passwd=password, db=db, charset=charset, port=port) return conn except: print(str(format_exc())) if log_instance != None: log_instance.error(str(format_exc())) return None