0

I need to build a query.

Below is the table of my DB:

ID          Name
20          ABC1
30          ABC2
40          ABC3
60          ABC4
70          ABC5
80          ABC6

I need to get the records for the ID's 30 , 40 , 70.

I have written a query as below , but its giving me error.

String query =  "SELECT * FROM tableName WHERE ID = '30','40','70' "

The above query I am passing to below method:

db.rawQuery(query ,null);

but its throwing below error:

I/SqliteDatabaseCpp(19091): sqlite returned: error code = 1, msg = near ",": syntax error, db=xxx

Please let me know what can be do to resolve the issue.

2
  • String query = "SELECT * FROM tableName WHERE ID IN ('30','40','70') " Commented Mar 25, 2014 at 10:17
  • techonthenet.com/sql/in.php Commented Mar 25, 2014 at 10:18

3 Answers 3

2

You need to write instead WHERE ID IN ('30', '40', '70')

Also I strongly suggest you using db.query instead of db.rawQuery to avoid the possible sql injection attack or general string escaping mistakes.

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

Comments

2

You can use regular SQL syntax for that:

String query =  "SELECT * FROM tableName WHERE ID = '30' OR ID = '40' OR ID = '70' "

By the way, you can use in convenient android way:

Exapmle

Comments

0
String query = "SELECT * FROM tableName WHERE ID in ('30','40','70')"

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.