I have a website that gets user input from checkboxes and returns the corresponding rows from a MySQL table.
For example, the user can select a couple colors and the website will show the objects from the table that are that color.
The problem is when the user only selects one color the MySQL query is not created properly and I get an error. Note the colors
array below:
So this works:
import MySQLdb
db = MySQLdb.connect(host="...", user="...", port=..., db="...", passwd="...")
colors = ["red", "blue"]
cursor.execute("SELECT * FROM `objects` WHERE `color` IN %s",(colors,))
And this does not:
import MySQLdb
db = MySQLdb.connect(host="...", user="...", port=..., db="...",passwd="...")
colors = ["red"]
cursor.execute("SELECT * FROM `objects` WHERE `color` IN %s", (colors,))
Is there a way to correct this? For now, I am temporarily adding a bogus "color" (one that has no objects linked to it in the database) but this is obviously not an ideal solution.