sql_util.py 561 B

12345678910111213141516171819202122
  1. import pymysql
  2. class Database:
  3. def __init__(self, host, user, password, database):
  4. self.connection = pymysql.connect(
  5. host=host,
  6. user=user,
  7. password=password,
  8. database=database
  9. )
  10. def query(self, sql):
  11. with self.connection.cursor() as cursor:
  12. cursor.execute(sql)
  13. result = cursor.fetchall()
  14. return result
  15. def execute(self, sql):
  16. with self.connection.cursor() as cursor:
  17. cursor.execute(sql)
  18. self.connection.commit()