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<Cursor> with aCursorLoader (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 withContentProviders. 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).

转载:http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html

Managing SQLite Database的更多相关文章

  1. How do I list all tables/indices contained in an SQLite database

    How do I list all tables/indices contained in an SQLite database If you are running the sqlite3 comm ...

  2. iOS - SQLite Database 操作数据库

    iOS - SQLite Database 操作数据库   Sqlite 能被用在ios上做数据处理用,只要你懂得一点sql 就很容易使用sqlite 1:创建一个简单的View based appl ...

  3. Using SQLite database in your Windows 10 apps

    MVP可以在channel 9上传视频了,所以准备做个英文视频传上去分享给大家,本文做稿子. Hello everyone, As we all know, SQLite is a great and ...

  4. [转]Android Studio SQLite Database Multiple Tables Example

    本文转自:http://instinctcoder.com/android-studio-sqlite-database-multiple-tables-example/ BY TAN WOON HO ...

  5. [转]Android Studio SQLite Database Example

    本文转自:http://instinctcoder.com/android-studio-sqlite-database-example/ BY TAN WOON HOW · PUBLISHED AP ...

  6. Persisting iOS Application Data in SQLite Database Using FMDB

    In previous articles we have utilized NSUserDefaults and .NET web services to persist iPhone data. N ...

  7. SQLite Database Browser 2.0使用方法

    在网上找一个SQLITE查看器 这个查看器叫做:www.jb51.net/database/118822.html 这个查看器可以新建SQLITE文件数据库,可以建立表索引,写SQL语句,编辑表数据 ...

  8. [转]Android | Simple SQLite Database Tutorial

    本文转自:http://hmkcode.com/android-simple-sqlite-database-tutorial/ Android SQLite database is an integ ...

  9. svn更新的时候断电,下次在更新出现svn: sqlite: database disk image is malformed

    svn更新的时候断电,下次在更新出现svn: sqlite: database disk image is malformed 这种悲催的事情竟然发生了 解决办法:

随机推荐

  1. centos7安装rlwrap

    http://utopia.knoware.nl/~hlub/uck/rlwrap/ 下载rlwrap-0.42.tar.gz 找到centos7 安装的iso中的 Packages的 ncurses ...

  2. poj 3678 XOR和OR和AND(简单2-sat问题)

    /* 题意:给你一些边,每条边有一个值和一个运算符XOR OR AND求是否存在一些点使得所有的边根据这些运算符 可以符合条件的权值. 建边方式参考:http://blog.csdn.net/shua ...

  3. hdu 2167 状态压缩

    /*与1565的解法差不多*/ #include<stdio.h> #include<string.h> int map[16][16]; int dp[2][1<< ...

  4. 6572平台上关于wifi热点切换跳的坑

    最近在做一个无屏的项目,需要开启设备的wifi热点,通过连接热点设置设备wifi,本来看起来很容易完成的一件事情,遇到了一下的坑 在wifi切换状态时,大概率出现不能切换的问题,比如从wifi状态切换 ...

  5. [NOIP1998] 普及组

    三连击 题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数构成1:2:3的比例,试求出所有满足条件的三个三位数. 输入输出格式 输入格式: 木有输入 输出格式: 若干行, ...

  6. msp430项目编程25

    msp430中项目---带有断电保护的电子密码锁 1.I2C工作原理 2.I2C通信协议 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习

  7. python学习之-- redis模块操作 HASH

    redis 操作 之 -Hash Hash 操作:hash在内存中的存储格式 name hash n1 ------> k1 -> v1 k2 -> v2 k3 -> v3hs ...

  8. 洛谷——P1262 间谍网络

    P1262 间谍网络 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意 ...

  9. 转:浅谈Linux的内存管理机制

    一 物理内存和虚拟内存          我们知道,直接从物理内存读写数据要比从硬盘读写数据要快的多,因此,我们希望所有数据的读取和写入都在内存完成,而内存是有限的,这样就引出了物理内存与虚拟内存的概 ...

  10. freeswitch对媒体的处理的三种方式

    一.默认方式:媒体通过freeswitch, RTP被freeswtich转发, freeswitch控制编码的协商并在协商不一致时提供语音编码转换能力, 支持录音,二次拨号等.   二.代理模式: ...