9951 explained code solutions for 126 technologies


python-mysqlHow do I print a MySQL query in Python?


To print a MySQL query in Python, you can use the mysql.connector module. This module provides an API for connecting to a MySQL database and executing a query.

The following example code will print the results of a query to the database:

import mysql.connector

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

# Create a cursor
cursor = db.cursor()

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

# Fetch the results
results = cursor.fetchall()

# Print the results
for row in results:
    print(row)

The output of the above code will be a list of tuples, where each tuple contains the values of each column in the result set. For example:

('value1', 'value2', 'value3')
('value4', 'value5', 'value6')

The code consists of the following parts:

  1. Import the mysql.connector module
  2. Connect to the database
  3. Create a cursor
  4. Execute a query
  5. Fetch the results
  6. Print the results

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

Edit this code on GitHub