1

I want to send start date and end date value into my sql query from 2 separate variables. Suppose, I have start_date = '2020-05-14' and end_date = '2020-07-08' stored in a variable. Now my query is:

db_connection_str = 'mysql+pymysql://username:password@host/table_name'
db_connection = create_engine(db_connection_str)
def myfunc(start_date, end_date):
    sql_syn = "SELECT col_id, col_a, col_b, date, FROM en_stat where date between :start_date and :end_date"
    sql_df = pd.read_sql(sql_syn, con=db_connection, chunksize=100)

how to pass the start_date and end_date values dynamically for this particular code.

1 Answer 1

3

Use python3 f-strings:

def myfunc(start_date, end_date):
    sql_syn = f"SELECT col_id, col_a, col_b, date, FROM en_stat where date between {start_date} and {end_date}"
    sql_df = pd.read_sql(sql_syn, con=db_connection, chunksize=100)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.