简体   繁体   English

使用getWritableDatabase()时是否需要调用close()?

[英]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? 我需要显式调用close()吗?

I am confused by this answer: https://stackoverflow.com/a/18595604/4932519 我对此答案感到困惑: https : //stackoverflow.com/a/18595604/4932519

Take a look to doc for getWritableDatabase(): 看一下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. ) 请确保在不再需要数据库时调用close()。

PS Sorry for my English. PS对不起,我的英语。

One call to close() should always be paired with every call to getWritableDatabase() . close()一次调用应始终与对getWritableDatabase()每次调用配对。

When you're calling getWritableDatabase() once for every query, you should also call close() when you've finished with the query. 当您为每个查询调用一次getWritableDatabase()时,在完成查询后也应该调用close()

When you're calling getWritableDatabase() when the activity starts up, and save the reference, you should call close() when the activity shuts down. 当活动启动时调用getWritableDatabase()并保存引用时,应在活动关闭时调用close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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