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 这种悲催的事情竟然发生了 解决办法:
随机推荐
- BeautifulSoup4系列二
前言 本篇详细介绍beautifulsoup4的功能,从最基础的开始讲起,让小伙伴们都能入门 一.读取HTML页面 1.先写一个简单的html页面,把以下内容copy出来,保存为html格式文件 &l ...
- Struts2的Action配置的各项默认值
1 如果没有为action指定class,默认是ActionSupport 2 如果没有为action指定method,默认执行action中的execute()方法 3 如果没有指定result的n ...
- 动手实操:如何用 Python 实现人脸识别,证明这个杨幂是那个杨幂?
当前,人脸识别应用于许多领域,如支付宝的用户认证,许多的能识别人心情的 AI,也就是人的面部表情,还有能分析人的年龄等等,而这里面有着许多的难度,在这里我想要分享的是一个利用七牛 SDK 简单的实现人 ...
- 洛谷P1244 青蛙过河
P1244 青蛙过河 362通过 525提交 题目提供者该用户不存在 标签 难度普及- 时空限制1s / 128MB 提交 讨论 题解 最新讨论更多讨论 题目什么意思 题目看不懂啊 题目描述 有一条河 ...
- (在线工具)JSON字符串转换成Java实体类(POJO)
http://www.bejson.com/json2javapojo/ 付代码代码转换示例: public static FixMixedOrderResponse serialization(St ...
- python-web apache mod_python 模块的安装
安装apache 下载mod_python 编译安装 测试 下载mod_python,下载地址:mod_python 在GitHub 上面, 下载之后:目录结构如下: 安装依赖: #查找可安装的依赖 ...
- 标准格式包含: 私有属性 无参构造 有参构造 setter 和getter 需求中的方法 需求一: 员工类Employee 属性:姓名name,工号id,工资salary 行为:显示所有成员信息的方法show() 需求二: 动物类Animal 属性:姓名name,年龄age 行为:吃饭
// 员工类 public class Employee { private String name; private int id; private double salary; public ...
- Java简单实验--关于课后提到的java重载函数的简单分析
根据这一小段代码,获得了以下的测试截图: 简单分析:根据输出结果,判断这段代码用到了两个不同的函数方法,输出的不止有double类型的数,还有整型的数. 又根据类中的定义情况,square是根据判断传 ...
- MySQL注释(转)
MySQL支持3种注释风格: 1.从‘#’字符从行尾. 2.从‘-- ’序列到行尾.请注意‘-- ’(双破折号)注释风格要求第2个破折号后面至少跟一个空格符(例如空格.tab.换行符等等). 3.从/ ...
- django 简易博客开发 5 markdown支持、代码高亮、gravatar头像服务
上一篇博客介绍了comments库使用及ajax支持,现在blog已经具备了基本的功能,但是只能发表文字,不支持富文本编辑.今天我们利用markdown添加富文本支持. markdown语法说明: h ...