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

in #utopian-io6 years ago

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 :) =========================================================

Sort:  

This post has received a 2.87 % upvote from @booster thanks to: @ahmadhassan.

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by ahmadhassan from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.