9951 explained code solutions for 126 technologies


python-mysqlHow can I read data from a MySQL database using Python?


To read data from a MySQL database using Python, you need to use a library called MySQL Connector/Python. This library is used to connect to MySQL databases and execute queries.

The following example code shows how to connect to a MySQL database and execute a query to retrieve all the rows from the users table:

import mysql.connector

# Connect to the database
db = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="password",
    database="mydatabase"
)

# Create a cursor object
cursor = db.cursor()

# Execute a query
cursor.execute("SELECT * FROM users")

# Fetch all the rows
results = cursor.fetchall()

# Print the results
print(results)

The code above consists of the following parts:

  1. Import the mysql.connector library.
  2. Connect to the MySQL database using the connect() method.
  3. Create a cursor object using the cursor() method.
  4. Execute a query using the execute() method.
  5. Fetch the results using the fetchall() method.
  6. Print the results.

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

Edit this code on GitHub