【发布时间】:2015-04-06 22:40:14
【问题描述】:
我已经使用 Xamarin 几个星期了,我正在尝试找到使用 SQLite ORM 查询的最佳实践。我有一个登录页面,在用户可以访问应用程序之前首先启动它。我有在第一个活动之前创建的数据库出现在屏幕上,一旦创建表,管理员登录详细信息就会插入到用户的表中。关键是,管理员要登录并导入一个包含所有其他内容的 xml 文件用户的个人信息。从文件中读取此信息并保存到 sqlite。
接下来,其他用户的详细信息导入并保存成功后才能登录。我的挑战是,在登录时,我想验证详细信息如下:
1.将输入的用户名与数据库中的单个用户名进行比较
**2.检查输入的密码是否与输入的用户名匹配**
我目前正在使用选择查询从数据库中提取所有密码,然后比较两个字符串(来自数据库和编辑文本字段)。但是,如果它是一个大数据库,读取所有数据将很贵。我该怎么做?
如何查找该用户名的密码?
下面是我的代码:
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;
}
}
【问题讨论】:
标签: android database sqlite xamarin xamarin.android