0

I am learning to use Sqlite on Android by using this tutorial. I am having trouble understanding some of the code.

  public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PH_NO + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

I make a new object of DatabaseHandler in my activity. The super in the constructor is SQLiteOpenHelper constructor. The code works great, it creates a new database, if there is none and it uses the old one if it exists. I would like to make some changes to this code(I want to add different tables to one db), but I dont understand how exactly does this work, how does the constructor know if it should create a new db or use the existing one?

2 Answers 2

3

The trick is that your class extends SQLiteOpenHelper, the call to super in your constructor triggers a lot of behind-the-scenes code.

If you read through the SQLiteOpenHelper source code you'll see that getWritableDatabase() and getReadableDatabase() call the same method: SQLiteOpenHelper#getDatabaseLocked(). This method does most of the work. It is the one that determines whether a database needs to be created, opened, upgraded, or anything else, from the information that you supplied in one simple command: super(context, DATABASE_NAME, null, DATABASE_VERSION);.

Sign up to request clarification or add additional context in comments.

4 Comments

OK. But when is the onCreate function called which creates the table?
If no database exist, it is only called by SQLiteOpenHelper after you call getWritableDatabase().
But what if want to create a new table in existing DB.
@BorutFlis: "But what if want to create a new table in existing DB" -- then you increment your schema version (DATABASE_VERSION in your code sample) and put the add-the-new-table logic in onUpgrade(). The process winds up being reminiscent of Rails migrations and similar sorts of database deployment mechanisms.
2

When you want to open your db, you call SQLiteOpenHelper.getReadableDatabase() or SQLiteOpenHelper.getWriteableDatabase(). If the database exists, these return a handle to it. If it doesn't exist, the system calls onCreate(), where you do the db.execSQL.

It would be better to call the class DatabaseOpenHelper instead of DatabaseHandler. What it does is defer creating the database until its needed, instead of creating it at the beginning of some other class. This is particularly useful for content providers backed by an SQLite database. You should use ContentProvider.onCreate() to do very quick initialization, and then implement SQLiteOpenHelper.onCreate() to create the database. That way, the system can load your ContentProvider when your app starts, but it doesn't have to do anything with your database until something tries to access it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.