Quiz

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

  2. 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 %s is 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:

    
        def string_type(s1):
            if len(s1) > 10:
                return 'long'
            else:
                return 'short'
        # and a call:
        string_type('if return')
    
    

    Thought question: How many occurrences of "if" and "return" appear in that function?

  3. 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:

    1. {}: Python strings:
      • f-strings (Python ≥ 3.6) print(f'{x} + {y} = {x+y}')
      • traditional (Python ≥ 2.6) print('{} + {} = {}'.format(x,y,x+y).
    2. {{}}: Jinja2 templates: <li>{{ person.name }} born on {{ person.birthdate }}</li>
    3. %s: PyMySQL parameterized (prepared) queries, like we just saw above.
    4. <name>: Flask: Used in parameterized endpoints, see below
    
    month_names = ['skip', 'January', 'February', 'March',
                   'April', 'May', 'June',
                   'July', 'August', 'September',
                   'October', 'November', 'December' ]
    
    @app.route('/people-born-in/<month>')
    def people_born_in_month(month):
        month_num = int(month)
        month_name = month_names[month_num]
        with dbi.connect() as conn:
            folk = people.people_born_in_month(conn,month_num)
            return render_template('people-list.html', 
                                   desc=f'WMDB People born in {month_name}',
                                   people=folk)
    
    
    
  4. 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 desc string. It's otherwise completely irrelevant. However, you can use it in today's exercise to generate the menu on the form. Hint, hint.

  5. 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!