Use pre-populated database with ormlite

I want to send my application with a large database in the resource folder. Android SQLiteAssetHelper seems to be the solution for this.

However, the application also uses ormlite, and it has many Java objects that map to database tables. How can I make ormlite use a pre-populated database?

+4
source share
2 answers

I created my own SqliteOpenHelper, which extends OrmLiteSqliteOpenHelper, and then, inside, I copied the code present in SQLiteAssetHelper.

0
source

You can get rid of SQLiteAssetHelper and implement this DatabaseInitializer class that copies your db (if it does not exist) to data/data/yourapp/databases/yourdb.db (the folder where ormlite generates your db)

In your DatabaseHelper constructor:

 public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); DatabaseInitializer initializer = new DatabaseInitializer(context); try { initializer.createDatabase(); initializer.close(); } catch (IOException e) { e.printStackTrace(); } } 

Clean and easy solution

Some minor cons:
- only works with Android 2.2.3+
- be careful with the size (did not encounter any problem with 3mb)

0
source

All Articles