Android SQLite not deleting row by id -
i trying delete row given specific id , see update reflected in listview, row not deleted. same process works update
, not delete. here code:
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(); } ...
if == 2, means user clicked on delete
button, id (in db) of item clicked passed, , call delete
. process works update, not delete.
edit: neat things
something weird found if try update
entry, delete any single entry db. if don't try update first, can't.
here table 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 change database schema, must increment 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) { // database cache online data, upgrade policy // discard data , start on db.execsql(sql_delete_entries); oncreate(db); } public void ondowngrade(sqlitedatabase db, int oldversion, int newversion) { onupgrade(db, oldversion, newversion); } }
solved
when clicking delete button, wasn't saving _id
sharedpreferences, in mainactivity
getting wrong value of id.
try this:
public void delete(int id) { sqlitedatabase db = getwritabledatabase(); db.delete("mst_item", "itemid=" + id, null); db.close(); }
Comments
Post a Comment