问题
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!
回答1:
Consider SQLAlchemy to connect pandas and use the params argument of pandas.read_sql to bind variable to SQL statement:
from sqlalchemy import create_engine
engine = create_engine("username/password@host:1521/sid")
sql_select_statement = "SELECT * FROM TABLE WHERE DATE BETWEEN :my_date AND sysdate-1"
my_var = '20-oct-2017'
df = pd.read_sql(sql_select_statement, engine, params={'my_date':my_var})
Alternatively, continue to use the raw connection with parameterization:
sql_select_statement = "SELECT * FROM TABLE WHERE DATE BETWEEN :my_date AND sysdate-1"
my_var = '20-oct-2017'
df = pd.read_sql(sql_select_statement, conn, params={'my_date':my_var})
来源:https://stackoverflow.com/questions/47043508/set-a-date-variable-to-sql-query-in-python