Skip to content

Apr 18: Transactions in Python/Flask

Learning Objectives

After today's class, you should be able to:

  • Describe how database transactions affect Python code.
  • Implement a web form that performs inserts or updates.

Lesson Outline

Python Demo [15 min]

Transactions in psycopg

Jinja Demo [20 min]

GP5 example reservation

  • Table chart add event listener
  • HTML element id vs name
  • redirect() and flash()

Work Time [40 min]

  • Make progress on GP5
  • Fix GP1–GP4 as needed

Your To-Do List

  • Finish reading zyBook sections 6.16.5
  • Finish working on GP5 (due Monday)

SQL Example

Transaction we ran in two different windows:

BEGIN;

INSERT INTO booking (booking_id, booking_ref, booking_name,
                     account_id, email, phone, update_ts, price)
VALUES (1, 'CS374', 'Your flight to EnGeo',
        805873, 'fake@email.com', '123-456-7890', now(), '199.34')

INSERT INTO booking_leg (booking_leg_id, booking_id, flight_id,
                         leg_num, is_returning, update_ts)
VALUES (1, 1, 574972, 1, false, now())

COMMIT;  -- or ROLLBACK

Code to undo the above insert statements:

DELETE FROM booking_leg WHERE booking_leg_id = 1;

DELETE FROM booking WHERE booking_id = 1;