Search code examples
pythonsql-serverpycharmpyodbc

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)

Output:

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)")
  • Server name is correct.
  • Database name is correct
  • ODBC driver exists.
  • Windows Authentication in SSMS works.

Solution

  • According to this microsoft documentation, recognized values are yes and no

    pyodbc documentation says, you can use your Windows account for authentication instead of username/password by providing Trusted_Connection attribute as: Trusted_Connection=yes

    So it looks the 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), it shows Trusted_Connection=yes