Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyodbc- connection failure to SQL Server

I have been trying to connect to Microsoft SQL Server. I have an ODBC connection set up and the test is successful. I am not using Windows Authentication to connect to SQL Server but it keep getting this error:

Cannot be used with Windows authentication

InterfaceError: ('28000', '[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. (18452) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. (18452); [28000] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')

Here is my code:

import pyodbc
cnxn = pyodbc.connect(Driver='{SQL Server}',
                      Server='servername.abc.xyz.co.com',
                      username = 'user_xyz', 
                      password = 'abcdfgh')

I am using Windows 7. Please help me debug this problem

Thanks

like image 728
Blue Avatar asked Jun 27 '18 02:06

Blue


People also ask

How do I fix ODBC connection failed in SQL Server?

Resolving The Problem. When you create the ODBC connection to SQL server, select the option of With SQL Server authentication using a login ID and password entered by the user. Also, confirm that in Microsoft Access, the Machine Data Source and System Data Source has been used to create the ODBC connection.


2 Answers

I was able to solve this by defining the dsn connection as below:

dsn="DRIVER={SQL 
SERVER};server=ip_address_here;database=db_name_here;uid=user;pwd=password"

This worked and I was able to connect and query the sql server.

like image 52
Blue Avatar answered Sep 25 '22 05:09

Blue


This is how I do it and it works:

import pyodbc 

server_name = "server_name"
db_name = "db_name"

server = "Server="+str(server_name)
db = "Database="+str(db_name)
key = "Driver={SQL Server Native Client 11.0};"+server+";"+db+";"+"Trusted_Connection=yes;"

cnxn = pyodbc.connect(key)
like image 22
gripep Avatar answered Sep 24 '22 05:09

gripep