Search code examples
pythonsqlselectmariadbwhere-clause

LIKE Command in SQL


I tried to write a query with Like command to search for special Name , but i need to get the value from User and i need to read the value but my sql code has problem with "LIKE" section , how can i fix it ?

sql = '''SELECT Name FROM newtest WHERE Name LIKE = %s'''
val = (n)
myobj = mycurs.execute(sql , val)

what's the problem ?

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= %s' at line 1

this is the error


Solution

  • If you need to search for non-exact matches (i.e. the name is anywhere inside the column) you can change the query as:

    SELECT Name FROM newtest WHERE Name LIKE concat('%', %s, '%')