dmls are done!
This commit is contained in:
@@ -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()
|
||||||
@@ -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()
|
||||||
@@ -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()
|
||||||
@@ -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()
|
||||||
@@ -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()
|
||||||
Reference in New Issue
Block a user