python-mysqlHow do I close a MySQL connection using Python?
To close a MySQL connection using Python, you must use the close()
method of the MySQL connection object. This will close the connection to the MySQL server.
Example code
# Import the mysql.connector library/module
import mysql.connector
# Establish a MySQL connection
myconn = mysql.connector.connect(host="localhost", user="user", passwd="passwd", database="mydatabase")
# Close the connection
myconn.close()
The code above will close the connection to the MySQL server.
Code explanation
import mysql.connector
: This imports the mysql.connector library/module.myconn = mysql.connector.connect(host="localhost", user="user", passwd="passwd", database="mydatabase")
: This establishes a MySQL connection.myconn.close()
: This closes the connection to the MySQL server.
Helpful links
More of Python Mysql
- How can I use Python to retrieve data from MySQL?
- How do I download MySQL-Python 1.2.5 zip file?
- How can I connect to MySQL using Python?
- How can I use Python to interact with a MySQL database using YAML?
- How do I access MySQL using Python?
- ¿Cómo conectar Python a MySQL usando ejemplos?
- How can I connect Python to a MySQL database?
- How can I troubleshoot a Python MySQL OperationalError?
- How can I use Python to fetch data from a MySQL database?
- How can I retrieve unread results from a MySQL database using Python?
See more codes...