Altered Table Column is not able to access using Greendao-Android

1.2k Views Asked by At

Using onUpgrade in SQLiteOpenHelper, i can alter the table and new column name with a default value is added.

But when i access that particular table inside the activity,it shows only the old column count.As per alteration,it should display 4 (column count).But it shows 3 Only.

My code inside onUpgrade method:

 db.execSQL("ALTER TABLE " + MyTableDaoDao.TABLENAME + " ADD COLUMN 'VRESION_NO' INTEGER NOT NULL DEFAULT '11';");

// As im sure that table is altered because when i try to alter it again using the same column name,it shows duplicate column name sqlite exception.

For getting all columns count:

 daoSession.getMyTableDao().getAllColumns().length  // returns old table count

How can get the refreshed table details in GreenDAO?. Is there anyone come across this situation.

Thanks in advance.

2

There are 2 best solutions below

3
On BEST ANSWER

Probably you forgot to recreate your dao code from an updated schema.

Greendao doesn't use reflection or scan the database for creating a mapping between database and java. This mapping is created by the dao-generator. So there are two tasks if you want to update your database schema:

  1. Update the entity-classes and daos by recreating them with the new schema and increase schema-version.
  2. Implement your update strategy in your app, i.e. drop all tables and recreate them or add/change/remove the columns/tables/indexes/... that have changed since the schema-version that was active before (see my answers here and here).
3
On

Only thing I could come up with is that you're getting the columns from the wrong table.

You could try to drop the table and then recreate it with the new column, maybe then it will register. Also think it is easier this way to write dynamic code. (just add the column to a list of Strings and loop trough it when creating the table).