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. 在mysql的操作界面中,如何清屏幕

    1.快捷键:Ctrl+L2.通过执行SHELL命令: \! clear实际上 \! 用来执行操作系统的shell命令,不仅是clear,其他命令也可以.shell命令执行完成后,会返回mysql Us ...

  2. 七、整合SQL基础和PL-SQL基础

    --Oracle数据库重要知识点整理 2017-01-24 soulsjie 目录 --一.创建及维护表... 2 --1.1 创建... 2 --1.2 维护表... 2 --二.临时表的分类.创建 ...

  3. XV6第一个进程

    第一个进程 本章通过第一个进程的创建来解释 xv6 是如何开始运行的,让我们得以一窥 xv6 提供的各个抽象是如何实现和交互的.xv6 尽量复用了普通操作的代码来建立第一个进程,避免单独为其撰写代码. ...

  4. Robberies(01背包)

    The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually g ...

  5. Linux 磁盘测速

    读: time dd if=/dev/zero of=/test.dbf bs=8k count=1000000 写: time dd if=/dev/zero of=/var/test bs=8k ...

  6. 服务器安装oracle前的内存调整

    #当前内存大小为512MB,安装oracle时执行检查... Checking physical memory requirements ... Expected result: 922MB Actu ...

  7. 路线统计(codevs 1482)

    题目描述 Description N个节点的有向图, 求从start到finish刚好经过时间time的总方案数 mod 502630. 输入描述 Input Description 第一行包含一个整 ...

  8. 通过rsync+inotify实现数据的实时备份 【转载】

       在前面的博文中,我讲到过利用rsync实现数据的镜像和备份,但是要实现数据的实时备份,单独靠rsync还不能实现,本文就讲述下如何实现数据的实时备份. 一.rsync的优点与不足  与传统的cp ...

  9. poj -1185 炮兵阵地 (经典状压dp)

    http://poj.org/problem?id=1185 参考博客:http://poj.org/problem?id=1185 大神博客已经讲的很清楚了,注意存状态的时候是从1开始的,所以初始化 ...

  10. UESTC 1087 【二分查找】

    问了某明==shit 中文题意不解释. 因为数据的范围是1e9以内的所以我们可以通过二分的方法枚举可能的中位数. 用二分法每次判断某一数字比中位数大还是比中位数小. 判断方法是,枚举以第n个数为起点的 ...