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

3
  • What SQL library are you using? Commented Apr 20, 2020 at 9:52
  • Does this answer your question? Python MYSQL update statement Commented Apr 20, 2020 at 9:52
  • Yeah sorry i should have mentioned that, i am using Microsoft SQL Server. Commented Apr 20, 2020 at 23:15

0

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.