Managing SQLite Database
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 ContentProvider
s, 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 withContentProvider
s. 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的更多相关文章
- 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 ...
- iOS - SQLite Database 操作数据库
iOS - SQLite Database 操作数据库 Sqlite 能被用在ios上做数据处理用,只要你懂得一点sql 就很容易使用sqlite 1:创建一个简单的View based appl ...
- Using SQLite database in your Windows 10 apps
MVP可以在channel 9上传视频了,所以准备做个英文视频传上去分享给大家,本文做稿子. Hello everyone, As we all know, SQLite is a great and ...
- [转]Android Studio SQLite Database Multiple Tables Example
本文转自:http://instinctcoder.com/android-studio-sqlite-database-multiple-tables-example/ BY TAN WOON HO ...
- [转]Android Studio SQLite Database Example
本文转自:http://instinctcoder.com/android-studio-sqlite-database-example/ BY TAN WOON HOW · PUBLISHED AP ...
- 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 ...
- SQLite Database Browser 2.0使用方法
在网上找一个SQLITE查看器 这个查看器叫做:www.jb51.net/database/118822.html 这个查看器可以新建SQLITE文件数据库,可以建立表索引,写SQL语句,编辑表数据 ...
- [转]Android | Simple SQLite Database Tutorial
本文转自:http://hmkcode.com/android-simple-sqlite-database-tutorial/ Android SQLite database is an integ ...
- svn更新的时候断电,下次在更新出现svn: sqlite: database disk image is malformed
svn更新的时候断电,下次在更新出现svn: sqlite: database disk image is malformed 这种悲催的事情竟然发生了 解决办法:
随机推荐
- 大数据学习——hive显示命令
show databases; desc t_partition001; desc extended t_partition002; desc formatted t_partition002; !c ...
- LoadRunner中,参数化时Unique取值方式的理解
LoadRunner中,参数化时Unique取值方式的理解 2012年10月15日 18:10:36 瑞秋 阅读数:10028 在LoadRunner中进行参数化时,Parameter的取值设置有 ...
- HDU1280前m大的数creat at 9:51,3.13,2016
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- spring security3.1升级到4.1问题(1)访问/j_spring_security_check 404
升级完后,发现登录不进去,把post改成get好了,但是系统的提交表单功能都不能用了,也是解决了很长时间,最后找到了根本原因. spring sercurity 4.0 csrf保护是默认开启的,cs ...
- BZOJ1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名
n<=1000头牛各有一个未知值Ai,已知m<=10000条形如Ax>Ay的不等关系,求将整个序列排序的最少比较次数. Aa>Ab,Ab>Ac -------> A ...
- Nginx,Apache,Tomcat区别
Nginx:一款能承受高并发的HTTP服务器,异步的,多个连接(万级别)可以对应一个进程,进行响应.基于事件驱动模型. Nginx优点:负载均衡.反向代理.处理静态文件优势. Apache:相对于Ng ...
- PatentTips - Wear Leveling for Erasable Memories
BACKGROUND Erasable memories may have erasable elements that can become unreliable after a predeterm ...
- 【Java源码】集合类-优先队列PriorityQueue
一.类继承关系 public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serial ...
- 洛谷—— P2002 消息扩散
P2002 消息扩散 题目背景 本场比赛第一题,给个简单的吧,这 100 分先拿着. 题目描述 有n个城市,中间有单向道路连接,消息会沿着道路扩散,现在给出n个城市及其之间的道路,问至少需要在几个城市 ...
- 最短路——Dijkstra算法
模板 水模板ing #include <cstdio> #include <cstring> #include <algorithm> #include <i ...