android login sqlite with username and password

2.5k Views Asked by At

i'm working sqlite.i successfully create database and table.and also i can register some new user.and also i wrote login function with username only this is a part my code

public String getSinlgeEntry(String username) {
    Cursor cursor = db.query(usm_Users, null, " UserName=?",
            new String[] { username }, null, null, null);
    if (cursor.getCount() < 1) // UserName Not Exist
    {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String customername = cursor.getString(cursor
            .getColumnIndex("UserName"));


    cursor.close();
    return customername;
}





String username=namefild.getText().toString();


            String password=passwordfild.getText().toString();

            String storedusername=loginDataBaseAdapter.getSinlgeEntry(username);

            if(username.equals(storedusername))
            {
                Toast.makeText(MainActivity.this, "Congrats: Login Successfully", Toast.LENGTH_LONG).show();
                Intent ii=new Intent(MainActivity.this,Home.class);
                startActivity(ii);
            }
            else
                if(username.equals("")){
                    Toast.makeText(MainActivity.this, "Please Enter Your Password", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(MainActivity.this, "Password Incorrect", Toast.LENGTH_LONG).show();
                }
        }

i can check login only username.how i can write two parameters function witch would can check username and password together?

public String getSinlgeEntry(String username,String password) {
    Cursor cursor = db.query(usm_Users, null, " UserName=?",
            new String[] { username }, null, null, null);
    if (cursor.getCount() < 1) // UserName Not Exist
    {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String customername = cursor.getString(cursor
            .getColumnIndex("UserName"));


    cursor.close();
    return customername;
}

i must change something my Cursor .if anyone knows solution please help me

2

There are 2 best solutions below

4
On

use double ? and send two parameter in array.

like:

Cursor cursor = db.query(usm_Users, null, " UserName=? and Password=?",
            new String[] { username,password }, null, null, null);
2
On

You can use the below function, this is more generic

public Boolean check_User_Pin_Number(String username, String password)
{
    SQLiteDatabase sql_Db = this.getReadableDatabase();

    // Setting up the cursor which points to the desired table
    Cursor cursor = sql_Db.rawQuery("SELECT * FROM " + usm_Users, null);

    Boolean records_Exist = false;

    // Checking if the table has values other than the header using the cursor      
    if(cursor != null && cursor.getCount() > 0)
    {
        // Moving the cursor to the first row in the table
        cursor.moveToFirst();

        do
        {   
            // Checking if the user name provided by the user exists in the database
            if(cursor.getString(cursor.getColumnIndex(NAME)).equals(username))
            {
                if(password.equals(cursor.getColumnIndex(PASSWORD))
                {
                      records_Exist = true;
                      break;
                }
            }

       }while(cursor.moveToNext()); // Moves to the next row
   };

    // Closing the cursor
    cursor.close();

    // Closing the database
    sql_Db.close();

   return records_Exist;
}