0

I am trying to use the Microsoft SQL server as my database of django project but when I am trying to connect it it give me a SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 9-10: truncated \uXXXX escape . The issue is with 'USER': 'INDIANLEO\user',.

Here is the database connection

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'ENR',
        'HOST': 'INDIANLEO',
        'USER': 'INDIANLEO\user',
        'PASSWORD': '',

        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
        }
    }
}

Here is the server name and details:

enter image description here

enter image description here

indianLeo
  • 153
  • 1
  • 11
  • Does this answer your question? [“Unicode Error ”unicodeescape" codec can't decode bytes… Cannot open text files in Python 3](https://stackoverflow.com/q/1347791/2029983) – Thom A Feb 08 '21 at 12:35
  • Side note, when using a trusted connection, you don't pass the username and password though; so the user and password options shouldn't even be there (and thus the error will disappear and there won't be a unicode escape codec). – Thom A Feb 08 '21 at 12:36
  • It now shows incorrect password or id – indianLeo Feb 08 '21 at 12:36
  • See my second [comment](https://stackoverflow.com/questions/66101720/unicode-error-when-trying-to-connect-to-microsoft-sql-server-19#comment116867218_66101720). – Thom A Feb 08 '21 at 12:37
  • should it be blank? – indianLeo Feb 08 '21 at 12:37
  • To quote myself... *"so the user and password options **shouldn't even be there**"* – Thom A Feb 08 '21 at 12:38

1 Answers1

0

It's because of the backslash in your username. Python interprets \u as a special character instead of \ then u.

You should provide a raw string r"INDIANLEO\user" or double backslashes INDIANLEO\\user.

Any way, you should use a trusted connection so that the connection uses the process security context to authenticate, so that you wouldn't need to provide the credentials yourself.

Dan Yishai
  • 726
  • 3
  • 12