Python String Formats with SQL Wildcards and LIKE

Question:

I’m having a hard time getting some sql in python to correctly go through MySQLdb. It’s pythons string formatting that is killing me.

My sql statement is using the LIKE keyword with wildcards. I’ve tried a number of different things in Python. The problem is once I get one of them working, there’s a line of code in MySQLdb that burps on string format.

Attempt 1:

“SELECT tag.userId, count(user.id) as totalRows FROM user INNER JOIN
tag ON user.id = tag.userId WHERE user.username LIKE ‘%%s%'” % (query)

This is a no go. I get value error:

ValueError: unsupported format character ”’ (0x27) at index 128

Attempt 2:

“SELECT tag.userId, count(user.id) as totalRows FROM user INNER JOIN
tag ON user.id = tag.userId WHERE user.username LIKE ‘%%s%'” %
(query)

I get the same result from attempt 1.

Attempt 3:

like = “LIKE ‘%” + str(query) + “%'” totalq = “SELECT tag.userId,
count(user.id) as totalRows FROM user INNER JOIN tag ON user.id =
tag.userId WHERE user.username ” + like

This correctly creates the totalq variable, but now when I go to run the query I get errors from MySQLdb:

File “build/bdist.macosx-10.6-universal/egg/MySQLdb/cursors.py”, line
158, in execute query = query % db.literal(args) TypeError: not enough
arguments for format string

Attempt 4:

like = “LIKE ‘%” + str(query) + “%'” totalq = “SELECT tag.userId,
count(user.id) as totalRows FROM user INNER JOIN tag ON user.id =
tag.userId WHERE user.username ” + like

This is the same output as attempt 3.

This all seems really strange. How can I use wildcards in sql statements with python?

Asked By: gngrwzrd

||

Answers:

To escape ampersands in Python string formatting expressions, double the ampersand:

'%%%s%%' % search_string

Edit: But I definitely agree with another answer. Direct string substitution in SQL queries is almost always a bad idea.

Answered By: jrdioko

Those queries all appear to be vulnerable to SQL injection attacks.

Try something like this instead:

curs.execute("""SELECT tag.userId, count(user.id) as totalRows 
                  FROM user 
            INNER JOIN tag ON user.id = tag.userId 
                 WHERE user.username LIKE %s""", ('%' + query + '%',))

Where there are two arguments being passed to execute().

Answered By: mechanical_meat

It’s not about string formatting but the problem is how queries should be executed according to db operations requirements in Python (PEP 249)

try something like this:

sql = "SELECT column FROM table WHERE col1=%s AND col2=%s" 
params = (col1_value, col2_value)
cursor.execute(sql, params)

here are some examples for psycog2 where you have some explanations that should also be valid for mysql (mysqldb also follows PEP249 dba api guidance 2.0: here are examples for mysqldb)

Answered By: dzida

I have a solution to your problem :

You can not use :

"SELECT tag.userId, count(user.id) as totalRows FROM user INNER JOIN tag ON user.id = tag.userId WHERE user.username LIKE '%%s%'" % (query)

you can change it with string template, such as :

import MySQLdb
import string # string module
.......
value = {'user':'your value'}
sql_template = string.Template("""
SELECT tag.userId, count(user.id) as totalRows FROM user INNER JOIN
tag ON user.id = tag.userId WHERE user.username LIKE '%$user%'
""")

sql = sql_template.substitute(value)

try:
    cursor.execute(sql)
    ...........
except:
    ...........
finally :
   db.close()
Answered By: kamzur

We could try escaping the percentage character by doubling them like this:

query_to_get_user_name = """ 
SELECT tag.userId, count(user.id) as totalRows FROM user INNER JOIN tag 
ON user.id = tag.userId 
WHERE user.username LIKE '%%%s%%' """ % (user_name,) 

cursor.execute(query_to_get_user_name)
Answered By: shreesh katti
import mysql.connector
mydatabase = mysql.connector.connect(host="localhost", user="root", passwd="1234", database="databaseName")
mycursor = mydatabase.cursor()
user_input =[]
item = str("s%")
user_input.append(item)
mycursor.execute("SELECT * FROM employees WHERE FIRST_NAME LIKE %s ESCAPE ''",user_input )
result = mycursor.fetchall()
for row in enumerate(result):
    print(row)
Answered By: Saeed Ali khan

So I tried this and I think I have got the answer which is simpler for me to understand , a school student .
In my code the table name is "books" and the column I’m Searching for is "Name".
If you need more xplaination , then feel free to drop a mail at [email protected] and I will try my best to answer ASAP

def S():

        n=str(input('Enter the name of the book: '))

        name='%'+n+'%'

        NAME=name

        query="select * from books where Name like '"+NAME+"' "

        c.execute(query)

        ans=c.fetchall()
        
        if len(ans)>0:
            print('')

            for i in ans:

                print(i)

             print('')

        else:
            print('')

            print('An error occured')

            print('Name you gave does not exist :( ')

            print('')
Answered By: Dhruv Joshi

I used the following and it worked:

my_str = 'abc'
query = f"""select * from my_table where column_a like '%%{my_str}%%' """
df=pandas.read_sql_query(query, engine)
Answered By: MOAR
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.