Connection string to SQL server with Python pypyodbc

Tal picture Tal · Jun 13, 2017 · Viewed 17.4k times · Source

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?

Answer

Michael Malone picture Michael Malone · Sep 7, 2018

If you're on Linux or macOS then you'll need to install the MS ODBC Driver, available on their website https://docs.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=..."