def increment(table, column_value, where): if not column_value: return False sets = '' for i in column_value: if where.startswith(i): continue if type(column_value[i]) is str: sets += f'''{i}='{column_value[i]}',''' else: sets += f'''{i}={i}+{column_value[i]},''' sql = f'''UPDATE {table} SET {sets[:-1]} WHERE {where}''' cursor = _conn.cursor() cursor.execute("BEGIN") try: cursor.execute(sql) except OperationalError: FileLogger.exception(f'Exception at {__file__} {__name__}\nSQL: {sql}') _conn.rollback() return False cursor.execute("COMMIT") _conn.commit() return True
def insert(table, column_value): if not column_value: return False columns = '' values = '' for i in column_value: columns += f'{i},' if type(column_value[i]) is str: values += f'\'{column_value[i]}\',' else: values += f'{column_value[i]},' play_date = get_settlement_time() sql = f'''INSERT INTO {table} ({columns}play_date) VALUES ({values}'{play_date}')''' cursor = _conn.cursor() cursor.execute("BEGIN") try: cursor.execute(sql) except OperationalError: FileLogger.exception(f'Exception at {__file__} {__name__}\nSQL: {sql}') _conn.rollback() return False cursor.execute("COMMIT") _conn.commit() return True
def query(table, where): cursor = _conn.cursor() sql = f'''SELECT * FROM {table} WHERE {where}''' result = [] try: cursor.execute(sql) row = cursor.fetchone() while row: column_value = {} for i in range(len(cursor.description)): column_value[str(cursor.description[i][0])] = row[i] result.append(column_value) row = cursor.fetchone() except OperationalError: FileLogger.exception(f'Exception at {__file__} {__name__}\nSQL: {sql}') return result
def parse_args(user_auth, string): args = strQ2B(string).split() response = '' if not args: return response # Find command, otherwise consider it as spam cmd = get_cmd(args[0]) if cmd: args = args[1:] else: cmd = "spam" # Create the instance try: inst = getattr(globals()[cmd], cmd)() except KeyError: FileLogger.warn(f'No command found') except Exception: FileLogger.exception(f'Exception at {__file__} {__name__}') # Execute the function FileLogger.info(f"{user_auth['user_id']} call {cmd} with {args}") try: if len(args) == 1 and args[0] == 'help': response = inst.usage elif not inst.check_param(args): response = inst.usage elif not inst.check_auth(user_auth): response = inst.auth_warning else: response = inst.run(user_auth, args) except Exception: FileLogger.exception(f'Exception at {__file__} {__name__}') return response