我需要获取用户名和密码...我正在我的应用程序中进行密码恢复,所以这是我的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();
}
当用户输入电子邮件地址时,必须首先检查是否存在并使用我的活动代码返回密码和用户名以发送电子邮件:
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
答案 0 :(得分:3)
我对您的代码感到困惑,但是当您想要从Cursor
获取数据时
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);
}
您必须使用getters
提供从数据库返回数据的Cursor
实例。
修改强>
我没有注意到你使用whereArgs所以当你使用WHERE
子句时,你必须将whereArgs特定为String[]
,其中包含将被替换而不是的数据?焦炭。
您可以使用关闭数据库:
@Override
protected void onDestroy() {
super.onDestroy();
if (yourDatabase != null) {
yourDatabase.close();
}
}
或者在打开数据库的每项工作之后,您应该close()
db。
答案 1 :(得分:1)
在
Cursor mCursor = db.rawQuery("SELECT Username, Passwords FROM " + USERS_TABLE + " WHERE
EmailNO=?",null);
您没有为EmailNO提供价值。将值替换为null
。
编辑:我认为您的代码应该是这样的:
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;
}
}
在此处编辑您的代码:
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();
}