tags:

views:

522

answers:

2

Is it possible to choose a custom location for the sqlite database file?

If possible I want to store the database file in the memory card. And if user switches from one memory card to the other I want my application to use whatever version of the database file available on the card.

+1  A: 

By default your database is stored in data/data/your_package/databases

You can use SQLiteDatabase openOrCreateDatabase where you can supply the path to your custom database as your first argument.

Anthony Forloney
Android data storage documentation (http://developer.android.com/guide/topics/data/data-storage.html#db) mentions that all databases are stored in a predefined folder. But API documentation for openOrCreateDatabase does not put any restrictions on file name. I think documentation is really confusing but this might be the solution.
Szere Dyeri
yeah the documentation is heavily lacking, although this answers what you need, I think you are better off using `SQLiteOpenHelper` to create the database and then copy the database file onto your memory card.
Anthony Forloney
+1  A: 

You can access a database stored on you sdcard by using:

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class MyClass {
    private SQLiteDatabase myDB = null;

    // Constructor
    public MyClass() {
        try {
            myDB = SQLiteDatabase.openDatabase(stPathToDB, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READONLY);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // Destructor
    public void finalize() {
        myDB.close();
    }
}

NO_LOCALIZED_COLLATORS = to open the database without support for localized collators.

In order to get the path to your sdcard you can use:

stPathToDB = android.os.Environment.getExternalStorageDirectory().toString()+"/dbase.sqlite"

Rgds Layne

Layne