9951 explained code solutions for 126 technologies


python-mysqlHow do I use Python to select data from a MySQL database?


To use Python to select data from a MySQL database, you can use the mysql.connector module. This module provides an API for connecting to and interacting with a MySQL database.

Example code

import mysql.connector

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

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

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

# Fetch the results
result = cursor.fetchall()

# Print the results
print(result)

Output example

[(1, 'John', 'Doe', '[email protected]'), (2, 'Jane', 'Doe', '[email protected]')]

The code above can be broken down into the following parts:

  1. Import the mysql.connector module.
  2. Connect to the database using mysql.connector.connect() and passing in the relevant connection details.
  3. Create a cursor object using db.cursor().
  4. Execute a query using cursor.execute().
  5. Fetch the results using cursor.fetchall().
  6. Print the results.

Helpful links

Edit this code on GitHub