This question already has an answer here:
- Ship an application with a database 16 answers
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?
You can do this by using a content provider. Each data item used in the application remains private to the application. If an application want to share data accross applications, there is only technique to achieve this, using a content provider, which provides interface to access that private data.
I had trouble with the other DatabaseHelpers regarding this problem, not sure why.
This is what worked for me:
If you already have a database, keep it in your asset folder and copy it in your application. For more detail, see Android database basics.
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 JB 4.2 multi users.
If you are having pre built data base than copy it in asset folder and create an new class as DataBaseHelper which implements SQLiteOpenHelper Than use following code:
Hope this will help you...