SQLite Android query

I'm having troubles with this query which doesn't seems to work. Can not figure it out. Is it a problem with this method ? I have manually entered one account before and checked that the database is not empty. Cursor getCount method return 0 systematically.

private static final int VERSION_BDD = 1;
private static final String NOM_BDD = "account.db";
private static final String TABLE_ACCOUNT = "table_account";
private static final String COL_EMAIL = "Email";
private static final String COL_PASSWORD = "Password";

...
...

public Account getAccountWithEmail(String email){
     Cursor c = bdd.rawQuery("select * from table_account where Email = '" + email + "'", null);
     return cursorToAccount(c);
}

private Account cursorToAccount(Cursor c){
    if (c.getCount() == 0){
        return null;
    }

    else{
        c.moveToFirst();
        Account account = new Account();
        account.setEmail(c.getString(NUM_COL_EMAIL));
        account.setPassword(c.getString(NUM_COL_PASSWORD));
        c.close();

        return account;
    }
}

Answers


Change to:

public Account getAccountWithEmail(String email){
    Cursor c = bdd.rawQuery("SELECT * FROM " + TABLE_ACCOUNT + " WHERE " + COL_EMAIL "= ?", new String[]{email});
    return cursorToAccount(c);
}

read this: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String, java.lang.String[])

selectionArgs You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.

You should use your static variables for those various column names and table names, as it is just better practice (avoid typos!). As for the query, you want to use the selectionArgs and use of ? which corresponds to the arrays index for each argument


Need Your Help

Context.xml file missing Tomcat 8

java xml servlets web context.xml

I'm a beginner at creating a dynamic web project in Eclipse. I need the context.xml file to set up a database realm, but it's not under META-INF, instead I have found it in Tomcat's conf directory....

iOS UIStackView unable to simultaneously satisfy constraints if set hidden

ios xcode swift constraints uistackview

I have a problem referring to an UIStackView. I created a StackView and added a few items bundled into another StackViews to it. Now I want to animate the inner StackViews with the hidden attribute...