9951 explained code solutions for 126 technologies


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

Edit this code on GitHub