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. win10 命令行无法直接ping的问题解决方法

    ping是在system32下的一个应用包里,设置完环境变量并保存之后若还是无法直接ping,可以考虑如下方法: 打开命令行窗口,键入以下命令: cd\ cd windows\system32 pin ...

  2. Boolean.valueOf("true")的用法

    Boolean.valueOf(a);a为true时返回true不管大小写,a为其他值时都返回false:

  3. Oracle 释放过度使用的Undo表空间

    故障现象:UNDO表空间越来越大,长此下去最终数据因为磁盘空间不足而崩溃: 问题分析:产生问题的原因主要以下两点: 1. 有较大的事务量让Oracle Undo自动扩展,产生过度占用磁盘空间的情况: ...

  4. POJ 1509 循环同构的最小表示法

    题目大意: 给定一个字符串,可以把一段尾部接到头部,这样找到一个最小的字符串 方案一: 利用循环同构中找最小表示的方法来解决 论文参考http://wenku.baidu.com/view/438ca ...

  5. tree(poj 1741)

    题意:给一颗n个节点的树,每条边上有一个距离v(v<=1000).定义d(u,v)为u到v的最小距离.给定k值,求有多少点对(u,v)使u到v的距离小于等于k. /* 照着点分治模板敲了敲,有很 ...

  6. 【BZOJ2982】combination(Lucas定理)

    题意:求C(n,m) n,m<=200000000 思路:c(n,m)=c(n mod mo,m mod mo)*c(n div mo,m div mo) mod mo (n>=mo或m& ...

  7. GDKOI2018游记

    D0 开开心心去酒店,在Vanda,资磁,然而和其他人住的比较远,不资磁. 开开心心打开玩具熊,吓尿了..第四部贼难. 晚上看了看网络流,1点才睡.3点多好像梦到玩具熊被吓醒,4点继续睡,6点起. D ...

  8. HUNAN 11567 Escaping (最大流)

    http://acm.hunnu.edu.cn/online/?action=problem&type=list&courseid=0&querytext=&pagen ...

  9. [洛谷U22157]刷水题(数位dp)(hash)

    题目背景 做正经题是不可能做正经题的,这辈子都不可能做正经题的,毒瘤题又不会做毒瘤题,就是水题这种东西,才维持了蒟蒻的信心: 题目描述 这里有N+1 道水题,编号分别为0 ~N+1 ,每道水题都有它自 ...

  10. [Bzoj3233][Ahoi2013]找硬币[基础DP]

    3233: [Ahoi2013]找硬币 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 924  Solved: 482[Submit][Status][ ...