Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to open database in read/write mode

I am getting below error:

not an error (code 0): Could not open the database in read/write mode.

I have added

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

I am trying to enter data in the database that is present in the SD card, I am able to read the data that is already present in the database through "OPEN_READONLY". I am getting error when using "OPEN_READWRITE"

like image 292
Sakshi Agrawal Avatar asked Jun 02 '16 13:06

Sakshi Agrawal


1 Answers

Your question is not that clear, but I'll try to answer.

Most likely, your database either:

  1. Don't exist yet, and you have to create it;
  2. Your database file is read only, you have to change it (This question might be related).

For #2, instead of using SQLiteOpenHelper#getReadableDatabase(), use SQLiteOpenHelper#getWritableDatabase


If your database is on an external storage unit, you have a few other things to check:

  1. Is the external storage currently mounted? If not, you can't access the db.
  2. Is it mounted as read only? If so, you'll have to change this.
  3. Have you checked the path? Is it correct?

The problem might be on any of those topics.

To check if it's mounted as read-only, try the following:

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

Taken from here.

For more info on the Environment class, please refer to the docs.

like image 128
Mauker Avatar answered Nov 19 '22 20:11

Mauker