[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 602
  • Last Modified:

Python MySQLdb parametrized query

Hi everybody!

I need to write a parametrized mysql query with python using MySQLdb

I did this successfully:

cursor.execute("SELECT * FROM foo WHERE bar = %s AND baz = %s", (param1, param2))

The problem comes when I want to do a LIKE with two parameters:

cursor.execute("SELECT * FROM foo WHERE bar like '%%s%' AND baz like '%%s%'", (param1, param2))

I get an error:

    query = query % db.literal(args)
ValueError: unsupported format character ''' (0x27) at index 47


How can I write this correctly?
0
ltpitt
Asked:
ltpitt
1 Solution
 
ltpittAuthor Commented:
Here's how :)

cursor.execute("SELECT * FROM data WHERE params LIKE %s LIMIT 1", ("%" + param + "%",))
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now