Modifying Data
The following code examples show how to use data modification statements.
Procedure for Updates
You can use execute method to execute data modification statements. This will update the cursor's rowcount property with the number of affected rows.
Note that you must commit the connection after performing the modification, or your changes may be lost.
INSERT
To insert updates:
cursor.execute("INSERT INTO [master].[dbo].Products (ProductName) VALUES ('Konbu')") cnxn.commit() affected = cursor.rowcount
UPDATE
To retrieve updates:
cursor.execute("UPDATE [master].[dbo].Products SET ProductName = 'Konbu' WHERE ProductName = 'Konbu'") cnxn.commit() affected = cursor.rowcount
DELETE
To delete updates:
cursor.execute("DELETE FROM [master].[dbo].Products WHERE Id = '22'") cnxn.commit() affected = cursor.rowcount