Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Database config for Django and SQL Server - escaping the host

$
0
0

I have setup my Django app to use Microsoft SQL Server database. This is my database config.

 DATABASES = {'default': {'ENGINE': 'mssql','NAME': "reporting",'HOST': '192.168.192.225\SQL2022;','PORT': 1433,'USER': "sa",'PASSWORD': "Root_1421",'OPTIONS': {'driver': 'ODBC Driver 17 for SQL Server',        },    }}

The SQL Server database is installed on my desktop machine and DESKTOP-RC52TD0\SQL2022 is the host\instance name. When print my config, I get the following.

{'default': {'ENGINE': 'mssql', 'NAME': 'reporting', 'HOST': 'DESKTOP-RC52TD0\\SQL2022', 'USER': 'sa', 'PASSWORD': 'Root_1421', 'OPTIONS': {'driver': 'ODBC Driver 17 for SQL Server'}}}

Notice that in HOST, the value has two slashes added to it, which is causing my app to not be able to connect to the database. I believe this is because of Python escaping feature for strings. How do can I escape so that I end up with a single slash and not double? As I am getting the following error:

django.db.utils.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

I've tried the following and still same result:

  1. r'DESKTOP-RC52TD0\SQL2022'
  2. 'DESKTOP-RC52TD0\\SQL2022'
  3. r'DESKTOP-RC52TD0\SQL2022'

Any ideas how I can escape in the context of a dictionary. In a string adding double slash \\ works but not when used in a dictionary.

I am running the app using docker-compose.

version: '3'services:  # sqlserver:  #   image: mcr.microsoft.com/mssql/server  #   hostname: 'sqlserver'  #   environment:  #     ACCEPT_EULA: 'Y'  #     MSSQL_SA_PASSWORD: 'P@55w0rd'  #   ports:  #     - '1433:1433'  #   volumes:  #     - sqlserver-data:/var/opt/mssql  web:    image: landsoft/reporting-api    network_mode: host    build:      context: ./app    command: >      sh -c "python manage.py migrate &&             python manage.py runserver 0.0.0.0:8000"    volumes:      - ./app:/code    ports:      - "8000:8000"    environment:      - DB_HOST="DESKTOP-RC52TD0\SQL2022"      - DB_NAME=reporting      - DB_USER=sa      - DB_PASSWORD=Root_1421      - DB_PORT=1433    # depends_on:    #   - sqlserver# volumes:#   sqlserver-data:#     driver: local

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>