androidsqldatabasesqlhelper

Android Pre Populated Database - Adding New Pre Populated Rows After Publication


I'm having trouble with a pre Populated database in android. Not the usual problems though. I've got the database working just fine.

My problem comes with adding new data after the app has been published.

I spent a lot of time with onupgrade method but then it dawned on me that my problem is elsewhere. Once I've added new lines to my database in my assets folder, how do I get these added to the database that was copied to my data/data folder.....

My database is where I store my level information for a game, the last column in the table is a flag to mark the level completed so I can't lose this information.


Solution

  • You could add some sql patch files, and then read them to upgrade your database.

    I used it simply with the FileHelper static class I copied from Danny Remington's post on SO and then do :

    try {
        InputStream in = mgr.open(assetFile);
        String[] statements = FileHelper.parseSqlFile(in);
        dao.mDb.execSQL("BEGIN TRANSACTION;");
        /*dao.mDb is a reference to the SQLiteDatabase from my dao*/
        for (String statement : statements) {
            dao.mDb.execSQL(statement);
        }
        dao.mDb.execSQL("COMMIT;");
        in.close();
    } catch (IOException e) {
        //
    }