user1642943
user1642943

Reputation: 23

Error in android - SQLite

I am trying to query my database to return the name entered into a text box. I have been able to view all the items in the database but i want to search for a name using a text box. When I enter the name and hit the search button, the log cat displays an error that "no such column: john: while compiling: SELECT name, gender, height, age, hair FROM details WHERE name= john ORDER by name.

 private void findRecords(){
    EditText find = (EditText)findViewById(R.id.nameSearch);
    EditText name = (EditText)findViewById(R.id.nameFound);
    EditText gender = (EditText)findViewById(R.id.genderFound);
    String s = find.getText().toString();
    //Long l = Long.parseLong(s);
    DatabaseClass hon = new DatabaseClass(this);

    String returnedName = hon.getName(s);
    String returnedGender = hon.getGender(s);

    name.setText(returnedName);
    gender.setText(returnedGender);

}


    public String getName(String s) {
    // TODO Auto-generated method stub
    String[] columns = new String[] {NAME_COLUMN, GENDER_COLUMN, HEIGHT_COLUMN, AGE_COLUMN, HAIR_COLUMN};
    Cursor c = sherlockdb.query(TABLE_NAME, columns, NAME_COLUMN +"="+ s, null, null, null, NAME_COLUMN);
    if (c != null){
        c.moveToFirst();
        String name = c.getString(1);
        return name;
    }
    return null;
}

Please kindly help

Upvotes: 1

Views: 86

Answers (3)

CommonsWare
CommonsWare

Reputation: 1006539

Replace:

Cursor c = sherlockdb.query(TABLE_NAME, columns, NAME_COLUMN +"="+ s, null, null, null, NAME_COLUMN);

with:

String[] args={s};
Cursor c = sherlockdb.query(TABLE_NAME, columns, NAME_COLUMN +"=?", args, null, null, NAME_COLUMN);

Upvotes: 3

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Try this:

Cursor c = sherlockdb.query(TABLE_NAME, columns, NAME_COLUMN +"='"+ s+"'", null, null, null, NAME_COLUMN);

Because you need String in 'john' format.

Upvotes: 0

matheuslf
matheuslf

Reputation: 309

What kind of data is your column? I think that you forgot to put the literal jonh between ''.

You can try this:

SELECT name, gender, height, age, hair FROM details WHERE name='john' ORDER by name;

Upvotes: 1

Related Questions