Domanda

I have been battling the \ escape character in my attempt to connect to an MS SQL database (that I have successfully connected to via tsql and isql) via pyodbc on a Linux Workstation and have decided to post it here in hopes somebody can help me out!

My UID for the SQL server includes both the domain and my username in the form domain\username. I have tried escaping the backslash with both \\ and r'...' (plus the methods listed below and many other absurd attempts I didn't bother documenting), and both escape the string entirely to give a blank entry.

The details of the attempts and errors are as follows. If anyone has an idea of what is wrong with my syntax or if this is an indication of a much larger problem, I would greatly appreciate your insight!

Attempted connection strings:

cnxn_str = 'DSN=sqlserverdatasource;TDS_Vers=8.0;Port=1433;UID=domain\\username'
cnxn_str = r'DSN=sqlserverdatasource;TDS_Vers=8.0;Port=1433;UID=domain\username'
cnxn_str = 'DSN=sqlserverdatasource;TDS_Vers=8.0;Port=1433;UID=domain\\\\username'
cnxn_str = ''.join(['DSN=sqlserverdatasource;TDS_Vers=8.0;Port=1433;UID=domain','\\','username'])
cnxn_str = 'DSN=sqlserverdatasource;TDS_Vers=8.0;Port=1433;UID=domain/username'

Attempted connection and resulting error:

cnxn = pyodbc.connect(cnxn_str)
Connection string configurations that fail with
pyodbc.ProgrammingError: ('42000', "[42000] [unixODBC][FreeTDS][SQL Server]Login failed for user ''. The user is not associated with a trusted SQL Server connection. (18452) (SQLDriverConnect)") :
È stato utile?

Soluzione 2

TryoLabs's tutorial Connecting to an MS SQL Server database from Python under Ubuntu provided the syntax included below which has served to reliably establish MS SQL connection from the Python application.

dsn = 'sqlserverdatasource'
user = '<domainname>\\<username>'
password = '<password>'
database = '<dbname>'

cnxn_str = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (dsn,user,password,database)
conn = pyodbc.connect(cnxn_str)
cursor = conn.cursor()

Altri suggerimenti

Since it appears you're trying to use integrated authentication, take a look at the Microsoft ODBC Driver for SQL Server on Linux.

Otherwise, do away with the DSN and supply a password:

cnxn_str = "DRIVER={FreeTDS};SERVER=DNS;PORT=1433;TDS_Version=8.0;UID=domain\\username;PWD=password"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top