Displaying query results

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Displaying query results

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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)
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]}")
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
4. Where should the column headings be printed?
A Inside the loop, on every pass
B Once before the loop starts
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")

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1for row in rows: print(f"{row[0]:<10}{row[1]}") AChecks whether the query returned no rows and shows a no records found message instead of a blank screen
2if len(rows) == 0: print("No records found") BLoops over every fetched row and prints its fields, with the first field padded to 10 characters so the columns line up
3Print column headings once, before the loop, using the same widths as the data rows so the headings sit above the columns CThe 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.
4Show each fetched row with its fields in aligned columns by looping over the list of rows DIt 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.
5for row in rows: print(f"{rows[0]:<10}{rows[1]}") Efor row in rows: print(f"{row[0]:<10}{row[1]}")
6for row in rows: print("No records found") Fprint(f"{'Name':<10}{'Points'}")

Definitions

for row in rows: print(f"{row[0]:<10}{row[1]}")

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for ___ in rows: print(f"{row[0]:<10}{row[1]}")
2.
for row in ____: print(f"{row[0]:<10}{row[1]}")
3.
print(f"{'Name':_10}{'Mark'}")
4.
if len(rows) == _: print("No records found")
5.
for row in rows: print(f"{row[0]:<10}{row[_]}")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A rows = cur.fetchall
B for r in rows:
C print(f"{r[0]:<9}{r[1]}")
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]}")
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]}")
Question 4
A for r of rows:
B name = r[0]
C pts = r[1]
D print(f"{name:<9}{pts}")
Question 5
A print(f"{'Name':<9}Mark")
B print("-" 14)
C for r in rows:
D print(f"{r[0]:<9}{r[1]}")

Answer Key

Displaying query results
Section A: Quick Questions
Q1
A — for row in rows: print(row)
Q2
A — print(f"{row[0]:<10}{row[1]}")
Q3
B — Left-aligns the value in a space 10 characters wide
Q4
B — Once before the loop starts
Q5
A — if not rows: print("No records found")
Section B: Match the Pairs
Q1
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
Q2
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
Q3
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'}")
Q4
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]}")
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
for row in rows: print(f"{row[0]:<10}{row[1]}")
Q2
for row in rows: print(f"{row[0]:<10}{row[1]}")
Q3
print(f"{'Name':<10}{'Mark'}")
Q4
if len(rows) == 0: print("No records found")
Q5
for row in rows: print(f"{row[0]:<10}{row[1]}")
Section E: Spot the Error
Q1
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().
Q2
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}.
Q3
D — A for statement must end with a colon. It should be for r in rows:.
Q4
A — Python loops use the keyword in, not of. It should be for r in rows:.
Q5
B — The underline is made by repeating a string, which needs the * operator between "-" and 14. It should be print("-" * 14).