2015-11-01 139 views
-1

對不起,如果問題可能是重複的。
我看了其他類似的問題和他們的答案,但由於我對SQL條款不太熟悉,我無法通過這些答案找到解決方案。SQLiteException:沒有這樣的列

你能檢查一下我的代碼,找出有什麼問題嗎?

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

} 
+0

@ FrankN.Stein但我從模擬器中卸載它比我再次運行該應用程序。還有什麼我該怎麼辦? – Recomer

+1

它應該夠了。 –

+0

你可以告訴行號,它給出錯誤 – Ambika

回答

1

胡亂猜測:您添加的(不指定)列後你運行應用程序一次。
Unistalling和重新安裝應用程序,通常可以解決問題。

相關問題