SQLiteException:没有这样的列

时间:2015-11-01 19:11:30

标签: android sql database sqlite android-sqlite

对不起,如果问题可能重复。
我看了其他类似的问题和他们的答案,但由于我对SQL术语不太熟悉,我无法通过这些答案找到解决方案。

请检查我的代码以找出问题所在?

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

}

1 个答案:

答案 0 :(得分:1)

狂野猜测:您在之后添加了(未指定的)列一次运行应用程序 Unistalling并重新安装应用程序,通常可以解决问题。