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
Method
Returns
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 cursor
for 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 attribute
Effect
conn.commit()
Makes all changes since the last commit permanent
conn.rollback()
Undoes all uncommitted changes
conn.autocommit
False 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.
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 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.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/data-access-using-python/reading-data-and-managing-transactions/ to read this page.