MCS-201 · Unit 16

Data Access Using Python

Section 6 of 7

Block 4 · Unit 16Data Access Using Python9 min · Section 6 of 7

16.6Reading Data and Managing Transactions

Covers IGNOU MCS-201 §16.5

Reading data is the most common database operation. This section shows the ways to fetch query results and how transactions keep a group of related changes all-or-nothing.

Fetching rows

MethodReturns
fetchone()The next row as a tuple, or None if there are no more
fetchmany(n)A list of up to n next rows
fetchall()A list of all remaining rows (empty list if none)
Iterating the cursorfor row in cur: gives rows one by one

Each row is a tuple of Python values converted from MySQL types (INT → int, VARCHAR → str, DECIMAL → decimal.Decimal, DATE → datetime.date). cursor(dictionary=True) returns rows as dictionaries keyed by column name.

For a SELECT, an ordinary cursor does not know how many rows the result has until the rows are fetched. A buffered cursor, conn.cursor(buffered=True), fetches the whole result immediately, so rowcount is correct straight after execute().

Transactions

Transactions guarantee the ACID properties: Atomicity (all or nothing), Consistency (rules are never broken), Isolation (concurrent transactions do not see each other’s half-finished work) and Durability (committed data survives crashes).

Method or attributeEffect
conn.commit()Makes all changes since the last commit permanent
conn.rollback()Undoes all uncommitted changes
conn.autocommitFalse by default in mysql.connector; if set to True, every statement is committed immediately and rollback can no longer undo it

Key takeaways

  • fetchone() → one tuple or None; fetchmany(n); fetchall() → list of tuples.
  • cursor(buffered=True) knows rowcount immediately; cursor(dictionary=True) gives dicts.
  • Transactions are all-or-nothing (ACID); autocommit is off by default.
  • Use try: … commit() / except: rollback().

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does fetchone() return when there are no more rows?

Show answer

Answer: C. None

None signals the end of the result set; fetchall() would return an empty list.

Question 2 · multiple choice

What type of object does fetchall() return?

Show answer

Answer: B. A list of tuples (one tuple per row)

Each row is a tuple of column values, collected in a list.

Question 3 · multiple choice

Which statement undoes all uncommitted changes?

Show answer

Answer: C. conn.rollback()

rollback() returns the data to its state at the last commit.

Question 4 · multiple choice

A program runs two UPDATE statements, the second fails, and the except block calls conn.rollback(). What happens to the first update?

Show answer

Answer: B. It is undone as well

Both statements belong to the same transaction, which is rolled back as a whole.

Question 5 · multiple choice

Why might cur.rowcount be wrong right after executing a SELECT with an ordinary cursor?

Show answer

Answer: C. An unbuffered cursor has not fetched the rows yet, so it cannot know how many there are

Use a buffered cursor or fetch the rows first; then the count is correct.

Question 6 · short answer

Explain the ACID properties of a transaction using a bank transfer.

Show worked answer

Atomicity: debiting account A and crediting account B happen together or not at all. Consistency: rules such as “balance cannot be negative” hold before and after; a transfer that would break them is rolled back. Isolation: another user checking balances during the transfer sees either the old or the new balances, never the half-finished state. Durability: once the transfer is committed, it survives a power failure or server restart.

↑ ↓ move↵ openTab switch type