try: self.connect_dbserver() self.cursor.execute(sql) res = self.cursor.fetchall() # 返回的是数组的类型 print(type(res)) print(res) # 查出当前查询的列名,保存到coloums coloums = [column[0] for column in self.cursor.description] # 定义一个数组,用来保存每一组的数组,格式为字典形式{"name":"database","age":18} sub_resdata = [] for row in res: print(row) # res(1,2,3,4)是数组类型将每行的结果和列名压缩在一起,并转换为字典 res_data = dict(zip(coloums, row)) sub_resdata.append(res_data) except Exception as e: mylog.exception("查询数据出错,请检查{0}".format(e)) finally: self.close_cursor() return sub_resdata