2017-04-07 23 views
0

刪除行我試圖刪除一行給出了具體的ID,看到反映在我的列表視圖更新,但該行不會被刪除。同樣的過程適用於update,但不能刪除。下面是代碼:該項目的Android SQLite的不是ID

MainActivity.java

... 

@Override 
    public void onPositiveClick(DialogFragment dialog, int which) { 

      //Shared Preferences 
      SharedPreferences pref = getSharedPreferences(EditLog.PREF_FILENAME, 0); 
      long id = 0; 
      String date = ""; 
      String hours = ""; 
      String lessonType = ""; 
      String weatherCondition = ""; 
      if (!(pref == null)) { 
       id = pref.getLong("rowId", 0); 
       date = pref.getString("date", ""); 
       hours = pref.getString("hours", ""); 
       lessonType = pref.getString("lessonType", ""); 
       weatherCondition = pref.getString("weatherCondition", ""); 
      } 

      if (which == 2) { 
       LessonLogDBHelper dbhelp = new LessonLogDBHelper(getApplicationContext()); 
       final SQLiteDatabase db = dbhelp.getWritableDatabase(); 
       db.delete(LessonLogContract.LogEntry.TABLE_NAME, "_id=?", new String[]{Long.toString(id)}); 
      } else { 
       LessonLogDBHelper dbhelp = new LessonLogDBHelper(getApplicationContext()); 
       final SQLiteDatabase db = dbhelp.getWritableDatabase(); 
       final ContentValues values = new ContentValues(); 
       values.put(LessonLogContract.LogEntry.CN_DATE, date); 
       values.put(LessonLogContract.LogEntry.CN_HOURS, hours); 
       values.put(LessonLogContract.LogEntry.CN_LESSON_TYPE, lessonType); 
       values.put(LessonLogContract.LogEntry.CN_WEATHER, weatherCondition); 
       db.update(LessonLogContract.LogEntry.TABLE_NAME, values, "_id=" + id, null); 

      } 

      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
      transaction.replace(R.id.content_main, new DrivingLog()); 
      transaction.commit(); 

    } 

... 

如果其== 2,則意味着用戶點擊了delete按鈕時,ID(在分貝)卡嗒一聲被傳遞,並我致電delete。此過程適用於更新,但不能刪除。

編輯:巧妙的事情

奇怪的事情是,我發現,如果我試圖update的條目,那麼我將刪除從數據庫中的任何一個條目。如果我不嘗試先更新,那我就不能。

這裏是我的表DEF:

LessonLogContract

package com.example.name.drivinglessona3; 

import android.provider.BaseColumns; 
import android.util.StringBuilderPrinter; 

public class LessonLogContract { 
    private LessonLogContract() {} 

    public static class LogEntry implements BaseColumns { 
     public static final String TABLE_NAME = "lesson"; 
     public static final String CN_DATE = "date"; 
     public static final String CN_HOURS = "hours"; 
     public static final String CN_LESSON_TYPE = "lesson_type"; 
     public static final String CN_WEATHER = "weather"; 
    } 
} 

LessonLogDBHelper

package com.example.name.drivinglessona3; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 



public class LessonLogDBHelper extends SQLiteOpenHelper { 
    // If you change the database schema, you must increment the database version. 
    private static final String SQL_CREATE_ENTRIES = 
      "CREATE TABLE " + LessonLogContract.LogEntry.TABLE_NAME + " (" + 
        LessonLogContract.LogEntry._ID + " INTEGER PRIMARY KEY," + 
        LessonLogContract.LogEntry.CN_DATE+ " TEXT," + 
        LessonLogContract.LogEntry.CN_HOURS+ " TEXT," + 
        LessonLogContract.LogEntry.CN_LESSON_TYPE+ " TEXT," + 
        LessonLogContract.LogEntry.CN_WEATHER + " TEXT)"; 

    private static final String SQL_DELETE_ENTRIES = 
      "DROP TABLE IF EXISTS " + LessonLogContract.LogEntry.TABLE_NAME; 

    public static final int DATABASE_VERSION = 1; 
    public static final String DATABASE_NAME = "LogEntry.db"; 

    public LessonLogDBHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(SQL_CREATE_ENTRIES); 
    } 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // This database is only a cache for online data, so its upgrade policy is 
     // to simply to discard the data and start over 
     db.execSQL(SQL_DELETE_ENTRIES); 
     onCreate(db); 
    } 
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     onUpgrade(db, oldVersion, newVersion); 
    } 
} 

解決

當我點擊我的刪除按鈕,我不節能_id到SharedPreferences,所以在MainActivity我得到了錯誤的id值。

+0

你知道,如果db.delete實際上是被稱爲? – M0rty

+0

是啊,是被稱爲 –

+0

[Rü確保您的物品點擊ID傳遞.. ??? ....而且這裏留出空間.....「_id =?」 –

回答

0

試試這個:

public void delete(int id) { 
     SQLiteDatabase db = getWritableDatabase(); 
     db.delete("MST_Item", "ItemID=" + id, null); 
     db.close(); 
} 
+0

編碼db.close();如果調用方法期望數據庫仍然處於打開狀態,則可能會導致問題。 – MikeT

+0

什麼是MST_Item? –

+0

Ya MST_Item是數據庫中的表名。 –

0

只要改變將String.valueOf()代替Long.toString()中刪除行。 這意味着更換db.delete()與下面的代碼行:

db.delete(LessonLogContract.LogEntry.TABLE_NAME, "_id=?", new String[]{String.valueOf(id)}); 

我希望這種方法工作妳。

+0

that does not work –

+0

@JeremyFisher然後可能你必須嘗試'db.delete(LessonLogContract.LogEntry.TABLE_NAME,「_id =」+ id +「」,null);'代碼。因爲我認爲當有一個整型或雙重類型比較時,最好直接比較而不是轉換並比較字符串.. –