android - Method to excute query and return results -
app won't run - trying execute query print value
method:
public cursor trying(string vg){ string q="select quantity " + table_contacts + " name=" + vg; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(q,null); if (cursor != null) { cursor.movetofirst(); } return cursor; }
calling method main
cursor wow = db.trying("gold"); text = (textview) findviewbyid(r.id.textview13); text.settext((charsequence) (wow));
at first. since directly adding trying variables statement, variable must wrapped single quotes or it's interpeted column.
"select quantity " + table_contacts + " name= '" + vg + "'";
and second "big" problem, here:
text.settext((charsequence) (wow));
here trying cast cursor
charsequence
it's not possible. if want retrieve data cursor
have use 1 getters methods of cursor class in case getstring() method:
string quantity = wow.getstring(0); // returns quantity cursor text.settext(quantity);
now should works.
recommendation:
i suggest usage of parametrized statements use placeholders in queries. provide more safer way adding , retrieving data / database.
let's rewrite code:
string q = "select quantity " + table_contacts + " name = ?"; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(q, new string[] {vg});
it works simply. placeholder ?
replaced string value.
Comments
Post a Comment