Connecting to SQL-Server

Grzegorz Makarewicz mak at
Fri Oct 8 07:46:08 CEST 2004

Greg Lindstrom wrote:
> Hello-
> I'm running Python 2.3 on a Windows XP box and am trying to get connected to
> an MS SQL-Server.  I have found (seemingly) conflicting methods to connect
> using the odbc module in the win32-all extensions.  I have seen both of the
> following:
> db = odbc.odbc('DSN/UID/PASSWORD)
> and
> db=odbc.odbc('DSN=dsn;UID=uid;PWD=password)
> I can connect when I use my computer, but when I transfer the code to a
> production box I am greeted with an error that the login is not associated
> with a trusted connection.  So which, if either, of the above methods should
> I use and what is the error about a trusted connection trying to tell me?


Trusted connection means user name and password are taken from windows 
login dialog and this information must be equal to login info stored on 
machine where sql server resides. Untrusted connection (SQL server 
authentication) requires account in sql databases - like 'sa' user.

DSN can be taken from odbc control panel, or can be constructed from 
scratch like in this snippet:

def MakeSQLServerDSN(ip, database='master', u=None, p='', port=1433):
	dsn = [
		'Driver={SQL Server}',
		'Server=%s' %ip,
		'Address=%s,%d' %(ip,port),
		'Database=%s' %database,
	if u is None:
		dsn.append('Uid=%s;Pwd=%s' %(u, p))
	return ';'.join(dsn)

untrusted connection:
dsn = dsnSQLServer('', 'master', 'sa', 'password', 1433)

trusted connection:
dsn = dsnSQLServer('', 'master')


More information about the Python-list mailing list