import mysql.connector
# Establish a connection to the database
db = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
# Create a cursor object to execute queries
cursor = db.cursor()
# Execute the query to select specific columns from a table
cursor.execute("SELECT column1, column2 FROM tablename")
# Fetch all the rows returned by the query
rows = cursor.fetchall()
# Display the rows
for row in rows:
print(row)
# Close the database connection
db.close()