select with param
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
import pyodbc
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import pyodbc
|
||||
|
||||
try:
|
||||
|
||||
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes')
|
||||
|
||||
cursor = connection.cursor()
|
||||
|
||||
prmID = 5
|
||||
|
||||
cursor.execute("select * from tblCustomers where id = ?",prmID)
|
||||
|
||||
print("[Query results...]")
|
||||
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