SQLite Database Error - No column named task

Viewed 52

Error_Database

enter code here

public class DataBaseHandler extends SQLiteOpenHelper {

private static final int VERSION = 1;
private static final String NAME = "toDoListDatabase";
private static final String TODO_TABLE = "todo";
private static final String ID = "id";
private static final String TASK = "task";
private static final String STATUS = "status";
private static final String CREATE_TODO_TABLE = "CREATE TABLE " + TODO_TABLE + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TASK + " TEXT, "
        + STATUS + " INTEGER)";

private SQLiteDatabase db;

public DataBaseHandler(Context context) {
    super(context, NAME, null, VERSION);
}

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

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TODO_TABLE);
    // Create tables again
    onCreate(db);
}

public void openDatabase() {
    db = this.getWritableDatabase();
}

public void insertTask(ToDoModel task){
    ContentValues cv = new ContentValues();
    cv.put(TASK, task.getTask());
    cv.put(STATUS, 0);
    db.insert(TODO_TABLE, null, cv);
}

public List<ToDoModel> getAllTasks(){
    List<ToDoModel> taskList = new ArrayList<>();
    Cursor cur = null;
    db.beginTransaction();
    try{
        cur = db.query(TODO_TABLE, null, null, null, null, null, null, null);
        if(cur != null){
            if(cur.moveToFirst()){
                do{
                    ToDoModel task = new ToDoModel();
                    task.setId(cur.getInt(cur.getColumnIndexOrThrow(ID)));
                    task.setTask(cur.getString(cur.getColumnIndexOrThrow(TASK)));
                    task.setStatus(cur.getInt(cur.getColumnIndexOrThrow(STATUS)));
                    taskList.add(task);
                }
                while(cur.moveToNext());
            }
        }
    }
    finally {
        db.endTransaction();
        assert cur != null;
        cur.close();
    }
    return taskList;
}

public void updateStatus(int id, int status){
    ContentValues cv = new ContentValues();
    cv.put(STATUS, status);
    db.update(TODO_TABLE, cv, ID + "= ?", new String[] {String.valueOf(id)});
}

public void updateTask(int id, String task) {
    ContentValues cv = new ContentValues();
    cv.put(TASK, task);
    db.update(TODO_TABLE, cv, ID + "= ?", new String[] {String.valueOf(id)});
}

public void deleteTask(int id){
    db.delete(TODO_TABLE, ID + "= ?", new String[] {String.valueOf(id)});
}

}

Can somebody proofread this? The error is : "no column named task in "INSERT INTO todo(status,task) VALUES (?,?)"

"android.database.sqlite.SQLiteException: table todo has no column named task (code 1 SQLITE_ERROR): , while compiling: INSERT INTO todo(status,task) VALUES (?,?)".

I also attached a ss of the error as a link.

I'm trying to create a database where I can store tasks. Does anyone has a fix for this?

Thanks!

1 Answers

If you're on an emulator, try reinstalling the application! It works sometimes.

Related