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

Ben Wong
Ben Wong
19,426 Points

We are going to release a new version that allows users to select a home planet as well. Migrating a SQLite Database.

Need help! Stuck:(

public class AlienSQLiteHelper extends SQLiteOpenHelper {

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

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 CREATE_ALIENS = "CREATE TABLE " + ALIENS_TABLE + " (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " TEXT, " + COLUMN_TYPE + " TEXT)";

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

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: sqLiteDatabase.execSQL(DB_ALTER);

}

} }

AlienSQLiteHelper.java
public class AlienSQLiteHelper extends SQLiteOpenHelper {

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

  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 CREATE_ALIENS = "CREATE TABLE " + ALIENS_TABLE + " (" +
          "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
          COLUMN_NAME + " TEXT, " +
          COLUMN_TYPE + " TEXT)";

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

  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:
     sqLiteDatabase.execSQL(DB_ALTER);

    }
  }
}

3 Answers

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

If you are stuck on 1st part, than you should just change the DB_VERSION = 1; to 2.

Ben Wong
Ben Wong
19,426 Points

Got it George. Much Appreciated it!

Just that thanks very much,got it too