简体   繁体   English

pyodbc-与SQL Server的连接失败

[英]pyodbc- connection failure to SQL Server

I have been trying to connect to Microsoft SQL Server. 我一直在尝试连接到Microsoft SQL Server。 I have an ODBC connection set up and the test is successful. 我已经建立了ODBC连接,并且测试成功。 I am not using Windows Authentication to connect to SQL Server but it keep getting this error: 我没有使用Windows身份验证来连接到SQL Server,但它不断出现此错误:

Cannot be used with Windows authentication 不能与Windows身份验证一起使用

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)') InterfaceError :(“ 28000”,“ [28000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]登录失败。登录来自不受信任的域,不能与Windows身份验证一起使用。(18452)(SQLDriverConnect); [ 28000] [Microsoft] [ODBC SQL Server驱动程序]无效的连接字符串属性(0); [28000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]登录失败,该登录来自不受信任的域,不能与Windows身份验证(18452); [28000] [Microsoft] [ODBC SQL Server驱动程序]无效的连接字符串属性(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 我正在使用Windows7。请帮助我调试此问题

Thanks 谢谢

I was able to solve this by defining the dsn connection as below: 我可以通过如下定义dsn连接来解决此问题:

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. 这行得通,我能够连接和查询sql server。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM