The requirement is to pass the parameters in a list to the sql query present in python code
parmlist.py
sale_call = [12,88]
yr_range = [2015,2016,2017,2018]
Codefile.py
conn = <connecting to MySQL>
cursor = conn.cursor()
cursor.execute(‘Insert into sales_repo select distinct a.yr_sll from cust_data a join proc_data b
On a.prd_key = b.prd_key where b.range1=?,b.range2=?,b.range3=? and b.yr in ?’)
I did the following:
cursor.execute(‘Insert into sales_repo select distinct a.yr_sll from cust_data a join proc_data b
On a.prd_key = b.prd_key where b.range1=?,b.range2=?,b.range3=? and b.yr in ?’ ,parmist.sale_call[0],parmist.sale_call[1],parmist.yr_range[3])
But it seems that the parmist.yr_range[3] is just taking 2018.
Instead of taking whole list of yr_range = [2015,2016,2017,2018]
It's just taking the last value i.e. 2018
How can I pass the whole list as a variable in the query?
Update_1:
I tried the following:
sale_call = [12,88]
yr_range = [2015,2016,2017,2018]
cursor.execute(‘Insert into sales_repo select distinct a.yr_sll from cust_data a join proc_data b
On a.prd_key = b.prd_key where b.range1=?,b.range2=?,b.range3=? and b.yr in (' + ','.join(map(str, yr_range))’)
The above is not working when I tried to execute it through the python code. But executing when using pandas as:
pd.read_sql_query(‘Insert into sales_repo select distinct a.yr_sll from cust_data a join proc_data b
On a.prd_key = b.prd_key where b.range1=?,b.range2=?,b.range3=? and b.yr in (' + ','.join(map(str, yr_range))’,conn)
Any hint why it's not working?
parmist.yr_range[3]the[3]specifies the fourth item. Tryparmist.yr_range. - docs.python.org/3/tutorial/introduction.html#lists2015 <= b.range <= 2018Because if so then you don't need to look for every matching value, only those greater then the smallest year and less then the largest year.?'s seeming like you want to pass in parameters to the query, but you never actually specify what those paremeters are supposed to be.