简体   繁体   中英

Do I need call close() when I use getWritableDatabase()?

If I have code like this

private final SQLiteDatabase database;
private final SQLiteOpenHelper helper;
// ...
this.database = helper.getWritableDatabase();
// ...

Do I need to call close() explicitly?

I am confused by this answer: https://stackoverflow.com/a/18595604/4932519

Take a look to doc for getWritableDatabase():

Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. ( Make sure to call close() when you no longer need the database. )

PS Sorry for my English.

One call to close() should always be paired with every call to getWritableDatabase() .

When you're calling getWritableDatabase() once for every query, you should also call close() when you've finished with the query.

When you're calling getWritableDatabase() when the activity starts up, and save the reference, you should call close() when the activity shuts down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM