sammiwei
sammiwei

Reputation: 3200

Should I call .close() on both SQLiteDatabase and SQLiteOpenHelper

SQLiteOpenHelper dbOpenHelper = new (ctx, nameofdb);
SQLiteDatabase db = dbOpener.getWritableDatabase();

Do I have to call .close() on both of these, or just one of them is enough? If yes, then which one?

SQLiteDatabase
SQLiteOpenHelper

The problem I am having is that I do not see one particular row in DDMS view in Eclipse, but when I use the Cursor to get it, it shows that I do have that entry. So I am wondering it might be caused by not closing the database properly? Anybody who can help would be great. Thanks!

Upvotes: 6

Views: 2775

Answers (1)

Squonk
Squonk

Reputation: 48871

If you look at the docs for SQLiteOpenHelper.close()...

public synchronized void close ()

Close any open database object.

It doesn't close the SQLiteOpenHelper, it closes the database.

Further to this, if you get your code right you can avoid dealing directly with the database object directly altogether.

For example, if you have a query you use regularly to get a cursor for an adapter to populate a view, create a method in your SQLiteOpenHelper class and put the query into that.

In other words, don't get a reference to the actual database in your main code just get the SQLiteOpenHelper to do everything for you.

Upvotes: 5

Related Questions