-1

I am trying to insert some data into a SQLite table using a ContentProvider. No matter how I get the instance to WritableDatabase or ReadableDataBase, I always get attempt to re-open an already-closed object: SQLiteDatabase:

Content Provider:

@Override
public boolean onCreate() {
    helper = DBHelper.getInstance(getContext());
    Log.d(TAG,"OnCreate");
    return true;
}

@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
    return helper.getReadableDatabase()
            .query(DBConstants.TABLE_NAME, DBConstants.ALL_COLUMNS,
                    selection,null,null,null,
                    DBConstants.COLUMN_TIMESTAMP_ADDED + " DESC");
}

@Nullable
@Override
public String getType(@NonNull Uri uri) {
    return null;
}

If I change the onCreate() method to:

@Override
public boolean onCreate() {
    helper = DBHelper.getInstance(getContext());
    database = helper.getWritableDatabase();
    Log.d(TAG,"OnCreate");
    return true;
}

I instantly get a illegalStateException with the same error when I call database.getWritableDatabase() in any of the methods in the class.

When searching in StackOverflow, I could find the issue due to calling database.close() method. But I am using ContentProvider to manage the database and not calling any close() method. So why do I get this error?

Shantanu Paul
  • 706
  • 10
  • 26

1 Answers1

0

Never Mind. In my DBHelper class, I was closing the database in onCreate() method. Commenting that line fixed the issue.

Shantanu Paul
  • 706
  • 10
  • 26