Files
mssql-interactions-python/update_with_param_tsql.py
T
2026-02-16 15:12:09 +00:00

41 lines
963 B
Python

import pyodbc
try:
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes')
cursor = connection.cursor()
print("select table before updating")
cursor.execute("select * from tblCustomers")
while 1:
row = cursor.fetchone()
if not row:
break
print(row.id, row.code, row.firstName, row.lastName)
prm_id = 7
cursor.execute("update tblCustomers set lastName = 'lastName New 7' where id = ?", prm_id)
connection.commit()
print("table after updating")
cursor.execute("select * from tblCustomers")
while 1:
row = cursor.fetchone()
if not row:
break
print(row.id, row.code, row.firstName, row.lastName)
cursor.close()
connection.close()
except pyodbc.Error as ex:
print()
print("exception: ",ex)
cursor.close()
connection.close()
print()
exit()
print()