I am trying to delete something from the database and then insert a new value. I know little about databases, so I would be grateful for the advice on what is going on here.
I keep getting the following error:
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:
Called from an async task, it works fine the first time, but the second time the task starts.
Here are some important snippets:
public void removeAndInsert(String configRaw) { SQLiteDatabase sqlite = null; try { sqlite = dbHelper.getWritableDatabase(); removeAndInsert(configRaw, sqlite); ..... finally { if (sqlite != null) { sqlite.close(); } } void removeAndInsert(String configRaw, SQLiteDatabase sqlite) throws SQLException { ContentValues initialValues = new ContentValues(); initialValues.put(DBOpenHelper.CONFIG_CACHE_RAW_DATA, configRaw); sqlite.delete(DBOpenHelper.CONFIG_CACHE_TABLE_NAME, null, null); sqlite.insert(DBOpenHelper.CONFIG_CACHE_TABLE_NAME, null, initialValues); }
source share