SQLITE查询不返回任何结果

时间:2015-03-22 17:38:13

标签: android sqlite

我在LoginAdapter类中有这个查询:

public Cursor FetchRow (String username){

  return db.query("LOGIN",new String[]{"ID","USERNAME","PASSWORD","COURSE","EMAIL","STUDENT_NAME"},"USERNAME" + "='" + username + "'",null,null,null,null);

}

当我从另一个活动中调用它时,模拟器会打开活动,但没有结果:

 String username = bundle.getString("username");

    loginDataBaseAdapter.FetchRow(username);

2 个答案:

答案 0 :(得分:1)

添加如下所示的Toast消息:

String username = bundle.getString("username");

    String result = loginDataBaseAdapter.FetchRow(username);

Toast.makeText(this, "The value of result is " +result, Toast.Length_Long).show;

在您的数据库消息中,您可以重写查询,如下所示:

    public String FetchRow (String username){
    String result = null;

      Cursor cursor = db.query("LOGIN",new String[]{"ID","USERNAME","PASSWORD","COURSE","EMAIL","STUDENT_NAME"},"USERNAME =" '" + username + "'",null,null,null,null);
      if (cursor != null) {
            cursor.moveToFirst();

   result  = cursor.getString(cursor.getColumnIndex("Name_GIven_For_UserName_column");
    }
    return result;


    }

如果值正确传递,那么您将得到答案。如果您有任何错误,请告诉我。在String username activity of activity之后添加日志消息:

Log.d("Test", "The value of UserName is " +username);

这有助于您检查用户名值是否正确。 执行此操作后,如果每件事情都正确,您将在活动中收到一条Toast消息。

如果您希望在textView中查看结果,则需要在xml中定义textView并将其链接到活动,如下所示:

TextView textView = (TextView) R.id.findViewById(R.id.textView_name);
textView.setText(result);

如果这样做,结果变量的值将显示在textView区域中。

答案 1 :(得分:0)

尝试以下修改后的代码版本。您需要在任何等号后添加问号,以便让查询函数知道要绑定的内容。

return db.query("LOGIN",new String[]{"ID","USERNAME","PASSWORD","COURSE","EMAIL","STUDENT_NAME"},"USERNAME =?, new String[]{username},null,null,null,null);