The "Don’t Panic" Guide: Top 20 Viva Questions for Python-MySQL Connectivity

The Complete Guide to Python-MySQL connectivity: Don’t Panic: The 20 most important questions in a viva situation. In case even the idea of an external examiner opposite you causes your heartbeat to increase, breathe in deep. I have found myself on each side of that table and I can tell you a secret, Examiners are not seeking geniuses they are seeking students who have literally created their own projects.



🧠 Section 1: Core Connectivity Logic

These questions explore the bridge between Python and MySQL.

Q1. What is the fundamental purpose of the `mysql-connector` library?

Answer: It serves as a Database Driver or interface. Since Python and MySQL are two different softwares, the connector translates Python objects and commands into a format that the MySQL server can interpret and execute.

Q2. What is a "Cursor" object, and why is it essential for database operations?

Answer: A Cursor is a control structure used to interact with the database. It is responsible for executing SQL queries, managing the context of the statement, and acting as a pointer to the result set returned by the server.

Q3. Why is the parameter `host='localhost'` commonly used in connection strings?

Answer: It specifies that the MySQL server is hosted locally on the same machine where the Python script is running. If the database were on a different computer, we would replace 'localhost' with that server's specific IP address.

Q4. What is the technical impact of failing to call the `close()` method on a connection?

Answer: Failing to close a connection leads to resource leakage. It keeps a network port and server memory occupied unnecessarily. Over time, this can exhaust the server's connection limit, causing the system to slow down or crash.


📊 Section 2: Data Retrieval Methods

These questions focus on how we "read" data from the database.

Q5. Differentiate between the `fetchone()` and `fetchall()` methods.

Answer: 
`fetchone()`: Retrieves the next single row from the result set as a tuple. It returns `None` if no more rows are available.
`fetchall()`: Retrieves all remaining rows from the result set and returns them as a **list of tuples**. It returns an empty list if no records are found.

Q6. What is the specific data structure returned by the `fetchall()` method?

Answer: It returns a List of Tuples. Each tuple in the list represents an individual record (row) from the SQL table.

Q7. What information does the `rowcount` attribute provide?

 Answer: It is a cursor property that returns the number of rows affected by the last DML statement (like `INSERT` or `UPDATE`) or the number of rows retrieved in a `SELECT` statement.


⚡ Section 3: Action & Transaction Control

These questions cover how we "write" and "save" data.

Q8. What is the significance of the `commit()` method in a transaction?

Answer: `commit()` ensures Data Persistence. In Python, changes made by DML commands are temporary by default. `commit()` tells the database to save these changes permanently to the disk.

Q9. Is it mandatory to use `commit()` after executing a `SELECT` query?

Answer: No. `SELECT` is a Data Retrieval operation that does not modify the database state. `commit()` is only required for operations that change data, such as `INSERT`, `UPDATE`, or `DELETE`.

Q10. Explain the role of the `rollback()` method.

Answer: `rollback()` is used for Transaction Recovery. If an error occurs during a multi-step operation, `rollback()` reverts the database to its last committed state, ensuring data integrity.

🗄️ Section 4: SQL & Database Fundamentals

Focusing on the core principles of Relational Databases.

Q11. Define a "Primary Key" in the context of a table.

Answer: A Primary Key is a unique identifier for each record in a table. It must contain unique values and cannot contain `NULL` values. It ensures that every row can be specifically identified.

Q12. What is the technical difference between DDL and DML commands?

Answer:
DDL (Data Definition Language): Commands like `CREATE` and `ALTER` that define or modify the schema (structure) of the database.
DML (Data Manipulation Language): Commands like `INSERT` and `UPDATE` that deal with the tuples (actual data) stored within those structures.


Q13. How does the `WHERE` clause function within a SQL statement?

Answer:
The `WHERE` clause acts as a Predicate or filter. It specifies the exact criteria that rows must meet to be included in the results of a query or to be affected by an update/delete command.


Section 5: Project & Connectivity (Questions 14-20)

Q14. Which specific database have you utilized in your project?

Answer:
I have utilized a database named `Library_DB` (or your specific name, e.g., `Pharmacy_System`). It contains tables such as `Books`, `Members`, and `Transactions` to manage the system's data.

Q15. Could you identify the specific segment of code responsible for establishing the database connection?

Answer:
The connection is established using the `mysql.connector.connect()` method. In my code, it looks like this:
 ```python
 con = mysql.connector.connect(
     host="localhost",
     user="root",
     password="my_password",
     database="Library_DB"
 )
 
 ```

Note: I then use `con.is_connected()` to verify that the link is active.

Q16. Is it possible to interface Python with other database management systems like Oracle or SQLite?

Answer:
Yes, Python is highly versatile. To connect with different databases, we simply need the corresponding library or "connector." For example, we use `cx_Oracle` for Oracle databases and the built-in `sqlite3` module for SQLite.

Q17. What were some of the technical challenges or errors you encountered while developing this project?

Answer:
One major challenge was a `ModuleNotFoundError` because the `mysql-connector-python` library wasn't initially installed in my environment. I also faced an `Access Denied` error due to an incorrect password configuration, which taught me the importance of verifying database credentials before running the script.

Q18. What strategy did you implement to handle potential runtime errors in your connectivity code?

Answer:
I implemented Exception Handling using `try` and `except` blocks. This ensures that if the database server is down or the credentials are wrong, the program displays a user-friendly error message instead of crashing abruptly.

Q19. What is the default port number used by the MySQL server for communication?

Answer:
The default port number for MySQL is 3306. This is the "gate" through which Python sends and receives data from the MySQL service.

Q20. Why is it common to see empty passwords (represented as `""`) in school computer laboratories?

Answer:
In educational environments, security is often minimized to simplify the installation and configuration process for students. By leaving the password blank, it reduces the likelihood of students getting locked out of the database during their initial learning phase.


Final Advice for the Day: 

You are not sure of something, do not keep silence. Ask, I do not know just the technical term, but I know it does so... Investigators are fond of hard work and truthfulness. 
Good luck-you've got this!

Post a Comment

Previous Post Next Post