/********************************************************************************************
* author:conowen@大钟
* E-mail:conowen@hotmail.com
* http://blog.csdn.net/conowen
* 注:本文为原创,仅作为学习交流使用,转载请标明作者及出处。
********************************************************************************************/
1、SQLiteOpenHelper介绍
通过上篇博文,http://blog.csdn.net/conowen/article/details/7276417,了解了SQLite数据库的相关操作方法,但是一般在实际开发中,为了更加方便地管理、维护、升级数据库,需要通过继承SQLiteOpenHelper类来管理SQLite数据库。
关于SQLiteOpenHelper的官方说明如下:
A helper class to manage database creation and version management.
You create a subclass implementing onCreate(SQLiteDatabase)
,onUpgrade(SQLiteDatabase, int, int)
and optionallyonOpen(SQLiteDatabase)
, and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state.
This class makes it easy for ContentProvider
implementations to defer opening and upgrading the database until first use, to avoid blocking application startup with long-running database upgrades.
For an example, see the NotePadProvider class in the NotePad sample application, in thesamples/ directory of the SDK.
简单翻译:SQLiteOpenHelper可以创建数据库,和管理数据库的版本。
在继承SQLiteOpenHelper的类(extends
SQLiteOpenHelper
)里面,通过复写
onCreate(SQLiteDatabase)
,onUpgrade(SQLiteDatabase, int, int)
和onOpen(SQLiteDatabase)
(可选)来操作数据库。
2、SQLiteOpenHelper()的具体用法
创建一个新的class如下所示,onCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)方法会被自动添加。
/*
* @author:conowen
*