DataBaseUtils.py 4.7 KB

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