Programming Tip -How can I write an SQL IN query with a Python tuple?

Programming Tip -How can I write an SQL IN query with a Python tuple?

EBOOK_PYTHON_no-name.png


Today i am going to show Programming Tip - How can I write an SQL IN query with a Python tuple with simple code. I hope you will understand this.
If you have any query you can ask.
i hope you will like it.


my_tuple = ("Hello", "world", "John")
placeholder= '?'
placeholders= ', '.join(placeholder for _ in my_tuple)
query= 'SELECT name FROM students WHERE id IN (%s)' % placeholders
print(query)
# now execute using the cursor

cursor.execute(query, my_tuple)


This will give the output

'SELECT name FROM students WHERE id IN (?, ?, ?)'


Follow me at : https://steemit.com/@ahmadhassan
=========================================================


Thanks for reading and always welcome your suggestions :) =========================================================

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now