def select(self, sql, params=()): try: self.connectiondatabase() self.cur.execute(sql, params) result = self.cur.fetchall() logger.info("select success:" + sql + params) return result except Exception as e: logger.error("execute failed:" + sql + params) logger.error(e) return (e)
def connectiondatabase(self): dbconfig = handle_ini.get_value('db_config', 'dbconfig') db_config = json.loads(dbconfig) try: self.conn = pymysql.connect(db_config['host'], db_config['user'], db_config['password'], db_config['database'], charset=db_config['charset']) except: logger.error("connectDatabase failed") return False self.cur = self.conn.cursor() return True
def execute(self, sql, params=()): self.connectiondatabase() try: if self.conn and self.cur: # 正常逻辑,执行sql,提交操作 self.cur.execute(sql, params) self.conn.commit() except: logger.error("execute failed: " + sql) logger.error("params: " + params) self.closedatabase() return False return True