Database not open

Database not open

A very common error when we are working with database is not check that we have the database opened. So, we will receive the exception java.lang.IllegalStateException: database not open. We can fix it with a simple condition before we execute any query.

// YourClass.java

SQLiteDatabase d = null;

// Initialization, other queries and so on...
// ...

if (d == null || !d.isOpen()) {
  d = db.getWritableDatabase(); 
  // d = db.getReadableDatabase();
}

// Your query here
// ...