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))
CL.
5,3351 gold badge23 silver badges23 bronze badges
asked Feb 24, 2020 at 6:08
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))
answered Feb 24, 2020 at 10:09
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')""")
answered Feb 24, 2020 at 7:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.