Stevens Moloto
Stevens Moloto

Reputation: 53

Retrieving a Username and Password in SQLITE DB by EMAIL_ADDRESS in ANDROID

I need to get a Username and Password...I'm doing Password Recovery in my Application so here is my code for DB:

public String getEmailAddr() throws SQLException {
    Cursor mCursor = db.rawQuery(
        "SELECT Username, Passwords FROM " + 
        USERS_TABLE + " WHERE EmailNO=?",null);

    if (mCursor != null) {           

        if(mCursor.getCount() > 0 {
            //return obj1.getpassword();
        }
    }

    //return false;
    return obj1.getpassword();
}

And when the User enters an Email Address it must first check for existence and return Password and the username with using my Activity code for sending Email:

public void onClick(View v) {
    String EmailAddress = txtEmailAddress.getText().toString();
    try {
        if(EmailAddress.length() > 0) {
            DBAdapter  dbUser = new DBAdapter (RecoverPassword.this);
            dbUser.open();

            if(dbUser.getEmailAddress(EmailAddress)) {
                Toast.makeText(RecoverPassword.this,
                    "Email Successfully ", 
                    Toast.LENGTH_LONG).show();
                    Toast.makeText String to = 
                        txtEmailAddress.getText().toString();
                    //String subject = EmailAddress.getText().toString();
                    // String message = EmailAddress.getText().toString();
                    Intent email = new Intent(Intent.ACTION_SEND);
                    email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
                    email.putExtra(Intent.EXTRA_SUBJECT, "Password Recovery");
                    email.putExtra(Intent.EXTRA_TEXT, dbUser.getEmailAddr());
                    //need this to prompts email client only 
                    email.setType("message/rfc822");
                    startActivity(Intent.createChooser(email, "gmail :"));                 
                }

                else {
                    Toast.makeText(RecoverPassword.this,
                        "Invalid Email", Toast.LENGTH_LONG).show();
                    txtEmailAddress.setText("");
                }

                dbUser.close();
            }
        }

        catch(Exception e) {
            Toast.makeText(RecoverPassword.
            this,e.getMessage(), Toast.LENGTH_LONG).show();
        }

        Please help me to check what is wrong...but there is no error but it cant return 
        username and password to the email sent

Upvotes: 4

Views: 10224

Answers (2)

Nishant
Nishant

Reputation: 32232

In

Cursor mCursor = db.rawQuery("SELECT Username, Passwords FROM " + USERS_TABLE + " WHERE
EmailNO=?",null);

You are not providing the value for EmailNO. Put the value in place of null.

Edit: I think your code should be like this:

public boolean getEmailAddress(String emailNo) 
    throws SQLException { //pass the EmailNo which you want in where clause
    Cursor mCursor = db.rawQuery(
        "SELECT Username, Passwords FROM sddashsd WHERE 
        EmailNO='"+emailNo+"'",null);

        if (mCursor != null) {         
            if(mCursor.getCount() > 0)
                return true;
        else 
            return false;
        }
    }

And edit your code here:

public void onClick(View v) {
    String EmailAddress = 
        txtEmailAddress.getText().toString();

    try {
        if(EmailAddress.length() > 0) {
            DBAdapter  dbUser = new DBAdapter (RecoverPassword.this);
            dbUser.open();

            if(dbUser.getEmailAddress(EmailAddress)) {
                Toast.makeText(RecoverPassword.this,
                    "Email Successfully ", Toast.LENGTH_LONG).show();
                String to = txtEmailAddress.getText().toString();
                //String subject = EmailAddress.getText().toString();
                // String message = EmailAddress.getText().toString();
                Intent email = new Intent(Intent.ACTION_SEND);
                email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
                email.putExtra(Intent.EXTRA_SUBJECT, "Password Recovery");
                email.putExtra(Intent.EXTRA_TEXT, dbUser.getEmailAddr());
                //need this to prompts email client only
                email.setType("message/rfc822");
                startActivity(Intent.createChooser(email, "gmail"));
            }

            else {
                Toast.makeText(RecoverPassword.this,"Invalid Email", 
                    Toast.LENGTH_LONG).show();                             
                    txtEmailAddress.setText("");
            }

        }

    }

    catch(Exception e) {
        Toast.makeText(RecoverPassword.this,
            e.getMessage(), Toast.LENGTH_LONG).show();
    }

    finally {
        dbUser.close();  
    }

Upvotes: 1

Simon Dorociak
Simon Dorociak

Reputation: 33515

I'm confused from your code but when you want to get data from Cursor so

String[] whereArgs = new String[<data>];
    Cursor mCursor = db.rawQuery("SELECT Username, Passwords FROM " + USERS_TABLE + " WHERE EmailNO = ?", whereArgs);
    mCursor.moToFirst();

    if(mCursor.getCount() > 0} {
       String userName = mCursor.getString(0);
       String password = mCursor.getString(1);

    }

You must use getters that offers Cursor instance which return data from database.

EDIT:

I have not noticed that you using whereArgs so when you use WHERE clause, you have to specific whereArgs as String[] that contains data which will be replaced instead of ? char.

Fo close database you can use:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (yourDatabase != null) {
        yourDatabase.close();
    }
}

Or after each work that opens your db you should close() db.

Upvotes: 3

Related Questions