问题
I am trying to search an item in sqlite by using String, & trying to return description contained in that row. items are stored in the table named Articles with column name AS_name, & the description column name is Desc_art which I am unable to return.
This is my code:
public String searchData(String item) {
String strvalue = null;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cur = null;
String strquery = "select * from Articles where AS_name = "+item;
cur = db.rawQuery(strquery,null);
if(cur! = null&&cur.moveToFirst()) {
do {
strvalue = cur.getString(0);
} while(cur.moveToNext());
}
return strvalue;
}
This is the error which I am getting:
I am confused please help..
回答1:
In your query item is string,So change
String strquery="select * from Articles where AS_name= "+item;
to
String strquery="select * from Articles where AS_name='" + item + "'";
Or this
Cursor cur=null;
cur = db.query("Articles", null, "AS_name" + "=?",
new String[] { item }, null, null, null, null);
instead of
Cursor cur=null;
String strquery="select * from Articles where AS_name= "+item;
cur=db.rawQuery(strquery,null);
And change
strvalue=cur.getString(0);
to
strvalue=cur.getString(cur.getColumnIndex("Desc_art"));
回答2:
add quotes to item:
"select * from Articles where AS_name= '" + item + "'";And try to uninstall and reinstall app. If you made changes to the tables you need to uninstall and do a clean install.
Thank you, the app is not crashing , but it is returning the indexing based on 1 , not the description contained in column named Desc_art, can you help me further
What column are you trying to return here in position 0 from the table?
strvalue=cur.getString(0);
来源:https://stackoverflow.com/questions/24611805/return-the-searched-data-from-sqlite