Files
mssql-interactions-python/basic-sql-server-interactions-and-dml/update_no_param_tsql.py
T

40 lines
940 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)
cursor.execute("update tblCustomers set lastName = 'lastName New 5' where id = 5")
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()