← QCE Digital Solutions Unit 2

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

Class: _____________________  ·  Name: _____________________  ·  Date: _________

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.

Learning
Duo
After cursor.fetchall(), which code prints every row from the rows list?
for row in cursor: print(rows)for row in rows: print(row)
Matches
for row in rows: print(f"{rows[0]:<10}{rows[1]}")
↓
It uses rows (the whole list) instead of row (the current row), so every pass prints the same first two records instead of each row's own fields.
Applying
Meanings
Term
for row in rows: print(f"{row[0]:<10}{row[1]}")
Meaning
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.
Practice
rows = [("Ava", 120), ("Liam", 95), ("Mia", 110)]
Testing
Gaps
for in rows:
Check Answer
Spots
1rows = cur.fetchall
2for r in rows:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Hedgehog' badge — First Steps, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. After cursor.fetchall(), which code prints every row from the rows list?
A. for row in rows: print(row) ✓
B. for row in cursor: print(rows)
The loop variable row takes each fetched row from rows in turn, and print uses that same row variable.
2. Which line prints the first two fields of a row in a readable column layout?
A. print(f"{row[0]:<10}{row[1]}") ✓
B. print("{row[0]:<10}{row[1]}")
An f-string needs the f before the opening quote, otherwise the curly braces are printed as plain text.
3. What does :<10 do in f"{row[0]:<10}"?
A. Stops the value at 10 characters and deletes the rest
B. Left-aligns the value in a space 10 characters wide ✓
The width setting pads short values with spaces, so the next field starts in the same column on every line.
4. Where should the column headings be printed?
A. Inside the loop, on every pass
B. Once before the loop starts ✓
Printing headings before the for loop shows them once at the top. Inside the loop they would repeat above every row.
5. Which check shows a no records found message when a query returns nothing?
A. if not rows: print("No records found") ✓
B. if rows: print("No records found")
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.

Match the Pairs

for row in rows: print(f"{row[0]:<10}{row[1]}") → Loops over every fetched row and prints its fields, with the first field padded to 10 characters so the columns line up
if len(rows) == 0: print("No records found") → Checks whether the query returned no rows and shows a no records found message instead of a blank screen
Print column headings once, before the loop, using the same widths as the data rows so the headings sit above the columns → print(f"{'Name':<10}{'Points'}")
Show each fetched row with its fields in aligned columns by looping over the list of rows → for row in rows: print(f"{row[0]:<10}{row[1]}")
for row in rows: print(f"{rows[0]:<10}{rows[1]}") → It uses rows (the whole list) instead of row (the current row), so every pass prints the same first two records instead of each row's own fields.
for row in rows: print("No records found") → The no records message is inside the loop, so it prints once for every row and never appears when the list is empty because the loop body does not run.

Key Term

for row in rows: print(f"{row[0]:<10}{row[1]}")
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.

Fill in the Blanks

1. for row in rows: print(f"{row[0]:<10}{row[1]}")
Hint: Type the loop variable that holds one fetched record on each pass.
2. for row in rows: print(f"{row[0]:<10}{row[1]}")
Hint: Type the name of the list of fetched records that the loop steps through.
3. print(f"{'Name':<10}{'Mark'}")
Hint: Type the alignment symbol that left-aligns text in a column heading.
4. if len(rows) == 0: print("No records found")
Hint: Type the number of rows that means the query returned nothing.
5. for row in rows: print(f"{row[0]:<10}{row[1]}")
Hint: Type the index of the second field in each row, since counting starts at zero.

Spot the Error

Question 1
A rows = cur.fetchall
B for r in rows:
C print(f"{r[0]:<9}{r[1]}")
Line A — 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().
Question 2
A rows = cur.fetchall()
B print(f"{'Name':<9}Points")
C for r in rows:
D print(f"{rows[0]:<9}{r[1]}")
Line D — 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}.
Question 3
A rows = cur.fetchall()
B if len(rows) == 0:
C print("No records found")
D for r in rows
E print(f"{r[0]:<9}{r[1]}")
Line D — A for statement must end with a colon. It should be for r in rows:.
Question 4
A for r of rows:
B name = r[0]
C pts = r[1]
D print(f"{name:<9}{pts}")
Line A — Python loops use the keyword in, not of. It should be for r in rows:.
Question 5
A print(f"{'Name':<9}Mark")
B print("-" 14)
C for r in rows:
D print(f"{r[0]:<9}{r[1]}")
Line B — The underline is made by repeating a string, which needs the * operator between "-" and 14. It should be print("-" * 14).

Student with a class code? Join your class →