fonter
fonter

Reputation: 141

How to change the android database storage path?

How do I change the android database storage path? The default path for database storage is / data / data / databases,I want to save it to sdcard, how to do?

Upvotes: 10

Views: 21341

Answers (3)

RandomUser
RandomUser

Reputation: 1224

This is and old question, but answering may help others.

The easiest way to do this (only for debuggin environments) is to modify the constructor of the class:

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    MySQLiteOpenHelper(Context context) {
        super(context, "/mnt/sdcard/database_name.db", null, 0);
    }
}

Remember to change for production environments with these lines:

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    MySQLiteOpenHelper(Context context) {
        super(context, "database_name.db", null, 0);
    }
}

Upvotes: 8

Josh
Josh

Reputation: 10738

SQLite's openDatabase() lets you provide a location for your database.

You should use Context.getExternalFilesDir() to find the SD card's path instead of hardcoding it, though. That link has some nice code samples which you should make use of.

Upvotes: 3

Mathias Conradt
Mathias Conradt

Reputation: 28705

You can put and open the database from wherever you like, i.e.

SQLiteDatabase.openDatabase("/sdcard/mydatabase.db", null, SQLiteDatabase.OPEN_READONLY);

Upvotes: 2

Related Questions