0

Code looks like this:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = """SELECT * FROM customers WHERE address LIKE '%way%'"""

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

This will select all the records whose address contain care like "way".

How to insert the wildcard dynamically by using %s

Basically I want to know how to use %s instead of 'way' in python so that the code will be more flexible.

shreesh katti
  • 759
  • 1
  • 9
  • 23

2 Answers2

0

try like below

 query ="SELECT * FROM customers   WHERE\
 address LIKE '%"+variable+"%'"
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

Try escaping the percentage by doubling them.

>>> res = """SELECT * FROM customers WHERE address LIKE '%%%s%%'""" % ('way')
>>> res
"SELECT * FROM customers WHERE address LIKE '%way%'"
The Pjot
  • 1,801
  • 1
  • 12
  • 20