0

I want to return the rows in my database that have the attribute in one of the fields equal to the parameter to the method. My code is:

    public Cursor fetchStatus(String status){
    Cursor mCursor =  

            mDb.query(DATABASE_TABLE, new String[] {KEY_COMPANY, KEY_POSITION, KEY_NOTES, KEY_WAGE, KEY_STATUS}, 
                    KEY_STATUS+ "= "+status, null, null, null, null)
            ;
    if(mCursor!=null) mCursor.moveToFirst();
    return mCursor;
}

I want to return the KEY_COMPANY, KEY_POSITION, KEY_NOTES, KEY_WAGE and KEY_STATUS columns where the KEY_STATUS is equal to the parameter 'status'

3
  • Which database (mySQL, Oracle, SQL server, etc.) Commented Oct 30, 2011 at 13:25
  • That is my code that I have tried, and it fails giving an error "SQLiteException near "for": syntax error: ..". The database is SQLite3, and my application is an android application Commented Oct 30, 2011 at 13:28
  • 1
    did you try rawQuery? mDb.rawQuery("sql statement", null); Your code seems to be correct. Commented Oct 30, 2011 at 13:33

2 Answers 2

1

Perhaps you may try something like this:

public object GetValues (string status)
{
   string query = "SELECT KEY_COMPANY, KEY_POSITION, KEY_NOTES, KEY_WAGE,KEY_STATUS 
   FROM DATABASE_TABLE 
   WHERE KEY_STATUS = "'" +status+ "'" " ;

   dbcomands part...

   List<object> =  execute db comand....
}

The error you specified in your comment might be caused by the lack of ' ' in you query. Just a thought.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked, thank you. I was unaware that you needed to use single quotes like that
1

Use PreparedStatement.

You can simply specify your query and set parameters in search query.

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.