import pyodbc as po
connection_string = """
driver=ODBC Driver 17 for SQL Server;
server=SHADOW-LN4F5NUO;
database=FBI_Crime_Data;
trusted_connection=True;
"""
connection = po.connect(connection_string)
输出:
Traceback (most recent call last):
File "C:Program FilesJetBrainsPyCharm 2019.3.1pluginspythonhelperspydevpydevd.py", line 1434, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:Program FilesJetBrainsPyCharm 2019.3.1pluginspythonhelperspydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"n", file, 'exec'), glob, loc)
File "C:UsersJackDesktopGitHubFBI_Crime_Data_Analysismain.py", line 133, in <module>
import_file(file_location="data/ASR122016.TXT")
File "C:UsersJackDesktopGitHubFBI_Crime_Data_Analysismain.py", line 122, in import_file
export_dataframe_to_SQL_Server(df=df, table_name=table_name)
File "C:UsersJackDesktopGitHubFBI_Crime_Data_Analysismain.py", line 40, in export_dataframe_to_SQL_Server
connection = po.connect(connection_string)
pyodbc.OperationalError: ('08001', "[08001] [Microsoft][ODBC Driver 17 for SQL Server]Invalid value specified for connection string attribute 'trusted_connection' (0) (SQLDriverConnect)")
- 服务器名称正确。
- 数据库名称正确
- ODBC 驱动程序存在。
- SSMS 中的 Windows 身份验证工作正常。
根据这个微软文档,识别的值是true
、false
、yes
、no
和sspi
pyodbc 文档说,您可以通过提供Trusted_Connection属性来使用您的 Windows 帐户而不是用户名/密码进行身份验证:Trusted_Connection=yes
因此,看起来连接字符串 sql 服务器主页显示 .Net 库的连接字符串。如果单击"ODBC 驱动程序"(如 17、13 或 11(下特定版本的驱动程序的任何页面,则会显示Trusted_Connection=yes
For [ODBC Driver 17 for SQL Server] driver
It should be like
connection_string = "
driver=ODBC Driver 17 for SQL Server;
server=SHADOW-LN4F5NUO;
database=FBI_Crime_Data;
trusted_connection=yes;
"""
For [SQL SERVER DRIVER]
It should be like :
connection_string = "
driver=SQL SERVER DRIVER;
server=SHADOW-LN4F5NUO;
database=FBI_Crime_Data;
trusted_connection=True;
"