0

While adding a new column, I noticed something peculiar: it wasn't being created. So I ran logs everywhere, but still cannot find the problem.

Log.d("DBInfo", "Columns created: " + Arrays.toString(cursor.getColumnNames()));

returns

/DBInfo: Columns created: [_id, note_title, note_content]

when it should be returning something along the lines of

/DBInfo: Columns created: [_id, note_title, note_content, alarm_set]

DBHelper.class

private static class DBHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "todolist.db";
    public static final int DB_VERS = 1;
    public static final String TABLE = "notes";

    public static final String _ID = BaseColumns._ID;
    public static final String NOTE_TITLE  = "note_title";
    public static final String NOTE_CONTENT = "note_content";
    public static final String ALARM_SET = "alarm_set";


    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERS);
    }
@Override
public void onCreate(SQLiteDatabase db) {
    // Creates Database

    String sqlQuery = "CREATE TABLE " + TABLE + " (" +
            _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            NOTE_TITLE + " TEXT, " +
            NOTE_CONTENT + " TEXT, " +
            ALARM_SET + " TEXT" + ");";
    db.execSQL(sqlQuery);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE);
    onCreate(db);
}
}

The sad yet exciting life of a programmer: I don't know what the problem is, I don't know why it doesn't work.

planetastro
  • 1,716
  • 2
  • 15
  • 28
  • 2
    Try uninstalling it and run again – Rod_Algonquin Mar 17 '16 at 14:24
  • 2
    My initial thought is you have to clear app data or increment the database version. "onCreate()" only gets called when the database doesn't exist. "onUpgrade()" gets called when you update to a new version (which in this case calls destroys the database and calls "onCreate()". – DeeV Mar 17 '16 at 14:26
  • Where does the cursor form your log come from? Was the query that returned it called with an empty projection? – Manuel Nunes Mar 17 '16 at 14:37

1 Answers1

1

The thing it´s, since you do not increase the database version, when installing will not pass on onUpgrade method, probably you should test the previous version to do the proper upgrade:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  if (oldVersion == 1 )
      // proper upgrade
      db.execSQL("DROP TABLE IF EXISTS " + TABLE);
  onCreate(db);
}

And remember to increase the DB_VERS for any database change.

Felipe R. Saruhashi
  • 1,709
  • 16
  • 22