Correctly Managing your SQLite Database
来源:互联网 发布:星网锐捷和锐捷网络 编辑:程序博客网 时间:2024/05/14 02:26
原文
关注微博:InfoStation
One thing that I’ve noticed other Android developers having trouble with is properly setting up their SQLiteDatabase. Often times, I come across questions on StackOverflow asking about error messages such as,
E/Database(234): Leak found
E/Database(234): Caused by: java.lang.IllegalStateException: SQLiteDatabase created and never closed
As you have probably figured out, this exception is thrown when you have opened more SQLiteDatabase instances than you have closed. Managing the database can be complicated when first starting out with Android development, especially to those who are just beginning to understand the Activity lifecycle. The easiest solution is to make your database instance a singleton instance across the entire application’s lifecycle. This will ensure that no leaks occur, and will make your life a lot easier since it eliminates the possibility of forgetting to close your database as you code.
Here are two examples that illustrates three possible approaches in managing your singleton database. These will ensure safe access to the database throughout the application.
Approach #1: Use a Singleton to Instantiate the SQLiteOpenHelper
Declare your database helper as a static instance variable and use the Singleton pattern to guarantee the singleton property. The sample code below should give you a good idea on how to go about designing the DatabaseHelper class correctly.
The static getInstance() method ensures that only one DatabaseHelper will ever exist at any given time. If the sInstance object has not been initialized, one will be created. If one has already been created then it will simply be returned. You should not initialize your helper object using with new DatabaseHelper(context)! Instead, always use DatabaseHelper.getInstance(context), as it guarantees that only one database helper will exist across the entire application’s lifecycle.
public class DatabaseHelper extends SQLiteOpenHelper { private static DatabaseHelper sInstance; private static final String DATABASE_NAME = "database_name"; private static final String DATABASE_TABLE = "table_name"; private static final int DATABASE_VERSION = 1; public static synchronized DatabaseHelper getInstance(Context context) { // Use the application context, which will ensure that you // don't accidentally leak an Activity's context. // See this article for more information: http://bit.ly/6LRzfx if (sInstance == null) { sInstance = new DatabaseHelper(context.getApplicationContext()); } return sInstance; } /** * Constructor should be private to prevent direct instantiation. * make call to static method "getInstance()" instead. */ private DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); }}
Approach #2: Wrap the SQLiteDatabase in a ContentProvider
This is also a nice approach. For one, the new CursorLoader class requires ContentProviders, so if you want an Activity or Fragment to implement LoaderManager.LoaderCallbacks with a CursorLoader (as discussed in this post), you’ll need to implement a ContentProvider for your application. Further, you don’t need to worry about making a singleton database helper with ContentProviders. Simply call getContentResolver() from the Activity and the system will take care of everything for you (in other words, there is no need for designing a Singleton pattern to prevent multiple instances from being created).
Leave a comment if this helped or if you have any questions!
- Correctly Managing your SQLite Database
- Using your own SQLite database in Android applications
- Using your own SQLite database in Android applications
- Sharpening Your Skills: Managing Teams
- Managing Versions of Your Project
- Managing Your App's Memory
- Managing Your App's Memory
- Managing Your App's Memory
- [SQLite]---database
- Creating and Managing a Database with DBCA
- managing database storage structures system表空间
- Oracle Database(2)--Managing the Redo Log
- 7 Managing Database Workload Using Services
- 7 Managing Users and Securing the Database
- How to Export/import database dump correctly
- Managing Your App's Memory 翻译
- Managing Your App's Memory(8)篇文章
- Your profile could not be opened correctly
- Android热补丁动态修复技术(二):实战!CLASS_ISPREVERIFIED问题!
- PostgreSQL pgcrypto模块加密解密函数
- MVP最新理解 不给Prensenter 写接口
- eclipse svn拿下代码后重新把java project变为web project
- ucenter及discuz整合,双向同步注册及登录注意事项2016年9月13
- Correctly Managing your SQLite Database
- HDU 5725 Game
- ${sessionScope.user}的使用方法
- iOS全网最详细高仿淘宝购物车,现已加入KFC豪华午餐,带走不要钱
- struts2下载的问题
- Java Bean 属性命名规范问题分析
- 初级排序-选择排序、插入排序、希尔排序总结
- (第三周项目4)顺序表的应用(2)
- Android疑问(一)