2011-03-05 43 views
0

我正在嘗試爲Android中的應用創建登錄屏幕。我已將有關用戶的信息存儲在數據庫的「用戶」表中。 我試圖匹配在登錄屏幕輸入的用戶名和密碼與數據庫中的值使用遊標對象,但它不起作用,導致應用程序崩潰。 有人可以請推薦或修改方法,如果可能的話用一些代碼片段。 請大感欣慰吧,謝謝。使用SQlite驗證Android中的登錄

下面是LoginForm類的代碼。 (它使用DBAdapter類連接到數據庫)

package com.androidbook.LoginForm;

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AutoCompleteTextView; 
import android.widget.Button; 
import android.widget.Toast; 

public class LoginForm extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final DBAdapter db = new DBAdapter(getBaseContext()); 
     final AutoCompleteTextView username = (AutoCompleteTextView)this.findViewById(R.id.AutoComUsernameLogin); 
     final AutoCompleteTextView password = (AutoCompleteTextView)this.findViewById(R.id.AutoComPasswordLogin); 

     Button Register = (Button) findViewById(R.id.ClicktoRegister); 
     Register.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), RegistrationForm.class); 
       startActivityForResult(myIntent, 0);  
      } 
     }); 
    //************************** LOG IN LOGIC******************************// 
     Button Login = (Button) findViewById(R.id.LoginButton); 
     Login.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       final String Username = username.getText().toString(); 
       final String Password= password.getText().toString(); 

       db.open(); 

       Cursor c = db.getAllTitles(); 

        while(c.moveToNext()) 
        { 
         String c1=c.getString(2); 
         String c2=c.getString(3); 

         if(c1 == Username) 
         { 
          if(c2 == Password) 
          { 
          Toast.makeText(LoginForm.this,     
          "You are succesfully logged in.", 
          Toast.LENGTH_LONG).show(); 

           Intent myIntent = new Intent(view.getContext(), Menu.class); 
           startActivityForResult(myIntent, 0); 
          } 
          else 
          { 
           Toast.makeText(LoginForm.this, "Incorrect password",Toast.LENGTH_LONG).show(); 
          } 
          Intent myIntent = new Intent(view.getContext(), LoginForm.class); 
          startActivityForResult(myIntent, 0); 
         } 

         else 
         Toast.makeText(LoginForm.this, "Incorrect",Toast.LENGTH_LONG).show(); 
        } 

       db.close(); 


      } 
     }); 
    } 
    } 
+0

你應該在處理用戶/通過之前插入到sql語句中。這就像java準備的語句來防止sql注入攻擊。 – 2011-10-04 07:56:24

回答

2

您可以在數據庫類中創建一個函數來返回int數。如果整數是1,那麼我們可以說用戶名和密碼匹配其他方面我們可以說登錄失敗。沒有必要編寫複雜的代碼。

public int Login(String username,String password) 
{ 
    try 
    { 
     int i = 0; 
     Cursor c = null; 
     c = db.rawQuery("select * from login_table where username =" + "\""+ username.trim() + "\""+" and password="+ "\""+ password.trim() + "\""+, null); 
     c.moveToFirst(); 
     i = c.getCount(); 
     c.close(); 
     return i; 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return 0; 
} 
+0

我真的很感激它.....謝謝你 – SurajRk 2011-03-05 09:37:15

+0

如果你認爲這是非常好的答案,那麼你可以投票,這樣其他用戶可以使用此代碼。 – 2011-03-05 09:52:52

+0

..當你完成一個Cursor對象時,通常調用Cursor#close()。 – Jens 2011-10-04 08:31:31

0

當我們嘗試這個代碼,它要求改變類型爲布爾...

public int Login(String username,String password) 
{ 
    try 
    { 
     int i = 0; 
     Cursor c = null; 
     c = db.rawQuery("select * from login_table where username =" + "\""+ username.trim() + "\""+" and password="+ "\""+ password.trim() + "\""+, null); 
     c.moveToFirst(); 
     i = c.getCount(); 
     c.close(); 
     return i; 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return 0; 
} 
4

通過奇拉格拉瓦爾回答上述功能,但很容易受到SQL注入。惡意用戶可以輕鬆繞過具有基本SQLi負載的身份驗證機制(只要數據庫中至少有一個條目被檢查)。

具有有界值的參數化查詢是更安全的方法。

固定的代碼片段:

public int Login(String username,String password) 
{ 
    String[] selectionArgs = new String[]{username, password}; 
    try 
    { 
     int i = 0; 
     Cursor c = null; 
     c = db.rawQuery("select * from login_table where username=? and password=?", selectionArgs); 
     c.moveToFirst(); 
     i = c.getCount(); 
     c.close(); 
     return i; 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return 0; 
} 

這種簡單的改進是更安全,更容易代碼。