搜索数据库并在android中显示结果(它不起作用)

时间:2017-07-01 19:09:29

标签: android database

这是我在Database类中的功能,当点击搜索按钮时它正在关闭我的应用程序:

public String search(String r) {
 String result = "";
 SQLiteDatabase db = this.getWritableDatabase();
 String query = "SELECT _Name,_City,_Job  FROM " + TABLE_Person + " WHERE _Job LIKE '" + r + "'";
 Cursor c = db.rawQuery(query, null);
 c.moveToFirst();

 while (!c.isAfterLast()) {
  if (c.getString(c.getColumnIndex("_Job")) == r) {
   result += c.getString(c.getColumnIndex("_Name"));
   result += c.getString(c.getColumnIndex("_City"));
   result += c.getString(c.getColumnIndex("_Job"));
  }
  c.moveToNext();
 }

 db.close();

 return result;
}

1 个答案:

答案 0 :(得分:1)

试试这个 -

使用equals()方法检查两个字符串是否具有相同的值。

  while(c.moveToNext()){

    if(r.equals(c.getString(c.getColumnIndex("_Job"))){
        result += c.getString(c.getColumnIndex("_Name"));
        result += c.getString(c.getColumnIndex("_City"));
        result += c.getString(c.getColumnIndex("_Job"));

    }