Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Data Persistence Migrating a SQLite Database Adding a New Field

MUZ140915 Ronald Dzingai
MUZ140915 Ronald Dzingai
5,248 Points

Where am I getting it wrong? Execute the appropriate statement in the onUpgrade() method on AlienSQLiteHelper.java

AlienSQLiteHelper.java public class AlienSQLiteHelper extends SQLiteOpenHelper { public static final String DB_NAME = "aliens.db"; public static final int DB_VERSION = 2; public static final String ALIENS_TABLE = "ALIENS"; public static final String COLUMN_NAME = "NAME"; public static final String COLUMN_TYPE = "TYPE"; public static final String COLUMN_HOME_PLANET = "HOME_PLANET"; public static final String CREATE_ALIENS = "CREATE TABLE " + ALIENS_TABLE + " (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " TEXT, " + COLUMN_TYPE + " TEXT," + COLUMN_HOME_PLANET + "TEXT)";

public static final String DB_ALTER = "ALTER TABLE " + ALIENS_TABLE + " ADD COLUMN " + COLUMN_HOME_PLANET + " TEXT ";

public AlienSQLiteHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); }

@Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL(CREATE_ALIENS); }

@Override public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {

switch (oldVersion) { case 1: database.execSQL(DB_ALTER); }

}

} }

AlienSQLiteHelper.java
public class AlienSQLiteHelper extends SQLiteOpenHelper {

  public static final String DB_NAME = "aliens.db";
  public static final int DB_VERSION = 2;

  public static final String ALIENS_TABLE = "ALIENS";
  public static final String COLUMN_NAME = "NAME";
  public static final String COLUMN_TYPE = "TYPE";
  public static final String COLUMN_HOME_PLANET = "HOME_PLANET";

   public static final String CREATE_ALIENS = "CREATE TABLE " + ALIENS_TABLE + " (" +
          "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
          COLUMN_NAME + " TEXT, " +
          COLUMN_TYPE + " TEXT," +
    COLUMN_HOME_PLANET + "TEXT)";

 public static final String DB_ALTER = "ALTER TABLE " + ALIENS_TABLE + " ADD COLUMN " + COLUMN_HOME_PLANET + " TEXT ";

  public AlienSQLiteHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(CREATE_ALIENS);
  }

  @Override
  public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
   switch (oldVersion) {
     case 1:
     database.execSQL(DB_ALTER); }

    //switch (oldVersion) { case 1: database.execSQL(DB_ALTER); }

    }
  }
}
Andrew Sheragy
Andrew Sheragy
16,379 Points

What is the problem exactly, the execSQL(DB_ALTER) not getting called or is it running but not adding the new column? I noticed this error in CREATE_ALIENS

COLUMN_HOME_PLANET + "TEXT)"

should be a space before TEXT

MUZ140915 Ronald Dzingai
MUZ140915 Ronald Dzingai
5,248 Points

Never mind, I finally got it, Thanks for the effort,

It should be: @Override public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { switch (oldVersion) { case 1:database.execSQL(DB_ALTER); }

} The mistake was on the final closing brace, if you can try this one