android - my Cursor always displaying true -
i have constructed piece of code search whether string value present within sqlite table. here have:
public boolean findvalue(string item) { sqlitedatabase db = this.getwritabledatabase(); cursor checkifexist = db.rawquery("select * "+ table_item + " " + "itemname= " + "'" + item+ "'", null); checkifexist.movetofirst(); if (checkifexist.movetofirst()){ return true;} else{ return false;} } code inside main:
store.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { boolean s= mydatabase.findvalue(item.gettext().tostring()); if (s= true) { toast.maketext(createacc.this, "item has been created", toast.length_long).show(); }else if (exists = false) { toast.maketext(getbasecontext(), " " + title.gettext().tostring() + " exists", toast.length_long).show(); } } } ); it gives me true though item string value present inside table. ideas problem might be?
1- if (s = true) not correct, should if (s==true)
2- gives me true though item string value present inside table current code supposed this, return true if value found, condition check s should !, i.e if (s != true)
3- particular case, getcount() may suite better.
public boolean findvalue(string item) { sqlitedatabase db = this.getwritabledatabase(); cursor checkifexist = db.rawquery("select * "+ table_item + " " + "itemname= " + "'" + item+ "'", null); return checkifexist.getcount()>0; } extra:
better make use of parameters option in rawquery()
cursor checkifexist = db.rawquery("select * "+ table_item + " itemname= ?", new string[]{item});
Comments
Post a Comment