2015-04-06 90 views
0

我已經與Xamarin合作了幾個星期,現在,我試圖找到最佳實踐使用SQLite ORM queries.I有一個登錄頁面,其首次推出在用戶可以訪問應用程序之前創建。在第一個活動進入屏幕之前創建數據庫,管理員在創建表後立即登錄插入到用戶表中的詳細信息。要點是,管理員要登錄並且導入一個包含所有其他用戶的個人信息的xml文件。該信息從文件中讀取並保存到sqlite中。Xamarin.Android - 挑戰與SQLite的ORM查詢

接下來,當他們的細節已導入並保存successfully.My的挑戰是,在登錄點的其他用戶可以登錄,我想覈實具體情況如下:

1。用一個用戶名從

** 2,檢查輸入的密碼,看它是否輸入的用戶名**

我目前使用select查詢拉起所有匹配的數據庫進行比較輸入的用戶名在比較兩個字符串(來自數據庫)之前來自數據庫的密碼並從編輯文本字段)。如果它是一個大型數據庫,但是,閱讀所有的數據將是非常昂貴的。我該怎麼做呢?

我該如何查找該用戶名的密碼?

下面是我的代碼:

namespace sample.NameSpace 
{ 
    [Activity (MainLauncher = true)] 
public class MainActivity : Activity 
{ 


Button login = FindViewById<Button> (Resource.Id.login); 

     try{ 

     login.Click += (object sender, EventArgs e) => { 

if (TextUtils.IsEmpty(userName.Text.ToString())) { 

       makeToast(); 

      } else if (TextUtils.IsEmpty(password.Text.ToString())) { 

       makeToast(); 
      } else { 


        returningUser = userName.Text.ToString().Trim(); 

        returningUserPassword = password.Text.ToString().Trim(); 


       } 

//Check to see if the name is in the db already 
      List<string> allUsers = GetAllUserNames(); 

       //Loop through the list of names and compare the retrieved username with the name entered in the text field 
       string retrievedDbName =""; 

       foreach(string name in allUsers) 
       { 

        retrievedDbName = name .Trim(); 

       } 

       //Verify name 
       if(retrievedDbName .Equals(returningUser)) 
        { 

       Toast.MakeText(this, 
        "Login Successful !", ToastLength.Short).Show(); 

       Intent intent = new Intent (this, typeof(HomeActivity)); 
       StartActivity (intent); 

      } 
      else 
      { 
       Toast.MakeText(this, "User Name or Password does not match", ToastLength.Short).Show(); 
      } 

     }; 
     } catch(Exception e){ 

      logger.Exception (this, e); 
     } 


public List<string>GetAllUserNames() 
    { 
     List<UserInfoTable> allUserNames = new List<UserInfoTable>(); 

     allUserNames = dataManager.GetSingleUserName(); 

     string name = ""; 

     foreach(var UserName in allUserNames) 
     { 
      Console.WriteLine ("Usernames from db :" + name.ToString()); 
     } 

     return allUserNames; 
    } 

然後DataManager類:

public List<UserInfoTable> GetSingleUserName() 
    { 
     UserInfoTable user = new UserInfoTable(); 

     using (var db = dbHandler.getUserDatabaseConnection()) { 

      var userName = db.Query<UserInfoTable> ("select * from UserInfoTable where user_name = ?", user.USER_NAME); 

      return userName; 
     } 

    } 

回答

1
public bool CheckLogin(string user, string password) 
{ 
    bool valid = false; 

    using (var db = dbHandler.getUserDatabaseConnection()) { 

    var user = db.Query<UserInfoTable> ("select * from UserInfoTable where user_name = ? and password = ?", user, password); 

    if (user != null) valid = true; 
    } 

    return valid; 
} 
+0

謝謝Jason.This工作 – naffie 2015-04-07 07:43:30