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?
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=..."
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.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")