2

I'm trying to use ?s as params building my query but there is clearly something wrong with it. Wish the documentation was providing at least one example...

SQLDb.rawQuery("SELECT C.?, B.?, B.?, B.? FROM ? C, ? B  WHERE C.? = B.?", new String[] 
               {Tables.CATEGORIES_NAME, Tables.BUDGET_DATE, Tables.BUDGET_VALUE, Tables.KIND, Tables.CATEGORIES, Tables.BUDGET, Tables.CATEGORIES, Tables.TABLE_ID, Tables.TABLE_ID});

Those const are all strings, simply names of the columns. What am I doing wrong?

4
  • 1
    I don't think you can actually use the question mark as place holders for the database tables. I don't know either why would you need that. Commented Apr 11, 2012 at 19:33
  • ? should be replaced with string so why would that matter. And i'm trying this to shorten my query... since there is such param as selectionArgs Commented Apr 11, 2012 at 19:38
  • 1
    Using ? doesn't append the String directly in the query String as it's used to prevent malicious stuff(like a DROP TABLE * added directly to the query String instead of one of the parameters). I'm no database expert so maybe someone with a little more knowledge would offer an answer. stackoverflow.com/questions/5491107/… Commented Apr 11, 2012 at 19:48
  • oh it seems not posible then, too bad... Commented Apr 11, 2012 at 19:58

2 Answers 2

5

The ? cannot be used for arbitrary pieces of the SQL query. It can be used for values in WHERE, ORDER BY, and so forth, but it cannot be used for database names, table names, column names, and so on to the left of the WHERE clause.

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

Comments

1

Instead of using ? in your query, use String.format with %s to fill-in table and column names in your 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.