title: Executing "SELECT ... WHERE ... IN ..." using MySQLdb
body: <p>I'm having a problem executing some SQL from within Python, despite similar SQL working fine from the <code>mysql</code> command-line.</p>
  
  <p>The table looks like this:</p>
  
  <pre><code>mysql&gt; SELECT * FROM foo;
  +-------+-----+
  | fooid | bar |
  +-------+-----+
  |     1 | A   | 
  |     2 | B   | 
  |     3 | C   | 
  |     4 | D   | 
  +-------+-----+
  4 rows in set (0.00 sec)
  </code></pre>
  
  <p>I can execute the following SQL query from the mysql command-line, without a problem:</p>
  
  <pre><code>mysql&gt; SELECT fooid FROM foo WHERE bar IN ('A','C');
  SELECT fooid FROM foo WHERE bar IN ('A','C');
  +-------+
  | fooid |
  +-------+
  |     1 | 
  |     3 | 
  +-------+
  2 rows in set (0.00 sec)
  </code></pre>
  
  <p>However, when I try to do the same from within Python, I get no rows, while I expected 2 rows:</p>
  
  <pre><code>import MySQLdb
  import config
  connection=MySQLdb.connect(
      host=config.HOST,user=config.USER,passwd=config.PASS,db='test')
  cursor=connection.cursor()
  
  sql='SELECT fooid FROM foo WHERE bar IN %s'
  args=[['A','C']]
  cursor.execute(sql,args)
  data=cursor.fetchall()
  print(data)
  # ()
  </code></pre>
  
  <p>So the question is: how should the python code be modified to select those <code>fooid</code>s where <code>bar</code> is in <code>('A','C')</code>?</p>
  
  <p>By the way, I noticed that if I switch the roles of <code>bar</code> and <code>fooid</code>, I can get the code to select those <code>bar</code>s where <code>fooid</code> is in <code>(1,3)</code> successfully. I don't understand why one such query (below) works, while the other one (above) doesn't.</p>
  
  <pre><code>sql='SELECT bar FROM foo WHERE fooid IN %s'
  args=[[1,3]]
  cursor.execute(sql,args)
  data=cursor.fetchall()
  print(data)
  # (('A',), ('C',))
  </code></pre>
  
  <p>And just to be absolutely clear, this is how the <code>foo</code> table was created:</p>
  
  <pre><code>mysql&gt; DROP TABLE IF EXISTS foo;
  Query OK, 0 rows affected (0.00 sec)
  
  mysql&gt; CREATE TABLE `foo` (
            `fooid` int(11) NOT NULL AUTO_INCREMENT,
            `bar` varchar(10) NOT NULL,
            PRIMARY KEY (`fooid`));
  Query OK, 0 rows affected (0.01 sec)
  
  mysql&gt; INSERT into foo (bar) values ('A'),('B'),('C'),('D');
  Query OK, 4 rows affected (0.00 sec)
  Records: 4  Duplicates: 0  Warnings: 0
  </code></pre>
  
  <hr>
  
  <p><strong>Edit</strong>: When I enable the general query log with <code>mysqld -l /tmp/myquery.log</code> 
  I see</p>
  
  <pre><code>mysqld, Version: 5.1.37-1ubuntu5.5-log ((Ubuntu)). started with:
  Tcp port: 3306  Unix socket: /var/run/mysqld/mysqld.sock
  Time                 Id Command    Argument
  110101 11:45:41     1 Connect   unutbu@localhost on test
              1 Query set autocommit=0
              1 Query SELECT fooid FROM foo WHERE bar IN ("'A'", "'C'")
              1 Query SELECT bar FROM foo WHERE fooid IN ('1', '3')
              1 Quit
  </code></pre>
  
  <p>Indeed, it looks like too many quotes are being placed around <code>A</code> and <code>C</code>.</p>
  
  <p>Thanks to @Amber's comment, I understand better what is going wrong. MySQLdb converts the parametrized argument <code>['A','C']</code> to <code>("'A'","'C'")</code>. </p>
  
  <p>Is there a way to make a parametrized query using the <code>IN</code> SQL syntax? Or must one manually construct the SQL string?</p>