1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import pymysql
- import traceback
- import time
- import datetime
- from configuration import Configuration
- class OperateMysql():
- config = Configuration().get_system_config()
- config = config["hardDiskdataBase"]
- def __init__(self, config=config, port=3306, charset='utf8'):
- self.host = config['ip']
- self.user = config['username']
- self.passwd = config['password']
- self.db = config['dataBaseName']
- self.port = port
- self.charset = charset
- self.conn = None
- self._conn()
- def _conn(self):
- try:
- self.conn = pymysql.connect(host=self.host, user=self.user, password=self.passwd, db=self.db, port=self.port, autocommit=True)
- return True
- except Exception as e:
- print(e)
- return False
- def _reConn(self, num=28800, stime=3):
- _number = 0
- _status = True
- while _status and _number <= num:
- try:
- self.conn.ping()
- _status = False
- except:
- if self._conn()==True:
- _status = False
- break
- _number +=1
- time.sleep(stime)
- def execute_sql(self, sql):
- try:
- self._reConn()
- self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
- self.cursor.execute(sql)
- results = self.cursor.fetchall()
- self.cursor.close()
- return results
- except:
- print(traceback.format_exc())
- return None
- def return_result(self, point_statistic, serial_number):
-
- begin_time = datetime.datetime.fromtimestamp(int(point_statistic['begin_time']))
- end_time = datetime.datetime.fromtimestamp(int(point_statistic['end_time']))
- sql = "SELECT device_name FROM data_point_tbl WHERE serial_number=%s;" % (serial_number)
- res = self.execute_sql(sql)
- table_name = 'table_' + res[0]['device_name']
- dict_res = {"table_name": table_name, "begin_time": begin_time, "end_time": end_time}
- return dict_res
|