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))
CL.
5,3351 gold badge23 silver badges23 bronze badges
2 Answers 2
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))
answered Feb 24, 2020 at 10:09
Do not transfer current month/day as parameters from python, SQLite can do it using its own functions.
cursor = conn.execute("""SELECT family,f_name
FROM birthday
WHERE b_month = STRFTIME('%m','now')
AND b_day = STRFTIME('%d','now')""")
answered Feb 24, 2020 at 7:42
lang-sql