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 with a wildcard character
query = "SELECT * FROM tablename WHERE column1 LIKE '%value%'"
cursor.execute(query)
# 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()
LIKE
` keyword in the `SELECT
` statement to search for rows that contain a certain pattern. The `%
` character is used as a wildcard, which matches any sequence of characters. column1
` contains the string `"value
"` anywhere in the text. You can replace these column names and table name with your own.execute()
` method is used to execute the query, and `fetchall()
` method is used to fetch all rows returned by the query. We then display the rows using a `for` loop.close()
` method.