Displaying query results
Loops over each fetched row and prints its fields with an f-string, padded into columns so results are readable. Check for an empty list to show a no records message.
Understanding Displaying query results in QCE Digital Solutions Unit 2
What it does: The heading uses the same :<10 width as the row output, so the heading text lines up with the data printed by the loop below it. A for loop handles every row returned by the query, and the f-string formats row[0] and row[1] into padded columns. An f-string needs the f before the opening quote, otherwise the curly braces are printed as plain text. Printing headings before the for loop shows them once at the top. Inside the loop they would repeat above every row. fetchall() gives an empty list when nothing matches. An empty list is False, so if not rows is True only when there are no records.
Common mistakes: fetchall is a method, so it needs brackets to run. Without them rows holds the method itself, not the list of rows. It should be rows = cur.fetchall(). Inside the loop each field must come from the current row r. Using rows[0] prints the first whole row every time. It should be {r[0]:<9}.
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
for row in rows: print(f"{row[0]:<10}{row[1]}")Fill in the Blanks
Spot the Error
Student with a class code? Join your class →