Authentication
First, you provide both uid / pwd (SQL Server authentication) and trusted_connection (Windows authentication). Choose one, you cannot use both. I assume SQL Server authentication for the following examples.
Connection strings
Connect to a named instance instance1 using the instance name:
connSqlServer = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=192.106.0.102\instance1;DATABASE=master;UID=sql2008;PWD=password123')
Connecting to a named instance using TCP / IP using port number 1443:
connSqlServer = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=192.106.0.102,1443;DATABASE=master;UID=sql2008;PWD=password123')
Keyword Alternative
pyodbc.connect () supports keywords, I think they are easier to read, and you don't need to do string formatting if you're using variables for connection string attributes:
Named instance:
connSqlServer = pyodbc.connect(driver='{SQL Server Native Client 10.0}', server='192.106.0.102\instance1', database='master', uid='sql2008',pwd='password123')
TCP / IP Port:
connSqlServer = pyodbc.connect(driver='{SQL Server Native Client 10.0}', server='192.106.0.102,1443', database='master', uid='sql2008',pwd='password123')
Bryan source share