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

Questions:

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,
                SQLiteDatabase.OPEN_READWRITE);

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);
     }

Answer:

try

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.

Answer:

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
}

Answer:

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