Fares Van Lierde
Fares Van Lierde

Reputation: 15

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)

Upvotes: 1

Views: 83

Answers (2)

Leo Arad
Leo Arad

Reputation: 4472

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)

Upvotes: 1

blacktj
blacktj

Reputation: 173

Concatenate the strings.

date = input("DATE:YYYY-MM-DD")
query = "SELECT timestamp , something , someotherthing \
FROM database \
WHERE timestamp = " + str(date)

pd.read_sql_query(query,con)

Upvotes: 0

Related Questions