Home > Database > Mysql Tutorial > body text

Why Does \'SELECT ... WHERE ... IN ...\' Fail in MySQLdb with Parameterized Queries?

Barbara Streisand
Release: 2024-10-29 21:46:02
Original
723 people have browsed it

Why Does

Executing "SELECT ... WHERE ... IN ..." using MySQLdb

Executing a SQL query with the IN clause using MySQLdb can be challenging, particularly when SQL parameters are employed. While a similar query may function correctly from the mysql command-line, its execution in Python may encounter difficulties.

Case Study

Consider the following SQL query:

SELECT fooid FROM foo WHERE bar IN ('A', 'C')
Copy after login

When attempting to execute this query from within Python using the mysql command, the following SQL is generated:

<code class="python">sql = 'SELECT fooid FROM foo WHERE bar IN %s'
args = [['A', 'C']]</code>
Copy after login

However, upon execution, this query returns no rows, despite expecting two. Interestingly, by switching the roles of bar and fooid in the Python query, the expected rows are successfully retrieved:

<code class="python">sql = 'SELECT bar FROM foo WHERE fooid IN %s'
args = [[1, 3]]</code>
Copy after login

Root Cause

The discrepancy in behavior stems from the way MySQLdb converts the parameterized argument ['A', 'C'] to ("'''A''"', "'''''C''"'") while executing the query. The excessive quotation marks render the IN clause ineffective.

Solution

Since MySQLdb lacks a built-in method to bind a list to an IN clause, the query parameters must be manually constructed as follows:

<code class="python"># Python 3
args = ['A', 'C']
sql = 'SELECT fooid FROM foo WHERE bar IN (%s)' 
in_p = ', '.join(list(map(lambda x: '%s', args)))
sql = sql % in_p

# Python 2
args = ['A', 'C']
sql = 'SELECT fooid FROM foo WHERE bar IN (%s)' 
in_p = ', '.join(map(lambda x: '%s', args))
sql = sql % in_p</code>
Copy after login

By manually constructing the IN clause with the correct number of placeholders, the query can be executed successfully, returning the expected rows.

The above is the detailed content of Why Does \'SELECT ... WHERE ... IN ...\' Fail in MySQLdb with Parameterized Queries?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template