0

I'm trying to change my DatabaseInterface implementation by opening it in MyApplication extends Application and basically never closing it (I did some research and a Google engineer recommends it, see the linked question Best place to close database connection). The modifications I have made are quite minor but now the system throws me the following error when opening the database:

E/SQLiteDatabase(15690): android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.

Here is relevant code

MyApplication.java

@Override
public void onCreate() {
    super.onCreate();
    try {
        DatabaseInterface.open();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

DatabaseInterface.java

public class DatabaseInterface extends SQLiteOpenHelper {

    private final static String DB_PATH = Constants.DB_PATH;
    private final static String DB_NAME = Constants.DB_NAME;
    private static SQLiteDatabase mDatabase;
    private Context mContext;
    private static DatabaseInterface mInstance;

    private DatabaseInterface(Context context) {
        super(context, DB_NAME, null, 1);
        this.mContext = context;
    }

    public static DatabaseInterface getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new DatabaseInterface(context);
        }
        return mInstance;
    }

    public static void open() throws SQLException, IOException {
        //Open the database
        String path = DB_PATH + DB_NAME;
        mDatabase = SQLiteDatabase.openOrCreateDatabase(new File(path), null);
    }
}
Community
  • 1
  • 1
chopchop
  • 1,905
  • 2
  • 22
  • 37

1 Answers1

3

Ok this was caused by me forgetting to add

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

to my AndroidManifest.xml as my database reside on the phone's external storage.

chopchop
  • 1,905
  • 2
  • 22
  • 37