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?

<p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmW2uViAfWVSCc4NRmA38Gv1zVQJwsz2ELN2xYq7UzMEga/EBOOK_PYTHON_no-name.png" alt="EBOOK_PYTHON_no-name.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmW2uViAfWVSCc4NRmA38Gv1zVQJwsz2ELN2xYq7UzMEga/EBOOK_PYTHON_no-name.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmW2uViAfWVSCc4NRmA38Gv1zVQJwsz2ELN2xYq7UzMEga/EBOOK_PYTHON_no-name.png 2x" /> <hr /> <blockquote> <p dir="auto">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.<br /> If you have any query you can ask.<br /> i hope you will like it.<br /> <br /><br /> <pre><code>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 (?, ?, ?)' <p dir="auto"><center> <p dir="auto"><br /><br />========================================================= <span>Follow me at : <a href="https://steemit.com/@ahmadhassan" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://steemit.com/@ahmadhassan <p dir="auto"><span><img src="https://images.hive.blog/0x0/https://steemitimages.com/DQmYfKYGX2M8Qc8nNUi6TqUzHKoeLmafZ6PVBMjt7wjXe5R/99.gif" /> <br /> Thanks for reading and always welcome your suggestions :) =========================================================
Sort:  

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

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.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

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.