mysql_search_utils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import pymysql
  2. class mysqlSearchUtils():
  3. def __init__(self):
  4. try:
  5. self.conn = pymysql.connect(host="139.186.27.96",port=4000,user="data",passwd="hcst@2021",db="jeecg-boot",charset="utf8mb4")
  6. self.cursor = self.conn.cursor()
  7. except pymysql.err as e:
  8. print("error"% e)
  9. def get_one(self):
  10. sql = "select * from ctop_user_allocation where account_id = %s order by id desc"
  11. self.cursor.execute(sql, ('9556344',))
  12. rest = self.cursor.fetchone()
  13. result = dict(zip([k[0] for k in self.cursor.description], rest))
  14. self.cursor.close()
  15. return result
  16. def get_all(self):
  17. sql = "select * from ctop_user_allocation where project_id = %s order by id desc"
  18. self.cursor.execute(sql, ('458',))
  19. rest = self.cursor.fetchall()
  20. result = [dict(zip([k[0] for k in self.cursor.description], row)) for row in rest]
  21. self.cursor.close()
  22. return result
  23. def db_close(self):
  24. self.conn.close()
  25. if __name__ == '__main__':
  26. obj = mysqlSearchUtils()
  27. reslt = obj.get_all()
  28. for item in reslt:
  29. print(item)
  30. print("-" * 10)
  31. obj.db_close()