How to store sqlite database directly on sdcard
Asked Answered
M

6

20

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?

Macpherson answered 17/1, 2013 at 7:9 Comment(0)
M
44

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.

Mcglone answered 17/1, 2013 at 7:15 Comment(4)
Great @Mcglone :) it worked i was missing Environment.getExternalStorageDirectory() Thanx a lot :)Macpherson
@Macpherson i had used this method for store db on sdcard now i want to restore one database to this location. can you have try this?Udall
and second thing that every device has Environment.getExternalStorageDirectory()? this becuse i had create emulator with no sdcard not this will not workUdall
Nice answer @koljaTM. But i have doubt FILE_DIR means what we need to give its a package name?Devon
C
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.

Ciao answered 17/1, 2013 at 7:17 Comment(0)
J
1
public DataBaseHelper(final Context context) {

    super(context, Environment.getExternalStorageDirectory()
    + File.separator+ MYDATABASE_NAME, null, MYDATABASE_VERSION);
}

**Also Add permission in android Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />**
Jenninejennings answered 11/3, 2015 at 6:47 Comment(0)
J
0

Just give path in constructor ;

 DatabaseHelper(Context context) {
    super(context, Environment.getExternalStorageDirectory()
            + File.separator + "/DataBase/" + File.separator
            + DB_NAME, null, DB_VERSION);
}
Jury answered 28/7, 2017 at 11:6 Comment(0)
C
0

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app.

For getting permissions at runtime, you will have to request the user. You can do that in following way.

First request for permissions.

String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        requestPermissions(permissions, WRITE_REQUEST_CODE);

And then you can check the results in

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
       case WRITE_REQUEST_CODE:
         if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
           //Permission granted.
           //Continue with writing files...
         }
       else{
           //Permission denied.
         }
        break;
    }
}

Here is good learning source requesting-runtime-permissions-in-android-marshmallow/

Comforter answered 8/5, 2018 at 21:40 Comment(0)
S
0

The use of Environment.getExternalStorageDirectory() method is deprecated,

This method was deprecated in API level 29. To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

now we must use getExternalFilesDir(), this is an example:

public class MyDataBaseHelper extends SQLiteOpenHelper {

    //Constructor
    MyDataBaseHelper(Context context) {
    super(context, context.getExternalFilesDir()
            + File.separator + "/Database/" + File.separator
            + DATABASE_QUESTION, null, DATABASE_VERSION);
    }
   ...
   ...
}
Software answered 14/2, 2020 at 2:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.