Android tip #019 – Database not open

Platform/Language: Java/XML/Android

Description: 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.

Code:

YourClass.java

1
2
3
4
5
6
7
8
9
10
11
12
SQLiteDatabase d = null;

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

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

// Your query here
// ...

Leave A Reply