title: How to use variables and OR cunjunctions in SQL statement in Python?
body: <p>I have a list of Ids in a list named <code>res</code> that I want to use line by line as WHERE conditions on a SQL query before saving the results in an array :</p>
  
  <pre><code>                              ids
  grupos                           
  0       [160, 161, 365, 386, 471]
  1                      [296, 306]
  </code></pre>
  
  <p>Here is what I tried to insert it in a SQL query :</p>
  
  <pre><code>listado = [None]*len(res)
  # We store the hashtags that describes the best the groups
  # We iterate on the people of a group to construct the WHERE condition
  print "res : ", res
  for i in (0,len(res)):        
  
  conn = psycopg2.connect(**params)
  cur = conn.cursor()
  
  listado = [None]*len(res)
  for i in (0,len(res)):        
      print "res[i:p] : ", res.iloc[i]['ids']
      cur.execute("""SELECT COUNT(swipe.eclipse_id), subscriber_hashtag.hashtag_id  FROM subscriber_hashtag
      -- join para que las publicidades/eclipses que gusta un usarios estan vinculadas con las de la tabla de correspondencia con los hashtag
      INNER JOIN eclipse_hashtag ON eclipse_hashtag.hashtag_id = subscriber_hashtag.hashtag_id
      -- join para que los usarios  estan vinculados con los de la tabla de correspondencia con los hashtag
      LEFT OUTER JOIN swipe ON subscriber_hashtag.subscriber_id = swipe.subscriber_id
      -- recobremos los "me gusta"
      WHERE subscriber_hastag.subscriber_id in (%s)
      GROUP BY subscriber_hashtag.hashtag_id
          ORDER BY COUNT(swipe.eclipse_id) DESC;""",(res.iloc[i]['ids']))
  
      n = cur.fetchall()
      listado[i] = [{"count": elem[0], "eclipse_id": elem[1]} for elem in n]
  </code></pre>
  
  <h3>Data for a reproducible example</h3>
  
  <p>Providing the further data informations :</p>
  
  <pre><code>subscriber_id hashtag_id
  160           345
  160           347
  161           345
  160           334
  161           347
  306           325
  296           362
  306           324
  296           326
  161           322
  160           322
  </code></pre>
  
  <p>The output should, here, be like :</p>
  
  <pre><code>{0:[324,1],[325,1],[326,1],[362,1], 1 : [345,2],[347,2],[334,1]}
  </code></pre>
  
  <h3>Current error message</h3>
  
  <p>ERROR: An unexpected error occurred while tokenizing input
  The following traceback may be corrupted or invalid
  The error message is: ('EOF in multi-line string', (1, 50))</p>
  
  <pre><code>---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-44-f7c3c5b81303&gt; in &lt;module&gt;()
       39         WHERE subscriber_hastag.subscriber_id in (%s)
       40         GROUP BY subscriber_hashtag.hashtag_id
  ---&gt; 41             ORDER BY COUNT(swipe.eclipse_id) DESC;""",(res.iloc[i]['ids']))
       42 
       43         n = cur.fetchall()
  </code></pre>
  
  <p>TypeError: not all arguments converted during string formatting</p>