Method To Excute Query And Return Results
App won't run - trying to execute query to print certain value Method: public Cursor trying(String vg){ String q='SELECT quantity FROM ' + TABLE_CONTACTS + ' WHERE name=' + vg
Solution 1:
At first. Since you are directly adding trying variables into statement, variable must be wrapped to single quotes or it's interpeted as column.
"SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name= '" + vg + "'";
And second "big" problem, look here:
text.setText((CharSequence) (wow));
Here you are trying to cast Cursor
to CharSequence
but it's not possible. If you want to retrieve data from Cursor
you have to use one from the getters methods of Cursor class in your case getString() method:
Stringquantity= wow.getString(0); // it returns your quantity from Cursor
text.setText(quantity);
Now it should works.
Recommendation:
I suggest you to an usage of parametrized statements which actually use placeholders in your queries. They provide much more safer way for adding and retrieving data to / from database.
Let's rewrite your code:
Stringq="SELECT quantity FROM " + TABLE_CONTACTS + " WHERE name = ?";
SQLiteDatabasedb=this.getReadableDatabase();
Cursorcursor= db.rawQuery(q, newString[] {vg});
It works simply. Placeholder ?
will be replaced with your string value.
Post a Comment for "Method To Excute Query And Return Results"