0% found this document useful (0 votes)
2 views

Interfacing_Python_with_MySQL_Questions-2 MARKS

The document provides a series of two-mark questions and answers regarding interfacing Python with MySQL using the `mysql.connector` module. Key topics include establishing connections, executing queries, handling transactions, and managing exceptions. It also covers the use of placeholders, cursor methods, and the default MySQL port number.

Uploaded by

Sarvesh Madhav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Interfacing_Python_with_MySQL_Questions-2 MARKS

The document provides a series of two-mark questions and answers regarding interfacing Python with MySQL using the `mysql.connector` module. Key topics include establishing connections, executing queries, handling transactions, and managing exceptions. It also covers the use of placeholders, cursor methods, and the default MySQL port number.

Uploaded by

Sarvesh Madhav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Two-Mark Questions: Interfacing Python with MySQL

1. 1. What is the purpose of the `mysql.connector` module in Python?


- The `mysql.connector` module provides the necessary tools to connect and interact
with a MySQL database using Python.

2. 2. How do you establish a connection to a MySQL database in Python?


- Use the `mysql.connector.connect()` function, providing parameters like host, user,
password, and database.

3. 3. Write the syntax to import the `mysql.connector` module in Python.


- `import mysql.connector`

4. 4. Which method is used to execute SQL queries in Python after establishing a


connection?
- The `execute()` method of the `cursor` object is used to execute SQL queries.

5. 5. What is the function of the `commit()` method in MySQL Python connectivity?


- The `commit()` method is used to save changes made to the database during a
transaction.

6. 6. How do you retrieve the results of a SELECT query in Python?


- Use the `fetchall()` or `fetchone()` methods of the cursor object to retrieve query
results.

7. 7. What is the use of the `close()` method in MySQL Python integration?


- The `close()` method is used to close the database connection to free resources.

8. 8. Mention two common exceptions raised while working with MySQL in Python.
- `mysql.connector.errors.InterfaceError` and `mysql.connector.errors.DatabaseError`.

9. 9. What are placeholders in MySQL queries, and how are they used in Python?
- Placeholders are symbols like `%s` used in parameterized queries to avoid SQL
injection.
For example:
cursor.execute("INSERT INTO table_name (col1) VALUES (%s)", (value,))

10. 10. Write the command to create a cursor object in MySQL Python connectivity.
- `cursor = connection.cursor()`
11. 11. What is the difference between `fetchone()` and `fetchall()` methods?
- `fetchone()` retrieves a single record, whereas `fetchall()` retrieves all records from the
query result.

12. 12. What is the default port number for MySQL?


- The default port number for MySQL is **3306**.

13. 13. Write the command to delete a record from a table in MySQL using Python.
- Example:

cursor.execute("DELETE FROM table_name WHERE condition")

14. 14. How can you handle errors during MySQL operations in Python?
- Use a `try-except` block to catch exceptions such as `mysql.connector.Erro

15. 15. What is the purpose of `cursor.rowcount`?


- The `cursor.rowcount` attribute returns the number of rows affected by the last
executed query.

You might also like