I'm trying to connect to SQL Server and run a query using the following code in Python:
import pyodbc
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server = server_name;"
"Database = database_name;"
"UID = user;"
"PWD = password;")
cursor = cnxn.cursor()
cursor.execute('SELECT TOP 10 [column] FROM [table]')
for row in cursor:
print('row = %r' % (row,))
I'm getting the following error:
Traceback (most recent call last):
File "filename", line 3, in <module>
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
pyodbc.Error: ('28000', "[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'username'. (18456) (SQLDriverConnect)")
("filename" and "username" inserted above as placeholders)
This is the same error, regardless of what I change the SQL Server username and password to in the code, and the user in the error is my windows login username.
I've also tried replacing UID and PWD with:
"Trusted_connection=yes"
... to no avail. I get the exact same error back.
I've tried several solutions for similar posts on Stackoverflow and elsewhere but no luck. Ideas to the problem or an alternative means of connecting to the database would be appreciated.
Thanks so much