9951 explained code solutions for 126 technologies


python-mysqlHow do I set up a remote connection to a MySQL database using Python?


To set up a remote connection to a MySQL database using Python, you need to:

  1. Install the MySQL connector for Python: pip install mysql-connector-python
  2. Create a connection object:
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="user",
  passwd="password"
)
  1. Create a cursor object: mycursor = mydb.cursor()
  2. Execute a query: mycursor.execute("SELECT * FROM customers")
  3. Fetch the results: myresult = mycursor.fetchall()
  4. Iterate over the result set:
for x in myresult:
  print(x)
  1. Close the connection: mydb.close()

Code explanation

**

  1. pip install mysql-connector-python - Install the MySQL connector for Python.
  2. import mysql.connector - Import the MySQL connector module.
  3. mydb = mysql.connector.connect(host="localhost", user="user", passwd="password") - Create a connection object to the MySQL database.
  4. mycursor = mydb.cursor() - Create a cursor object to execute queries.
  5. mycursor.execute("SELECT * FROM customers") - Execute a query to select all data from the customers table.
  6. myresult = mycursor.fetchall() - Fetch the results of the query.
  7. for x in myresult: print(x) - Iterate over the result set and print each row.
  8. mydb.close() - Close the connection to the database.

## Helpful links

Edit this code on GitHub