If your application requires a database and it comes with built in data, what is the best way to ship that application? Should I:
Precreate the SQLite database and include it in the
.apk
?Include the SQL commands with the application and have it create the database and insert the data on first use?
The drawbacks I see are:
Possible SQLite version mismatches might cause problems and I currently don't know where the database should go and how to access it.
It may take a really long time to create and populate the database on the device.
Any suggestions? Pointers to the documentation regarding any issues would be greatly appreciated.
If the required data is not too large (limits I don´t know, would depend on a lot of things), you might also download the data (in XML, JSON, whatever) from a website/webapp. AFter receiving, execute the SQL statements using the received data creating your tables and inserting the data.
If your mobile app contains lots of data, it might be easier later on to update the data in the installed apps with more accurate data or changes.
I guess the best and the newest way till today is using
SQLiteAssetHelper
class.This tutorial guides you perfectly through Importing and Using External Database in Android
In addition to this article you can download
SQLiteAssetHelper
hereShipping the database inside the apk and then copying it to
/data/data/...
will double the size of the database (1 in apk, 1 indata/data/...
), and will increase the apk size (of course). So your database should not be too big.In November 2017 Google released the Room Persistence Library
From the documentation:
The Room database has a callback when the database is first created or opened. You can use the create callback to populate your database.
Code from this blog post.
I modified the class and the answers to the question and wrote a class that allows updating the database via DB_VERSION.
Using a class.
In the activity class, declare variables.
In the onCreate method, write the following code.
If you add a database file to the folder res/raw then use the following modification of the class.
http://blog.harrix.org/article/6784
Finally I did it!! I have used this link help Using your own SQLite database in Android applications, but had to change it a little bit.
If you have many packages you should put the master package name here:
private static String DB_PATH = "data/data/masterPakageName/databases";
I changed the method which copies the database from local folder to emulator folder! It had some problem when that folder didn't exist. So first of all, it should check the path and if it's not there, it should create the folder.
In the previous code, the
copyDatabase
method was never called when the database didn't exist and thecheckDataBase
method caused exception. so I changed the code a little bit.If your database does not have a file extension, don't use the file name with one.
it works nice for me , i hope it whould be usefull for u too