I mean something like this:
from django.db import connection
cursor.execute('SELECT * FROM mytable where id IN (%s)', [params])
c.execute('select * from mytable where id in (%s)', [','.join(params)])
select * from mytable where id in ('1,2,3,4,5')
select * from mytable where id in (1,2,3,4,5)
You cannot use
IN for this since indeed, in expects a sequence of ints, but the ORM translates a list to an
ARRAY, and if you use
join you'll end up with a string.
The solution is to use the equivalent
ANY. The formatting is slightly different, but this should work for you:
c.execute('select * from mytable where id = ANY(%s)', [params])
params = [1, 2, 3, 4, 5], the resulting SQL will be:
SELECT * FROM mytable where id = ANY(ARRAY[1, 2, 3, 4, 5])
Note that this requires the list of ids to be made up of ints, so if you have a list of strings be sure to convert them first.