Home » Android » sqlite – java.lang.illegalstateexception database not open android

sqlite – java.lang.illegalstateexception database not open android

Posted by: admin June 15, 2020 Leave a comment


When I am trying to insert to data base log cat shows an error like java.lang.illegalstateexception database not open android.

But I have opened the db using

   db = SQLiteDatabase.openDatabase(DATABASE_PATH, null,

The error is not occurring frequently.Anybody know the reason for this?

How to&Answers:

Try it like this:

  if (!db.isOpen()) {
       db = getApplicationContext().openOrCreateDatabase(DATABASE_PATH, SQLiteDatabase.OPEN_READWRITE, null);



DatabaseHelper dataHelper;
SQLiteDatabase mDb;
    public DbManager openDB() throws SQLException {

    mDb = dataHelper.getWritableDatabase();
    return this;

and call this method where your re writing your current code.


Try this code

 public class DataBaseHelper extends SQLiteOpenHelper{
    public SQLiteDatabase openDataBase() throws SQLException{

            //Open the database

            File dbFile = _myContext.getDatabasePath( DB_PATH + DB_NAME ); 
            _myDataBase = SQLiteDatabase.openDatabase(dbFile.toString(), null, SQLiteDatabase.OPEN_READWRITE);

            return _myDataBase;
        }//end of openDataBase() method


maybe you are opening an opened Database. close your database every time your work is done.