0

Hi apologies if this is a dumb question, super new to python.

Context: I have a dataframe with values that need to be updated into a table in a database. The idea is to loop through each row, pass a required value from each row to the SQL string, execute sql string.

What i have tried so far:

sales_output = merge_view[['SALES_FORECAST_ID','SALES_DATE','WEEK_NO','ADJUSTED_BASE']]
cursor = sql_connection.cursor()

for row in sales_output:
       sales = row['ADJUSTED_BASE']
       fid = row['SALES_FORECAST_ID']
       date = row['SALES_DATE']

print('''UPDATE a SET a.WEEKLY_SALES =? FROM MA.SALES_FORECAST_VW a WHERE a.SALES_FORECAST_ID =? AND 
SALES_DATE =?''',(sales,fid,date))

I am using a print statement to test if this is producing the correct SQL string to update.

This doesn't work as i get a TypeError: string indices must be, i think i understand why. you cannot select columns like that in a for loop but I'm not sure what the alternative is. I've done some googling but I think I may have confused myself.

Any help is much appreciated.

Cheers

tpbrenna
  • 1
  • 1

0 Answers0