def new_table_record(self, table_name=None, column_name=None, column_value=None, p_conn=None): if column_name is None or column_value is None or table_name is None: log.log(log.set_loglevel().error(), table_name, column_name, column_value, p_conn) raise err.MyError(log.debug().position(), err.MyError.errcode[4], err.MyError.errmsg[4]) if p_conn is None: try: # conn = mysql.connector.connect(**config.env['db_connection']) db = dbconn.db() conn = db.create_conn() except mysql.connector.Error as e: log.log( log.set_loglevel().error(), log.debug().position(), e, ) log.debugfile().printlog2( log.debug().position(), e, ) raise err.MyError(log.debug().position(), err.MyError.errcode[1], err.MyError.errmsg[1]) else: conn = p_conn insert_sql, column_value = component.c_db_dml.mysql().new_record_sql( table_name, column_name, column_value) cursor = conn.cursor() try: if config.env['env'] == 'dev': log.log(log.set_loglevel().debug(), log.debug().position(), insert_sql, column_value) cursor.execute(insert_sql, column_value) new_id = cursor._last_insert_id except Exception as e: log.log(log.set_loglevel().error(), log.debug().position(), e, insert_sql, column_value) raise err.MyError( log.debug().position(), err.MyError.errcode[1], err.MyError.errmsg[1] + insert_sql + ' ' + column_value) finally: if cursor: cursor.close() if p_conn is None: if conn: conn.commit() conn.close() db.close_conn() if 'new_id' in locals(): return new_id
def delete_table_record(self, table_name=None, column_name=None, column_value=None, p_conn=None): if column_name is None or column_value is None or table_name is None: raise err.MyError(log.debug().position(), err.MyError.errcode[4], err.MyError.errmsg[4]) if p_conn is None: try: # conn = mysql.connector.connect(**config.env['db_connection']) db = dbconn.db() conn = db.create_conn() except mysql.connector.Error as e: log.log( log.set_loglevel().error(), log.debug().position(), e, ) log.debugfile().printlog2( log.debug().position(), e, ) raise err.MyError(log.debug().position(), err.MyError.errcode[1], err.MyError.errmsg[1]) else: conn = p_conn delete_sql = component.c_db_dml.mysql().delete_record_sql( table_name, column_name, column_value) cursor = conn.cursor() try: if config.env['env'] == 'dev': log.log(log.set_loglevel().debug(), log.debug().position(), delete_sql) cursor.execute(delete_sql) delete_count = cursor._rowcount except Exception as e: log.log(log.set_loglevel().error(), log.debug().position(), e, delete_sql) raise err.MyError(log.debug().position(), err.MyError.errcode[1], err.MyError.errmsg[1], delete_sql) finally: if cursor: cursor.close() if p_conn is None: if conn: conn.commit() conn.close() db.close_conn() if 'delete_count' in locals(): return delete_count
def query_table_record(self, sql=None, p_conn=None): if sql is None: log.log(log.set_loglevel().error(), sql, p_conn) raise err.MyError(log.debug().position(), err.MyError.errcode[4], err.MyError.errmsg[4]) if p_conn is None: try: # conn = mysql.connector.connect(**config.env['db_connection']) db = dbconn.db() conn = db.create_conn() except mysql.connector.Error as e: log.log( log.set_loglevel().error(), log.debug().position(), e, ) log.debugfile().printlog2( log.debug().position(), e, ) raise err.MyError(log.debug().position(), err.MyError.errcode[1], err.MyError.errmsg[1]) else: conn = p_conn cursor = conn.cursor() try: if config.env['env'] == 'dev': log.log(log.set_loglevel().debug(), log.debug().position(), sql) cursor.execute(sql) results = cursor.fetchall() column_names = cursor.column_names except Exception as e: log.log(log.set_loglevel().error(), log.debug().position(), e, sql) raise err.MyError(log.debug().position(), e, sql) finally: if cursor: cursor.close() if p_conn is None: if conn: conn.commit() conn.close() db.close_conn() if 'results' in locals(): return column_names, results
def update_table_record(self, table_name=None, column_name=None, column_value=None, p_conn=None): if column_name is None or column_value is None or table_name is None: raise err.MyError(log.debug().position(), err.MyError.errcode[4], err.MyError.errmsg[4]) if p_conn is None: try: # conn = mysql.connector.connect(**config.env['db_connection']) db = dbconn.db() conn = db.create_conn() except mysql.connector.Error as e: log.log( log.set_loglevel().error(), log.debug().position(), e, ) log.debugfile().printlog2( log.debug().position(), e, ) raise err.MyError(log.debug().position(), err.MyError.errcode[1], err.MyError.errmsg[1]) else: conn = p_conn update_sql, column_value = component.c_db_dml.mysql( ).update_record_sql(table_name, column_name, column_value) cursor = conn.cursor() try: try_count = 1 while try_count <= 5: try: if config.env['env'] == 'dev': log.log(log.set_loglevel().debug(), log.debug().position(), update_sql, column_value) cursor.execute(update_sql, column_value) update_count = cursor._rowcount break except Exception as e: # log.log(log.set_loglevel().error(),log.debug().position(),err.err_catch().catch(sys.exc_info()), update_sql, column_name, column_value,e) log.log(log.set_loglevel().error(), log.debug().position(), update_sql, column_name, column_value, e) process_db = dbconn.db() process_conn = process_db.create_conn() process_cursor = process_conn.cursor() process_cursor.execute("show full processlist;") process_lists = process_cursor.fetchall() log.log(log.set_loglevel().error(), log.debug().position(), process_lists) process_cursor.close() process_conn.close() process_db.close_conn() cursor.close() cursor = conn.cursor() if str(e).count('Lock wait timeout exceeded') > 0: try_count += 1 time.sleep(5) if try_count > 5: raise else: raise finally: if cursor: cursor.close() if p_conn is None: if conn: conn.commit() conn.close() db.close_conn() if 'update_count' in locals(): return update_count