2

By default database file of an application is created in the application data. Is it possible to create database file in our own path like "/mnt/sdcard/test.db" ?

I am creating database file by extending SQLiteOpenhelper, but its creating in app data location. I want to create in sdcard location. I tried with "db.OpenorCreateDatabase(Path)". Its creating a db file in that location, when i tried to read that file its crashing.How to do it?

2
  • 3
    yes, but it's not safe since the sdcard is accessible by other apps. Commented Dec 11, 2013 at 13:37
  • could you elaborate on "it is crashing"? Commented Dec 11, 2013 at 13:53

2 Answers 2

3

You can use the SQLiteOpenHelper with a custom path (at least with android 2.2) if you provide a custom ContextClass and if you have write access in the target directory.

public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 3;
    .....

DatabaseHelper(final Context context, String databaseName) 
    {
       super(new DatabaseContext(context), databaseName, null, DATABASE_VERSION);
    }
}

And here is the custom DatabaseContext class that does all the magic

class DatabaseContext extends ContextWrapper {

private static final String DEBUG_CONTEXT = "DatabaseContext";

public DatabaseContext(Context base) {
    super(base);
}

@Override
public File getDatabasePath(String name) 
{
    File sdcard = Environment.getExternalStorageDirectory();    
    String dbfile = sdcard.getAbsolutePath() + File.separator+ "databases" + File.separator + name;
    if (!dbfile.endsWith(".db"))
    {
        dbfile += ".db" ;
    }

    File result = new File(dbfile);

    if (!result.getParentFile().exists())
    {
        result.getParentFile().mkdirs();
    }

    if (Log.isLoggable(DEBUG_CONTEXT, Log.WARN))
    {
        Log.w(DEBUG_CONTEXT,
                "getDatabasePath(" + name + ") = " + result.getAbsolutePath());
    }

    return result;
}

@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) 
{
    SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), null);
    // SQLiteDatabase result = super.openOrCreateDatabase(name, mode, factory);
    if (Log.isLoggable(DEBUG_CONTEXT, Log.WARN))
    {
        Log.w(DEBUG_CONTEXT,
                "openOrCreateDatabase(" + name + ",,) = " + result.getPath());
    }
    return result;
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

this is old, but for those are looking for placing the database in accessible directory for debugging purpose, the simplest way:

in your dbHelper constructor, add the alternative path to the db name:

  private DbHelper(Context context)
{
    super(context, "/mnt/sdcard/"+DATABASE_NAME, null, DATABASE_VERSION);
}

and remember add the permission:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.