Create database of static (non-editable) data when installed in Android app

Information:

So, I want to create a database when the application is installed, insert some data and never modify it again, but only read from the database. As an example: think of the levels in Angry Birds, when installed, they probably read from somewhere and put in the database when they first install and just read from there. never changed (unless the game is updated).

I have the following skeleton code for my Angry Birds thought levels the same way:

public class GameDBHelper extends SQLiteOpenHelper{
    private SQLiteDatabase database;
    public static final String TAG = GameDBHelper.class.getSimpleName();

    //Insert static database variables here

    public GameDBHelper( Context ctx ) {
        super(ctx,DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE_STATEMENT);
        insertAllGames(); //Method to read data from all game files and insert into database 
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists " + TABLE_GAMES); //Should be "alter table"
        Log.d(TAG, "onUpgrade dropped table " + TABLE_GAMES);
        this.onCreate(db);
    }

    private void insertAllGames(){
        //Method that reads all files and inserts into database
    }
}

      

As usual, I also have some skeletal code for the class using GameDBHelper. This should only have a request method to be used in the Activity:

public class GameDataSource {
    private static final String TAG = GameDataSource.class.getSimpleName();
    private SQLiteDatabase database;
    private GameDBHelper gameDBHelper;

    public GameDataSource(Context con, OTHER_DATA_AS_NEEDED) {
        gameDBHelper = new GameDBHelper(con);
        Log.d(TAG, "GameDataSource object created!");
    }

    public Cursor query(DATA_AS_NEEDED) throws SQLException{
        database = gameDBHelper.getReadableDatabase();
        return database.query(QUERY_STATEMENT);
    }
}

      

Questions:

  • Where should the insertAllGames () method be called? Should there be some kind of "create if not exist" here?
  • Is skeletal code a good practice for creating such a database?
  • Since it is a static table, is it possible to use a "drop table" or should we always strive to use an "alter table"? If yes, could you please provide an example for good use of "alter table"?
  • Is the "drop table" (or "alter table") dropped in the right place?

Thanks in advance!

+3


source to share


1 answer


Storing such configuration data in a static database is great - there is no reason to come up with your own file format (and write a parser) if the data fits the database well. A good way is described well here:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/



and is discussed in detail here:

Submitting an app using a database

+1


source







All Articles