SQLiteOpenHelper - how is the database created? SQLiteOpenHelper - how is the database created? sqlite sqlite

SQLiteOpenHelper - how is the database created?


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:

  • Define the SQLiteDatabase db variable (and control passing db to your callback methods)
  • Initialize db by calling your onCreate(db) method or opening the existing database
  • Check the version number and call your 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.