attempt to re-open an already-closed object: SQLiteDatabase:
Try this.
public void removeAndInsert(String configRaw) {
SQLiteDatabase sqlite = null;
try {
sqlite = dbHelper.getWritableDatabase();
synchronized(sqlite) {
removeAndInsert(configRaw, sqlite);
}
.....
finally {
if (sqlite != null && sqlite.isOpen()) {
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);
}
Android Studio - attempt to re-open an already-closed object: SQLiteDatabase
Try to remove:
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_USER);
db.close(); // remove this line
}
And also this:
onCreate(this.getWritableDatabase());
From onUpgrade() method, add:
db.execSQL("DROP TABLE IF EXISTS " + YOUR_TABLE_NAME);
onCreate(db);
Attempt to re-open an already closed object error Android SQLite
Try this :
public List<Task> getAllTasks() {
List<Task> taskList = new ArrayList<Task>();
String selectQuery = "SELECT * FROM " + TABLE_TASKS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
try {
if (cursor.moveToFirst()) {
do {
Task task = new Task();
task.setId(cursor.getInt(0));
task.setTaskName(cursor.getString(1));
task.setStatus(cursor.getInt(2));
// Adding contact to list
taskList.add(task);
} while (cursor.moveToNext());
}
finally
{
if (cursor != null)
{
cursor.close();
db.close();
}
// return task list
return taskList;
}
And in your OnCreat()
you should not close your DB.
Related Topics
Data Sharing Between Fragments and Activity in Android
How to Display List of Resource Drawables
Gradle Is Failing on Syncing My Project in the New Android Studio 2.0
How to Add a Bullet Symbol in Textview
Android App Installation Failed: Package Com.My.App Has No Certificates at Entry Androidmanifest.Xml
Using Katzer Local Notification in Ibm Worklight
Why Should We Use Sp for Font Sizes in Android
How to Close Activity and Go Back to Previous Activity in Android
Retrofit 2 with Only Form-Data
Startactivityforresult from Activitygroup
How to Store Hashmap So That It Can Be Retained It Value After a Device Reboot
Is Fcm (Firebase Cloud Messaging) Token for One Device or for One Account
Play Youtube HTML5 Embedded Video in Android Webview
How to Update Google Play Services for Android Studio 2.2 Emulators
How to Make a Dialog Slide from Bottom to Middle of Screen in Android