Android SQLite没有结果

时间:2012-09-12 09:07:33

标签: java android sqlite

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

    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();
    }
    }

2 个答案:

答案 0 :(得分:0)

将支票更改为:

if(loginQueryCheck != null && !loginQueryCheck.equals("") && loginQueryCheck.length() > 0)

答案 1 :(得分: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);