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

android, sqlite

Solution

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.

Problem

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 ```

Original source