tags:

views:

46

answers:

1

Hello android programmers!

I've completed my login page. My problem now is I can only log in using the first account created. I found out that I have not complete my login page. The missing item is the while..loop code so that the application will check the other existing user instead of only looking for a match for the first user.

if(username.equals(c.getString(1)))
            {
                if(password.equals(c.getString(2)))
                {
                    Context context = getApplicationContext();
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "LOGIN SUCCESS", duration);
                    toast.show();

                    Intent intent=new Intent(Login.this,Test.class);
                    startActivity(intent);

                }

I do know that the code is something like while (c.moveToNext()) but I do not know how to apply it on my if..else statement.

I hope you can help me out on this simple task. Thank you!

A: 

Instead of looping through all users to find the right one you should load the user associated with the given username and match the password against his.

It makes no sense to match any other user than the one the user of your application has entered.

EDIT: Based on your comment it could be like this:

//make sure the user input is tested against SQL-injections etc.
Cursor c = db.rawQuery("SELECT username FROM Users WHERE username='?' AND password='?'", 
              new String[] {username, password}); 

       if(c.moveToFirst()) { 
          // at least one row was returned, this should signal success
          Context context = getApplicationContext();
          int duration = Toast.LENGTH_LONG;
          Toast toast = Toast.makeText(context, "LOGIN SUCCESS", duration);
          toast.show();

          Intent intent=new Intent(Login.this,Test.class);
          startActivity(intent);

       } else { 
          // authentication failed, handle error (message etc) 
       }
stefan
Hi stefanI do not know what you mean.The only way and basic thing that I can think of t=is to do while loop to check the existing user in the database. How can I load the user associated with the given username? I must use while loop to do that am I right then I can match the password.
Dayne
Instead of quering all users from the database you should use the given username to make a query.I don't have the code right now but in SQL it would be sth like:SELECT username,password from users where username = "enteredusername";If your resultset is empty, the username doesn't exist if the resultset is filled you can match the password against the one in the result.HTH
stefan
is it something like this? "Cursor c = db.rawQuery("SELECT username FROM Users WHERE username='?' AND password='?'", new String[] {username, password});if(c.moveToFirst()) { // at least one row was returned, this should signal success} else { // authentication failed}"I do not know how to implement this. Instead, I use the code i posted above.
Dayne
I can't make it work..
Dayne
I do not know what does the rawQuery do. I didnt declare. secondly, the two strings username an password can't be resolved.
Dayne