SQLite - 通过id删除不删除表行

时间:2014-12-21 19:06:38

标签: android database sqlite model-view-controller android-sqlite

在我的数据库中,我有一个名为Place(id,place_name,latitude,longitude,place_details)

的简单表

我想删除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)?,我认为我不需要明确地识别。它将为Place的每个对象创建。 //我错了吗?

在ListView的onitemclicklistener中,以下代码用于删除地点:

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

在扩展SQLiteHelper MySQLiteHelper 中,方法如下:

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 个答案:

答案 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);
}

并在Activity中插入了这个rowID:

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

我也使用我的setter方法在这里设置了rowID:

 place.setID(tablerowid);

现在,我将所有int ID替换为long中的Place.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();
}