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?
Creating And Updating Database In Android It provides two methods onCreate(SQLiteDatabase db), onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion). The SQLiteOpenHelper is responsible for opening database if exist, creating database if it does not exists and upgrading if required.
Create a database using an SQL helperTABLE_NAME; Just like files that you save on the device's internal storage, Android stores your database in your app's private folder.
SQLiteOpenHelper class is used for database creation and version management. For performing any database operation, you have to provide the implementation of onCreate() and onUpgrade() methods of SQLiteOpenHelper class.
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 of getWritableDatabase() or getReadableDatabase() is called.
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 databaseonUpgrade(db)
or onDowngrade(db)
if necessaryThey 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With