为连接字符串属性指定的值无效
Invalid value specified for connection string attribute
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 Files\JetBrains\PyCharm 2019.3.1\plugins\python\helpers\pydev\pydevd.py", line 1434, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2019.3.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:\Users\Jack\Desktop\GitHub\FBI_Crime_Data_Analysis\main.py", line 133, in <module>
import_file(file_location="data/ASR122016.TXT")
File "C:\Users\Jack\Desktop\GitHub\FBI_Crime_Data_Analysis\main.py", line 122, in import_file
export_dataframe_to_SQL_Server(df=df, table_name=table_name)
File "C:\Users\Jack\Desktop\GitHub\FBI_Crime_Data_Analysis\main.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 驱动程序。
- Windows SSMS 中的身份验证有效。
根据this microsoft documentation,可识别的值为true
、false
、yes
、no
和sspi
pyodbc documentation 说,您可以通过提供 Trusted_Connection 属性来使用您的 Windows 帐户而不是 username/password 进行身份验证:Trusted_Connection=yes
所以它看起来是 connectionstrings sql server main page shows you the connection string for .Net libraries. If you click on any of the pages for a specific version of the driver under "ODBC drivers" (like 17 or 13 or 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;
"
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 Files\JetBrains\PyCharm 2019.3.1\plugins\python\helpers\pydev\pydevd.py", line 1434, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2019.3.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:\Users\Jack\Desktop\GitHub\FBI_Crime_Data_Analysis\main.py", line 133, in <module>
import_file(file_location="data/ASR122016.TXT")
File "C:\Users\Jack\Desktop\GitHub\FBI_Crime_Data_Analysis\main.py", line 122, in import_file
export_dataframe_to_SQL_Server(df=df, table_name=table_name)
File "C:\Users\Jack\Desktop\GitHub\FBI_Crime_Data_Analysis\main.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 驱动程序。
- Windows SSMS 中的身份验证有效。
根据this microsoft documentation,可识别的值为true
、false
、yes
、no
和sspi
pyodbc documentation 说,您可以通过提供 Trusted_Connection 属性来使用您的 Windows 帐户而不是 username/password 进行身份验证:Trusted_Connection=yes
所以它看起来是 connectionstrings sql server main page shows you the connection string for .Net libraries. If you click on any of the pages for a specific version of the driver under "ODBC drivers" (like 17 or 13 or 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;
"