conn=pyodbc.connect('Driver={SQL Server};'
'Server=test123;'
'Trusted_Connection=yes;')
data = []
date_x = ["202101","202010","202007"]
business_y = ["sales","marketing","tech"]
for i in date_x, business_y:
sql_query = """
Select name, period, business, purchase_date
From test_table
Where period = ? #(date_x)
AND business = ? #(business_y)
"""
conn.execute(sql_query)
data.append(pd.read_sql(sql_query, conn))
print(data)
I am trying to run this SQL statement through a loop, and each iteration I want to populate the placeholder "?" with the correct variables as shown in my example above and execute the queries, writing results to the variable data