答案 0 :(得分:1)
如果用户名和密码匹配,您可以在SQLiteOpenhelper子类中创建一个返回true或false的方法。
public boolean ifValidUserLogin(String username, String password) {
SQLitedarabase db = this.getWriteableDatabase();
Cursor csr = db.query(YOUR_TABLE_NAME,
null, // null equates to all columns
"username" + "=? AND " +
"password" + "=?", // the WHERE clause less WHERE
new String[]{username,password}, // the arguments that replace ?'s
null,null,null
};
boolean rv = (csr.getCount > 0);
csr.close();
return rv;
}
然后你可以在一个活动中调用它,这可能是: -
MySQLiteOpenHelper myhlpr = new MySQLiteOpenhelper(this); // parms might be different
if (myhlpr.ifValidUserLogin(username_from_ui,password_from_ui)) {
// username and password match so do what you need to do here!
} else {
// mismatch so do what you need to do here!.
}
SELECT * FROM table WHERE username=?????? AND password=???????
(表示用户输入值)如果你想要id,那么你可以使用: -
public long getValidUserId(String username, String password) {
long rv = -1; // default to mismatch and thus no id (could use 0)
SQLitedarabase db = this.getWriteableDatabase();
Cursor csr = db.query(YOUR_TABLE_NAME,
null, // null equates to all columns
"username" + "=? AND " +
"password" + "=?", // the WHERE clause less WHERE
new String[]{username,password}, // the arguments that replace ?'s
null,null,null
};
if (csr.moveToFirst) {
rv = csr.getLong(csr.getColumnIndex("YOUR_ID_COLUMNNAME"));
}
csr.close();
return rv;
}
答案 1 :(得分:0)
您可以使用以下查询:
"SELECT * FROM " + TABLE_EMP
+ " WHERE " + KEY_USERNAME + " = '" + userName + "'"
假设您的用户名必须是唯一的才能获得唯一记录。如果您想要更具体,那么您也可以添加密码条件。
答案 2 :(得分:0)
为什么不直接检查您的密码是否与您的用户名相符并忘记了ID?