0
votes

I'm trying to connect to it from another domain with this code below but I'm getting an error like this

InterfaceError: (pyodbc.InterfaceError) ('28000', '[28000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication. (18452) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication. (18452)') (Background on this error at: http://sqlalche.me/e/13/rvf5)

This is my code that I'm using:

import pyodbc
from sqlalchemy import create_engine

server = 'shul'

database = 'db_pre'

uid = 'pre'
pwd = 'pr@20si'

engine = create_engine('mssql+pyodbc://' + server + '/' + database + '/' + uid + '/' + pwd + '?trusted_connection=no&driver=ODBC+Driver+13+for+SQL+Server')

#engine = create_engine('mysql://root:@localhost/daming') # enter your password and database names here

col_names = ["month", "price", "change"]
df = pd.read_csv("kcl.csv",sep=',',quotechar='\'',encoding='utf8', names=col_names,skiprows = 1) # Replace Excel_file_name with your excel sheet name
df.to_sql('kcl',con=engine,index=False,if_exists='replace') # Replace Table_name with your sql table name

In SQL Server Management Studio I used SQL Server authentication instead of Windows Authentication.

Please help me. Thank you in advance.

1
The order of your server, database, uid and pwd doesn't look correct. I've put it in an f-string to try make it clearer. f'mssql+pyodbc://{uid}:{pid}@{server}/{database}?driver=ODBC+Driver+13+for+SQL+Server;Trusted_Connection=no' - PGHE
Also just noticed the @ in your password. If that is in your password it'll cause problems. - PGHE
@PGHE thank you! but i still got the same error, im trying to change the pwd like this pwd = 'predik@2021si'.format(pwd='predikpwd') and yeah its still the same error - adinda aulia
@adindaaulia 1) why such an old driver? Why not v. 17 ?2) the problem is caused by using a URL and SQL Alchemy, not PyODBC. The pyodbc example doesn't use a URL - Panagiotis Kanavos
@adindaaulia if you use a URL from strings you need to ensure each part is URL-encoded. You can't just concatenate strings that may contain special characters like @ or / or #. It's quite likely that an unexpected character made SQL Alchemy ignore the user/password pair and everything after it and connect using the default (and far more secure) mechanism, Windows Authentication - Panagiotis Kanavos

1 Answers

0
votes

You can study this link :

https://docs.djangoproject.com/en/2.2/ref/settings/#databases

Set similar config :

   DATABASES = {
        "default": {
            "ENGINE": "sql_server.pyodbc",
            "NAME": env("database_name"),
            "USER": env("database_user"),
            "PASSWORD": env("database_password"),
            "HOST": env("database_host"),
            "PORT": env("database_port"),
            "OPTIONS": {
                "driver": "ODBC Driver 17 for SQL Server",
                "unicode_results": True,
            },
        }
    }