0

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))
0

2 Answers 2

3

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))
1

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')""")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.