Inserting multiple values into SQL with %

pythonsqlite

This works:

cursor = conn.execute("""Select FAMILY,F_NAME from birthday where B_MONTH = '%s' """ % (currentMonth))

I cannot seem to expand it to work using an AND clause to also include day, I get a syntax error:

cursor = conn.execute("""Select FAMILY,F_NAME from birthday where B_MONTH = '%s' AND B_DAY = '%s' """ % (currentMonth),%(currentDay))

Best Answer

The % operator takes a string on its left side, and a list of values on its right side:

execute("SELECT ... WHERE B_MONTH = '%s' AND B_DAY = '%s'" % (currentMonth, currentDay))

Please note that using % introduces the risk of SQL injections when used with strings, so you should always use SQL parameters instead:

execute("SELECT ... WHERE B_MONTH = ? AND B_DAY = ?", (currentMonth, currentDay))