I am a new to coding in android. I coded a very simple register and login method. Every time i try logging in after registering, the app stops working.
My OnClickListener
bLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
String pass = helper.searchPass(username);
if(password.equals(pass))
{
Intent i = new Intent(LoginActivity.this, UserAreaActivity.class);
LoginActivity.this.startActivity(i);
}
else
{
Toast wrgPass = Toast.makeText(LoginActivity.this, "Username and password don't match!" , Toast.LENGTH_SHORT);
wrgPass.show();
}
}
});
My searchPass method
public String searchPass(String username)
{
db = this.getReadableDatabase();
String query = "select username, password from "+ TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
String a,b;
//a is Username and b is Password
b = "not found";
if(cursor.moveToFirst())
{
do{
a = cursor.getString(0);
b = cursor.getString(1);
if (a.equals(username))
{
b = cursor.getString(1);
break;
}
}
while(cursor.moveToNext());
}
return b;
}
Also my declaring of SQLite DB
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table " +TABLE_NAME + "("
+ COLUMN_ID +"integer primary key,"
+ COLUMN_NAME + " text,"
+ COLUMN_EMAIL + " text,"
+ COLUMN_PASSWORD + " text,"
+ COLUMN_USERNAME + " text )";
public DatabaseHelper(Context context)
{
super(context , DATABASE_NAME , null , DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(TABLE_CREATE);
this.db = db;
}
Stack trace
Process: com.example.lenovo.loginappv2, PID: 6022
android.database.sqlite.SQLiteException: no such column: username (code 1): , while compiling: select username, passwordcontacts
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)
I am unsure of where I went wrong. It would be helpful if explanation could be provided for the solution as I am still very new to Android language.
EDIT: Added Imgur to stackTrace
EDIT 2: Removed Imgur and added first few lines of StackTrace