DataBaseUtils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """
  2. @desc 数据库连接
  3. @auth chenkai
  4. @date 2020/11/19
  5. """
  6. from model.DataBaseOperation import MysqlOperation
  7. from model.log import logger
  8. import yaml
  9. import os
  10. import pandas as pd
  11. from clickhouse_driver.client import Client
  12. log = logger()
  13. class MysqlUtils:
  14. _quchen_text = None
  15. _zx=None
  16. _dm=None
  17. _zx_test =None
  18. def __init__(self):
  19. p_path =os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
  20. path = os.path.join(p_path,"config", "db_config.yaml")
  21. f = open(path, encoding="utf-8")
  22. self.config = yaml.load(f.read(), Loader=yaml.FullLoader)
  23. @property
  24. def quchen_text(self):
  25. conf = self.config['quchen_text']
  26. self._quchen_text = MysqlOperation(host=conf['host'],
  27. user=conf['user'],
  28. passwd=conf['passwd'],
  29. db=conf['db'])
  30. return self._quchen_text
  31. @property
  32. def zx(self):
  33. conf = self.config['zx']
  34. self._zx = MysqlOperation(host=conf['host'],
  35. user=conf['user'],
  36. passwd=conf['passwd'],
  37. db=conf['db'])
  38. return self._zx
  39. @property
  40. def zx_test(self):
  41. conf = self.config['zx_test']
  42. self._zx_test = MysqlOperation(host=conf['host'],
  43. user=conf['user'],
  44. passwd=conf['passwd'],
  45. db=conf['db'])
  46. return self._zx_test
  47. @property
  48. def dm(self):
  49. conf = self.config['dm']
  50. self._dm = MysqlOperation(host=conf['host'],
  51. user=conf['user'],
  52. passwd=conf['passwd'],
  53. db=conf['db'])
  54. return self._dm
  55. def find_db(self, db):
  56. if db == "quchen_text":
  57. self._quchen_text = self._quchen_text
  58. return self._quchen_text
  59. else:
  60. log.debug("输入数据库有误")
  61. def close(self):
  62. if self._quchen_text:
  63. self._quchen_text.cursor.close()
  64. self._quchen_text.conn.close()
  65. if self._zx:
  66. self._zx.cursor.close()
  67. self._zx.conn.close()
  68. if self._dm:
  69. self._dm.cursor.close()
  70. self._dm.conn.close()
  71. if self._zx_test:
  72. self._zx_test.cursor.close()
  73. self._zx_test.conn.close()
  74. class CkUtils:
  75. def __init__(self):
  76. self.client = Client(host='cc-bp1h3yc7o3g3o7k64o.ads.aliyuncs.com',
  77. user='qucheng_ck',
  78. password='Qc123456',
  79. port='3306',
  80. send_receive_timeout=5)
  81. def execute(self, sql):
  82. return self.client.execute(sql)
  83. def getData_pd(self,sql,col):
  84. data=self.execute(sql)
  85. df=pd.DataFrame(data,columns=col)
  86. return df
  87. def getColumns(self,table,is_list=False):
  88. data=self.execute("desc "+table)
  89. li=[]
  90. str = ''
  91. for i in data:
  92. li.append(i[0])
  93. str+=i[0]+','
  94. if is_list:
  95. return li
  96. else:
  97. return str[:-1]
  98. def insertMany(self,table,col,data):
  99. """
  100. :param table: 表名 srt
  101. :param col: 字段名 srt eg: ”a,b,c“
  102. :param data: tuple/list
  103. :return:
  104. """
  105. max=1000
  106. sql="insert into {} ({}) values ".format(table,col)
  107. if len(data) == 0:
  108. log.debug("data.len==0")
  109. return
  110. if len(data) <= max:
  111. sql = sql+str(data)[1:-1]
  112. # log.info(sql)
  113. # log.info("insert {} rows".format(len(data)))
  114. self.execute(sql)
  115. return
  116. else:
  117. sql2=sql+str(data[:max])[1:-1]
  118. # log.info(sql2)
  119. self.execute(sql2)
  120. # log.info("insert {} rows".format(max))
  121. self.insertMany(table,col,data[max:])
  122. if __name__ == '__main__':
  123. # p_path = os.path.dirname(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
  124. # print(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
  125. ck=CkUtils()
  126. print(ck.execute("desc order"))