2014-04-14 133 views
0

我正在登录页面。在以下代码中,userEnteredpswdEnteredEditText收集用户输入。我在我的数据库中通过userEntered作为方法getSingleEntry中的参数。我想用这种方法检查数据库中的用户名并返回相应的密码。方法getSingleEntry将如何查看?Android登录错误

 boolean diditwork = true; 
     try { 
     String userEntered= user.getText().toString(); 
     String pswdEntered = pswd.getText().toString(); 
    String storedPassword=LoginDB.getSingleEntry(userEntered); 

     Log.d("blahhhhhhhhh", storedPassword); 

     if(pswdEntered.equals(storedPassword)) 
     { 

       Dialog id=new Dialog(Login.this); 
       id.setTitle("LOGIN"); 
       TextView tyv=new TextView(Login.this); 
       tyv.setText("Registration Successful!"); 
       id.setContentView(tyv); 

     } 
     else 
     { 

       Dialog id=new Dialog(Login.this); 
       id.setTitle("LOGIN"); 
       TextView tyv=new TextView(Login.this); 
       tyv.setText("Incorrect username or password!"); 
       id.setContentView(tyv); 
       id.show(); 

     } 

     } /* catch(SQLException ee) 
     { 
      diditwork=false; 
      String error=ee.toString(); 
      Dialog id=new Dialog(Login.this); 
      id.setTitle("SQLSQLSQLSQL"); 
      TextView tyv=new TextView(Login.this); 
      tyv.setText(error); 
      id.setContentView(tyv); 
      id.show(); 
     }*/ 
     catch(Exception e) 
     { 
      diditwork=false; 
      String error=e.toString(); 
      Dialog id=new Dialog(Login.this); 
      id.setTitle("LOGIN"); 
      TextView tyv=new TextView(Login.this); 
      tyv.setText("Retype password"+error); 
      id.setContentView(tyv); 
      id.show(); 
     } 

这些是数据库定义。

    public static final String KEY_ROWID="_id"; 
        public static final String KEY_NAME= "name"; 
        public static final String KEY_USERNAME="username"; 
        public static final String KEY_PASSWORD="password"; 
       public static final String KEY_MOBILE= "mobile"; 
       public static final String KEY_EMAIL= "email"; 

        private static final String DATABASE_NAME="LoginDB"; 
        private static final String DATABASE_TABLE="DoctorTable"; 
        private static final int DATABASE_VERSION=2; 
+0

发表您的'getSingleEntry()' –

回答

0
public static String getSingleEntry(String userName) 
{ 
    Log.d("db", userName); 
    Cursor c; 
    String[] columns= new String[]{KEY_ROWID,KEY_NAME,KEY_USERNAME,KEY_PASSWORD,KEY_MOBILE,KEY_EMAIL}; 
// String[] columns= new String[]{KEY_PASSWORD}; 

    c = db.rawQuery("SELECT password FROM " + DATABASE_TABLE + " WHERE username=?", new String[]{userName}); 
// c=db.query("LoginDB", columns,KEY_USERNAME + "=" + userName ,null, null, null, null); 
    // c = db.rawQuery("SELECT password FROM " + DATABASE_NAME + " WHERE username ='"+userName+"'", null);  
    // c= db.rawQuery("SELECT "+ KEY_PASSWORD + "FROM" + DATABASE_NAME + "WHERE" + KEY_USERNAME + "= ?", new String[] {userName}); 
    // c= db.rawQuery("SELECT password FROM 'LoginDB' WHERE username ="+userName, null); 


    if(c!=null) 
    { 
    c.moveToFirst(); 
    String password= c.getString(3); 
    Log.d("ruuuu",password); 
    c.close(); 
    return password;  
    } 
    return null; 


}