Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLiteOpenHelper - 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?

like image 493
Tim Avatar asked Sep 03 '12 20:09

Tim


People also ask

Where is database created in Android?

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.

Which method is used to create a database in Android?

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.

Which method create and or open a database in SQLite?

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.

What is the purpose of SQLiteOpenHelper?

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.


2 Answers

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.

like image 106
Sam Avatar answered Sep 22 '22 03:09

Sam


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

like image 30
AlexN Avatar answered Sep 20 '22 03:09

AlexN