如何从数据库中检索密码值

时间:2015-02-11 00:48:33

标签: android sqlite

在这个userdetails.java中,我创建了一个名为userdetails并插入值

的表
public class userdetails extends Activity implements OnClickListener {

@Override

public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userdetails);

Button dbsave = (Button)findViewById(R.id.save);
    dbsave.setOnClickListener(this);

SQLiteDatabase db;
db = openOrCreateDatabase( "Student.db" , SQLiteDatabase.CREATE_IF_NECESSARY        , null          );
try {
    final String UserDetails = "CREATE TABLE IF NOT EXISTS UserDetails ("
            + "NAME,"
            + "MOB_NO,"
            + "Q_NO,"
            + "Q_ANS,"
            + "MAIL_ID,"
            + "PASSWD);";
    db.execSQL(UserDetails);

    Toast.makeText(userdetails.this, "table created ", Toast.LENGTH_LONG).show();



}
catch (Exception e) {
    Toast.makeText(userdetails.this, "ERROR "+e.toString(),          Toast.LENGTH_LONG).show();  
    }
   }
    public void onClick(View view){

    SQLiteDatabase db;

     if(view.getId()==R.id.save)
     {

         EditText e1 = (EditText) findViewById(R.id.editText1);
         String s1=e1.getText().toString();

         EditText e2 = (EditText) findViewById(R.id.editText2);


         EditText e3 = (EditText) findViewById(R.id.editText3);
         String s3=e3.getText().toString();


         EditText e4 = (EditText) findViewById(R.id.editText4);
         String s4=e4.getText().toString();


         EditText e5 = (EditText) findViewById(R.id.editText5);
         String s5=e5.getText().toString();


         EditText e6 = (EditText) findViewById(R.id.editText6);
         String s6 =e6.getText().toString();

       // Toast.makeText(Databasedb.this, "table created ", Toast.LENGTH_LONG).show();

        String sql =
            "INSERT or replace INTO UserDetails (" +
            "Name, " +
            "Mob_no, " +
            "Q_no, " +
            "Q_ans,"+
            "Mail_id,"+
            "Passwd) " +
            "VALUES('"+s1+"','"+e2+"','"+s6+"','"+s3+"','"+s4+"','"+s5+"')" ;       

        db = openOrCreateDatabase( "Student.db" , SQLiteDatabase.CREATE_IF_NECESSARY        , null          );

        db.execSQL(sql);




         AlertDialog alertDialog = new AlertDialog.Builder(userdetails.this).create();
          //  alertDialog.setTitle("Alert Dialog");

            alertDialog.setMessage("uservalues are successfully added/updated to the database");

            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

            Intent intent = new Intent(userdetails.this, Login.class);
            userdetails.this.startActivity(intent);

               }
        });
            alertDialog.show();     
     }}

}

这是我需要访问userdetails表的login.java文件

 public class Login extends Activity implements OnClickListener {


 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    Button login = (Button) findViewById(R.id.login);
    login.setOnClickListener(this);

    Button exit = (Button) findViewById(R.id.exit);
    exit.setOnClickListener(this);

    TextView txt = (TextView) findViewById(R.id.sub);
    txt.setOnClickListener(this);

    TextView forgot = (TextView) findViewById(R.id.code);
    forgot.setOnClickListener(this);

    }

@Override

public void onClick(View view) {

         if(view.getId() == R.id.login) 
         {   

      EditText p1 = (EditText)findViewById(R.id.inputpasswd);    
       String s = p1.getText().toString(); 

       Intent intent1= new Intent(this,homepage.class);
       startActivity(intent1);

         }


         else 
             if(view.getId()==R.id.sub)
         {
         Intent intent2 = new Intent(this, userdetails.class);
        startActivity(intent2); 
         }
        else 
         {
             Intent intent3 = new Intent(this, Forgotpassword.class);
          startActivity(intent3);   

        }


            }

}

请任何人帮我从数据库中检索值(来自login.java)和comapare来自用户输入值的值

提前完成......

2 个答案:

答案 0 :(得分:0)

你没有。

您执行一般表格的查询

SELECT COUNT(*) FROM USERS WHERE USERNAME = ? AND PASSWORD = ?

并查看返回的计数是1还是0。

  • 这就是进行比较的数据库
  • 此外,您不必像在接收和比较自己时那样处理代码中密码哈希的详细信息。
  • 此外,数据移动也少得多。
  • 此过程也意味着,如果发生故障,您的用户名或密码不正确,无法向用户泄露,这符合良好的安全设计惯例。

答案 1 :(得分:-1)

检查用户是否输入了正确的凭据。请使用以下代码。

String username="";
String password=""; 
Cursor c = mydb.rawQuery("select * from Users where Username="+username+ " and Password="+password);
if(c.getCount()>0){
    Toast.makeText(getApplicationContext(), "Login Succesful",Toast.LENGTH_LONG).show();
}
else{
    Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_LONG).show();
}