Which datatype is mostly returned by fetchall() method in Python–MySQL…
2023
Which datatype is mostly returned by fetchall() method in Python–MySQL connectivity?
- A.
A single list or an empty list, if no more rows are available
- B.
A list of tuples or an empty list, if no more rows are available tuples
- C.
A list of dictionaries or an empty list, if no more rows are available dictionaries
- D.
A single tuple or an empty tuple, if no more rows are available
Attempted by 1328 students.
Show answer & explanation
Correct answer: B
The fetchall() method returns a list of rows, where each row is represented as a tuple. If no rows are available, it returns an empty list.
Each tuple contains values corresponding to the selected columns in the query, in the order they appear.
If the query returns no results, fetchall() returns an empty list.
To return rows as dictionaries instead of tuples, use a dictionary-style cursor (e.g., DictCursor or connection.cursor(dictionary=True)).
Example: After executing SELECT id, name FROM users, cur.fetchall() might return [(1, 'Alice'), (2, 'Bob')].