0

I'm trying to create a db file (SQLite) on the device disk - and all works fine with no exception - but the file does not created.

added these permissions

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

The code:

public class DbHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "myAppDatabase.db";                

private static final String PersistenceFolder = Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator;
private static final String  PersistenceFilePath = PersistenceFolder + DATABASE_NAME;


public DbHandler(Context context) {
    super(context, PersistenceFilePath, null, DATABASE_VERSION);
}



@Override
public void onCreate(SQLiteDatabase db) {

    createTable1();
    createTable2();
}

private void createTable1(){
    // create table 1 with no exception
}

private void createTable2(){
    // create table 2 with no exception
}
}
6
  • How have you determined that the file is not created? You have a lot of unused code here related to external storage; your database is being created on internal storage. Commented Oct 3, 2017 at 14:32
  • I don't see the file in the folder that i define in the constructor. Commented Oct 3, 2017 at 14:34
  • Please explain exactly how you have determined that the file is not created. Are you using a desktop file manager? Are you using an on-device file manager? Are you using adb shell? Are you using something else? Commented Oct 3, 2017 at 14:36
  • To see the created db file you have to use a rooted device. If your device is not rooted, when you explore the destination path you can not see anything and the content of it is hidden from you. Commented Oct 3, 2017 at 14:39
  • I create the file on specific path - please read the code Commented Oct 3, 2017 at 14:42

1 Answer 1

1

Ok lets make this simple the code below decides if I have a SD Card if not we use internal storage but it gets the path then passes that PATH to DBHelper who creates the DB I will post the code to get the PATH first then the DBHelper code

    public void onAvail() {

    String state = Environment.getExternalStorageState();

    if (state.equals(Environment.MEDIA_MOUNTED) && (!state.equals(Environment.MEDIA_MOUNTED_READ_ONLY))) {

        File removable = ContextCompat.getExternalFilesDirs(this, null)[1];
        THE_PATH = String.valueOf(removable) + "/Documents/";

        //System.out.println("ALL TRUE ==> " + THE_PATH);

    } else {// if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        THE_PATH = "";
        //System.out.println("ALL FALSE ==> "+ THE_PATH);
    }
}

partial DBHelper code to create DB

public class DBHelper extends SQLiteOpenHelper {

public static final String DB_NAME = THE_PATH +"PassWord";
//public static final String DB_NAME = "PassWord";
// Code Above for Internal Storage ONLY
public static final Integer DB_VERSION = 1;
Sign up to request clarification or add additional context in comments.

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.