我正在使用SQLite数据库的登录系统上工作。我已经有一个注册表单,它可以很好地创建所有用户,并且我还创建了一种方法来检查输入到EditText中的用户名是否存在。但是现在我的问题是,您将如何从该用户那里检索所有相关信息,例如,我还需要检查该特定用户的密码是否匹配,并且还需要从该用户那里检索一些测试成绩。还可以在数据库中显示它们,然后在主页中显示它们。这是用于登录用户的主要活动中的代码:
public void loginUser() {
EditText usernameInput = findViewById(R.id.login_username);
EditText passwordInput = findViewById(R.id.login_password);
String loginusername = usernameInput.getText().toString();
String loginPassword = passwordInput.getText().toString();
if(loginusername.matches("") || loginPassword.matches("")) {
Toast.makeText(this,"Fields cannot be empty",Toast.LENGTH_SHORT).show();
} else {
if (dbHelper.ifUserExists(loginusername)) {
//password verification and login
//how to retrieve relevant users password and compare to password input?
} else {
Toast.makeText(this,"Incorrect username or password",Toast.LENGTH_SHORT).show();
}
}
}
这是databaseHelper中用于检查用户是否存在的方法:
public boolean ifUserExists(String user) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USERS,new String[]{KEY_ID,KEY_username,KEY_PASSWORD,KEY_SAT_READING,KEY_SAT_MATH,KEY_act_READING,KEY_act_MATH},KEY_username + "=?",new String[]{user},null,null);
if(cursor != null && cursor.moveToFirst()&& cursor.getcount() > 0) {
return true;
} else {
return false;
}
}
可以使用的多种方法之一是使User对象包含包含保存数据的变量。与进程的这种结合,而不是返回布尔值,将返回User或null。
也许可以根据您的代码来考虑这个演示应用程序,为简便起见,它在数据库帮助器中包括一个User类。
DatabaseHelper.java :-
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String TABLE_USERS = "users";
public static final String KEY_ID = BaseColumns._ID;
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String KEY_SAT_READING = "reading";
public static final String KEY_SAT_MATH = "satmath";
public static final String KEY_ACT_READING = "actreading";
public static final String KEY_ACT_MATH = "actmath";
SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context,DBNAME,null,DBVERSION);
db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String users_crt_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_USERS +
"(" +
KEY_ID + " INTEGER PRIMARY KEY," +
KEY_USERNAME + " TEXT UNIQUE," +
KEY_PASSWORD + " TEXT," +
KEY_SAT_READING + " TEXT," +
KEY_SAT_MATH + " TEXT," +
KEY_ACT_READING + " TEXT," +
KEY_ACT_MATH + " TEXT" +
")";
db.execSQL(users_crt_sql);
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
}
public long insertNewUser(String name,String password) {
ContentValues cv = new ContentValues();
cv.put(KEY_USERNAME,name);
cv.put(KEY_PASSWORD,password);
return db.insert(TABLE_USERS,cv);
}
public User checkUser(String name,String password) {
User rv = null;
Cursor csr = db.query(
TABLE_USERS,KEY_USERNAME + "=? AND " + KEY_PASSWORD + "=?",new String[]{name,password},null
);
if (csr.moveToFirst()) {
rv = new User();
rv.setUserId(csr.getLong(csr.getColumnIndex(KEY_ID)));
rv.setUserName(name);
rv.setUserPassword("");
rv.setUserSatReading(csr.getString(csr.getColumnIndex(KEY_SAT_READING)));
rv.setUserSatMath(csr.getString(csr.getColumnIndex(KEY_SAT_MATH)));
rv.setUserActReading(csr.getString(csr.getColumnIndex(KEY_ACT_READING)));
rv.setUserActMath(csr.getString(csr.getColumnIndex(KEY_ACT_MATH)));
}
csr.close();
return rv;
}
public class User {
long userId;
String userName;
String userPassword;
String userSatReading;
String userSatMath;
String userActReading;
String userActMath;
public User() {
}
public User(String name,String password) {
this(-1L,name,password,null);
}
public User(long id,String name,String password,String satReading,String satMath,String actReading,String actmath) {
this.userId = id;
this.userName = name;
this.userPassword = password;
this.userSatReading = satReading;
this.userSatMath = satMath;
this.userActReading = actReading;
this.userActMath = actmath;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserSatReading() {
return userSatReading;
}
public void setUserSatReading(String userSatReading) {
this.userSatReading = userSatReading;
}
public String getUserSatMath() {
return userSatMath;
}
public void setUserSatMath(String userSatMath) {
this.userSatMath = userSatMath;
}
public String getUserActReading() {
return userActReading;
}
public void setUserActReading(String userActReading) {
this.userActReading = userActReading;
}
public String getUserActMath() {
return userActMath;
}
public void setUserActMath(String userActMath) {
this.userActMath = userActMath;
}
}
}
上面的内容在以下活动中使用,它反映了您现有的代码和想要实现的目标。
最初启动时,应用程序会添加一个用户(用于演示),其名称为 Admin ,其密码为 adminpassword 。用户界面包括一个显示未登录!!!! (显示顶部)的textView,两个编辑文本(根据您的代码)和一个用于登录的按钮。
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper dbHelper;
DatabaseHelper.User current_user = null;
EditText loginUsername,loginPassword;
TextView status;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = this.findViewById(R.id.status);
loginUsername = this.findViewById(R.id.login_username);
loginPassword = this.findViewById(R.id.login_password);
login = this.findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginUser();
}
});
dbHelper = new DatabaseHelper(this);
dbHelper.insertNewUser("Admin","adminpassword");
}
private void loginUser() {
if (
loginUsername.getText().toString() == null
|| loginUsername.getText().toString().length() <1
||loginPassword.getText().toString() == null
|| loginPassword.getText().toString().length() <1) {
Toast.makeText(this,"Fields cannot be empty",Toast.LENGTH_SHORT).show();
return;
}
if ((current_user = dbHelper.checkUser(loginUsername.getText().toString(),loginPassword.getText().toString())) != null) {
loginUsername.setVisibility(View.GONE);
loginPassword.setVisibility(View.GONE);
login.setText("DO SOMETHNG");
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"Hello " +
current_user.getUserName() +
" you are logged in so you can do something!",Toast.LENGTH_SHORT).show();
}
});
status.setText("Successfully Logged-In as " + current_user.getUserName());
} else {
Toast.makeText(this,"Incorrect username or password",Toast.LENGTH_SHORT).show();
}
}
}
结果