Best practice for passing a SQL query in Python is to use (?) placeholders. I'm running into an issue where I have an IN expression for my SQL query and cannot determine how to pass the variable arguments to the placeholder. Edit: This differs from other answers (as pointed out in comments) in that other answers don't include unpacking. My working code is:
cursor = conn.cursor()
query = "Select touchtonekey, COUNT(touchtonekey) as touchedthismanytimes from vw_callhandlertraffic\
where callhandlername = ? and createddatetime between ?\
and ? and touchtonekey IN ('1','2') Group By touchtonekey Order by touchtonekey"
data = cursor.execute(query,'My CallHandler','2019-10-09 13:00:00',
'2019-12-09 13:59:59')
But when I try to remove the IN arguments with this code:
query = "Select touchtonekey, COUNT(touchtonekey) as touchedthismanytimes from vw_callhandlertraffic\
where callhandlername = ? and createddatetime between ?\
and ? and touchtonekey IN ? Group By touchtonekey Order by touchtonekey"
data = cursor.execute(query,'My CallHandler','2019-10-09 13:00:00',
'2019-12-09 13:59:59', "('1','2')")
I get:
Right hand side of IN expression must be a COLLECTION type.
And If I remove the quotes from the parenthesis, I get:
Invalid application buffer type. (-11116) (SQLBindParameter)