I’m using the method explained in this blogpost to create the initial populated database for my Android app.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
The blog explains how to use a database file stored in the assets folder and copy it to the database folder of the app. I use one variation though. I want to play nice with the framework and call the copy functions in the onCreate() and onUpdate() functions of my implementation of the SQLiteOpenHelper. This initially works: when I pull the database file from an emulator, I can verify that the database gets copied properly over the standard location of my database.
Now the weird thing is, that I also have a breakpoint immediately after the first call to SQLiteOpenHelper.getWritableDatabase(). When I pull the file at that breakpoint, the database is empty except for the standard Android table ‘android_metadata’.
Somehow Android overwrites the file, or the file isn’t properly saved on the file system. Does anybody have an idea where I could look for a solution?
Yes this will happen, because the
oncreateandonUpgradeare called in the constructor of theSQLiteOpenHelper. They can edit on the passed-in database object, but the constructor will keep on modifying the database further afterwards (e.g. adding theandroid_metadatatable). This might overwrite your copy-pasted file.I think once upon a time I tried doing it your way, couldn’t get round it and finally gave up on the neat solution. Finally I ended up doing:
This in the
SQLiteOpenHelper