Binding list to params in Pandas read_sql_query with other params
<p>I've been trying to test various methods for making my code to run. To begin with, I have this list:</p>

<p><code>member_list = [111,222,333,444,555,...]</code></p>

<p>I tried to pass it into this query:</p>

<pre class="lang-py prettyprint-override"><code>query = pd.read_sql_query(
"""
select member id
    ,yearmonth
from queried_table
where yearmonth between ? and ?
    and member_id in ?
""", db2conn, params = [201601, 201603, member_list])
</code></pre>

<p>However, I get an error that says:</p>

<blockquote>
  <p>'Invalid parameter type.  param-index=2 param-type=list', 'HY105'</p>
</blockquote>

<p>So I looked around and tried using formatted strings:</p>

<pre class="lang-py prettyprint-override"><code>query = pd.read_sql_query(
"""
select member id
    ,yearmonth
from queried_table
where yearmonth between ? and ?
    and member_id in (%s)
""" % ','.join(['?']*len(member_list), db2conn, params = [201601, 201603, tuple(member_list)])
</code></pre>

<p>Now, I get the error:</p>

<blockquote>
  <p>'The SQL contains 18622 parameter markers, but 3 parameters were supplied', 'HY000'</p>
</blockquote>

<p>because it's looking to fill in all the <code>?</code> placeholders in the formatted string.</p>

<p>So, ultimately, is there a way to somehow evaluate the list and pass each individual element to bind to the <code>?</code> or is there another method I could use to get this to work?</p>

<p>Btw, I'm using <code>pyodbc</code> as my connector.</p>

<p>Thanks in advance!</p>