lundi 6 avril 2015

Xamarin.Android - Challenge with SQLite ORM queries

I've been working with Xamarin for a few weeks now and i'm trying to find the best practices for using SQLite ORM queries.I have a log in page which is launched first before users can access the app.I have the database created before this first activity comes to the screen and administrator log in details inserted into the user's table as soon as the tables are created.The point is,the admin is meant to log in and import an xml file containing all of the other user's personal information.This information is read from the file and saved to sqlite as well.


Next,the other users can log in after their details have been imported and saved successfully.My challenge is,at the point of logging in,i would like to verify the details as follows:


1.Compare the entered username,with a single username from the db

2.Check the password entered to see if it matches the username entered


I'm currently using a select query to pull up all the passwords from the database,before comparing the two strings(from the db and from the edit text field).If it's a large db however,reading all the data will be quite expensive.How do i go about this?


How do i also look up the password for that username?


Below is my code:



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;
}


Then the DataManager class:



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;
}

}

Aucun commentaire:

Enregistrer un commentaire