def insert_many(sql, value_list): """ Basic method to batch execute the insert string. @ExecuteNoQuery parameter shows whether we need the returned value. False by default which means the results will be returned. """ conn = connDatabase() cur = conn.cursor() ret = None # batchCount是字段长度, batch_count = 10000 bug_count = len(value_list) times = bug_count / batch_count times = times + 1 if bug_count % batch_count > 0 else times try: if config.DEBUG: print('Execute SQL query: %s' % sql) for i in range(times): count = cur.executemany( sql, value_list[i * batch_count:(i + 1) * batch_count]) conn.commit() ret = count except Exception as e: log.info('Error when execute SQL string. Exception: %s' % e) finally: cur.close() conn.close() return ret
def execute(query, ExecuteNoQuery=False, dictCursor=False): """ Basic method to execute the query string. @ExecuteNoQuery parameter shows whether we need the returned value. False by default which means the results will be returned. """ conn = connDatabase() if dictCursor: cur = conn.cursor(cursor=pymysql.cursors.DictCursor) else: cur = conn.cursor() ret = None try: if config.DEBUG: print('Execute SQL query: %s' % query) count = cur.execute(query) if ExecuteNoQuery: conn.commit() ret = count else: ret = cur.fetchmany(count) conn.commit() except Exception as e: log.info('Error when execute SQL string. Exception: %s' % e) finally: cur.close() conn.close() return ret
def wrapper(self, *args, **kwargs): try: func(self, *args, **kwargs) except Exception as e: log.info(traceback.print_exc()) finally: pass
def wrapper(self, *args, **kwargs): self.ret = {'succ': True, 'err': '', 'data': {}} try: func(self, *args, **kwargs) except tornado.web.MissingArgumentError as e: log.info(traceback.format_exc()) self.ret['succ'] = False self.ret['err'] = e if e != '' else e.log_message finally: # pass if not self._finished: self.write(json.dumps(self.ret, cls=DatetimeEncoder))
def wrapper(self, *args, **kwargs): self.ret = {'succ': True, 'err': '', 'data': {}} try: func(self, *args, **kwargs) except tornado.web.MissingArgumentError as e: log.info(traceback.format_exc()) self.ret['succ'] = False self.ret['err'] = e if e != '' else e.log_message finally: if not self._finished: self.write(json.dumps(self.ret, cls=DatetimeEncoder)) util.ignoreException(lambda: loghelper.add_log_record(ip=self.request.remote_ip, req_item=self.__class__.__name__))
def connDatabase(): global pool if pool is None: print('connect database') pool = PooledDB(creator=pymysql, maxconnections=None, mincached=2, maxcached=5, maxshared=0, blocking=True, maxusage=None, setsession=[], ping=0, host=config.db_host, port=config.db_port, user=config.db_user, passwd=config.db_pwd, db=config.db_name, charset='utf8') if not pool: log.info("connect failed") return pool.connection()