database_util.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import traceback
  2. from sqlite3 import Connection, Cursor
  3. # import MySQLdb
  4. import pymysql
  5. from common.log_util import error_log
  6. class DatabaseUtil:
  7. def __init__(self):
  8. self.cs = None
  9. self.conn = None
  10. def create_conn(self):
  11. self.conn: Connection = pymysql.connect(
  12. host='10.12.43.2',
  13. user='root',
  14. password='Xx98$kdiesJS',
  15. database='jwtest',
  16. port=3409
  17. )
  18. return self.conn
  19. # 执行sql语句
  20. def execute_sql(self, sql):
  21. # 创建游标
  22. self.cs: Cursor = self.create_conn().cursor()
  23. # 通过游标对象,执行SQL
  24. try:
  25. self.cs.execute(sql)
  26. except:
  27. error_log("SQL查询异常:%s" % str(traceback.format_exc()))
  28. # 提取值
  29. value = self.cs.fetchall()
  30. self.close_resuoe()
  31. print(value)
  32. return value
  33. def close_resuoe(self):
  34. self.cs.close()
  35. self.conn.close()
  36. # if __name__ == '__main__':
  37. # print(DatabaseUtil().execute_sql('select * from uisql '))