I have a flask application that connects to a database on SQL Server which I am running on windows. I am using an Active Directory Service account for establishing the connection and this is how my connection string is -
# connect_db function is used to connect with the database for read and write purposes
def connect_db():
connection = pyodbc.connect(r"Driver={SQL Server Native Client 11.0};Server=server,port;Database=db;UID=domain\username;PWD=password;", autocommit=True)
return connection
Here is the error message -
pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'domain\\username'. (18456) (SQLDriverConnect); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'domain\\username'. (18456)")
Looks like the string is not escaping the \
in the UID
I have tried \\
and also used r'
as a raw string at the beginning of the string but it does not work.
I cannot use runas
as I would like the web application to be deployed on a server in the future. When I use the same account on SSMS I am able to login successfully.