0

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

Steve
  • 475
  • 4
  • 12
  • 25
  • Take out the `try` ... `except` and show us the traceback. – cco Nov 12 '16 at 10:27
  • OK - varchar incompatibility with the = operator. Sorted substituting = with a "LIKE". Thanks for the pointer re removing the "try / except" to view the traceback. Newbie learning :) – Steve Nov 12 '16 at 10:44

1 Answers1

1

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).

cco
  • 5,873
  • 1
  • 16
  • 21