I'm making a Flask application that queries a postgreSQL database using psycopg2. The database has a column timestamps in the format YYYY-MM-DD HH:MI:SS. I want to print ONLY the results that match the current time up to the minute (the moment I query the db). I think what I'm looking for is a dynamic query.
For example, here I want to print the number of users CURRENTLY active.
def userCount(conn):
cur = conn.cursor()
cur.execute("SELECT * from user_list WHERE timestamps = ?")
print 'Users currently active: ' + str(len(cur.fetchall()))
I've tried placing several different things where the question mark is above, including a variable dt defined using python's datetime module. Nothing has worked--I either end up getting a count of all the users in the database, or an error that the column does not exist.
To reiterate, I know how to specify date-time parameters but what I need is the CURRENT date-time. What's the proper way to do this?
intervalInSecs = 30;
def userCount(conn):
cur = conn.cursor()
cur.execute("SELECT * from user_list WHERE timestamps > (current_timestamp - make_interval(secs := %s))", [intervalInSecs])
print 'Users currently active: ' + str(len(cur.fetchall()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With