import pymysql class mysqlSearchUtils(): def __init__(self): try: self.conn = pymysql.connect(host="139.186.27.96",port=4000,user="data",passwd="hcst@2021",db="jeecg-boot",charset="utf8mb4") self.cursor = self.conn.cursor() except pymysql.err as e: print("error"% e) def get_one(self): sql = "select * from ctop_user_allocation where account_id = %s order by id desc" self.cursor.execute(sql, ('9556344',)) rest = self.cursor.fetchone() result = dict(zip([k[0] for k in self.cursor.description], rest)) self.cursor.close() return result def get_all(self): sql = "select * from ctop_user_allocation where project_id = %s order by id desc" self.cursor.execute(sql, ('458',)) rest = self.cursor.fetchall() result = [dict(zip([k[0] for k in self.cursor.description], row)) for row in rest] self.cursor.close() return result def db_close(self): self.conn.close() if __name__ == '__main__': obj = mysqlSearchUtils() reslt = obj.get_all() for item in reslt: print(item) print("-" * 10) obj.db_close()