How to store sqlite database directly on sdcard

i want to create my sqlite database in sdcard instead of default path...i want to access all my data from sdcard also I have Used This Code:

            private static class OpenHelper extends SQLiteOpenHelper {

    OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        SQLiteDatabase.openOrCreateDatabase("/sdcard/"+DATABASE_NAME,null);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "
                + TABLE_NAME
                + " (id INTEGER PRIMARY KEY, name TEXT, number TEXT, skypeId TEXT, address TEXT, image BLOB)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

Problem:

When i see the database file in default path i can see all the data and table but when i see the database file created in sd card it doesnot shows any data but it only shows the database file

IN constructor it only creates the file in sdcard but in default path it does everything well..... How to store all Sqlitedata on sdcard for further access?


Solution 1:

I created my DB with

    public DatabaseHelper(final Context context) {
    super(context, Environment.getExternalStorageDirectory()
            + File.separator + FILE_DIR
            + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
}

and had no problems further on. I guess your call to super() should reference the sdcard as well.

Solution 2:

You are providing an incomplete name in your super() call. Try using:

OpenHelper(Context context) {
    super(context, "/sdcard/"+DATABASE_NAME, null, DATABASE_VERSION);
    SQLiteDatabase.openOrCreateDatabase("/sdcard/"+DATABASE_NAME,null);

}

Other than that, you should always use Environment.getExternalStoreDirectory() to get the path to the external storage, and you should also check the state of the external storage before attempting to use it.