Domanda

I have been practicing a lot with the SQLiteDatabase and SQLiteOpenHelper classes recently, there is something that is puzzling me quite a bit.

I have this constructor and onCreate() method:

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

@Override
    public void onCreate(SQLiteDatabase db) {
        String create_table = "CREATE TABLE " + MEASUREMENT_TYPE_TABLE +
                "(" + idCol + " integer primary key autoincrement, " +
                typeCol + " varchar(255) not null) ";

        db.execSQL(create_table);

        populateMeasurementTable();

}

the method being called at the end is as below:

public void populateMeasurementTable() {
        SQLiteDatabase db = this.getWritableDatabase();
        for(int i = 0; i < measurementType.length; i++) {
            ContentValues values = new ContentValues();
            values.put(typeCol, measurementType[i]);
            db.insert(MEASUREMENT_TYPE_TABLE, null, values);
        }
}

The point of the method is to populate the table that is created in the onCreate() method. However when I go to tools -> Android -> Monitor (DDMS included) in IntelliJ and look at the database file, it doesn't contain the table or any data (of course).

measurementType is simply an array that is being iterated through:

private static final String[] measurementType = new String[] {"acceleration", "angles", "area", "astronomical",
            "density", "energy", "force", "frequency", "length/distance", "power", "pressure", "speed",
            "temperature", "torque", "volume", "weight"};

I have tried passing the reference to the database like this:

populateMeasurementTable(db);

and removing this statement in the method:

db = this.getWriteableDatabse();

I am pretty sure that it would complain about already having the connection open otherwise. But it still didn't populate the create the table or populate it.

The onCreate() method in the activity is this:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DataBaseHandler handler = new DataBaseHandler(getApplicationContext());
}

Nothing special, just instantiating the handler class. My understanding is that at that point the onCreate() method would be called and everything would follow through.

Bare in mind that I get no SQLExceptions or errors of any kind.

So I tried this instead in the activity's onCreate() method:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DataBaseHandler handler = new DataBaseHandler(getApplicationContext());
        handler.populateMeasurementTable();
}

I opened the database file from in a SQLite browser and not only was the table there, but the data was in there as well...

So my question I guess is in three fold:

  • Is simply instantiating the database handler not enough to invoke its onCreate() method? Because that would be why the table isn't created as well as the data not being inserted?
  • If instantiating is enough to invoke the method, then why didn't it create the table and populate it with data.

At the moment I am guessing that the first question has the answer (if that makes sense)

  • Finally for the third question, is there a way that I can simply instantiate it and have it run initialisation methods? Because that populate method is the first of many and I don't really want to be calling them in the activity, doesn't feel like good design habit to be getting into.
È stato utile?

Soluzione

Is simply instantiating the database handler not enough to invoke its onCreate method? because that would be why the table isn't created as well as the data not being inserted?

Yes, in addition to creating the helper, call getWritableDatabase(). If the database file didn't exist, your onCreate() callback is invoked.

Calling getWritableDatabase() in onCreate() or a method called from there is an error. You'll get an exception about recursive call. Pass the db argument to onCreate() around in case you need to work on the database in other methods.

See also: When is SQLiteOpenHelper onCreate() / onUpgrade() run?

If instantiating is enough to invoke the method, then why didn't it create the table and populate it with data.

It's not enough.

Finally for the third question, is there a way that I can simply instantiate it and have it run initialisation methods? Because that populate method is the first of many and I don't really want to be calling them in the activity, doesn't feel like good design habit to be getting into.

Have the database population in database helper onCreate().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top