I have implemented a method to validate a user against an SQLite DB but the program keeps throwing an error when the login button is clicked the error codes follow
2019-04-15 21:38:27.455 4601-4601/com.example.mymedicare E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.mymedicare, PID: 4601 android.database.sqlite.SQLiteException: no such column: Brad (code 1): , while compiling: SELECT * FROM users WHERE Brad =? AND Brad1994 =? at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318) at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257) at com.example.mymedicare.MyDBHelper.checkLogin(MyDBHelper.java:96)
I have tried reading the error log but can't find any serious errors my methods will be shown below
// This is my check login method in mydbhelper
public boolean checkLogin(String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
String s;
Cursor c = db.rawQuery("SELECT * FROM users WHERE " + username + " =? AND " + password + " =?", null);
if (c.getCount() <= 0) {
c.close();
db.close();
return false;
} else {
c.close();
db.close();
return true;
}
}
//This is LoggingIn activity where I have tried to implement the above
method
public class LoggingIn extends AppCompatActivity {
//defines page objects
private Button SignIn;
private EditText Usernames;
private EditText Passwords;
private MyDBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logging_in);
//Finds objects by ID
//edit text to string
Usernames = (EditText) findViewById(R.id.Usernames);
Passwords = (EditText) findViewById(R.id.Passwords);
Button SignIn = (Button) findViewById(R.id.SignIn);
db = new MyDBHelper(this);
SignIn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Boolean validUser =
db.checkLogin(Usernames.getText().toString(),
Passwords.getText().toString());
if (validUser == true) {
Intent i = new Intent(getApplicationContext(),
HomePage.class);
startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Invalid login",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
You are passing the parameters username
and password
as column names.
The correct way is:
Cursor c = db.rawQuery(
"SELECT * FROM users WHERE usenamecolumn = ? AND passwordcolumn = ?",
new String[] {username, password});
Change usenamecolumn
and passwordcolumn
to your table's column names for username and password.