Search code examples
androidsqliteandroid-sqlite

How to create a login function to a specific user


This app can register 2 main users student and teacher. After registering the users, they can login. If the user is a teacher it will redirect to teacher activity and if it's a student it will redirect to a different activity in the register page. Student and teacher will selected by a checkbox.

this is the code I've tried so far :

 String user = username.getText().toString();
                String pass = password.getText().toString();

                cursor = db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_USER + " WHERE " + DatabaseHelper.NAME + "=? AND " + DatabaseHelper.PASSWORD + "=?", new String[]{user,pass});
                if(username.getText().toString().trim().length() == 0 ||
                        password.getText().toString().trim().length() == 0){
                    toast(MainActivity.this,"All fields are required");
                }
                else if (cursor != null){
                    if(cursor.getCount() > 0){
                        if(cursor.getString(cursor.getColumnIndex(TYPE)).equals("Teacher")){
                            view_teacher();
                            toast(MainActivity.this,"Welcome " + username.getText().toString().toUpperCase());
                        }
                        else{
                            view_student();
                            toast(MainActivity.this,"Welcome " + username.getText().toString().toUpperCase());
                        }
                    }
                    else {
                        toast(MainActivity.this,"Username or Password invalid");
                    }
                }
                else{
                    toast(MainActivity.this,"Error Login in");
                }
            }
        });

This is how the register function looks like

}else if (db.registeruser(username.getText().toString(), password.getText().toString(),teacher.isChecked() ? "Teacher" : (student.isChecked() ? "Student" : "N/A"))){
                    loginScreen();
                    toast(Register.this,"Successfully Registered Please Login");
                }

When i tried to execute the login function the app will crash


Solution

  • You did not post the error log, but I think the error you get is thrown because you do not call and check cursor.moveToFirst() before you try to access the row returned by rawQuery() so the cursor's position is before the 1st row.
    Also move the call to rawQuery() inside the else block:

    String user = username.getText().toString().trim();
    String pass = password.getText().toString().trim();
    
    if(user.length() == 0 || pass.length() == 0){
        toast(MainActivity.this,"All fields are required");
    } else {
        cursor = db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_USER + " WHERE " + DatabaseHelper.NAME + "=? AND " + DatabaseHelper.PASSWORD + "=?", new String[]{user,pass});
        if(cursor.moveToFirst()) {
            if(cursor.getString(cursor.getColumnIndex(TYPE)).equals("Teacher")){
                view_teacher();
            } else {
                view_student();
            }
            toast(MainActivity.this,"Welcome " + username.getText().toString().toUpperCase());
        } else {
            toast(MainActivity.this,"Username or Password invalid");
        }
    }