PCPP1 practice question 2 of 10

Select the true statements about the sqlite3 module. (Select two answers.)

Full set: 45 questions
Answer options
A: The fetchalt method returns None when no rows are available
B: The execute method allows you to perform several queries at once
C: The execute method is provided by the Cursor class Correct answer
D: The fetchone method returns None when no rows are available Correct answer
Correct Answer:
C. The execute method is provided by the Cursor class; D. The fetchone method returns None when no rows are available
Explanation

The Python sqlite3 module implements the DB-API 2.0 specification (PEP 249). In this API, database operations are performed using a Cursor object. The Cursor.execute() method is used to execute a single SQL statement. After executing a SELECT query, data is retrieved using fetch methods. The Cursor.fetchone() method retrieves the next available row from the result set. If no more rows are available to be fetched, it returns None, signaling the end of the result set.

Why Incorrect

A. The sqlite3.Cursor object does not have a method named fetchalt. The fetchall() method returns an empty list ([]), not None, when no rows are available.
B. The execute() method is designed to execute only one SQL statement at a time. For executing multiple statements, the non-standard executescript() method must be used.

References

1. Python 3 Documentation, sqlite3 — DB-API 2.0 interface for SQLite databases:

Section 13.13.4.2. Cursor Objects: States that Cursor.execute(sql[, parameters]) executes an SQL statement. This validates option C.

Section 13.13.4.2. Cursor Objects: Describes Cursor.fetchone() as: "Fetches the next row of a query result set, returning a single tuple, or None when no more data is available." This validates option D.

Section 13.13.4.2. Cursor Objects: Describes Cursor.fetchall() as: "Fetches all (remaining) rows of a query result, returning a list." This confirms an empty list is returned for no rows, invalidating the premise of option A.

Section 13.13.4.2. Cursor Objects: Describes Cursor.executescript() as the method for executing multiple SQL statements, which invalidates option B.

2. PEP 249 -- Python Database API Specification v2.0:

Cursor Objects Section: The specification mandates that cursor objects must have an execute() method, confirming option C.

fetchone() Method Description: The specification requires that fetchone() "return a single sequence, or None when no more data is available," which directly supports option D.

Scroll to Top