简体   繁体   English

修复在 Python 中连接 Microsoft SQL 服务器时出现“用户登录失败..”错误

[英]Fix "login failed for user.." error in connecting Microsoft SQL server in Python

I am receiving a very common error while trying to connect Microsoft SQL Server using pyodbc .我在尝试使用pyodbc连接 Microsoft SQL Server 时收到一个非常常见的错误。 I've tried the solutions suggested here before, but none of them solved my problem.我之前尝试过这里建议的解决方案,但没有一个能解决我的问题。

import pyodbc 
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server= server_name;'
                      'Database= database_name;'     
                      'UID= user_id;'
                      'PWD= password;')


InterfaceError: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]
Login failed for user ' '. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver]
[SQL Server]Login failed for user ' '. (18456)")

When I connect the database via Microsoft SQL Server Management Studio 18 app, below is how I connect.当我通过Microsoft SQL Server Management Studio 18应用程序连接数据库时,以下是我的连接方式。 I copy and paste server name, login, and password information to my Python code as shown above.我将服务器名称、登录名和密码信息复制并粘贴到我的 Python 代码中,如上所示。 My OS is Windows 10. When I call select @@version , I get Microsoft SQL Server 2014 (SP3-GDR) (KB4583463)...我的操作系统是 Windows 10。当我调用select @@version时,我得到Microsoft SQL Server 2014 (SP3-GDR) (KB4583463)...

在此处输入图像描述

Admittedly my python skills are not the best but I'm suspicious of all the single quotes in your connection string.诚然,我的 python 技能不是最好的,但我怀疑你的连接字符串中的所有单引号。

In the error message Login failed for user ' ' is that blank value for the use what you actually see or have you stripped that out?在错误消息Login failed for user ' '中,使用的空白值是您实际看到的还是您已将其删除? If that's what you actually see then I think your connection string is malformed.如果这是您实际看到的,那么我认为您的连接字符串格式不正确。

Refer to the sample here for how to build out a connection string.有关如何构建连接字符串的信息,请参阅此处的示例。

暂无
暂无

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

相关问题 使用 Python 连接到 SQL 服务器时,用户 '' 登录失败 - Login failed for user '' when connecting to SQL Server using Python 在 Python 中连接到 Microsoft SQL Server - Connecting to Microsoft SQL Server in Python 如果用户登录失败,则 Python SQL Server 登录重试逻辑 - Python SQL Server login retry logic if login failed for user 无法连接到数据库。 (pyodbc.Error) [Microsoft][用于 SQL 服务器的 ODBC 驱动程序 17]用户“%user%”登录失败。 (18456) (SQLDriverConnect) - Could not connect to the database. (pyodbc.Error) [Microsoft][ODBC Driver 17 for SQL Server]Login failed for user '%user%'. (18456) (SQLDriverConnect) 使用 Python 连接到 Microsoft SQL 服务器 - Connecting to Microsoft SQL server using Python 将 python 3.3 连接到 microsoft sql server 2008 - Connecting python 3.3 to microsoft sql server 2008 从SQL Server执行Python脚本时,用户登录失败 - Login failed for user while executing Python script from SQL Server 登录失败 (pyodbc.InterfaceError) ('28000', "[28000] [Microsoft][ODBC SQL 服务器驱动程序][SQL Server]用户 'xxxx' 登录失败 - Logon failed (pyodbc.InterfaceError) ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'xxxx' 在Ubuntu上通过pyODBC连接到Microsoft SQL Server - Connecting to Microsoft SQL Server through pyODBC on Ubuntu 连接到Outlook IMAP时发生Python错误-引发self.error(dat [-1])错误:登录失败 - Python error when connecting to Outlook IMAP - raise self.error(dat[-1]) error: LOGIN failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM