dimanche 21 décembre 2014

SQLite - delete by id not deleting table row

In my databse I have simple table named Place(id,place_name,latitude,longitude,place_details)


I want to delete a row on ListView's item click.


I have tried following:


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;
}


}


The Activity in which I want to insert places has something like:



MySqliteHelper db = new MySqliteHelper(this);

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


As explained here: Android table creation Failure (near "autoincrement": syntax error)? , I don't think I need to explicitly id. It will be created for each object of Place. //Am I wrong here?


In ListView's onitemclicklistener, following code is for deleting place:



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


And In MySQLiteHelper which extends SQLiteHelper, methods are as following:



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();
}


While deleting, in logcat the value of id is showing always 0....and according table row is not deleteing from database.


Please help me if anyone know anything is wrong.


Aucun commentaire:

Enregistrer un commentaire