jeudi 24 décembre 2015

Select statement in SQLite does not return result

Then the following must return the password of the user stored in the DB

public String getPasswordByUsername(String userName)
{
    SQLiteDatabase db = this.getReadableDatabase();

    String selectQuery = "SELECT  * FROM " + TABLE_USERS + " WHERE "
            + KEY_USERS_USERNAME + " = " + userName;

    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.getCount() < 1) {
        cursor.close();
        return "NOT EXIST";
    }

    cursor.moveToFirst();
    String password = cursor.getString(cursor.getColumnIndex("password"));
    cursor.close();
    return password;
}

When I try to get the password of the user "w", I get:

12-24 10:54:23.639: E/AndroidRuntime(11237): FATAL EXCEPTION: main
12-24 10:54:23.639: E/AndroidRuntime(11237): Process: com.example.doit, PID: 11237
12-24 10:54:23.639: E/AndroidRuntime(11237): android.database.sqlite.SQLiteException: no such column: w (code 1): , while compiling: SELECT  password FROM users WHERE username = w

Here is what is stored in DB: enter image description here

How can I fetch the right password? Thank you.

Aucun commentaire:

Enregistrer un commentaire