I've got this Sqlite database named DatabaseHelper and i need to register the data (username, pass, email, and phone) i need to check if username already exists when button register pushed and check user and pass data at login activity. Here is my code :
At first i build the the onCreate and insert function on the database with only user and password, and i can insert that 2 values to the database and can do the validation when press the regis and login button, but after i add 2 more values (email and phone number), when i press the register button nothing will happened, and the app showing no errors.
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "login.db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE user(ID INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT, email TEXT, phone INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS user");
}
public boolean Insert(String username, String password, String email, Integer phone){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("username", username);
contentValues.put("password", password);
contentValues.put("email", email);
contentValues.put("phone", phone);
long result = sqLiteDatabase.insert("user", null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
// check if username already exists
public Boolean CheckUsername(String username){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM user WHERE username=?", new String[]{username});
if(cursor.getCount() > 0){
return false;
}else{
return true;
}
}
// check user and pass matching at login activity
public Boolean CheckLogin(String username, String password){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM user WHERE username=? AND password=?", new String[]{username, password});
if(cursor.getCount() > 0){
return true;
}else{
return false;
}
}
}
Register.java
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String User = user.getText().toString().trim();
String Pass = pass.getText().toString().trim();
String Email = email.getText().toString().trim();
String Phone = phone.getText().toString().trim();
Integer phoneNumber = Integer.parseInt(Phone);
Boolean checkUsername = databaseHelper.CheckUsername(User);
if(checkUsername){
Boolean insert = databaseHelper.Insert(User, Pass, Email, phoneNumber);
if(insert){
Intent registerIntent = new Intent(Register.this,MainActivity.class);
startActivity(registerIntent);
}
}else{
Toast.makeText(getApplicationContext(), "Username already taken", Toast.LENGTH_SHORT).show();
}
}
});
Login.java
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String User = username.getText().toString();
String Pass = password.getText().toString();
Boolean checklogin = databaseHelper.CheckLogin(User, Pass);
if(username.getText().toString().isEmpty()){
Toast.makeText(getApplicationContext(), "Username must not be empty!", Toast.LENGTH_SHORT).show();
}else if(password.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(), "Password must not be empty!", Toast.LENGTH_SHORT).show();
}else if(checklogin){
Intent homeintent = new Intent(getBaseContext(),Home.class);
startActivity(homeintent);
}else{
Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
}
}
});
Potential Issue 1 (probable cause according to your symptoms)
Your
checkUsernamemethod is probably incorrect as it returns false if the username has been located, true if not. I suspect that you really want:-However, you might want to consider using the more compact/consice :-
Potential Issue 2 (may well cause future issues)
You don't say what doesn't happen i.e. what you expect to happen.
A lot could depend upon the code that you haven't shown and you may encounters issues due to using
startActivityto apparently return to the invoking/parent activity (without the complete code this may or may not be the case).Using 'startActivity' will not return to the actual activity, instead it will end (destroy) the invoking activity and start a brand new one.
Instead, you should return to the invoking/parent activity by finishing the child activity using
finish().**Potential Issue 3 (may well result in issues) The phone number column is indicated as being a java integer. This would be insufficient to hold the full range of 10 figure phone numbers.
That is the highest value a Java int can be is 2147483647.
Potential Issue 4 (unlikely, according to your sysmptoms, to be an issue)
This could also be an issue, if you used the App, then altered the onCreate code to add the new columns and just reran the App. The
onCreatemethod is only run once for the lifetime of the database, which unless you specifically delete the database or uninstall the App would then result in the new columns not existing. However, you would encounter column not found errors which would contradict you saying that there are no errors.Demo
Perhaps consider the following that was used to debug Issue 1.
The used a modified DatabaseHelper with the following modifications:-
i.e.:-
The DBHelper was then used in MainActivity to check the code and additionally the resultant data in the database using :-
The result written to the log:-
As can be seen:-
Output if using UNIQUE constraint on username column :-