Android的sqlite的保持连接开放 [英] Android sqlite leaving connection open

查看:154
本文介绍了Android的sqlite的保持连接开放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个SQLiteDatabase实例,并调用 getReadableDatabase() getWritableDatabase()和处理数据的方式,但我永不落幕通过 db.close数据库()。它是不好不收呢?我尝试添加 db.close()我的的onStop()的onDestroy()方法,但它只是强制关闭。

I create a SQLiteDatabase instance and call getReadableDatabase() or getWritableDatabase() and manipulate data that way but I never close the database via db.close(). Is it bad to not close it? I tried adding db.close() to my onStop() or onDestroy() methods but it just force closes.

推荐答案

尽可能晚地打开一个连接。尽快将其关闭。关闭它在一个finally块,以确保它被关闭。

Open a connection as late as possible. Close it as soon as possible. Close it in a "finally" block to make sure it gets closed.

和在一般情况下,在相反的次序将获得它们处置资源的

And in general, dispose of resources in the reverse order that you obtain them.

这篇关于Android的sqlite的保持连接开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆