Return the searched data from sqlite

烈酒焚心 提交于 2019-12-11 18:50:16

问题


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:


  1. add quotes to item: "select * from Articles where AS_name= '" + item + "'";

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!