简体   繁体   中英

Create database of static (non-editable) data on installation in Android application

The info:

So I want to create a database when the application is installed, insert some data and never change them again, only read from the database. As an example: think of the levels in Angry Birds when it is installed, they are likely read from somewhere and put into a database at first installation and just read from there on. never changed (unless the game is updated).

I have the following skeleton code for the levels of my thought "Angry Birds" game in 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 is common practice I also have a skeleton code for a class using the GameDBHelper. This should only have a query method which will be used in an 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);
    }
}

The questions:

  1. Where should the insertAllGames() method be invoked? Should there be some "create if not exists" involved here?
  2. Does the skeleton code look like a good practice to create such a database?
  3. Since this is a static table, is it ok to use "drop table" or should one always strive to use "alter table"? If so, could you please provide an example for good use of "alter table"?
  4. Is the "drop table" (or "alter table") placed in the correct spot?

Thanks in advance!

Storing this sort of configuration data in a static database is fine - no reason to invent your own file format (and write a parser) if the data is well suited to a database. A good method for doing this is covered here:

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

and discussed in detail here:

Ship an application with a database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM