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.
Shipping the app with a database file, in Android Studio 3.0
Shipping the app with a database file is a good idea for me. The advantage is that you don't need to do a complex initialization, which sometimes costs lots of time, if your data set is huge.
Step 1: Prepare database file
Have your database file ready. It can be either a .db file or a .sqlite file. If you use a .sqlite file, all you need to do is to change file extension names. The steps are the same.
In this example, I prepared a file called testDB.db. It has one table and some sample data in it like this
Step 2: Import the file into your project
Create the assets folder if you haven't had one. Then copy and paste the database file into this folder
Step 3: Copy the file to the app's data folder
You need to copy the database file to the app's data folder in order to do further interaction with it. This is a one time action (initialization) to copy the database file. If you call this code multiple times, the database file in data folder will be overwritten by the one in assets folder. This overwrite process is useful when you want to update the database in future during the app update.
Note that during app update, this database file will not be changed in the app's data folder. Only uninstall will delete it.
The database file needs to be copied to
/databases
folder. Open Device File Explorer. Enterdata/data/<YourAppName>/
location. This is the app's default data folder mentioned above. And by default, the database file will be place in another folder called databases under this directoryNow, the copy file process is pretty much like the what Java is doing. Use the following code to do the copy paste. This is the initiation code. It can also be used to update(by overwriting) the database file in future.
Then refresh the folder to verify the copy process
Step 4: Create database open helper
Create a subclass for
SQLiteOpenHelper
, with connect, close, path, etc. I named itDatabaseOpenHelper
Step 5: Create top level class to interact with the database
This will be the class that read & write your database file. Also there is a sample query to print out the value in the database.
Step 6: Test running
Test the code by running the following lines of codes.
Hit the run button and cheer!
I'm using ORMLite and below code worked for me
Please note, The code extracts database file from a zip file in assets
The
SQLiteAssetHelper
library makes this task really simple.It's easy to add as a gradle dependency (but a Jar is also available for Ant/Eclipse), and together with the documentation it can be found at:
https://github.com/jgilfelt/android-sqlite-asset-helper
Note: This project is no longer maintained as stated on above Github link.
As explained in documentation:
Add the dependency to your module's gradle build file:
Copy the database into the assets directory, in a subdirectory called
assets/databases
. For instance:assets/databases/my_database.db
(Optionally, you may compress the database in a zip file such as
assets/databases/my_database.zip
. This isn't needed, since the APK is compressed as a whole already.)Create a class, for example:
I just found a way to do this in
ReignDesign blog
in an article titled Using your own SQLite database in Android applications. Basically you precreate your database, put it in your assets directory in your apk, and on first use copy to/data/data/YOUR_PACKAGE/databases/
directory.