-4

I dont get it, it keep crashing even though i had check the code is correct. It just keeps telling me there is no such column date. I will post my codes here for DBHelper class.

private static final String DATABASE_NAME = "tasks.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_TASK = "tasks";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_DESCRIPTION = "description";
private static final String COLUMN_DATE = "date"; 

 @Override
public void onCreate(SQLiteDatabase db) {
    String createTaskTableSql = "CREATE TABLE " + TABLE_TASK + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_NAME + " TEXT, "
            + COLUMN_DESCRIPTION + " TEXT, "
            + COLUMN_DATE + " DATETIME DEFAULT CURRENT_TIMESTAMP )";
    db.execSQL(createTaskTableSql);
}

 public ArrayList<Lists> getAllLists() {
    ArrayList<Lists> lists = new ArrayList<Lists>();

    String selectQuery = "SELECT " + COLUMN_ID + ", "
            + COLUMN_NAME + ", "  + COLUMN_DESCRIPTION + ", " + COLUMN_DATE  + " FROM " + TABLE_TASK;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            int id = cursor.getInt(0);
            String listsName = cursor.getString(1);
            String desc = cursor.getString(2);
            String date = cursor.getString(3);
            Lists allLists = new Lists(id, listsName, desc, date);
            lists.add(allLists);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return lists;
}

I cant identify the issue. Someone please help me out.

Mobile Developer
  • 191
  • 1
  • 3
  • 10

1 Answers1

1

no such column date

You have two option .

  1. You should Uninstall app & Run again .
  2. Or you can Rectify onUpgrade Method .

DO

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

    // If you want to add a column
    if (newVersion > oldVersion) {
        db.execSQL("ALTER TABLE ADD_NAME ADD COLUMN NEW_COLOUM INTEGER DEFAULT 0"); // You can add TEXT Field
    }
}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    *You can't use Name fieldshould date* yes, he can ... `date` **is not** SQLite keyword – Selvin Aug 02 '17 at 09:30
  • 1
    It works fine already. The issue is only i must reinstall app and run again That part you were right. – Mobile Developer Aug 02 '17 at 09:32
  • @MobileDeveloper indeed . Check my edited answer – IntelliJ Amiya Aug 02 '17 at 09:41
  • 1
    yeah I noticed that and I did undone the downvote (btw it's internet, I'm not "sir" but "you") ... the comments will stay for 2 reasons: 1. because you are answering for duplicates instead closing it 2. to remeber that you are writeing wrong answers and fixing after reading comments – Selvin Aug 02 '17 at 09:41