I have a SQL Server 2017 database table with 10,000+ records with an EmailAddress
column that needs to be updated for every row in the table incremented by 1 (i.e. a+1@email.com
, a+2@email.com
up to a+10000@email.com
).
I'm thinking the easiest way to do this is with Python using pypyodbc
SQLCommand
and a for loop. Additionally, the SET value in the SQL command needs to retrieve from a xlsx
file using xlrd
that will go through each row and update the value of SET.
Other than scouring the web for answers, I've tried changing the code many different ways but can't seem to find an answer to this issue.
import pypyodbc
import xlrd
connection = pypyodbc.connect('Driver=SQL Server;'
'Server=DESKTOP-YourMomsServer\SQLEXPRESS;'
'Database=testtime;'
'Trusted_Connection=yes;'
)
cursor = connection.cursor()
xlf = xlrd.open_workbook("testdatafile.xlsx")
sh = xlf.sheet_by_name("Sheet1")
SQLCommand = ("SELECT * FROM anothertest")
cursor.execute(SQLCommand)
for val in cursor:
a, b = val
for i in range(sh.nrows):
c, d = sh.row_values(i)
SQLCommmand = ("UPDATE anothertest SET test =" d "WHERE "c = a)
cursor.execute(SQLCommand)
#print(a,b)
connection.commit()
connection.close()
The expected results is that the script would loop through the entire result set starting with Row-1 up to the last row- Row-10000 and update the EmailAddress
column that is read from the xlsx file.
Error
"invalid syntax" in the SQLCommand