no such column: task_date (code 1):, while compiling: SELECT * FROM tasks_table WHERE task_status =? OR task_status =? ORDER BY task_date

I checked the code many times and could not 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

 ublic 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; } } 

    1 answer 1

    Maybe you need to do: 1. - change the table creation code to

     CREATE TABLE "tasks_table" ( `_ID` INTEGER PRIMARY KEY AUTOINCREMENT, `task_title` TEXT NOT NULL, `task_date` INTEGER, `task_priority` INTEGER, `task_status` INTEGER, `task_time_stamp` INTEGER ) 

    2. redo the query `

     > SELECT * FROM tasks_table WHERE task_status = ? OR task_status = ? > ORDER BY task_date 

    `on

      `SELECT * FROM tasks_table WHERE task_status = ? ORDER BY task_date;` 

    I think my advice will help you.

    • also in WHERE task_status =? OR task_status =? ORDER BY task_date, you are trying to select between two identical salt cells with unknown variables and still want to sort it all out. The database does not understand your request, you may need to change (or remove) one column from the request - zloj
    • It's not exactly WHERE and Order By , my query with similar conditions works fine. - temq
    • I meant to remove one of the columns task_status =? in the request - zloj
    • With two columns, too, everything is OK - temq
    • I found a solution, the problem is that if I add a column to the table, the database structure changes and I need to increase the database version as I call the onUpgrade () method, which will add the column to the table. Whenever a structure is made into a database, the version must be increased, so the appropriate callbacks are initiated for the SQLite database to recreate the table with new fields / columns. public static final int DATABASE_VERSION = 2; - omk