I\'m having a real trouble in building an android application that has a prepopulated database.
Please take a look at the entire source code.
I created a dat
Replace the line which says copyDBFromResource();
with the following code -
SQLiteDatabase readDB = null;
readDB = this.getReadableDatabase();
readDB.close();
readDB = null;
copyDBFromResource();
I think this is needed because your code cannot itself create files in the Data folder, and making a call to getReadableDatabase()
creates the db file for you there. And Android 2.2 onwards you need to close the database after getting a handle to it otherwise the copy won't succeed.
Edit:
If it still doesn't work, try giving the Write External Storage permission as follows -
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I use the onCreate event handling and an set of *.sql scripts (executed line-by-line in simple loop).
This is handy when you need also to write a database update routines. I've one sql script to initialize a new (up-to-date) database and a set of sql scripts for each outdated version of the database.
e.g. "create" sql script is called whenever new database has to be initialized, or a chain of scripts "v1-update-to-v2" and "v2-update-to-v3" is called whenever user has to update his database from version 1 to version 3. This is slower than single update script per version (like "v1-update-to-v3" and "v2-update-to-v3") but this "chain" is easier to maintain as I add new versions (I don't need to change anything in the old scripts).
Well DataHelper comes with a onCreate method you can create the database there a even populates there.
Official Android documentation
Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.
in fact you haved override that method
@Override
public void onCreate(SQLiteDatabase db)
{
}
Here is a good link to get your started.
Essentially the idea is to use the SQLiteOpenHelper
class and when no database exists you copy a your pre-seeded database byte for byte into the correct location. I have it in a project of mine slightly modified but it works appears to work just fine