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 do I connect to XAMPP MySQL using Python?
- How do Python MySQL and SQLite compare in terms of performance and scalability?
- How can I install the MySQL-Python (Python 2.x) module?
- How do I download MySQL-Python 1.2.5 zip file?
- How can I connect Python to a MySQL database?
- How do I use Python to update multiple columns in a MySQL database?
- How do I use Python to query MySQL with multiple conditions?
- How do I use Python to authenticate MySQL on Windows?
- How do I use a Python MySQL refresh cursor?
- How do I use a cursor to interact with a MySQL database in Python?
See more codes...