This question already has an answer here:
- How to pass variable values dynamically in pandas sql query 1 answer
I want to do sql query in python. I could use cx_oracle to connection database in python:
# Build connection
conn_str = u'username/password@host:1521/sid'
conn = cx_Oracle.connect(conn_str)
Now I'm trying to retrieve data from the database by using SQL query in Python:
sql_select_statement = """SELECT * FROM TABLE
WHERE DATE BETWEEN '20-oct-2017' AND '30-oct-2017'"""
Assume we don't know the starting date, we only have a date variable called starting_time, and its value is a datetime %m/%d/%Y. Also, ending_time is yesterday, I would like to modify my SQL query as:
sql_select_statement = """SELECT * FROM TABLE
WHERE DATE BETWEEN '20-oct-2017' AND sysdate-1"""
df = pd.read_sql(sql_select_statement, conn)
It works and generate a new df, but how to replace '20-oct-2017' with the variable starting_time? It's inside the sql query, and it's datetime format, so general python method like 'd%' % variable doesn't work. How to solve this problem? Thanks!