9

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.

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Android data storage documentation (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.
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.
9

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.