TypeError: 'dict' object does not support indexing thrown on second instance of this query
<p>So I am building a query based on user input in flask with this code:</p>

<pre><code>    if empty_indic_form.validate_on_submit():
    query='select name, year, value, display_name from literal inner join ent on ent_id=ent.id where display_name in ('
    for i in indic_form.indicators.data:
        query=query+'\''+i+'\','
    query=query[:-1]+') '
    query=query+'and name in ('
    for c in order_c:
        query=query+c+','
    query=query[:-1]+')'
    data_return=db.engine.execute(query).fetchall()
</code></pre>

<p>I have confirmed that query looks like what it is supposed to, and even had an earlier session where it was returning a list of rowproxy objects like I was expecting. But now I am getting this error no matter what I do!</p>

<p>I have set query to a variable in the templates so I can print it out, and here is what I get:</p>

<blockquote>
  <p><code>select name, year, value, display_name from literal 
  inner join ent 
  on ent_id=ent.id 
  where display_name in ('Energy savings of primary energy (TJ)','Adolescent birth rate (women aged 15-19 years)','Net migration rate','Transmission and distribution losses (%)') 
  and name in ('Burkina Faso', 'Ghana', 'Saudi Arabia', 'Pakistan')</code></p>
</blockquote>

<p>I ran that directly on my Postgres DB and the result was grand.</p>

<p>In the error dump I notice that the <code>data_return=db.engine.execute(query).fetchall()</code> line is building with an empty dictionary as the parameters, which of course throws that error in the end. Can I force it not to do this? The query object looks like it does above, whats wrong with it now? Should I perhaps be killing the db session upon refreshing the page or going to the home page?</p>