如何从sqlite查询返回字符串值?

时间:2018-11-21 08:55:53

标签: android sqlite

这是我的查询。我想获取字符串值,并将单个值返回到我叫getSelectedMerchantCode函数的位置的活动类。我的代码无法正常工作。返回的“错误”

public String getSelectedMerchantCode(String merchantname){
            String selection = "Error";
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor c = db.rawQuery("SELECT merchantCode FROM " + "merchantList"+ " WHERE " + "merchantName" + "='" + merchantname + "'", null);
            if(c.getCount() == 1){
                c.moveToFirst();
                selection = c.getString(c.getColumnIndex("merchantCode"));
                return selection;
            }
            c.close();
            db.close();

            return null;
        }

1 个答案:

答案 0 :(得分:0)

尝试这种方式。

public List<String> getMyItems(String name) {
    List<String> stringList = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    String selectQuery = "SELECT Item_Name FROM " + USER_TABLE_NAME + " WHERE Item_Name= " + name;
    Cursor c = db.rawQuery(selectQuery, null);
    if (c != null) {
        c.moveToFirst();
        while (c.isAfterLast() == false) {
           String name = (c.getString(c.getColumnIndex("Item_Name")));

            stringList.add(name);
            c.moveToNext();
        }
    }
    return stringList;
}