how is the database created?
I'm making a database application, and my program works and I've understood most of the tutorial I've been following. However, one aspect remains unclear to me.
There is an inner class of MyDBHelper extending SQLiteOpenHelper. Outer variables include the SQLiteDatabase called d. The code for the MyDBHelper is:
private static class MyDBHelper extends SQLiteOpenHelper {
MyDBHelper(Context c) {
super(c, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVers, int newVers) {
Log.w(TAG, "Upgrading database from version " + oldVers + " to " + newVers + ", which will destroy all old data.");
db.execSQL("DROP TABLE IF EXISTS GM");
onCreate(db);
}
}
My question is how does this actually create the initial database. It occurs in the onCreate() method, but as far as I can see, this is never called. I understand that it is called when the database is created for the first time, but where? And furthermore, how is it passed a SQLiteDatabase db? I haven't passed any database to the method. And how is my SQLiteDatabase db variable from the outer class set to the created database? Could someone talk me through this like an idiot?
onCreate()
and onUpgrade()
methods are really called the first time when Db is created. In facts, it's checked in getReadableDatabase() or getWritebleDatabase()
methods of SQLiteOpenHelper
. It will check if the DB already exist on the data directory and what is it's version. According to this it will either call onCreate(), or onUpgrade()
. Or nothing, if db file exist and of correct version.
You can search your code for executing myDBHelper.getReadable(Writable)Database()
. That is the time when this check will be performed.
Please let me know if more details are needed. Good luck
Keep in mind that you are extending SQLiteOpenHelper, all of the magic happens in this super class, specifically the database is initially created (or just re-opened) when you call either getReadableDatabase()
or getWritableDatabase()
. These two methods:
SQLiteDatabase db
variable (and control passing db
to your callback methods) db
by calling your onCreate(db)
method or opening the existing database onUpgrade(db)
or onDowngrade(db)
if necessary They also call a few more callback methods like onConfigure(db)
, onOpen(db)
, etc. (Read more about these methods.) If it will help, you can read through the source code yourself to understand the structure of how and when all of this happens.
The onCreate() method is not a constructor for this class. onCreate is called when you create the DB.
Here PeopleDB extends SQLiteOpenHelper. This code is from a different class and onCreate is called when getWritableDatabase() or getReadableDatabase(), or anything of the sort is called
PeopleDB db = null; //onCreate NOT called here
db=new PeopleDB(getContext());
db.getWritableDatabase(); //onCreate is called here!
Hope that helps.
链接地址: http://www.djcxy.com/p/42622.html上一篇: onMeasure自定义视图说明
下一篇: 数据库是如何创建的?