Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

STAGE 6 CHALLENGE 4

Okay, new database creation is set. But we still need to alter existing databases. The statement to alter it is already started below. Add the new column at the end. Don't forget its type!

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 DB_ALTER = "ALTER TABLE " + ALIENS_TABLE + " ADD COLUMN " +COLUMN_HOME_PLANET + "TEXT";

  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 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:

    }
  }
}

I have followed every step of the video but it not working ....help ..thanks in advance

House Trigo
House Trigo
36,620 Points

Add a space between the "+" sign to the left of COLUMN_HOME_PLANET in the alter statement.

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

1 Answer

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

Hi Cathrina, I hope you figured out that.. -Just in case if u didn't-

All i did differently than your code was to add " TEXT " as here: public static final String DB_ALTER = "ALTER TABLE " + ALIENS_TABLE + " ADD COLUMN " + COLUMN_HOME_PLANET + " TEXT ";

All the best!