9951 explained code solutions for 126 technologies


python-mysqlHow can I host a MySQL database using Python?


You can host a MySQL database using Python by using the MySQL Connector/Python library. This library allows you to connect to a MySQL database, execute SQL queries, and perform other operations.

Example code

import mysql.connector

# Establish connection to MySQL
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)

# Create a cursor to perform operations
mycursor = mydb.cursor()

# Execute a query
mycursor.execute("SELECT * FROM customers")

# Fetch all results
result = mycursor.fetchall()

# Print results
print(result)

Output example

[(1, 'John', 'Highway 21'),
 (2, 'Peter', 'Lowstreet 4'),
 (3, 'Amy', 'Apple st 652'),
 (4, 'Hannah', 'Mountain 21'),
 (5, 'Michael', 'Valley 345')]

Code explanation

  1. import mysql.connector: imports the MySQL Connector/Python library so that you can use it to connect to the MySQL database.
  2. mydb = mysql.connector.connect(host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase"): creates a connection to the MySQL database with the given parameters.
  3. mycursor = mydb.cursor(): creates a cursor object that can be used to execute SQL queries.
  4. mycursor.execute("SELECT * FROM customers"): executes the given SQL query.
  5. result = mycursor.fetchall(): fetches all the results of the query.
  6. print(result): prints the results of the query.

Helpful links

Edit this code on GitHub