0

I'm using this connection string but something is wrong.

conn = pyodbc.connect
(DRIVER={SQL Server};SERVER=localhost;DATABASE=test;UID=YYY;PWD=XXXX)
connection.close()

How can I do this?

4 Answers 4

4

If you're on Linux or macOS then you'll need to install the MS ODBC Driver, available on their website https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017

Then use the Driver String {ODBC Driver 17 for SQL Server}

connection_string = "DRIVER={ODBC Driver 17 for SQL Server};Server=..."
Sign up to request clarification or add additional context in comments.

3 Comments

If you're on Windows, the procedure is the same actually, as the DRIVER={SQL Server} is a legacy driver from pre-2005 era. While it has the obvious benefit of being shipped with Windows, it's not recommended for new development.
@Nickolay I specified that this answer was only for Linux or MacOS, not Windows
and I commented that it's most appropriate for users on Windows as well...
1

Assuming you are on MacOS, firstly use homebrew to install msodbcsql:

brew tap microsoft/msodbcsql https://github.com/Microsoft/homebrew-msodbcsql
brew install msodbcsql

Then in Python:

If you don't have pypyodbc installed then use pip to install it:

pip install pypyodbc

Then create a Python script to connect to the db:

import pypyodbc as pyodbc

cnxn = pyodbc.connect("DRIVER={ODBC Driver 13 for SQL Server};"
                  "SERVER=localhost;"
                  "DATABASE=test;"
                  "UID=YYY;"
                  "PWD=XXX;"
                  "TrustServerCertificate=no;"
                  "Connection Timeout=60")

1 Comment

Getting error pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 13 for SQL Server' : file not found (0) (SQLDriverConnect)"). Any idea what is wrong
-1

try this

import pypyodbc

connection = pypyodbc.connect("DRIVER={SQL Server}; Server = servername;DATABASE=MyDatabase;Trusted_Connection = Yes")

Comments

-2
import pypyodbc
connection = pypyodbc.connect('Driver={SQL Server};'
  'Server=localhost;'
  'Database=test;'
  'uid=YYY;pwd=XXX') 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.