Skip to content Skip to sidebar Skip to footer

Why Is My Constructor Not Getting Called?

I'm trying to add some records to a SQLite table, but LogCat is telling me the table does not exist. And DDMS shows that, yes, that table is not being/has not been created. Yet I d

Solution 1:

  1. You must call getWritableDatabase() or getReadableDatabase() at some point.

  2. In your constructor for SQLiteHandlerDeliveryItem, you must call super(...).

Solution 2:

Firstly, the method SQLiteOpenHelper.onCreate() is not the constructor. I'm not sure, but it looks like you are mixing the 2 concepts up in the question.


Now onto trying to solve the problem...

It seems like your table is not being created. From the online help for SQLiteOpenHelper.onCreate():

publicabstractvoidonCreate (SQLiteDatabase db)

Called when the database is created for the first time. This iswhere the creation of tables and the initial population of the tables should happen.

So this method will be called only once for a typical use case - when the app is first installed (first uses the database). At that point, onCreate() is called which creates the actual tables. But this is not called every time you run the app.

My advice to check if this is the problem - uninstall the app, and try again. Put a breakpoint in this method to make sure it does actually get run.

What can sometimes happen is that we have a database on the app, then update things to add a table, and forget to uninstall. Another correct way to do it would be to use the onUpgrade() method to add the new table.

Let me know how this pans out, or if I have misunderstood the issue.

Solution 3:

As I was hoping, it actually turns out to be an easy fix: simply increment the value of your database version. I read this on p. 262 of O'Reilly's "Programming Android":

DATABASE_VERSION ...If the version of the database on the machine is less than DATABASE_VERSION, the system runs your onUpgrade method to upgrade the database to the current level.

Thus, all you need to do is increment that number:

privatestaticfinalint DATABASE_VERSION = 2; 
    // I changed it from "1" to "2", but you could change it to anything you want, I reckon (42, or 1776, or whatever).

Incrementing the version value causes onUpgrade to run, which drops the old version of the database and then calls onCreate:

publicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DELIVERYITEMS);
    onCreate(db);
}

The onCreate() event then does exactly that - adding the DDL to create the table.

Here is the pertinent code in the class that extends SQLiteOpenHelper in context:

publicclassSQLiteHandlerDeliveryItemextendsSQLiteOpenHelper{

privatestaticfinalint DATABASE_VERSION = 2; 
privatestaticfinalString DATABASE_NAME = "HHS.db";
privatestaticfinalString TABLE_DELIVERYITEMS = "deliveryitems";

privatestaticfinalString COLUMN_ID = "_id";
. . .
privatestaticfinalString COLUMN_QTY = "quantity";

public SQLiteHandlerDeliveryItem(Context context, SQLiteDatabase.CursorFactory factory)  
{
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
publicvoid onCreate(SQLiteDatabase db) {
    String CREATE_DELIVERYITEMS_TABLE = "CREATE TABLE " +
            TABLE_DELIVERYITEMS + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_INVOICENUM + " TEXT,"
. . .
            + COLUMN_DEPTNUM + " INTEGER," + COLUMN_SUBDEPT + " TEXT," + COLUMN_QTY + " 
TEXT"
            + ")";
    db.execSQL(CREATE_DELIVERYITEMS_TABLE);
}

@Override
publicvoid onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DELIVERYITEMS);
    onCreate(db);
}
. . .

So it seems to me having multiple classes that extend SQLiteOpenHelper is a good thing, since you can thus keep your code separated, rather than have one gigantic/humongous pair of onUpgrade/onCreate spaghetti.

Post a Comment for "Why Is My Constructor Not Getting Called?"