I want to create a variable now that is set to the current time, and then use it in my psycopg2 queries. I've tried using python's datetime as well as current_timestamp, in the following way but it produces an error and can't recognize the variable:
@app.route('/_ajax', methods=['GET', 'POST'])
def userCount():
now = current_timestamp
cur.execute("""SELECT count(DISTINCT(username)) from user_table WHERE tstampz >
(now - INTERVAL '60 mins')""")
userCount=cur.fetchone()[0]
return jsonify (userCount=userCount)
If I use datetime.datetime.now instead of current_timestamp an error reports that datetime cannot be indexed. How can I set this variable properly?
To clarify: The reason why I do not want to use current_timestamp or 'now()` directly in the query is because the time will default to the current time, but it will be changed at some point (based on user input), therefore changing the query.
now()instead ofnowin query?.. I dont think mixing database server time and client time in ajax is a good idea at allnow; you are using a (nonexistant) SQL column name. Check your DB library to see how to create a parameterized SQL query.current_timestampworks, but I wanted a variable in order to change it dynamicallynow?..