原文出自:author:conowen * E-mail:conowen@hotmail.com * http://blog.csdn.net/conowen
1、SQLiteOpenHelper介绍
通过上篇博文,http://www.52php.cn/article/p-hzhgfvjf-gp.html,了解了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) ,int) 和onOpen(SQLiteDatabase) (可选)来操作数据库。
2、SQLiteOpenHelper()的具体用法
创建一个新的class如下所示,onCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)方法会被自动添加。
[java]
view plain
copy
print
?
- packagecom.conowen.sqlite;
- importandroid.content.Context;
- importandroid.database.sqlite.SQLiteDatabase;
- importandroid.database.sqlite.SQLiteDatabase.CursorFactory;
- importandroid.database.sqlite.SQLiteOpenHelper;
- publicclassDbHelperextendsSQLiteOpenHelper{
- publicDbHelper(Contextcontext,Stringname,CursorFactoryfactory,
- intversion){
- super(context,name,factory,version);
- }
- @Override
- publicvoidonCreate(SQLiteDatabasedb){
- }
- @Override
- publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){
- }
- }
方法详解
[java]
view plain
copy
print
?
- publicSQLiteOpenHelper(Contextcontext,SQLiteDatabase.CursorFactoryfactory,intversion)
Since:
API Level 1
Create a helper object to create,open,and/or manage a database. This method always returns very quickly. The database is not actually created or opened until one ofgetWritableDatabase() orgetReadableDatabase() is called.
Parameters
|
Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.
Called when the database needs to be upgraded. The implementation should use this method to drop tables,add tables,or do anything else it needs to upgrade to the new schema version.
The SQLite ALTER TABLE documentation can be found here. If you add new columns you can use ALTER TABLE to insert them into a live table. If you rename or remove columns you can use ALTER TABLE to rename the old table,then create the new table and then populate the new table with the contents of the old table.
更新数据库,包括删除表,添加表等各种操作。若版本是第一版,也就是刚刚建立数据库,onUpgrade()方法里面就不用写东西,因为第一版数据库何来更新之说,以后发布的版本,数据库更新的话,可以在onUpgrade()方法添加各种更新的操作。
getWritableDatabase() 方法————以读写方式打开数据库,如果数据库所在磁盘空间满了,而使用的又是getWritableDatabase() 方法就会出错。
=========================================================================================================