I have already created an SQLite database. I want to use this database file with my Android project. I want to bundle this database with my application.
Instead of creating a new database, how can the application gain access to this database and use it as its database?
NOTE:
Before trying this code, please find this line in the below code:
DB_NAME here is the name of your database. It is assumed that you have a copy of the database in the assets folder, so for example, if your database name is ordersDB, then the value of DB_NAME will be ordersDB,
Keep the database in assets folder and then follow the below:
DataHelper class:
Write a DataAdapter class like:
Now you can use it like:
EDIT: Thanks to JDx
For Android 4.1 (Jelly Bean), change:
to:
in the DataHelper class, this code will work on Jelly Bean 4.2 multi-users.
EDIT: Instead of using hardcoded path, we can use
which will give us the full path to the database file and works on all Android versions