SQLiteOpenHelper的使用

来源:互联网 发布:linux 远程拷贝文件夹 编辑:程序博客网 时间:2024/05/16 17:14

SQLiteOpenHelper:这是一个辅助类,这个类主要用于生产一个数据库,并对数据库的版本进行管理。此类为一抽象类,使用时需要继承此类并实现该类的方法 :

onCreate(SQLiteDatabase):Called when the database is created for the first time.通常用来创建数据库表。

onUpgrade(SQLiteDatabase, int, int):Called when the database needs to be upgraded.通常用来删除old表,创建new表。

 

optionally:

onOpen(SQLiteDatabase):Called when the database has been opened.打开数据库时的回调函数,通常不用。

 

在构造方法中传入4个参数(Context,DB_NAME,CursorFactory,DB_VERSION),此时并没有创建数据库。而在第一次调用getReadableDatabase()或getWritableDatabase()时,才会去创建数据库,并调用onCreate()方法,只要数据库存在,就不会再次调用onCreate()方法。

 

getReadableDatabase():Create and/or open a database.

getWritableDatabase():Create and/or open a database that will be used for reading and writing.

原创粉丝点击