Quiz
- Could we go over the connection map again in class, I was a a bit confused by the first image in the reading and just need an overview of the order of options of how to understand it.
Sure. I think you mean this picture: overview
- Can you explain more about using %s? The syntax is confusing to me in this example:
curs.execute('''select name,birthdate from person where month(birthdate) = %s''', [month])Could you go over this example in class?I'd be glad to. This is fundamental to the really important idea of prepared queries.
The
%sis a place where a dynamic value will be filled in (there, the month, which is a variable containing an integer; it's not a string). This is similar to Python's f-strings:curs.execute(f'''select name,birthdate from person where month(birthdate) = {month}''' )The important difference, however, is in the implementation. In a prepared query, the value is not filled in until after the query has been parsed and the execution plan determined.
That makes it more similar in meaning to a function parameter:
Thought question: How many occurrences of "if" and "return" appear in that function?
- I still get kind of confused on when to use {} or {{}} or <> or %
That's understandable, because we've learned several parameterization approaches. Let's review:
{}: Python strings:- f-strings (Python ≥ 3.6)
print(f'{x} + {y} = {x+y}') - traditional (Python ≥ 2.6)
print('{} + {} = {}'.format(x,y,x+y).
- f-strings (Python ≥ 3.6)
{{}}: Jinja2 templates:<li>{{ person.name }} born on {{ person.birthdate }}</li>%s: PyMySQL parameterized (prepared) queries, like we just saw above.- <name>: Flask: Used in parameterized endpoints, see below
- Did creating the list of months "parametrize" the months and associate the number with the month? Or is that a result of something else?
The list of months just makes for a nicer
descstring. It's otherwise completely irrelevant. However, you can use it in today's exercise to generate the menu on the form. Hint, hint. - No questions, thanks! Also, I think under "Running the App" the code should use ~cs304flask instead of ~cs304 - I got a file not found error with the code that's there.
Ah, good catch; thanks. I'd fixed it a couple of days ago, so you must've done the reading early; bravo!