So I am building a query based on user input in flask with this 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()
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!
I have set query to a variable in the templates so I can print it out, and here is what I get:
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')
I ran that directly on my Postgres DB and the result was grand.
In the error dump I notice that the data_return=db.engine.execute(query).fetchall()
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?
"query= u'select name, year, value, display_name from literal inner join ent on ent_id=ent.id where display_name in ('Old-age dependency ratio, ages 65 and older','Forest area (% of total land area)','Energy intensity of transportation sector (MJ/2011 USD PPP)','Divisia Decomposition Analysis - Energy Intensity component Index','HDI: Average annual growth rate, 2000-2013','GDI: Expected years of schooling, males') and name in ('Saudi Arabia', 'Pakistan', 'Finland', 'Jamaica')" "
so Ill try coercing from unicode to str? – Outlying