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()
# Define a parameterized query with placeholders
query = "SELECT * FROM tablename WHERE column1 = %s AND column2 = %s"
# Define the values to be inserted into the placeholders
values = ("value1", "value2")
# Execute the query with the values as a parameter
cursor.execute(query, values)
# Fetch all rows returned by the query
rows = cursor.fetchall()
# Display the rows
for row in rows:
print(row)
# Close the database connection
db.close()