diff --git a/delete_no_param_tsql.py b/delete_no_param_tsql.py new file mode 100644 index 0000000..b0f83ba --- /dev/null +++ b/delete_no_param_tsql.py @@ -0,0 +1,43 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + print("select before deleteing...") + + cursor.execute("select * from tblCustomers") + + while 1: + row = cursor.fetchone() + if not row: + break + print(row.id, row.code, row.firstName, row.lastName) + + cursor.execute("delete from tblCustomers where id = 2") + connection.commit() + + print("select after deleting") + 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/delete_with_param_tsql.py b/delete_with_param_tsql.py new file mode 100644 index 0000000..10269bd --- /dev/null +++ b/delete_with_param_tsql.py @@ -0,0 +1,44 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + print("select before deleteing...") + + cursor.execute("select * from tblCustomers") + + while 1: + row = cursor.fetchone() + if not row: + break + print(row.id, row.code, row.firstName, row.lastName) + + prm_code = "code6" + cursor.execute("delete from tblCustomers where code = ?", prm_code) + connection.commit() + + print("select after deleting") + 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/other_queries_tsql.py b/other_queries_tsql.py new file mode 100644 index 0000000..a38fb55 --- /dev/null +++ b/other_queries_tsql.py @@ -0,0 +1,58 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + + # wuery 1 + print("counting all records in table...") + cursor.execute("select count(*) as total_records from tblCustomers") + + + while 1: + row = cursor.fetchone() + if not row: + break + print(row.total_records) + print("--- next query ----") + + # query 2 + print("fetching server name...") + cursor.execute("select @@SERVERNAME as server_name") + + while 1: + row = cursor.fetchone() + if not row: + break + print(row.server_name) + print("--- next query ----") + + # query 3 + print("some DMV stuff...") + cursor.execute("select top 10 wait_type, wait_time_ms from sys.dm_os_wait_stats where wait_time_ms > 5 order by wait_time_ms desc") + + while 1: + row = cursor.fetchone() + if not row: + break + print(row.wait_type, row.wait_time_ms) + + print("--- no more queries ... ----") + + + + + cursor.close() + connection.close() + +except pyodbc.Error as ex: + print() + print("exception: ",ex) + cursor.close() + connection.close() + print() + exit() + +print() diff --git a/update_no_param_tsql.py b/update_no_param_tsql.py new file mode 100644 index 0000000..7b3d6a5 --- /dev/null +++ b/update_no_param_tsql.py @@ -0,0 +1,39 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + print("select table before updating") + + cursor.execute("select * from tblCustomers") + while 1: + row = cursor.fetchone() + if not row: + break + print(row.id, row.code, row.firstName, row.lastName) + + cursor.execute("update tblCustomers set lastName = 'lastName New 5' where id = 5") + connection.commit() + + print("table after updating") + 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/update_with_param_tsql.py b/update_with_param_tsql.py new file mode 100644 index 0000000..33eadd3 --- /dev/null +++ b/update_with_param_tsql.py @@ -0,0 +1,40 @@ +import pyodbc + +try: + connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes') + + cursor = connection.cursor() + + print("select table before updating") + + 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 + cursor.execute("update tblCustomers set lastName = 'lastName New 7' where id = ?", prm_id) + connection.commit() + + print("table after updating") + 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()