Legend 7
Legend 7

Reputation: 31

How to find the full and partial match in Mysql server

User gives the input and input could be a full or partial name. The query should display all the matching output. This is the code that I am using

c.execute("SELECT name FROM ALLNAMES WHERE Cname ='" + ('%'+ Input.get()+'%') + "';")

I am using python and the output is an empty list which doesn't look right.

Upvotes: 0

Views: 185

Answers (2)

arturwwl
arturwwl

Reputation: 1059

You meant to have like query, not equality. So in this case it will be:

c.execute("SELECT name FROM ALLNAMES WHERE Cname LIKE '"+ ('%'+ Input.get()+'%') + "';")

https://www.mysqltutorial.org/mysql-like/

Beware of that is not good idea to put query parameters in query string. It is vulnerable for easy sql injection. Use at least query params.

Upvotes: 0

Jayvee
Jayvee

Reputation: 10873

You need like instead of =

c.execute("SELECT name FROM ALLNAMES WHERE Cname LIKE '" + ('%'+ Input.get()+'%') + "';")

Upvotes: 1

Related Questions