I have a summarized query that I am trying to put into a pandas data frame in a Flask app. I have figured out how to convert the sqlalchemy query into a string query that pandas can read into a dataframe. The variable has a date value that has been converted to a string.
The issue I am having is I am trying to use a variable in the filter and, when I convert it to the query string, it leaves the variable out, like I didn't provide a value. This results in a sqlalchemy.exc.programmingerror.
Here's my date variable:
this_week = datetime.strftime(get_friday(datetime.now().date()),'%m/%d/%Y')
The get_friday() function just gets the Friday of the week from a given date. In this case, I am just looking at the current week.
Here's my query set to a variable:
grouped_entries = Entry.query \
.with_entities(Entry.entry_date.label('entry_date'), \
Entry.client.label('client'),Entry.day.label('day'), \
func.sum(Entry.total_time).label('time_sum')) \
.filter(Entry.week_end == this_week) \
.group_by(Entry.client,Entry.entry_date,Entry.day) \
.order_by(Entry.entry_date.asc())
Here's the pandas code converting the query to a string query to read into a dataframe:
pd.read_sql(str(grouped_entries.statement.compile(dialect=sqlite.dialect())), db.engine)
Here's the error I get when I try to run it:
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) Incorrect number of bindings supplied. The current statement uses 1, and there are 0 supplied.[SQL: 'SELECT entry.entry_date AS entry_date, entry.client AS client, entry.day AS day, sum(entry.total_time) AS time_sum \nFROM entry \nWHERE entry.week_end = ? GROUP BY entry.client, entry.entry_date, entry.day ORDER BY entry.entry_date ASC']
I know the query works on it's own as a sqlalchemy query but when it is converted to a string query, it adds a ? in place of the variable value in the filter/where clause. I imagine it is some sort of security measure for sql injection or something like that but I can't seem to figure out how to get the variable value in the string query.