SQLITE ANDROID STUDIO. How can I tell if the user is admin or user?

The super admin can only enter username and password as well as user and admin. The super admin can crude accounts, while admin can also do the same except delete. and the user can only login. 

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class myDbAdapter {

    myDbHelper myhelper;
    public myDbAdapter(Context context) {
        myhelper = new myDbHelper(context);
    }

    static class myDbHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "LoginSystem_db";
        private static final String TABLE_NAME = "LoginSystem_tb";
        private static final int DATABASE_Version = 1;
        private static final String USERID = "Id";
        private static final String USERNAME = "Username";
        private static final String USERPASSWORD = "Password";
        private static final String ACCOUNTTYPE = "User_type";
        private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
    " (" + USERID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + USERNAME + " VARCHAR(255) UNIQUE ," + USERPASSWORD + " VARCHAR(225)," + ACCOUNTTYPE + " VARCHAR(225));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private Context context;

        public myDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_Version);
            this.context = context;
        }

        public void onCreate(SQLiteDatabase db) {
            try {

                db.execSQL(CREATE_TABLE);
            } catch (Exception e) {
                Message.message(context, "" + e);
            }
        }

  

  @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }


    public Boolean insertData(String name, String pass, String type) {
        SQLiteDatabase db = myhelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(myDbHelper.USERNAME, name);
        contentValues.put(myDbHelper.USERPASSWORD, pass);
        contentValues.put(myDbHelper.ACCOUNTTYPE, type);
        long dData = db.insert(myDbHelper.TABLE_NAME, null, contentValues);
        if (dData == -1)
            return false;
        else
            return true;



    }

    public String getData() {

        SQLiteDatabase db = myhelper.getWritableDatabase();
        String[] columns = {
            myDbHelper.USERID,
            myDbHelper.USERNAME,
            myDbHelper.USERPASSWORD,
            myDbHelper.ACCOUNTTYPE
        };
        Cursor cursor = db.query(myDbHelper.TABLE_NAME, columns, null, null, null, null, null);
        StringBuffer buffer = new StringBuffer();
        while (cursor.moveToNext()) {
            @SuppressLint("Range") int cid = cursor.getInt(cursor.getColumnIndex(myDbHelper.USERID));
            @SuppressLint("Range") String username = cursor.getString(cursor.getColumnIndex(myDbHelper.USERNAME));
            @SuppressLint("Range") String password = cursor.getString(cursor.getColumnIndex(myDbHelper.USERPASSWORD));
            @SuppressLint("Range") String usertype = cursor.getString(cursor.getColumnIndex(myDbHelper.ACCOUNTTYPE));
            buffer.append(cid + "   " + username + "   " + password + "  " + usertype + " \n");
        }
        return buffer.toString();
    }
    public Boolean checkusername(String username) {
        SQLiteDatabase MyDB = myhelper.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from LoginSystem_tb where USERNAME = ?", new String[] {
            username
        });
        if (cursor.getCount() > 0)
            return true;
        else
            return false;
    }
    public Boolean checkusernamepassword(String username, String password) {
        SQLiteDatabase MyDB = myhelper.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from LoginSystem_tb where USERNAME = ? and PASSWORD = ?", new String[] {
            username,
            password
        });
        if (cursor.getCount() > 0)
            return true;
        else
            return false;
    }
    public Boolean checkuserType() {
        SQLiteDatabase MyDB = myhelper.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from LoginSystem_tb where USERTYPE= 'Admin'", new String[] {});
        if (cursor.getCount() > 0)
            return true;
        else
            return false;
    }

the super admin, admin and user. Super admin can create username, password and usertype(Radiobutton) for the user and admin. What I want to happen is when I enter login username and password the system will automatically know that this user is admin or user. How can I get the data of radiobutton that is already inserted in the sql db

}

How can I get the data which is the user type(radio button) that is already stored in the sql db. Then put it in if conditions. Idk if my questions are right but What I want is differentiate user and admin. Thank you!
output

MainActivity.java

 buttonLogin.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
     String usernameLog = usernameOutside.getText().toString();
     String passwordLog = passwordOutside.getText().toString();

     if ((usernameLog.equals("admin")) && (passwordLog.equals("admin"))) {
         Intent intent = new Intent(MainActivity.this,
             MainActivity2.class);
         startActivity(intent);
     } else {

         Boolean checkUserPass = mynewdbAdapter.checkusernamepassword(usernameLog, passwordLog);

         if (checkUserPass) { //check if existing
             if ( //check if this is user or admin) {
                 Toast.makeText(MainActivity.this, "Login Successful.", Toast.LENGTH_SHORT).show();
             }
             else {
                 Toast.makeText(MainActivity.this, "Invalid.", Toast.LENGTH_SHORT).show();
             }

         } else {
                 Toast.makeText(MainActivity.this, "Account doesn't exist.", Toast.LENGTH_SHORT).show();
             }
         }
     }
 });


 How can I get the data which is the user type(radio button) that is already stored in the sql db. Then put it in if conditions. Idk if my questions are right but What I want is differentiate user and admin. Thank you!

output

output inside

>Solution :

I think you need to fix the getUserType() function

You can:

  1. Change the return type to list containing all admin users. You can then compare the username of the user with the list of admins
  2. The better way to do it would be to keep the function boolean, but to pass the user id, check if the account type is ADMIN, then return True, or False otherwise. You can then call that function from the main activity similar to calling checkUsernamePassword function.

I hope this helps you

To put it into perspective, the function could look like this:

public Boolean checkuserType(string input_name) {

    SQLiteDatabase MyDB = myhelper.getWritableDatabase();
    Cursor cursor = MyDB.rawQuery("Select USERTYPE from LoginSystem_tb where USERNAME= input_name", new String[] {});
    if (cursor.fetchOne().equals("ADMIN"))
        return true;

    else
        return false;
}

Leave a Reply