1

Am using database in my application when i execute my application i shows following error

android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

pls help me how to solve this error

Vicky
  • 285
  • 2
  • 4
  • 13

4 Answers4

0

You didn't close your database after using it. So you must close your database using below statement.

db.close(); 
selva_pollachi
  • 4,147
  • 4
  • 29
  • 42
0

You should always close the cursor & Db after doing a transaction..
And remember to close it also in case of exceptions..

you have to implement like this in your class which is extending SQLite Helper class:

@Override
    public synchronized void close() {
        if(db != null){
            db.close();
        super.close();
        }   
    }
Nermeen
  • 15,883
  • 5
  • 59
  • 72
0

in your mainclass call this method

db.close();

add this method to your helperclass

@Override
    public synchronized void close() {
        if(db != null){
            db.close();
        super.close();
        }   
    }

More information about Andorid Sqlite Database

Sardor Dushamov
  • 1,665
  • 3
  • 17
  • 44
0

You can completely avoid opening/closing the database by making your database a singleton, which is the recommended approach in Android.

The problem only gets worse when you want to access the database across multiple threads.

Check out this question for examples of how to keep a single open database Android SQLite DB When to Close

Although this solves the problem you still need to make sure you close your cursors when done, unless your using the Loader API which will handle this for you.

Community
  • 1
  • 1
Ian Warwick
  • 4,774
  • 3
  • 27
  • 27