我已经通过SQLite database
创建了一个注册和登录页面。我正在尝试创建一个函数,如果要注册的新用户的用户名与另一个创建的帐户的用户名相似,则该新用户将无法注册。
我还需要在用户尝试时提示您"Username not available"
。我确实尝试在数据库帮助器中对其进行编码,但它导致我的android-studio
意外崩溃,因此我将其删除。 我已经尝试在线搜索教程,但无济于事。
活动数据库助手:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "register.db";
public static final String TABLE_NAME = "registeruser";
//Column 1 is for ID
public static final String COL_1 = "ID";
//Column 2 is for username
public static final String COL_2 = "username";
//Column 3 is for password
public static final String COL_3 = "password";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null , 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE registeruser (ID INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT, password TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int ii) {
sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
//function to add new users when registering
public long adduser (String user, String password)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("username", user);
contentValues.put("password", password);
long res = db.insert("registeruser",null,contentValues);
db.close();
return res;
}
//function to check if user exists
public boolean checkUser(String username, String password)
{
String[] columns = { COL_1};
SQLiteDatabase db = getReadableDatabase();
String selection = COL_2 + "=?" + " and " + COL_3 + "=?";
String[] selectionArgs = { username, password };
Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
int count = cursor.getCount();
cursor.close();
db.close();
if(count>0)
return true;
else
return false;
}
public int updatePassword(String username, String newPassword){
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("password",newPassword);
String whereClause = "username = ?";
String[] whereArgs = { username };
int result = db.update(TABLE_NAME,contentValues,whereClause,whereArgs);
db.close();
return result;
}
}
活动注册活动:
public class RegisterActivity extends AppCompatActivity {
DatabaseHelper db;
EditText mTextUsername;
EditText mTextPassword;
EditText mTextCnfPassword;
ImageButton mButtonRegister;
TextView mTextViewLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
db = new DatabaseHelper(this);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mTextCnfPassword = (EditText)findViewById(R.id.edittext_cnf_password);
mButtonRegister = (ImageButton)findViewById(R.id.imagebutton_register);
mTextViewLogin = (TextView)findViewById(R.id.textview_login);
mTextViewLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent LoginIntent = new Intent(RegisterActivity.this,LoginPageActivity.class);
startActivity(LoginIntent);
}
});
mButtonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = mTextUsername.getText().toString().trim();
String pwd = mTextPassword.getText().toString().trim();
String cnf_pwd = mTextCnfPassword.getText().toString().trim();
if(pwd.length() > 0 && user.length() > 0 && pwd.equals(cnf_pwd)) {
Long val = db.adduser(user,pwd);
if(val > 0){
Toast.makeText(RegisterActivity.this,"Successfully Registered.",Toast.LENGTH_SHORT).show();
Intent movetoLogin = new Intent(RegisterActivity.this,LoginPageActivity.class);
startActivity(movetoLogin);
}
else{
Toast.makeText(RegisterActivity.this,"Registration Error.",Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(RegisterActivity.this,"Check Your Fields Again.",Toast.LENGTH_SHORT).show();
}
}
});
}
}