0

Hi im very new to Android dev so i have this query that will look into my table and check whether a certain record is existing given the username and password to as of now i have this

public user getLOGusr(String uname, String pass)
{
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_USRS, new String[]{KEY_ID,KEY_NAME,KEY_E_ADDR,KEY_PASS} , KEY_NAME + "=?", KEY_PASS + "=?",
                    new String[]{String.valueOf(uname), String.valueOf(pass)});

    return somestuff;
}

but it is riddled with a lot of errors. any improvements on my code? thanks so much

2 Answers 2

2

Something like this should work:

public user getLOGusr(String uname, String pass)
{
    SQLiteDatabase db = getReadableDatabase();

    // replace * with comma-separated column names
    // if you do not wish to return all columns
    String select = "SELECT * FROM " + TABLE_USRS + " WHERE " + KEY_NAME + " = ? AND " + KEY_PASS + " = ?";

    Cursor cursor = db.rawQuery(select, new String[]{uname, pass});

    // get the data from the Cursor and return stuff
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

String[] tableColumns = new String[] {
  KEY_ID,KEY_NAME,KEY_E_ADDR,KEY_PASS
};
String whereClause = KEY_NAME + " = ? AND " + KEY_PASS  + " = ?";
String[] whereArgs = new String[] {
  uname,
  pass
};
Cursor c = db.query(TABLE_USRS, tableColumns, whereClause, whereArgs, null, null, null);

1 Comment

What is the point of calling String.valueOf() when the params are both Strings?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.