问题
I am using the following code to add items to the Database :
public static void insertQuestion(Context c,JSONArray jarr,String search) throws JSONException {
DatabaseWrapper databaseWrapper = new DatabaseWrapper(c);
SQLiteDatabase database = databaseWrapper.getWritableDatabase();
ContentValues values = postToContentValues2(jarr);
values.put(QuestionORM.COLUMN_SEARCH,search);
long questionId = database.insert(QuestionORM.TABLE_NAME, "null", values);
Log.e(TAG, "Inserted new Question with ID: " + questionId);
database.close();
}
But I get an error saying
"attempt to re-open an already-closed object"
on this line :
SQLiteDatabase database = databaseWrapper.getWritableDatabase();
How do I resolve this ?
Thanks !
回答1:
check before open it
if(database != null && database.isOpen()){
// if open then don't open
}
---- UPDATE ----
// add this method in your class calss
private SQLiteDatabase myDataBase;
...
public boolean isDatabaseOpened() {
if (myDataBase == null) {
return false;
}
return myDataBase.isOpen();
}
if method returns false then only call getWritableDatabase() or getReadableDatabas() as per your requirement.
来源:https://stackoverflow.com/questions/29961755/android-sqlite-illegal-state-exception