Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
736 views
in Technique[技术] by (71.8m points)

android - 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"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...