Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to MS SQL Server with Windows Authentication using Python?

How do I connect MS SQL Server using Windows Authentication, with the pyodbc library?

I can connect via MS Access and SQL Server Management Studio, but cannot get a working connection ODBC string for Python.

Here's what I've tried (also without 'Trusted_Connection=yes'):

pyodbc.connect('Trusted_Connection=yes',                driver='{SQL Server}', server='[system_name]',                database='[databasename]')  pyodbc.connect('Trusted_Connection=yes', uid='me',                driver='{SQL Server}', server='localhost',                database='[databasename]')  pyodbc.connect('Trusted_Connection=yes',                driver='{SQL Server}', server='localhost',                uid='me', pwd='[windows_pass]', database='[database_name]')  pyodbc.connect('Trusted_Connection=yes',                driver='{SQL Server}', server='localhost',                database='[server_name]\\[database_name]')  pyodbc.connect('Trusted_Connection=yes',                driver='{SQL Server}', server='localhost',                database='[server_name]\[database_name]')  pyodbc.connect('Trusted_Connection=yes',                driver='{SQL Server}',                database='[server_name]\[database_name]') 
like image 554
stackoverflowuser95 Avatar asked May 13 '13 05:05

stackoverflowuser95


People also ask

How do I connect to SQL Server using Windows Authentication?

Open SQL Server Management Studio. In Connect to Server, select Database Engine, enter your SQL Server name, and enter administrator credentials to connect to the server. Select Connect. In Object Explorer, expand the SQL Server, expand Security, right-click Logins, and then select New Login.

Can a SQL Server login use Windows Authentication?

A connection made using Windows Authentication is sometimes called a trusted connection, because SQL Server trusts the credentials provided by Windows. By using Windows Authentication, Windows groups can be created at the domain level, and a login can be created on SQL Server for the entire group.


1 Answers

You can specify the connection string as one long string that uses semi-colons (;) as the argument separator.

Working example:

import pyodbc cnxn = pyodbc.connect(r'Driver=SQL Server;Server=.\SQLEXPRESS;Database=myDB;Trusted_Connection=yes;') cursor = cnxn.cursor() cursor.execute("SELECT LastName FROM myContacts") while 1:     row = cursor.fetchone()     if not row:         break     print(row.LastName) cnxn.close() 

For connection strings with lots of parameters, the following will accomplish the same thing but in a somewhat more readable way:

conn_str = (     r'Driver=SQL Server;'     r'Server=.\SQLEXPRESS;'     r'Database=myDB;'     r'Trusted_Connection=yes;'     ) cnxn = pyodbc.connect(conn_str) 

(Note that there are no commas between the individual string components.)

like image 199
Gord Thompson Avatar answered Oct 11 '22 07:10

Gord Thompson