So far we have developed apps in android that create database on runtime. We like to know how can we access a pre-built or existing database/sqlite file in our android app? Please provide detail
-
@Yaqub Ahmad Thanks for the editing to make it more clear. – Muhammad Maqsoodur Rehman Dec 19 '11 at 12:13
-
You will want to try [android sqlite asset helper](https://github.com/jgilfelt/android-sqlite-asset-helper). It makes it a piece of cake. – craned Jan 06 '16 at 18:11
4 Answers
Take a look at the documentation for android.database.sqlite.SQLiteDatabase.
In particular, there's an openDatabase() command that will access a database given a file path.
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, 0);
Just specify the path to your database as the path
variable, and it should work.

- 15,682
- 3
- 55
- 54
-
3Suppose i have my db file in my assets folder then what path i will provide to the path variable? – Muhammad Maqsoodur Rehman Dec 23 '09 at 14:01
-
6See this other, related question: http://stackoverflow.com/questions/513084/how-to-ship-an-android-application-with-a-database In short, you can't do it directly. You need to copy the data out of your APK and into a new file in /data, then you can open it directly. This blog post has more information: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ – Trevor Johns Dec 29 '09 at 00:53
-
Wayback Machine archive for the tutorial that was linked above: https://web.archive.org/web/20161117041450/http://blog.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ – Pikamander2 Oct 24 '21 at 18:30
I have followed the same road and found two issues:
- Include the _id field on any table you create and make sure it is part of the columns to return when you query any table.
You may run into an error like Failed to open the database. closing it.. This is because we are copying the database from an existing file instead of creating tables on top of a db created by the sqlite android API and the table android_metadata may not exist. For creating it just run the following query on your db:
CREATE TABLE android_metadata (locale TEXT);
And add the corresponding locale, for example
en_US

- 625
- 7
- 24
-
Can I run that sql on the existing SQLite database or should I run it on the database created by copying data from the existing database in /assets/? – marienke Jul 16 '13 at 13:53
-
It should be run on the existing database in "assets" folder or once copied on the application database, but I guess is easier having the table in the database before copying – Andres Felipe Jul 17 '13 at 15:54
Use this for accessing database in application :
Context context = getApplicationContext();
context.getDatabasePath(DataBaseHelper.database_Name);

- 2,034
- 26
- 22
First copy your SDCARD and give it's path in the variable "DBNAME" in the following example. it will be something like "/sdcard/persons.db" if you are directly pulling it to sdcard.

- 398,270
- 210
- 566
- 880

- 13,891
- 26
- 68
- 93
-
Could you expand ? It's not entirely clear to me how this solves the problem. The link in the first version of your answer is also broken - could you fix it ? – Jonas Czech Apr 18 '16 at 14:36