0

I have this on my DBHelper

public Cursor selectUser(String username){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM user_tbl WHERE username = '" +
            username+"'", null);
    return res;
}

And i also have this try-catch block on my Activity.

DBHelper mydb;
Cursor res;
user = LoginActivity.usernamee;        
    try{
                res = mydb.selectUser(user);
                if(res.getCount() > 0){
                    String fullnamee = res.getString(res.getColumnIndex(DBHelper.USER_NAME));
                    fullname.setText(fullnamee);
                    fullname.setFocusable(false);
                    fullname.setClickable(false);
                }

            }catch(Exception e){
                System.out.println("Error retrieving database record.");
            }

However, I am getting the catch exception. What I am trying to do is that, I would like to check my database where username is equal to LoginActivity.usernamee. Then display the full name of the user from my database table to my Android text view.

I would like to ask for assistance. Thank you!

2
  • From your current block of code, I don't see you initialize DBHelper Instead of printing a generic error in your catch, you should get the error message from the exception. It'll help you debug any problems. Commented Oct 13, 2016 at 19:51
  • The exception object contains useful information. Why are you throwing it away? Commented Oct 14, 2016 at 7:18

1 Answer 1

1

i think you are missing "moveToFirst"

SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM user_tbl WHERE username = '" + username + "'", null);

if(res.getCount() > 0){
  res.moveToFirst();
  String fullnamee = res.getString(res.getColumnIndex(DBHelper.USER_NAME));
  // do what you want
}
Sign up to request clarification or add additional context in comments.

Comments

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.