def execute(self, sql): try: if getattr(config_db, 'debug') and config_db.debug == True: log.info(sql) return self.db_cursor.execute(sql) except Exception as e: log.error(e) return False
def __init__(self): if hasattr(config_db, self.db_connect): config_params = getattr(config_db, self.db_connect) if self.db == None: self.db = mysqlDb() self.db_instance = self.db.db_connect(**config_params) self.db_cursor = self.db_instance.cursor() log.info(config_params['host'] + ':connect database') else: log.error("There is no " + db_connect + " db's config ")
def transaction(self, kw): if len(kw) > 0: try: self.db_instance.begin() for i in kw: log.info(i) self.execute(i) #self.db_cursor.execute(i) self.db_instance.commit() return True except Exception as e: log.error(e) self.db_instance.rollback() return False
def run(self): module = 'core.Module.' + self.params['m'] module_class = self.params['c'] + '_module' module_method = self.params['a'] self.module = create_object_class(module, module_class) self.module.params = self.params log.warn("start") try: func = getattr(self.module, module_method) try: func() except TypeError as ty_e: log.error(ty_e) pass except AttributeError as e: log.error(e) print(e) pass log.warn("end")
def insert_many(self, params, *kw): if len(kw) == 0 or len(params) == 0: return False tag = '%s,' * len(params) tag = tag[:len(tag) - 1] fields = ','.join(params) sql = 'insert into ' + self.db_table + '(' + fields + ') values(' + tag + ')' try: if getattr(config_db, 'debug') and config_db.debug == True: log.info(sql) log.info(kw) self.db_cursor.executemany(sql, list(kw)) return True except Exception as e: log.error(e) log.error(sql) log.error(kw) return False