[Python] Pycharm Django connect to MSSQL database

Discussão em 'Python' iniciado por Stack, Junho 25, 2025.

  1. Stack

    Stack Membro Participativo

    I already created the Django project in PyCharm and want to connect to the MSSQL database. I see most of the solutions use django-mssql-backend, but I am using a company computer and our system does not have this package.

    I used to use Jupyter notebook to connect the database successfully. Here is the code I used in the Jupyter notebook:

    database = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
    "Server= DBName;"
    "Database=Staging;"
    "Trusted_Connection=yes;")
    query1 = """
    select *
    FROM [Staging].[dbo].[viewname]

    df1=pd.read_sql_query(query1,database)
    df1


    Can I use the same way in my Django project to connect to the database?

    Here is the Django project folder structure:

    [​IMG]

    If I can do what I do in Jupyter notebook, which .py should I add the code in?

    Continue reading...

Compartilhe esta Página