2015-11-01 97 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和重新安装应用程序,通常可以解决问题。

相关问题