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.

brig
  • 3,721
  • 12
  • 43
  • 61

3 Answers3

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

Community
  • 1
  • 1
sygi
  • 4,557
  • 2
  • 32
  • 54
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.

alandarev
  • 8,349
  • 2
  • 34
  • 43
0
String query = "SELECT * FROM tableName WHERE ID in ('30','40','70')"
hooknc
  • 4,854
  • 5
  • 31
  • 60