DataBaseUtils.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """
  2. @desc 数据库连接
  3. @auth chenkai
  4. @date 2020/11/19
  5. """
  6. from .DataBaseOperation import *
  7. from model.common.log import logger
  8. import yaml
  9. import os
  10. log = logger()
  11. class MysqlUtils:
  12. _quchen_text = None
  13. def __init__(self):
  14. p_path = os.path.dirname(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
  15. path = os.path.join(p_path,"config", "db_config.yaml")
  16. f = open(path, encoding="utf-8")
  17. self.config = yaml.load(f.read(), Loader=yaml.FullLoader)
  18. @property
  19. def quchen_text(self):
  20. conf = self.config['quchen_text']
  21. self._quchen_text = MysqlOperation(host=conf['host'],
  22. user=conf['user'],
  23. passwd=conf['passwd'],
  24. db=conf['db'])
  25. return self._quchen_text
  26. def find_db(self, db):
  27. if db == "quchen_text":
  28. self._quchen_text = self._quchen_text
  29. return self._quchen_text
  30. else:
  31. log.debug("输入数据库有误")
  32. def close(self):
  33. if self._quchen_text:
  34. self._quchen_text.cursor.close()
  35. self._quchen_text.conn.close()