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: 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);
}
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.
Room attempt to re-open an already closed database
It's because you are trying to modify the schema of the existing database without giving it any migration information. So basically it attempts to write the new database schema to the existing DB which doesn't work.
There are two ways around this. If you are in your development environment what you can do is fallback to a destructive migration, to do this your database creation code would look something like the following:
MyDatabase myDatabase = Room.databaseBuilder(context, MyDatabase.class, "my-db")
.fallbackToDestructiveMigration()
.build();
This means when you provide the database with an updated or new entity it will do what the answer from @huw said and just delete the database on the application's installation removing all the data from it and give you a fresh install.
The other method is to use a migration function. They are pretty long so unless someone wants me to write it up here I'll leave it for now but basically, the documentation can be found here:
Room DB Migration Documentation
This essentially causes the DB to run some SQL provided by yourself to update the database to the new version. This way you can ensure that none of your data is lost while doing the migration; or as little as possible depending on what you are doing. This is the preferred method for production apps as it means users won't lose their pre-existing data and you won't get a lot of angry reviews/lost customers.
Hope that makes helps!
Android SQLite: attempt to re-open an already-closed object
The error only occurs when I click an item, go to a different screen, go back to this page via "finish()", and then try to click on another listView object.
I moved the String searchEntries[] = InventoryAdapter.getInventoryEntriesByISBN(searchQuery, isbn[position]); from the onClickListener to the previous for loop before the onClickListener and now it works.
The correct SearchResultsScreen is below:
SearchResultsScreen.java
// Set up search array
final String Entries[][] = new String[isbn.length][9];
for(int i = 0; i < isbn.length; i++)
{
searchArray.add(new InventoryItem(isbn[i], InventoryAdapter.getTitleAndAuthorByISBN(isbn[i])));
Entries[i] = InventoryAdapter.getInventoryEntriesByISBN(searchQuery, isbn[i]);
}
Toast.makeText(getApplicationContext(), "searchArray.size()="+searchArray.size(), Toast.LENGTH_LONG).show();
// add data in custom adapter
adapter = new CustomAdapter(this, R.layout.list, searchArray);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
// On Click ========================================================
dataList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String searchEntries[] = Entries[position];
attempt to re-open an already-closed object sqlite
I used Room version 1.1.1-rc1 and things are working fine now.
Related Topics
How to Calculate the Pixel Width of a String in Javafx
How to Increase the Number of Displayed Lines of a Java Stack Trace Dump
How to Add One Month to Current Date in Java
Reading Properties File in Java
What Is the Easiest Way to Parse an Ini File in Java
"Invalid Privatekey" When Using Jsch
String-Date Conversion with Nanoseconds
Polygon Touch Detection Google Map API V2
How to Get Country Phone Prefix from Iso
Sending Http Post Request with Android
Android Resource Not Found Exception
Android Proguard JavaScript Interface Fail
How to Convert Rgb Color to Int in Java
How to Resume Android Activity Programmatically from Background
Java.Net.Unknownhostexception: Invalid Hostname for Server: Local