I have been trying to link my mssql database to Django. When I run the Django server I get the error below. For normal query out of Django, it works fine, however, the Django doesnt even connect. My normal username when login into windows is kmoh from EMEA domain, when I normaly login into my windows account it looks like this: EMEA\kmoh but as you can see in the error it looks like this EMEA\\kmoh. So How can I remove this another backslash?
File "C:\ProgramData\Anaconda3\lib\site-packages\sql_server\pyodbc\base.py", line 307, in get_new_connection
timeout=timeout)
django.db.utils.InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'EMEA\\kmoh'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0); [28000]
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'EMEA\\kmoh'. (18456); [28000] [Microsoft][ODBC Driver 17 for SQL Server]Invalid connection string attribute (0)")
I am using these:
Windows 10
django-pyodbc-azure: version 2.1
Django 2.1
Pyodbc: 4.0.25
my Django settings.py looks like this:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'xxx', #has been hide for this post
'PORT': '1433',
'NAME': 'BBL_Result',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
I would expect to see the user and password set explicitly in the settings:
DATABASES = {
'default': {
...
'USER': 'db_username',
'PASSWORD': 'db_password',
Related
I can successfully connect to database with SQL Management Studio with Authentication "Windows Authentication" or "Azure Active Directory - Universal with MFA".
I have currently login with MFA to SQL Server Management Studio.
However Windows login with pyodbc fails. I wonder what is wrong.
I have tried to add 'UID=my.name#domain.com;' , but same error.
CODE:
import pyodbc
conndev = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
'Server=server124.database.windows.net,1433;Trusted_Connection=No;'
'Database=db345;'
'UID=username;'
'PWD=password;')
ERROR:
conndev = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
pyodbc.Error: ('FA004', "[FA004] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Failed
to authenticate the user '' in Active Directory (Authentication option is
'WindowsIntegrated').\r\nError code 0xCAA82EEF; state 10\r\nThe request to connect and log on
to an server failed. (0) (SQLDriverConnect); [FA004] [Microsoft][ODBC Driver 17 for SQL
Server]
[SQL Server]Failed to authenticate the user '' in Active Directory (Authentication option is
'WindowsIntegrated').\r\nError code 0xCAA82EEF; state 10\r\nThe request to connect and log on
to an server failed. (0)")
I wasn't sure what to title my post, if you have a better idea, feel free to edit the title.
I have not used SQL Alchemy before and the documentation that I have looked at located in the following places, is not helpful:
Connecting to SQL Database Using SQL Alchemy in Python
Tutorial Point
Here is the code I am using:
import sqlalchemy as sal
from sqlalchemy import create_engine
#Here are the parameters I am using:
- server = 'Q-20/fake_example'
- database = 'AdventureWorks2017'
- driver = 'ODBC Driver 17 for SQL Server'
- trusted_connection='yes'
DATABASE_CONNECTION = 'mssql+pyodbc://#server = ' + server + '/database = ' + database + '?trusted_connection = ' + trusted_connection + '&driver=' + driver
engine = sal.create_engine(DATABASE_CONNECTION)
All of that seems to work fine without any problems; however, when I add this line:
connection=engine.connect()
I get the following error message:
sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('08001',
'[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes
Provider: Could not open a connection to SQL Server [53]. (53)
(SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL
Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17
for SQL Server]Invalid connection string attribute (0); [08001]
[Microsoft][ODBC Driver 17 for SQL Server]A network-related or
instance-specific error has occurred while establishing a connection
to SQL Server. Server is not found or not accessible. Check if
instance name is correct and if SQL Server is configured to allow
remote connections. For more information see SQL Server Books Online.
(53)')
I am not sure what is wrong with what I am doing, does anyone have any suggestions?
What I have tried so far:
I have confirmed that SQL Server is configured to allow remote connections. I did this check by following the instructions here
Removing the "#" sign before the server, but this just generated the same error message.
I figured out part of what I needed to do. I needed to change my parameters.
Old Parameters:
server = 'Q-20/fake_example'
database = 'AdventureWorks2017'
driver = 'ODBC Driver 17 for SQL Server'
trusted_connection='yes'
New Parameters:
server = 'Q-20'
database = 'AdventureWorks2017'
driver = 'SQL+SERVER+NATIVE+CLIENT+11.0'
trusted_connection='yes'
This is what my code ultimately looked like:
database_connection = 'mssql+pyodbc://Q-20/AdventureWorks2017?trusted_connection=yes&driver=SQL+SERVER+NATIVE+CLIENT+11.0'
I am connecting to a SQL Server hosted on a remote desktop using Windows server through VBA with this code:
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
'Open Connection
objMyConn.ConnectionString = "Provider=SQLOLEDB.1;User ID=sa;Password=xxxxx;Persist Security Info=True;Initial Catalog=databaseName;Data Source=192.168.1.xxx;"
objMyConn.Open
Currently trying to use python to connect to the same SQL Server database with this code:
import pyodbc
server_name='192.168.1.xxx'
db_name='databaseName'
username='sa'
password='xxxxx'
conn = pyodbc.connect('DRIVER={ODBC Driver 11 for SQL Server};'
'Server=server_name;'
'Database=db_name;'
'UID=username;'
'PWD=password;'
'Trusted_Connection=yes;')
cursor=conn.cursor()
TRACEBACK:
File "x/test.py", line 6, in <module>
conn = pyodbc.connect('DRIVER={ODBC Driver 11 for SQL Server};'
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (53)')
You should omit argument trusted_connection since you are providing UID and PWD. Trusted_connection fills UID and PWD values with your current windows user values so it is probably reserved for a local connection within the host.
I belive connection string should look like this:
'DRIVER={ODBC Driver 11 for SQL Server};'
'Server=server_name;'
'Database=db_name;'
'UID=username;'
'PWD=password;'
I would like to use Django 2.0 with legacy MS SQL Server database.
Latest information regarding usage Django with MS SQL Server i could find is Using Sql Server with Django in production/These days and it is about Django 1.11
Most supported seems django-pyodbc-azure but it's not supporting Django 2.0 yet:
django-pyodbc-azure issue #124
Is there any alternative?
Found the solution and post it if anyone facing same problem.
I used django-pyodbc-azure 2.0.4.1 in my Django 2.0.4
The settings that worked for me:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'dbName',
'USER': 'yourUserName',
'PASSWORD': 'yourPassword',
'HOST': '(local)',
'PORT': '',
'OPTIONS': {
'driver': 'ODBC Driver 11 for SQL Server',
},
}
}
I was facing same error of ODBC Driver Manager
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manag
er] Data source name not found and no default driver specified (0) (SQLDriverCon
nect)')
Solution: When i added below code in database connection, then my error fixed.
'OPTIONS': {
'host_is_server': True,
'driver': 'ODBC Driver 11 for SQL Server',
}
I use django-pyodbc on Django 1.6.1
when I run manage.py syncdb, everything is fine.
I use 2 database setttings in Django to read some data from my legacy database, when I what to read the data from a model name T_AllStation:
all_t_station = T_AllStation.objects.using('SQL_Server').all()
An error is raised:
DatabaseError at /company/get_station_info
('42S22', "[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]\xc1\xd0\xc3\xfb 'id' \xce\xde\xd0\xa7\xa1\xa3 (207) (SQLExecDirectW)")
Request Method: GET
Request URL: http://127.0.0.1:8000/company/get_station_info
Django Version: 1.6.1
Exception Type: DatabaseError
Exception Value:
('42S22', "[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]\xc1\xd0\xc3\xfb 'id' \xce\xde\xd0\xa7\xa1\xa3 (207) (SQLExecDirectW)")
Exception Location: E:\VirtualEnvs\EnvMonitor\lib\site-packages\django_pyodbc\base.py in execute, line 416
Python Executable: E:\VirtualEnvs\EnvMonitor\Scripts\python.exe
Python Version: 2.7.2
What does the string \xc1\xd0\xc3\xfb 'id' \xce\xde\xd0\xa7\xa1\xa3 (207) mean ?
Try making your configuration file use options like this, to account for Unicode:
DATABASES = {
'default': {
'ENGINE': 'django_pyodbc',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'your_password',
'HOST': 'database.domain.com,1433',
'PORT': '1433',
'OPTIONS': {
'host_is_server': True,
'autocommit': True,
'unicode_results': True,
'extra_params': 'tds_version=8.0'
},
}
}
(You're on Windows, so you don't need the tds_version, but it can't hurt so your config is portable to Linux.)