17

How can i pass intger to a rawquery in sqlite for android. Here its not taking g and s into query. Please guide.

    int g, s;

    Cursor cur3 = database2.rawQuery("select max(UnixTimeStamp) from Quote where EmoticonID=%d and SubCategoryID=%d" ,new String [] {g,s});

3 Answers 3

46

All parameter places should be designated with ?. The parameters to the query are always string nontheless, so nothing special about the integers. This should work for you:

Cursor cur3 = database2.rawQuery("select max(UnixTimeStamp) from Quote where EmoticonID=? and SubCategoryID=?" ,new String [] {String.valueOf(g),String.valueOf(s)});
Sign up to request clarification or add additional context in comments.

Comments

-3

Parameter in your query needs to be string so please convert these parameters into string and write your query as follows.

Use String.valueOf();

Cursor c = db.rawQuery(SELECT max(UnixTimeStamp) 
                       FROM Quote 
                       WHERE EmoticonID ='"+d+"' AND SubCategoryID ='"+ s +"'");

Thanks

1 Comment

This allows for sql injections.
-6
Cursor cur3 = database2.rawQuery("select max(UnixTimeStamp) from Quote where EmoticonID="
                                 + g + " and SubCategoryID = " + s + " ");

1 Comment

Broken on multiple levels, but advocating a solution significantly worse than what the OP asked is irresponsible. Please read up on SQL injection.

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.