Search code examples
javaandroiddatabasesqliteadk

What should be the location of sqlite database in android application?


Currently, I am developing an android app that will download the sqlite database from the server as soon as it is opened. I have to use this database file very frequently in the app and have to frequently fetch the data from this database file. So, what should be the correct location or directory of this database in the mobile , so i can read/ write this database file efficiently.


Solution

  • By Default, a newly created database is stored in:

    //data/data/<Your-Application-Package-Name>/databases/<your-database-name>
    

    But this folder is inaccessible on non-rooted devices. Transmitting a database over the Internet is risky for security, but if it's absolutely necessary, you can modify MySQLiteOpenHelper constructor like this:

    public class MySQLiteOpenHelper extends SQLiteOpenHelper {
        MySQLiteOpenHelper(Context context) {
           super(context, "/mnt/sdcard/<your-app-name>/database_name.db", null, 0);
    }
    

    }

    By doing this, you are storing your database in a folder on the SD Card.

    Remember that by using this approach, you'll need extra permissions for writing on external storage in manifest.xml:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    You will find more info and examples here: Location of sqlite database on the device