Tuesday, November 18, 2025

Using pyodbc to update a record

This post shows how to use pyodbc to update a record. We are continuing with the bookstore case, updating a book price in our SQL Server database. The SQL syntax is simple:

update <table name>
set <field name> = value
where <field name> = value;

There are two items to pay particular attention to in the screenshot below: the placeholders in the sql statement denoted by ? and the params variable. Note that params is just a common variable name not a keyword. When the SQL statement is executed, the placeholders will be replaced by actual values from the params variable. Params must be a tuple with values in the same order as the placeholders. The values are matched to the placeholder by position, not name.


The following is the result. We updated the retail price of The Black Tulip by Alexandre Dumas. Note once again that a commit would need to be issued to persist the update.





No comments:

Post a Comment