def reset(): """ Reset the database to make it newly generated NOTE: This function will be called during the game initiation. Args: none Return: none Raise: none """ service.log("Resetting the database") # Open the database database.connect() try: # Delete the old database file os.remove(configuration.CONFIG['database_path']) # Create the new database file file = open(configuration.CONFIG['database_path'], 'w+') file.close() except Exception as inst: # Rollback the change service.error(inst) pass
def insert(table_name: str, values: list): """ Do an INSERT script on database INSERT INTO <table_name> VALUES (<values>); Args: values: table_name: Return: none Raise: none """ # Open the database database.connect() # Change the values of list into a string values = str(values).lstrip('[').rstrip(']') # Execute a SQL command try: sql_script = "INSERT INTO {} VALUES ({})".format(table_name, values) DATABASE.execute(sql_script) DATABASE.commit() DATABASE.close() service.log("Execute SQL : {}".format(sql_script)) except Exception as inst: DATABASE.rollback() service.error(inst) pass
def update(table_name, column, operator, column_quantity, where_column, where_column_value): """ Do an UPDATE script on database UPDATE <table_name> SET <column> <operator> <column_quantity> WHERE <where_column> = <where_column_value> Args: table_name : column : operator : column_quantity : where_column : where_column_value : Return: none Raise: none """ # Open the database database.connect() # Make transaction on UPDATE try: DATABASE.execute("UPDATE {} SET {} {} {} WHERE {} = {};" .format(table_name, column, operator, column_quantity, where_column, where_column_value)) # make sure the database change is committed + closed. DATABASE.commit() DATABASE.close() except Exception as inst: DATABASE.rollback() service.error(inst)
def check_database_file(): """ """ # Check for old database file if not os.path.exists(configuration.CONFIG['database_path']): service.error("There is no game.sqlite in {}".format( configuration.CONFIG['database_path']))
def check_game_file(): """ Use for checking the game package configuration. Raises: IOError : File cannot be found in a correct folder """ game_package = configuration.CONFIG['game_package'] if not os.path.isdir("config/{}".format(game_package)): service.error("Path does not exists!") raise IOError for i in configuration.CONFIG['file_to_check']: if not os.path.exists("config/{}/{}".format(game_package, i)): service.error( "{} does not exists in game package folder. Creating new one.".format(i)) file = open("config/{}/{}".format(game_package, i), 'w+') file.close() return
def connect(): """ Connect the database Using the SQLite, the database is stored locally. Args: none Returns: none Raises: IOError : """ # Open the database try: global DATABASE DATABASE = sqlite3.connect(configuration.CONFIG['database_path']) global DATABASE_CURSOR DATABASE_CURSOR = DATABASE.cursor() except Exception as inst: service.error(inst) return