数据库
首页 > 数据库> > 何时关闭游标和数据库?

何时关闭游标和数据库?

作者:互联网

我在自定义视图中以不同的方法多次使用同一光标.每次使用后我应该关闭游标还是可以保持游标打开直到视图被破坏?与数据库相同,一旦创建保存该视图的活动并在活动被销毁时关闭,是否可以将其打开?如上所述,我不断收到错误消息“ close()从未在数据库上显式调用…”.

解决方法:

Should I close the cursor after each use or can I keep it open until
the view is destroyed?

要看.如果游标结果不会同时更改(例如,远程连接修改数据库),则可以.别忘了使用CursorLoader让Android自动管理光标(例如,如果您的应用崩溃,则关闭光标).

And the same for the database, can it be opened once the activity that
holds this view is created and closed when the activity is destroyed?

是的,您可以在onResume()中打开并在onPause()回调中关闭,或者当您知道不再查询数据库时.

I continuously receive an error ‘close() was never explicitly called
on database…’ when I do as described above.

仅当您的应用崩溃时,这是正常的.如果您像我上面所说的那样做,就不会发生.

标签:android-sqlite,android-cursor,android
来源: https://codeday.me/bug/20191031/1978291.html