1Z0-909 · Question #45
Examine these lines of Python code: You must add a line of code to complete the code to return data to the variable d. Which line will do this?
The correct answer is D. d = cursor.fetch(query, (hire_start, hire_end)). Option D passes both required arguments - the query and the parameter tuple - to a fetch method that retrieves and returns actual row data. The key insight is that you need two things: a retrieval method (not just execution) and the parameterized values (hire_start, hire_end)…
Question
Examine these lines of Python code:
You must add a line of code to complete the code to return data to the variable d. Which line will do this?
Options
- Ad = cursor.execute(query)
- Bd = cursor.execute(query, (hire_start, hire_end) )
- Cd = cursor.fetch(query % (hire_start, hire_end))
- Dd = cursor.fetch(query, (hire_start, hire_end))
- Ed = cursor . f etchall (query)
- Fd = cursor.fetchall(query, (hire_start, hire_end))
How the community answered
(49 responses)- B2% (1)
- D92% (45)
- E2% (1)
- F4% (2)
Explanation
Option D passes both required arguments - the query and the parameter tuple - to a fetch method that retrieves and returns actual row data. The key insight is that you need two things: a retrieval method (not just execution) and the parameterized values (hire_start, hire_end).
Why each distractor fails:
- A & B use
cursor.execute(), which runs the query but returnsNone(or a row count), not the actual data - A also omits the parameters entirely. - C uses string formatting (
%) to embed parameters directly into the query, which is dangerous (SQL injection risk) andcursor.fetch()doesn't accept a pre-formatted string like that. - E calls
cursor.fetchall()correctly as a method name, but passesqueryas an argument - fetch methods take no arguments in standard usage; they operate on already-executed results. - F has the same problem as E:
fetchall()takes no arguments, making the parameter tuple meaningless.
Memory tip: Think of the database cursor workflow as a two-step handshake - execute sends the query, fetch collects the results. Options that use execute to "get" data or fetch with wrong/missing arguments are breaking one half of that handshake.
Topics
Community Discussion
No community discussion yet for this question.