Solved

Python MySQLdb parametrized query

Posted on 2014-03-30
1
579 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

710 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