insert statements

This commit is contained in:
Bartal Læarsson
2026-02-16 14:37:03 +00:00
parent 1c18cb01dd
commit 072c162f2b
3 changed files with 113 additions and 0 deletions
+45
View File
@@ -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()
+46
View File
@@ -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()
+22
View File
@@ -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()