From 072c162f2b6c72584446b55904733f5ebfe38c1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartal=20L=C3=A6arsson?= Date: Mon, 16 Feb 2026 14:37:03 +0000 Subject: [PATCH] insert statements --- insert_no_param_tsql.py | 45 ++++++++++++++++++++++++++++++++++++++ insert_with_param_tsql.py | 46 +++++++++++++++++++++++++++++++++++++++ template | 22 +++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 insert_no_param_tsql.py create mode 100644 insert_with_param_tsql.py create mode 100644 template diff --git a/insert_no_param_tsql.py b/insert_no_param_tsql.py new file mode 100644 index 0000000..2baf8a3 --- /dev/null +++ b/insert_no_param_tsql.py @@ -0,0 +1,45 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + print("") + print("select statement before insert") + cursor.execute("select * from tblCustomers") + + while 1: + row = cursor.fetchone() + if not row: + break + print(row.id, row.code, row.firstName, row.lastName) + + print("inserting into tblCustomers...") + cursor.execute("insert into tblCustomers values (6, 'code6', 'firstName6', 'lastName6')") + connection.commit() + + print("select statement after insert") + 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() diff --git a/insert_with_param_tsql.py b/insert_with_param_tsql.py new file mode 100644 index 0000000..3ac16a1 --- /dev/null +++ b/insert_with_param_tsql.py @@ -0,0 +1,46 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + print("") + print("select statement before insert") + 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 + prm_code = "code7" + prm_first_name = "firstName7" + prm_last_name = "lastName7" + print("inserting into tblCustomers...") + cursor.execute("insert into tblCustomers values (?, ?, ?, ?)", prm_id, prm_code, prm_first_name, prm_last_name) + connection.commit() + + print("select statement after insert") + 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() diff --git a/template b/template new file mode 100644 index 0000000..143bdf6 --- /dev/null +++ b/template @@ -0,0 +1,22 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + + + + cursor.close() + connection.close() + +except pyodbc.Error as ex: + print() + print("exception: ",ex) + cursor.close() + connection.close() + print() + exit() + +print()