jeudi 31 mars 2016

Trying to store username and password in a database and having it check if the username and password match to log in

package com.example.chris.thegiver;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LogIn extends AppCompatActivity implements View.OnClickListener {
    private Button btnRegister2, btnLogIn;
    private EditText etUser, etPass;

    DatabaseHelper helper = new DatabaseHelper(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);

        btnRegister2 = (Button) findViewById(R.id.btnRegister2);
        btnLogIn= (Button) findViewById(R.id.btnLogIn);

        btnRegister2.setOnClickListener(this);

    }
**I think this is where I am having my problem. The app doesnt seem to access the database at all and in order to log in I need to check if the Username matches the password. If the password == the username then it will allow the user to log in. Unforunately that is not happening.**


    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btnRegister2){
            startActivity(new Intent(LogIn.this, RegisterActivity.class));
        }
        if(v.getId() == R.id.btnLogIn){
            EditText a = (EditText) findViewById(R.id.etUser);
            String str = a.getText().toString();
            EditText b = (EditText) findViewById(R.id.etPass);
            String pass = b.getText().toString();
            String password = helper.searchPass(str);
            if(pass.equals(password)){
                Intent i = new Intent(LogIn.this,HomeActivity.class);
                i.putExtra("Username: ", str);
                startActivity(i);
            }
            else{
                Toast temp = Toast.makeText(LogIn.this, "Username and Password dont match!", Toast.LENGTH_SHORT);
                temp.show();
            }

        }
    }
}
**DATABASE CLASS**

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "contacts.db";
    private static final String TABLE_NAME = "contacts";
    private static final String COL_ID = "id";
    private static final String COL_NAME= "name";
    private static final String COL_EMAIL= "email";
    private static final String COL_USERNAME= "username";
    private static final String COL_PASSWORD= "password";
    SQLiteDatabase db;
    private static final String TABLE_CREATE = "create table contacts (id integer primary key not null, name text not null," +
            "email text not null, username text not null, password text not null)";



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

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE);
        this.db = db;

    }

This method give functionality to the database and insert a users information whenever a new user object is created

    public void insertContact(Contact c){
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        // * means all values
        String query = "select * from contacts";
        Cursor cursor = db.rawQuery(query,null);
        int count = cursor.getCount();

        values.put(COL_ID, count);
        values.put(COL_NAME,c.getName());
        values.put(COL_EMAIL, c.getEmail());
        values.put(COL_USERNAME, c.getUsername());
        values.put(COL_PASSWORD, c.getPassword());

        **// this will insert the contact object into the database**
        db.insert(TABLE_NAME, null, values);
        db.close();
    }
**This method is where everything may be going wrong this searches the database for the username and checks if the username is equal to the password. If it isnt then b will be printed to the screen**

    public String searchPass(String uname){
        db = this.getReadableDatabase();
        String query = "select uname, pass from" + TABLE_NAME;
        Cursor cursor = db.rawQuery(query, null);
        String a, b;
        b = "not found";
        if(cursor.moveToFirst()){
            do{
                //usernam
                a = cursor.getString(0);

                if(a.equals(uname)){
                    b = cursor.getString(1);
                    break;
                }enter code here
            }
            while(cursor.moveToNext());
        }
        return b;
    }

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

    }
}
**REGISTRATION CLASS This sends the database all of the usernames information.**

package com.example.chris.thegiver;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    // calls the constructor of the DatabaseHelper class which will create the database and table
    DatabaseHelper registerDB = new DatabaseHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
    }

    public void onRegisterClick(View v) {
        if (v.getId() == R.id.btnReg) {

            EditText etName = (EditText) findViewById(R.id.etName);
            EditText etEmail = (EditText) findViewById(R.id.etEmail);
            EditText etUsername = (EditText) findViewById(R.id.etUsername);
            EditText etPassword = (EditText) findViewById(R.id.etPassword);
            EditText etConfirm = (EditText) findViewById(R.id.etConfirm);


            String name = etName.getText().toString();
            String email = etEmail.getText().toString();
            String user = etUsername.getText().toString();
            String password = etPassword.getText().toString();
            String confirm = etConfirm.getText().toString();

****If password equals confirm then store everything in the java sqlite database created above**.

If password and confirm password do not match then a toast message will appear saying passwords dont match.** if (password.equalsIgnoreCase(confirm)) { Contact c = new Contact(); // Uses the contact class created earlier c.setName(name); c.setEmail(email); c.setUsername(user); c.setPassword(password);

                registerDB.insertContact(c);
                startActivity(new Intent(RegisterActivity.this,LogIn.class));
            }
            else {
                // Pop up message
                Toast pass = Toast.makeText(RegisterActivity.this, "Passwords Dont Match!", Toast.LENGTH_SHORT);
                pass.show();
            }

        }
    }

Aucun commentaire:

Enregistrer un commentaire