Skip to content Skip to sidebar Skip to footer

Sql Lite Android App Login

I am working on a game at the moment and all I want is my login screen to be able to check that the username and password I have already stored in my database is correct and when

Solution 1:

You can try below way.

Create class called DBAdapter.java and add following code.

publicclassDBAdapter{
    privatestaticfinalString DATABASE_NAME         = "Your database name"; 
    privatestaticfinalString DATABASE_CREATE_USERS = "create table TABLE_NAME (
    UserID integer primary key autoincrement, " + "Username text, Password text);" ; 

    privatestaticfinalString DATABASE_SELECT_USERS = "users";
    publicstaticfinalString USER_ID       = "UserID";
    publicstaticfinalString USER_NAME     = "Username";
    publicstaticfinalString USER_PASSWORD = "Password ";

    privatefinal Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;  

    DBAdapter(Contex ctx) {
        this.context = ctx;
        DBHelper     = new DatabaseHelper(context);
    }

    privatestaticclassDatabaseHelperextendsSQLiteOpenHelper{
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        publicvoid onCreate(SQLiteDatabase db) {
            System.out.println("Creating table");
            db.execSQL(DATABASE_CREATE_USERS);
        }

        publicvoid onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

        }
    }   

    public DBAdapter open() throws SQLException {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    publicvoid close() {
        DBHelper.close();
    }

    public Cursor fetchUser(String username, String password) {  
        Cursor myCursor = db.query(DATABASE_SELECT_USERS,   
        newString[] { USER_ID, USER_NAME, USER_PASSWORD },   
                         USER_NAME + "='" + username + "' AND " +   
                         USER_PASSWORD + "='" + password + "'", null, null, null, null);  

        if (myCursor != null) {  
            myCursor.moveToFirst();  
        }  
        return myCursor;  
    }  

    publicvoid InsertData(String username, String password) {
        String sql = "INSERT INTO users (Username,Password) VALUES('"+username+"','"+password+"')";
        db.execSQL(sql);
    }
}

and then create your Java file

Login.java

publicclassLoginextendsActivity 
{

    publicvoidonCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        finalEditTexttxt_username= (EditText) findViewById(R.id.editText1);
        finalEditTexttxt_psw= (EditText) findViewById(R.id.editText2);

        Buttonbtn_signin= (Button) findViewById(R.id.button1);

        // Inner class to implement Button Listener when button is clicked.
        btn_signin.setOnClickListener(newOnClickListener() {
            publicvoidonClick(View v) {                   
                DBAdapterdb=newDBAdapter(getBaseContext());
                db.open();                              
                db.InsertData("Chao", "1234");              
                Log.v("LoginDetails", txt_username.getText().toString()+"../.."
                      +txt_psw.getText().toString());

                // Accessing user using the fetchUser method we created in DBAdapterCursorcur= db.fetchUser( txt_username.getText().toString(), 
                                           txt_psw.getText().toString());

                // Use this line if you want to see the number of users with these login details
                System.out.println("cur.getCount()   "+cur.getCount());

                if(cur.getCount()!=0) {
                    String usn=cur.getString(1);
                    if(usn.equals("Chao")) {   

                    }
                    else { 

                    }

                    System.out.println("Query succedded");
                    Toast.makeText(getBaseContext(), "Success! Valid User name and password", Toast.LENGTH_LONG).show();

                    db.close();
                }
                else
                    Toast.makeText(getBaseContext(), "Please enter Valid User name and password", Toast.LENGTH_LONG).show();
            } //Closes the onClick method
        }); //Closes the onClickListener
    }
}

Post a Comment for "Sql Lite Android App Login"