8

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?

4 Answers 4

13

Try it like this:

  if (!db.isOpen()) {
       db = getApplicationContext().openOrCreateDatabase(DATABASE_PATH, SQLiteDatabase.OPEN_READWRITE, null);
     }
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this but instead of getApplicationContext() I have added SQLiteDatabase.How will get applicationcontext in a class extended from SQLiteOpenHelper
so contructor of your SQLiteOpenHelper takes one parameter Context context so context.openOrCreateDatabase() ...
2

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.

Comments

0

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
}

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.