Solved

Python MySQLdb parametrized query

Posted on 2014-03-30
1
568 Views
Last Modified: 2014-03-30
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
Comment
Question by:ltpitt
1 Comment
 
LVL 1

Accepted Solution

by:
ltpitt earned 0 total points
Comment Utility
Here's how :)

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now