Validating data before it is stored
Run the validation function first and only execute the INSERT or UPDATE when it returns True, so bad data is rejected before SQL runs and never reaches the database.
Understanding Validating data before it is stored in QCE Digital Solutions Unit 2
What it does: The execute call sits inside the if block, so it is only reached when validation passes. student_exists() checks the parent table first, so an order is only inserted when its foreign key has a matching student. Validation comes first. If is_valid() returns False, the INSERT never runs, so bad data never reaches the database. Rejecting bad data early keeps the database clean. SQL can store data without Python, but it would store the bad values too. The else branch handles rejected data. It tells the user the problem and does not run the SQL.
Common mistakes: After the valid INSERT runs, the change must be saved with conn.commit(). conn.rollback() undoes it, so the valid data is never stored. The return stops the function when the data is invalid. With pass, the function carries on and save_points() runs with bad data. It should be return.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 337–409) and word search (Modules 234–409).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
if is_valid(points):Fill in the Blanks
Spot the Error
Student with a class code? Join your class →