I am trying to extract data from a SQL Server Database using pypyodbc. But it appears my code breaks when I try to construct the SELECT Statement using
myCursor.execute(SQLCommand,values)
Can anyone spot an issue and point me in the right direction?
import pypyodbc
try:
myConnection = pypyodbc.connect('Driver={SQL Server};'
'Server=THINKPAD\STEVE_DEVELOPER;'
'Database=PythonTest;'
'uid=sa; pwd=passwordCC')
myCursor = myConnection.cursor()
print("Connection Made")
SQLCommand =("SELECT First_Name, Date FROM [PythonTest].[dbo].[Names] WHERE First_Name =?")
values = ['Mike']
print("SQL command elements Created")
#After this is where it falls over
myCursor.execute(SQLCommand,values)
print("SQL statement constructed ")
results = myCursor.fetchone()
print(results[0])
print("Sucessfully retreive record")
myconnection.close()
except:
print('Record NOT sucessfully retreived')
Cheers Steve
In Python, exceptions are your friend. The traceback tells you where things went wrong, and the exception usually (hopefully) tells you what went wrong.
Suppressing all exceptions using except:
is (almost) always a bad idea - if you catch an exception, you should know what you're expecting to catch and how to deal with it; if you don't, you usually want to let it go to the next handler out, which will either handle it or show the traceback (or both).