Google News
logo
Python Program to Wildcards characters
Here's a Python program that uses wildcard characters in a MySQL query :
Program :
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()
In the above program, we are using the `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.

In this example, we are searching for rows where `column1` contains the string `"value"` anywhere in the text. You can replace these column names and table name with your own.

The `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.

Finally, we close the database connection using the `close()` method.