9951 explained code solutions for 126 technologies


python-mysqlHow can I keep a MySQL connection open in Python?


To keep a MySQL connection open in Python, you can use the mysql-connector-python library. This library allows you to establish a connection to a MySQL server and keep it open. Here is an example of how to use it:

import mysql.connector

# Create connection
mydb = mysql.connector.connect(
  host="localhost",
  user="user",
  passwd="passwd"
)

# Keep connection open
mydb.ping(reconnect=True)

This code will create a connection to a MySQL server and keep it open. You can also use the mysql-connector-python library to perform queries and other operations on the MySQL server.

The code consists of the following parts:

  1. import mysql.connector: This imports the mysql.connector library, which is used to connect to a MySQL server.
  2. mydb = mysql.connector.connect(host="localhost", user="user", passwd="passwd"): This creates a connection to the MySQL server with the given host, user, and password.
  3. mydb.ping(reconnect=True): This keeps the connection open, and reconnects if the connection is lost.

For more information, see the MySQL Connector/Python documentation.

Edit this code on GitHub