SQLite Android : SQLite Delete command not working properly -
i creating android application need able delete records table consists of 2 columns name , _id both of type text _id primary key. given value of _id record deleted sqlite query not working. app runs throughout without problems doesn't delete required rows. sure records exist in database. new android applications , sqlite. read other similar posts couldn't find what's wrong code. please help.
note: tried db.execsql() in place of db.delete() didn't either.
here's contact_delete.java file.
package com.tintin.prototype_2; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class contact_delete extends activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.contact_delete); textview tv = (textview) findviewbyid(r.id.deletenumtv); final edittext et = (edittext) findviewbyid(r.id.deletenumet); button b = (button) findviewbyid(r.id.submitdelete); b.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { dbtest db = new dbtest(contact_delete.this); string numtobedelted = et.gettext().tostring(); if(numtobedelted.compareto("")==0)toast.maketext(getapplicationcontext(), "invalid entry", toast.length_short).show(); else{ if(db.deletevalues(numtobedelted)>0)toast.maketext(getapplicationcontext(), "deleted", toast.length_short).show(); else toast.maketext(getapplicationcontext(), "total number of contacts="+db.getnumberofcontacts(), toast.length_short).show(); } et.settext(""); db.close(); } }); } }
here's dbtest.java file
package com.tintin.prototype_2; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.databaseutils; import android.database.sqlite.sqliteconstraintexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class dbtest extends sqliteopenhelper{ static final string dbname = "demodb"; static final string contacttable = "contacts"; static final string id = "index"; static final string name = "name"; static final string number = "_id"; static int idx=1; public dbtest(context context){ super(context, dbname, null, 3); } public void oncreate(sqlitedatabase db){ db.execsql("create table contacts (name text , _id text primary key);"); } public void onupgrade(sqlitedatabase db,int oldversion, int newversion){ log.v("upgrade", "upgrading database version " + oldversion + " " + newversion); db.execsql("drop table if exists contacts"); oncreate(db); } public boolean insertvalues(string x, string y){ boolean ret=true; sqlitedatabase db = this.getwritabledatabase(); contentvalues cv = new contentvalues(); log.v("value of x", x); log.v("value of y", y); cv.put(name, "'"+x+"'"); cv.put(number, "'"+y+"'"); try { db.insertorthrow(contacttable, null, cv); } catch (sqliteconstraintexception e) { log.v("exception","number in database"); ret=false; e.printstacktrace(); } log.v("done", "done"); db.close(); return ret; } public int deletevalues(string num){ sqlitedatabase db = this.getwritabledatabase(); int = db.delete(contacttable, "_id = '"+num+"'", null); db.close(); log.v("end", "delete query ran"); return i; } public cursor getallcontacts(){ sqlitedatabase db = this.getreadabledatabase(); cursor mcursor = db.rawquery("select * contacts", null); db.close(); return mcursor; } public int getnumberofcontacts(){ sqlitedatabase db = this.getreadabledatabase(); int numrows = (int) databaseutils.querynumentries(db, "contacts"); cursor c = db.rawquery("select * contacts",null); log.v("number of records"," :: "+c.getcount()); c.close(); return numrows; } public cursor getcontact(string name){ sqlitedatabase db = this.getreadabledatabase(); cursor ret = db.query(contacttable, null, "name="+name, null, null, null, null); if(ret!=null)ret.movetofirst(); db.close(); return ret; } }
in helper class mention :
public void deletedatabase(string search, string status,string col) { cursor c = null; int id = 0; boolean statu = false; string[] columns = new string[] { "id", "name", "address", "department" }; c = db.query(student_table, columns, "name=? or address=? or department=?", new string[] { search, search, search }, null, null, "id desc"); if (c.movetofirst()){ { if (c.getcolumncount() == 4) { string[] str = new string[3]; str[0] = c.getstring(1); str[1] = c.getstring(2); str[2] = c.getstring(3); id = c.getint(0); statu = true; } } while (c.movetonext()); } if (c != null && !c.isclosed()) { c.close(); } if (statu) { if (status.equals("update")) { updatedata(col, search,id); } if (status.equals("delete")) { if (id != 0) { deletedata(id); } } } } public void deletedata(int id) { db.delete(student_table, "id=?", new string[] { string.valueof(id) }); }
then in acitivity :
string searc=null; if(!nameet.gettext().tostring().equals("")){ searc=nameet.gettext().tostring(); sqlhelper.deletedatabase(searc,"delete",""); data=sqlhelper.selectalldatabase(); lv.setadapter(new mycustombaseadapter(this, data)); }else if(!addresset.gettext().tostring().equals("")){ searc=addresset.gettext().tostring(); sqlhelper.deletedatabase(searc,"delete",""); data=sqlhelper.selectalldatabase(); lv.setadapter(new mycustombaseadapter(this, data)); } else if(!deptet.gettext().tostring().equals("")){ searc=deptet.gettext().tostring(); sqlhelper.deletedatabase(searc,"delete",""); data=sqlhelper.selectalldatabase(); lv.setadapter(new mycustombaseadapter(this, data)); } else{ toast.maketext(this, "please enter search keywork of 1 field", 10).show(); }
Comments
Post a Comment