SQL Connector with Python - Class 12 Questions
### Short Answer Questions:
1. What is MySQL Connector in Python?
2. How do you install MySQL Connector in Python?
3. What is the purpose of `cursor()` in MySQL Connector?
4. How do you establish a connection to a MySQL database in Python?
5. What is the difference between `execute()` and `executemany()` methods?
6. How do you fetch all records from a table using Python?
7. Explain the role of `commit()` in database operations.
8. What is the use of `rollback()` in database transactions?
9. How can you check if a connection to the MySQL database is successful?
10. What is the significance of `fetchone()` and `fetchall()` methods?
### Multiple Choice Questions (MCQs):
1. Which module is used to connect MySQL with Python?
a) sqlite3
b) mysql-connector-python
c) psycopg2
d) pandas
2. Which of the following is the correct method to execute an SQL query in Python?
a) `cursor.run(query)`
b) `cursor.execute(query)`
c) `cursor.exec(query)`
d) `cursor.sql(query)`
3. The function used to close a MySQL connection in Python is:
a) `disconnect()`
b) `stop()`
c) `close()`
d) `exit()`
### Practical Questions:
1. Write a Python program to establish a connection with MySQL database named "school".
2. Write a Python script to create a table "students" with fields (id, name, age, grade) in MySQL.
3. Write a Python program to insert multiple records into the "students" table.
4. Write a Python function to retrieve and display all records from the "students" table.
5. Write a Python program to update the age of a student in the "students" table based on their id.
6. Write a Python program to delete a student record from the "students" table based on id.
7. Write a program to handle database connection errors using `try-except` blocks.
8. Write a Python program that demonstrates the use of transactions (commit and rollback) in
MySQL.
### Case Study-Based Questions:
1. A school maintains student records in MySQL. Write a Python program to:
- Connect to the database
- Retrieve student details who scored above 90 in a subject
- Display the results in tabular form.
2. A company wants to store employee details in MySQL. Create a Python script to:
- Establish a database connection
- Create a table "employees" (id, name, department, salary)
- Insert sample data and retrieve all employees from the IT department.
3. Write a Python program to generate a report that lists students along with their grades, sorted in
descending order.