Tuesday, November 11, 2025

Inserting a record with pyodbc

Here is the Python code to insert a new record into the bookstore database using pyodbc. We have used a parameterized query again. The key statement is the cursor.execute() with two arguments - the SQL statement and the parameter list. The SQL statement lists the required fields we are going to insert into and a list of ?'s in the value list, one for each of the four parameters. The execute method has the SQL statement as one argument and a tuple with the actual values as the second. The default is not to commit the record so you have to use the commit method to persist the record.


The following shows the first few records in the database after the insert.





No comments:

Post a Comment