Skip to content
Advertisement

Trying to insert a python input command into my SQL query

So this is what I’m trying to do I just want to know if there’s a way to link the date input command to my SQL query so that anyone that runs the code can enter a date and get the info for only that specific date. (super-duper noob here)

date = input("DATE:YYYY-MM-DD")
query = '''
SELECT timestamp , something , someotherthing 
FROM database
WHERE timestamp = date
'''
pd.read_sql_query(query,con)

Advertisement

Answer

To get the date for the input to commend you can format your string

date = input("DATE:YYYY-MM-DD")
query = '''
SELECT timestamp , something , someotherthing 
FROM database
WHERE timestamp = {date}
'''
pd.read_sql_query(query.format(date=date),con)

or without the date param

date = input("DATE:YYYY-MM-DD")
query = '''
SELECT timestamp , something , someotherthing 
FROM database
WHERE timestamp = {}
'''
pd.read_sql_query(query.format(date),con)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement