java - db.delete deletes the first row instead of the row specified -
i want able delete specific column in sqlite database.
i have tried pass id of column whereclause
, keep getting error saying whereclause
needs string value.
i have combed so solution have not found answers question. have tried google search , have written question many different ways, no avail.
here code
delete button
public void todeletenote(view view) { dbhelper dbhelper = new dbhelper(this); int deletedrows = dbhelper.deletedata(selectedid); if (deletedrows > 0){ toast.maketext(getbasecontext(), selectedname + " deleted successfully", toast.length_long).show(); } else { toast.maketext(getbasecontext(), "something went wrong!", toast.length_long).show(); } intent intent5 = new intent(editdata.this, mainactivity.class); startactivity(intent5); } }
dbhelper delete method
public int deletedata(int id){ string id = string.valueof(id); sqlitedatabase database = this.getwritabledatabase(); return database.delete(table_name, _id + " = ?",new string[]{id});
create table
@override public void oncreate(sqlitedatabase db) { string createtable = "create table " + table_name + "(_id integer primary key autoincrement, " + map_no + " integer, " + location + " text, " + date + " integer, " + notathomes + " text)"; db.execsql(createtable); }
if need more informantion, please feel free ask.
you should not rely on _id
column of database uniquely identify row in table, if have 3 rows , table have _id
0, 1, 2. not case.
that value _id
column gets incremented irrespective of total number of rows in table. example, if have 3 rows initially, might have _id
0, 1, 2. if delete first 2 rows , add same 2 rows again, have 2, 3, 4 _id
.
so uniquely identify row in table, should define own unique id (like uuid) in separate column, not change row deletion , addition.
Comments
Post a Comment