Solved

Python MySQLdb parametrized query

Posted on 2014-03-30
1
582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
ltpitt earned 0 total points
ID: 39965516
Here's how :)

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

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this article, we’ll look at how to deploy ProxySQL.
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 …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

621 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