1

在我DATABSE我有一個名爲Place(id,place_name,latitude,longitude,place_details)SQLite的 - 通過ID刪除不刪除錶行

我想刪除ListView的項目點擊一排簡單的表。

我曾嘗試以下操作:

Place.java

public class Place { 
    int _id; 
    private String _name; 
    private double _latitude,_longitude; 
    private String _placedetails; 


    public Place(int id, String name, double latitude, double longitude,String placedetails) 
     { 
      this._id = id; 
      this._name = name; 
      this._latitude = latitude; 
      this._longitude = longitude; 
      this._placedetails = placedetails; 


    } 

    public Place(String name, double latitude, double longitude,String placedetails) 
    { 

     this._name = name; 
     this._latitude = latitude; 
     this._longitude = longitude; 
     this._placedetails = placedetails; 

    } 

    public Place() { 

    } 
    public int getID(){ 
     return this._id; 
    } 


    public void setID(int id){ 
     this._id = id; 
    } 

    public void setname(String placename) 
    { 
     this._name = placename; 


    } 

    public String getname() 
    { 
     return this._name; 


    } 

    public void setlatitude(double latitude) 
    { 
     this._latitude=latitude; 


    } 

    public double getlatitude() 
    { 
     return this._latitude; 


    } 


    public void setlongitude(double longitude) 
    { 
     this._longitude = longitude; 


    } 

    public double getlongitude() 
    { 
     return this._longitude; 


    } 


    public void setPlacedetails(String placedetails) 
    { 

     this._placedetails = placedetails; 
    } 


    public String getplacedetails() 
    { 
     return this._placedetails; 
    } 


} 

中,我要插入的地方的活動已經是這樣的:

MySqliteHelper db = new MySqliteHelper(this); 

    db.addPlaces(new Place(myplacename, mylatitude, mylongitude, myplacedetails)); 

按如下說明:Android table creation Failure (near "autoincrement": syntax error)? ,我不認爲我需要明確的ID。它將爲Place的每個對象創建。 //我在這裏錯了嗎?

在ListView的onitemclicklistener,下面的代碼是用於刪除的地方:

Place place = places_adapter.getItem(pos); 
    db.deletePlaces(place); 

並在MySQLiteHelper延伸SQLiteHelper,方法如下:

public void addPlaces(Place place) 
    { 

     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues convalues = new ContentValues(); 


     convalues.put(KEY_PLACENAME,place.getID()); 
     convalues.put(KEY_PLACENAME,place.getname()); 
     convalues.put(KEY_LATITUDE,place.getlatitude()); 
     convalues.put(KEY_LONGITUDE,place.getlongitude()); 
     convalues.put(KEY_PLACEDETAILS,place.getplacedetails()); 

      db.insert(TABLE_NAME, null, convalues); 
      Log.d("my","db.insert(TABLE_NAME, null, convalues)"); 
      Log.d("my", "Values inserted"); 
      db.close(); 

    } 

    public void deletePlaces(Place place) 
    { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     int id = place.getID(); 

     Log.d("my",Integer.toString(id)); 
     db.delete(TABLE_NAME, KEY_ID + " = ?", 
       new String[]{Integer.toString(id)}); 
     db.close(); 
    } 

雖然刪除,在logcat中的值id始終顯示0 ....並根據錶行不從數據庫中刪除。

請幫助我,如果有人知道什麼是錯的。

回答

1

我解決了它。

每次在數據庫中插入記錄時,不要忘記取回它的值... db.insert返回long值,它不是rowID

因此,我改變

public void addPlaces(Place place){ 
     db.insert(TABLE_NAME, null, convalues); 
    } 

public long addPlaces(Place place){ 

long newRowId = db.insert(TABLE_NAME, null, convalues); 
} 

,並得到這個ROWID在活動中,我將地點:

Place place = new Place(myplacename, mylatitude, mylongitude, myplacedetails); 
      long tablerowid= db.addPlaces(place); 

,也是我設置的rowid在這裏用我的setter方法:

012現在

,我全部換成int ID來longPlace.java和如下實施deletePlaces()方法:

public void deletePlaces(Place place){ 
    SQLiteDatabase db = this.getWritableDatabase(); 

    long id = place.getID(); 

    db.delete(TABLE_NAME, KEY_ID + " = ?", 
      new String[]{Long.toString(id)}); 
    db.close(); 
}