Running a SELECT from code and fetching rows

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

Running a SELECT from code and fetching rows

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line runs a SELECT query from Python using a cursor?
A cursor.execute("SELECT * FROM students")
B cursor.fetchall("SELECT * FROM students")
2. What does cursor.fetchall() return after a SELECT runs?
A A list of tuples, one tuple per row
B A single string containing all the data
3. Which is the correct order of steps?
A rows = cursor.fetchall() then cursor.execute(sql)
B cursor.execute(sql) then rows = cursor.fetchall()
4. A SELECT matches no rows. What does fetchall() return?
A An error that stops the program
B An empty list: []
5. What does cursor.fetchone() return?
A Just one row, as a single tuple
B Every row, as a list of tuples

Match the Pairs

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

Left columnRight column
1cursor.execute("SELECT name, price FROM products") Afetchall() is called before execute(), so there is no query to fetch rows from. The query must be run first.
2rows = cursor.fetchall() BThe brackets are missing after fetchall, so the method is never called. rows holds the method itself, not the data.
3fetchone() returns just the first row as a single tuple, so you can read one record without a list. Cif rows == []:
4A SELECT that matches no rows gives an empty list, so you can check for it with an if statement. DSends the SELECT query to the database to run it. The rows are not returned yet; they wait to be fetched.
5rows = cursor.fetchall print(rows) ECollects every row from the SELECT as a list of tuples, or an empty list [] if nothing matched.
6rows = cursor.fetchall() cursor.execute("SELECT * FROM students") Frow = cursor.fetchone()

Definitions

cursor.fetchall()

Fill in the Blanks

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

1.
cursor._______("SELECT * FROM students")
2.
cursor.execute("SELECT name FROM subjects") rows = cursor.________()
3.
cursor.execute("SELECT * FROM students") row = cursor.________()
4.
rows = cursor.fetchall() if len(rows) == _: print("No rows found")
5.
rows = cursor.fetchall() for row in rows: print(row[_])

Spot the Error

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

Question 1
A cur = conn.cursor()
B cur.execute(sql)
C rows = cur.fetchall()
D for row in rows
E print(row)
Question 2
A cur = conn.cursor()
B cur.execute(sql)
C rows = cur.fetchall()
D print(rows.len())
Question 3
A cur = conn.cursor
B sql = "SELECT * FROM results"
C cur.execute(sql)
D rows = cur.fetchall()
Question 4
A cur = conn.cursor()
B sql = "SELECT * FROM results"
C cur.fetchall(sql)
D row = cur.fetchone()
E print(row)
Question 5
A cur = conn.cursor()
B sql = SELECT * FROM results
C cur.execute(sql)
D rows = cur.fetchall()

Answer Key

Running a SELECT from code and fetching rows
Section A: Quick Questions
Q1
A — cursor.execute("SELECT * FROM students")
Q2
A — A list of tuples, one tuple per row
Q3
B — cursor.execute(sql) then rows = cursor.fetchall()
Q4
B — An empty list: []
Q5
A — Just one row, as a single tuple
Section B: Match the Pairs
Q1
cursor.execute("SELECT name, price FROM products") → Sends the SELECT query to the database to run it. The rows are not returned yet; they wait to be fetched.
Q2
rows = cursor.fetchall() → Collects every row from the SELECT as a list of tuples, or an empty list [] if nothing matched.
Q3
fetchone() returns just the first row as a single tuple, so you can read one record without a list. → row = cursor.fetchone()
Q4
A SELECT that matches no rows gives an empty list, so you can check for it with an if statement. → if rows == []:
Q5
rows = cursor.fetchall print(rows) → The brackets are missing after fetchall, so the method is never called. rows holds the method itself, not the data.
Q6
rows = cursor.fetchall() cursor.execute("SELECT * FROM students") → fetchall() is called before execute(), so there is no query to fetch rows from. The query must be run first.
Section C: Definitions
Q1
cursor.fetchall() — Returns every row from the SELECT that cursor.execute() ran, as a list of tuples. An empty result gives an empty list []. fetchone() returns just one row.
Section D: Fill in the Blanks
Q1
cursor.execute("SELECT * FROM students")
Q2
cursor.execute("SELECT name FROM subjects") rows = cursor.fetchall()
Q3
cursor.execute("SELECT * FROM students") row = cursor.fetchone()
Q4
rows = cursor.fetchall() if len(rows) == 0: print("No rows found")
Q5
rows = cursor.fetchall() for row in rows: print(row[0])
Section E: Spot the Error
Q1
D — A for statement must end with a colon. Line 4 should be 'for row in rows:' so it can loop over the list of tuples returned by fetchall().
Q2
D — fetchall() returns a normal Python list, and lists have no .len() method. Use the len() function: print(len(rows)) to count the rows.
Q3
A — conn.cursor is a method and must be called with brackets. Without () cur just holds the method, so cur.execute() fails. It should be cur = conn.cursor().
Q4
C — It is cur.execute() that runs the SELECT. fetchall() only collects rows after the query has run and takes no query argument. Line 3 should be cur.execute(sql).
Q5
B — The SQL statement is passed to execute() as a string, so it must be inside quotes. It should be sql = "SELECT * FROM results".