2012-04-20 152 views
2

我目前正在致力於在android數據庫中添加,查看和刪除值,但不幸的是,這本書只包含了添加和查看部分,所以我來這裏尋求幫助。從android數據庫中刪除一行

我有一個名爲DeleteEntry的類,它輸入一個變量moduleDelete,它通過類DeleteEntryViewModule。該類然後使用這個值來返回使用此showEvent()方法

private void showEvents(Cursor cursor) { 
StringBuilder builder = new StringBuilder("Saved Events:\n"); 
while (cursor.moveToNext()){ 
    long id = cursor.getLong(0); 
    String module = cursor.getString(1); 
    String day = cursor.getString(2); 
    String starttime = cursor.getString(3); 
    String duration = cursor.getString(4); 
    String typeofsession = cursor.getString(5); 
    String room = cursor.getString(6); 
    builder.append(id).append(": "); 
    builder.append(module).append(": "); 
    builder.append(day).append(": "); 
    builder.append(starttime).append(": "); 
    builder.append(duration).append(": "); 
    builder.append(typeofsession).append(": "); 
    builder.append(room).append("\n "); 
} 
TextView text = (TextView) findViewById(R.id.text); 
text.setText(builder); 
} 

我如何取值從建設者和它們放入變量,這樣我就可以執行該代碼行的所有值。

private void removeEvent (String module, String day, String starttime, String duration, String typeofsession, String room){ 
    try{ 
    SQLiteDatabase db = events.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(DAY, day); 
    values.put(MODULE, module); 
    values.put(STARTTIME, starttime); 
    values.put(DURATION, duration); 
    values.put(TYPEOFSESSION, typeofsession); 
    values.put(ROOM, room); 
    db.replaceOrThrow(TABLE_NAME, null, values); 
    }catch(SQLiteException ex){ 
     Toast toast = Toast.makeText(this, "Broken", 10); 
     toast.show(); 
    } 

}

感謝

回答

0

嘗試使用下面的代碼,而不是db.replaceOrThrow

​​

你能解釋一下爲什麼要使用db.replaceOrThrow?

+1

我正在按照我所做的添加一行db.insertOrThrow,但沒有放棄replace,只是用null填充列。我已經插入了你的修改,並且正確地編譯了所有東西,但是當我真正運行它時,拋出了一個sql異常。我調試它縮小到這個android.database.sqlite.SQLiteException:沒有這樣的列:CM111:,編譯時:DELETE從時間表WHERE模塊= CM111,但我不能看到任何錯誤的SQL語句? 謝謝你的幫助 – user1341620 2012-04-20 15:27:36