'Can I use a python argument in a postgres variable for a query call?
I want to create a python function that takes a start date and end date as arguments. I want the dates passed to the function to be the start date and end date in my postgres query. I'm struggling with understanding if I pass the argument from the function to postgres or if I can declare the variable in postgres and then somehow assign it to the python argument?
I'm also not sure if this is possible or if I'm going about it the wrong way.
def get_data(start_time, end_time):
_format = datetime.datetime.strptime("2017-11-22",'%Y-%m-%d')
start_time = _format.strftime('%Y%m%d')
end_time = _format.strftime('%Y%m%d')
query = '''
DO $$
DECLARE start_date;
DECLARE end_date;
BEGIN
start_date := 'start_time' ;
end_date := 'end_time':
SELECT *
FROM datatable
WHERE time > start_date AND time < end_date
ORDER BY time ASC;
-- ...
END $$;
'''
cur.execute(query)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
