1

我正在使用 databaseutils 在我的数据库上运行 SQL 查询。基本上只是一个登录,但是我想获取登录的人的用户 ID,所以我选择了它。如果有结果它可以正常工作,但是当没有结果时会摔倒。如果有办法设置 if is set 或类似的?

    if(String.valueOf(loginCount).equals("2")) {

    File dbfile = new File(Global.StaticDB + "/Users" ); 
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

    String loginQueryCheck=(String)DatabaseUtils.stringForQuery(db,"SELECT UserID from Users where Username like '" + txtUsername.getText().toString() + "' AND Password like '" + txtPassword.getText().toString() + "'",null);

    Log.e("Login Query:", loginQueryCheck);
    db.close();  

    if(loginQueryCheck.length() > 1) {
        Context context = getApplicationContext();
    CharSequence text = "Logged In";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    Intent ShowMainPage = new Intent(view.getContext(), MainPage.class);
    startActivityForResult(ShowMainPage, 0);
    } else {
        Context context = getApplicationContext();
    CharSequence text = "Login Failed";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    }
    }
4

2 回答 2

0

将检查更改为:

if(loginQueryCheck != null && !loginQueryCheck.equals("") && loginQueryCheck.length() > 0)
于 2012-09-12T09:15:15.637 回答
0

我认为您可以尝试计算 UserID 而不是在 SQL 语句中查询 UserID。

int loginQueryCheck=(int)DatabaseUtils.stringForQuery(db,
      "SELECT COUNT(DISTINCT UserID) FROM Users WHERE Username like '" +
      txtUsername.getText().toString() + "' AND Password like '" +
      txtPassword.getText().toString() + "'",null);
于 2012-09-12T09:19:35.560 回答