dimanche 1 novembre 2015

SQLiteException : No such column

Sorry if the question is possibly a duplicate.
I've looked the other similar questions and their answers, but since I'm not so familiar with the SQL terms, I'm not able to find a solution trough these answers.

Can you please check my code to find what's wrong?

public class DBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "todolist_db";
    private static final String TABLE_TODOS = "todos";
    private static final String TODO_TITLE = "todo_title";
    private static final String TODO_CATEGORY = "todo_category";
    private static final String TODO_YEAR = "todo_year";
    private static final String TODO_MONTH = "todo_month";
    private static final String TODO_DAY = "todo_day";
    private static final String TODO_HOUR = "todo_hour";
    private static final String TODO_MINUTE = "todo_minute";
    private static final String TODO_PRIORITY = "todo_priority";


    public DBHelper(Context context) {
        // context is context , name is DATABASE_NAME, no factory, version is 1
        super(context, DATABASE_NAME, null, 1);
    }

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    //Creating the table for the first time
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE " + TABLE_TODOS +
                " (" + TODO_TITLE + " TEXT, " +
                TODO_CATEGORY + " TEXT, " +
                TODO_YEAR + " INTEGER , " +
                TODO_MONTH + " TEXT, " +
                TODO_DAY + " INTEGER, " +
                TODO_HOUR + " TEXT, " +
                TODO_MINUTE + " TEXT, " +
                TODO_PRIORITY + " INTEGER);";

        Log.d("DBHelper", "SQL : " + sql);
        db.execSQL(sql);
    }

    //Upgrading the table in  the other versions of our program
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXIST " + TABLE_TODOS);
        onCreate(db);
    }

    //Add new todoObj to database
    public void insertTodo(TodoObj todoObj) {
        //Hangi database olduðunu belirt
        //Dadatabase'i yazılabilir yap
        SQLiteDatabase db = this.getWritableDatabase();

        //Verileri tutması için container olustur.
        ContentValues values = new ContentValues();

        //verileri ekle
        values.put("todo_title", todoObj.getmTitle());
        values.put("todo_category", todoObj.getmCategory());
        values.put("todo_year", todoObj.getmYear());
        values.put("todo_month", todoObj.getmMonth());
        values.put("todo_day", todoObj.getmDay());
        values.put("todo_hour", todoObj.getmHour());
        values.put("todo_minute", todoObj.getmMinute());
        values.put("todo_priority", todoObj.getmPriorityDrawableID());

        //Yeni satır olustur
        db.insert(TABLE_TODOS, null, values);
        //Commit
        db.close();
    }

    public int deleteTodo(String title) {
        String where = "title=?";
        SQLiteDatabase db = this.getWritableDatabase();
        int numberOFEntriesDeleted = db.delete(DATABASE_NAME, where, new String[]{title});

        return numberOFEntriesDeleted;
    }

    public ArrayList<TodoObj> getAllTodos() {

        // Veritabanından gelen sonuçları saklayacağımız liste
        ArrayList<TodoObj> todos = new ArrayList<TodoObj>();
        SQLiteDatabase db = this.getWritableDatabase();

        //Cursor methodu basit bir select oluþturmak için idealdir
        //Cursor objesi bize sonuçlar içinde dolaþma olanaðý saðlar
        Cursor cursor = db.query(TABLE_TODOS,
                new String[]{"todo_title", "todo_category", "todo_year",
                        "todo_month", "todo_day", "todo_hour", "todo_minute","todo_priority"}
                , null, null, null, null, null);

        while (cursor.moveToNext()) {
            TodoObj todoObj = new TodoObj();
            todoObj.setmTitle(cursor.getString(0));
            todoObj.setmCategory(cursor.getString(1));
            todoObj.setmYear(cursor.getInt(2));
            todoObj.setmMonth(cursor.getString(3));
            todoObj.setmDay(cursor.getInt(4));
            todoObj.setmHour(cursor.getString(5));
            todoObj.setmMinute(cursor.getString(6));
            todoObj.setmPriorityDrawableID(cursor.getInt(7));
            todos.add(todoObj);
        }

        return todos;
    }

    public void deleteAll() {
        SQLiteDatabase db = this.getWritableDatabase();
        /*db.execSQL("delete from " + TABLE_TODOS);
        db.close();*/
        db.delete(TABLE_TODOS, null, null);
        db.close();
    }

    public ArrayList<TodoObj> returnByCategory (String categoryName) {
        // Veritabanından gelen sonuçları saklayacağımız liste
        ArrayList<TodoObj> todos = new ArrayList<TodoObj>();

        SQLiteDatabase db = this.getWritableDatabase();

        //Cursor methodu basit bir select oluþturmak için idealdir
        //Cursor objesi bize sonuçlar içinde dolaþma olanaðý saðlar
        Cursor cursor = db.query(TABLE_TODOS,
                new String[]{"todo_title", "todo_category", "todo_year",
                        "todo_month", "todo_day", "todo_hour", "todo_minute","todo_priority"}
                , null, null, null, null, null);

        while (cursor.moveToNext()) {
            if(cursor.getString(1).toString().equalsIgnoreCase(categoryName)){

                TodoObj todoObj = new TodoObj();
                todoObj.setmTitle(cursor.getString(0));
                todoObj.setmCategory(cursor.getString(1));
                todoObj.setmYear(cursor.getInt(2));
                todoObj.setmMonth(cursor.getString(3));
                todoObj.setmDay(cursor.getInt(4));
                todoObj.setmHour(cursor.getString(5));
                todoObj.setmMinute(cursor.getString(6));
                todoObj.setmPriorityDrawableID(cursor.getInt(7));
                todos.add(todoObj);
            }
        }

        return todos;
    }

}

Aucun commentaire:

Enregistrer un commentaire