0

no such column: task_date (code 1): , while compiling: SELECT * FROM tasks_table WHERE task_status = ? OR task_status = ? ORDER BY task_date checked the code many time and couldn't find the error!!

DBHelper

public class DBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;

    public static final String DATABASE_NAME = "reminder_database";

    public static final String TASKS_TABLE = "tasks_table";

    public static final String TASK_TITLE_COLUMN = "task_title";
    public static final String TASK_DATE_COLUMN = "task_date";
    public static final String TASK_PRIORITY_COLUMN = "task_priority";
    public static final String TASK_STATUS_COLUMN = "task_status";
    public static final String TASK_TIME_STAMP_COLUMN = "task_time_stamp";

    private static final String TASKS_TABLE_CREATE_SCRIPT =  "CREATE TABLE "
            + TASKS_TABLE + " (" + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + TASK_TITLE_COLUMN + " TEXT NOT NULL, "
            + TASK_DATE_COLUMN + " LONG, " + TASK_PRIORITY_COLUMN + " INTEGER, "
            + TASK_STATUS_COLUMN + " INTEGER, " + TASK_TIME_STAMP_COLUMN + " LONG);";


    public static final String SELECTION_STATUS = DBHelper.TASK_STATUS_COLUMN + " = ?";
    public static final String SELECTION_TIME_STAMP = TASK_TIME_STAMP_COLUMN + " = ?";

    private DBQueryManager dbQueryManager;
    private DBUpdateManager dbUpdateManager;



    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        dbQueryManager = new DBQueryManager(getReadableDatabase());
        dbUpdateManager = new DBUpdateManager(getWritableDatabase());
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TASKS_TABLE_CREATE_SCRIPT);

    }

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

    public void saveTask(ModelTask task){

        ContentValues newValues = new ContentValues();

        newValues.put(TASK_TITLE_COLUMN,task.getTitle());
        newValues.put(TASK_DATE_COLUMN,task.getDate());
        newValues.put(TASK_STATUS_COLUMN,task.getStatus());
        newValues.put(TASK_PRIORITY_COLUMN,task.getPriority());
        newValues.put(TASK_TIME_STAMP_COLUMN, task.getTimeStamp());

        getWritableDatabase().insert(TASKS_TABLE, null, newValues);

    }

    public DBUpdateManager update() {
        return dbUpdateManager;
    }

    public DBQueryManager query() {
        return dbQueryManager;
    }

    public void removeTask(long timeStamp){
        getWritableDatabase().delete(TASKS_TABLE,SELECTION_TIME_STAMP,new String[]{Long.toString(timeStamp)});

    }
}

DBQueryManager

public class DBQueryManager {
    private SQLiteDatabase sqLiteDatabase;

    public DBQueryManager(SQLiteDatabase sqLiteDatabase) {
        this.sqLiteDatabase = sqLiteDatabase;
    }


    public ModelTask getTask(long timeStamp){
        ModelTask modelTask = null;
        Cursor c = sqLiteDatabase.query(DBHelper.TASKS_TABLE,null,DBHelper.SELECTION_TIME_STAMP,
                new String[]{Long.toString(timeStamp)},null,null,null);

        if (c.moveToFirst()){

            String title = c.getString(c.getColumnIndex(DBHelper.TASK_TITLE_COLUMN));
            long date =  c.getLong(c.getColumnIndex(DBHelper.TASK_DATE_COLUMN));
            int priority = c.getInt(c.getColumnIndex(DBHelper.TASK_PRIORITY_COLUMN));
            int status = c.getInt(c.getColumnIndex(DBHelper.TASK_STATUS_COLUMN));
            modelTask = new ModelTask(title,date,priority,status,timeStamp);

        }
        c.close();
        return modelTask;

    }


    public List<ModelTask> getTask(String selection , String[] selectionArgs,String orderBy){
        List<ModelTask> tasks = new ArrayList<>();

        Cursor c = sqLiteDatabase.query(DBHelper.TASKS_TABLE, null,selection,selectionArgs,null,null,orderBy);

        if (c.moveToFirst()){
            do {

                String title = c.getString(c.getColumnIndex(DBHelper.TASK_TITLE_COLUMN));
                long date =  c.getLong(c.getColumnIndex(DBHelper.TASK_DATE_COLUMN));
                int priority = c.getInt(c.getColumnIndex(DBHelper.TASK_PRIORITY_COLUMN));
                int status = c.getInt(c.getColumnIndex(DBHelper.TASK_STATUS_COLUMN));
                long timeStamp = c.getInt(c.getColumnIndex(DBHelper.TASK_TIME_STAMP_COLUMN));

                ModelTask modelTask = new ModelTask(title,date,priority,status,timeStamp);
                tasks.add(modelTask);



            }while (c.moveToNext());
        }
        c.close();
        return tasks;


    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
o.m.k
  • 45
  • 1
  • 4

1 Answers1

1

Let's bet you added task_date after a previous run?

As Hrundi V. Bakshi stated, if you added that column to your table after running the program once, you will need to increment your database version so your onUpgrade() method is called, which will add the column to your table.

Whenever structure changes are made to a database, the version should be incremented, so that the proper callbacks are initiated for the SQLite DB to recreate the tables with the new fields/columns.

public static final int DATABASE_VERSION = 1; // Your database version, increment it.

to

public static final int DATABASE_VERSION = 2;
Andrew Senner
  • 2,479
  • 1
  • 18
  • 24
  • so I have to get public List getTask methods in the onUpgrade() ,how i can to increment my database version ???? – o.m.k Jan 14 '16 at 18:51
  • I don't think you understood me, I was simply asking for the code that was calling that `getTask()` method. But if you did indeed, add the column `task_date` after running the program, change `public static final int DATABASE_VERSION = 2` and run the program again. Your tables will be cleared and deleted, and recreated with the new columns. – Andrew Senner Jan 14 '16 at 18:53
  • I understand you, thank you. – o.m.k Jan 14 '16 at 21:47