1

This query keeps returning errors like error near ?.

public Cursor getRow2( String st, String dr) throws SQLException {
   String whereClause = "(adate ?) AND (station ?)";
   String[] whereArgs = new String[] { dr, st };
   String orderBy = "adate";

   Cursor mCursor = db.query(DATABASE_TABLE, columns, whereClause, whereArgs, 
        null, null, orderBy);

String dr is for "data-range".

  • If the user does specify the two dates, then dr gets a value like BETWEEN 2004-03-01 AND 2004-06-01.
  • Othewise dr gets NOT NULL so that the query finds ALL dates.

String st is for "gas station".

  • If the user provides the station name, st gets a string like 'Shell'.
  • Otherwise st gets NOT NULL so as to find ALL stations.

Thank you very much.

0

1 Answer 1

1

You can use ? to bind only literals, not partial expressions.

(adate ?) AND (station ?) is not a valid expression. Binding the first arg to BETWEEN 2004-03-01 AND 2004-06-01 and the second Shell makes it essentially

(adate 'BETWEEN 2004-03-01 AND 2004-06-01') AND (station 'Shell')

which is syntactically incorrect.

To use bind args with a query like this, make the query like

(adate BETWEEN ? AND ?) AND (station = ?)

binding args as "2004-03-01", "2004-06-01", "Shell".

For the "not specified" cases it's probably best to use different queries altogether.

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.