How to connect to MsSQL server with Django - sql-server

I am struggling with connecting to my local MsSQL server with Django. It says when I make command 'makemigrations':
django.core.exceptions.ImproperlyConfigured: 'sqlserver_ado' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
My settings:
DATABASES = {
'default': {
'NAME': 'TestDB',
'ENGINE': 'sqlserver_ado',
'HOST': 'STEPAN',
'USER': '',
'PASSWORD': '',
}
}
In this solution: Setting up django-mssql issues I found that my 'sqlserver_ado' should be in the same folder 'site-packages', which it is (Python IDLE shell):
>>> import sqlserver_ado
>>> sqlserver_ado
<module 'sqlserver_ado' from 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\lib\\site-packages\\sqlserver_ado\\__init__.py'>
>>> import django
>>> django
<module 'django' from 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python36_64\\lib\\site-packages\\django\\__init__.py'>
I have a PyWin32 installed and importing in IDLE is fine (even the path is fine), yet still I get this error message.
Any ideas or advices?
Spec: Win10, Python 3.6, Visual Studio 2017, django-mssql 1.8, django 2.0.6, MsSQL (developer) and this DB is present (with flask I have no problem to connect to my local server [just for comparsion my connection string in another flask project:
SQLALCHEMY_DATABASE_URI = "mssql+pyodbc:///?odbc_connect={}".format(urllib.parse.quote_plus(
"DRIVER={SQL Server};Server=STEPAN;Database=TestDB;Trusted_Connection=yes;"))
]), project was automaticly created by Visual Studio, command that throws this error:
python manage.py makemigrations

Responding to an old post, but - in case this will help others:
I found that, in some cases, you need to specify the SQL Server driver version too:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': '<dbNAME_HERE',
'HOST': '<SERVERNAME_HERE>\SQLEXPRESS',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
}
}
}
This is for a DJANGO project, but the issue may be similar...

Related

Problem connecting existing MSSQL server database to django project.

I need to connect a django project to an excisting MS SQL-Server database however I'm getting an error I don't understand:
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][Driver Manager ODBC] Nome origine dati non trovato e driver predefinito non specificato. (0) (SQLDriverConnect)')
English translation I found after googling this:
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I have no idea what this means, however, I found this question which showed how to setup the DATABASES values in django. Currently my DATABASES looks like this:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': '<DATABASE NAME>',
'HOST': '<PCNAME>\SQLEXPRESS',
'USER': '<USERNAME>',
'PASSWORD': '<PASSWORD>',
'PORT': '',
'OPTIONS': {
'driver': 'SQL Native Client',
'dsn': 'dsn_entry',
'host_is_server': True
}
}
}
the server is on localhost and I'm using windows 7, django 2.1, python 3.7 and SQL Server 2014 Express
What is this error and what am I doing wrong?
A DSN is a pre-defined hostname and driver (and potentially other things) that are defined in a configuration file like ~/.odbc.ini. change the driver inside the odbc.ini file.

Django+sql server - No module named sql_server.pyodbc.base

I want to use SQL Server as the backend for Django.
I install pyodbc, django-pyodbc, django-pyodbc-azure
In settings.py I replace a Database
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'xxx',
'USER': 'xxx',
'PASSWORD': 'xxx',
'HOST': 'xxx.database.windows.net',
'PORT': '1433',
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
'MARS_Connection': 'True',
}
}
}
if I run python manage.py syncdb
Error:
django.core.exceptions.ImproperlyConfigured: 'sql_server.pyodbc' isn't an available database backend. Available options are: 'dummy', 'mysql', 'oracle', 'postgresql', 'postgresql_psycopg2', 'sql_server', 'sqlite3'
Error was: No module named sql_server.pyodbc.base
I use Visual Studio + Python 2.7
You may want to follow that documentation https://azure.microsoft.com/en-us/documentation/articles/web-sites-python-ptvs-django-sql/. The Python tools for Visual Studio makes things easier; still, even if you don't use them, the documentation will show you how they do, how they install pyodbc locally, then push it to the server while publishing.
Note that while Azure has 64 bit VMs, the Python environment must be 32 bits.

Django-pydobc SQL server connection problems on windows

Another developer and I are setting up a django (v1.4.2) project using a legacy SQL server database (SQLEXPRESS) on another server. So far, we have been able to connect to the database from linux and mac using django-pyodbc, and from a laptop running windows 7 using django-mssql. I would like to use django-pyodbc on the laptop to keep the environments in sync.
On the laptop:
pyodbc (3.0.6) is installed and in a non-django .py script I can connect and run sql statements
Downloaded django-pyodbc 1.4 by downloading the zip; I'm not sure I installed it right:
I unzipped the file, and ran the setup.py file in the top directory; it puts a sql_server directory in the /lib/site-packages directory
Copied this sql_server directory to /django/db/backends
Created a PYTHONPATH environment variable pointing to /django/db/backends/sql_server
not sure if it's supposed to point to /site-packages/sql_server instead?
Created an ODBC Data Source (System DSN)
testing the connection option works
Editted the DATABASE entry in settings.py to be almost exactly like the linux version (details below)
So, it doesn't work, and I get the following error message, and have no idea what to do next:
('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53); [01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')
I setup the django settings.py file as like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sql_server.pyodbc',
'NAME': 'test',
'USER': 'test',
'PASSWORD': 'something_else',
'HOST': 'mssqlx',
'PORT': '12345',
'OPTIONS': {
'driver': 'SQL Server',
},
},
}
On linux, my settings file has a DATABASES entry like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sql_server.pyodbc',
'NAME': 'test',
'USER': 'test',
'PASSWORD': 'something_else',
'HOST': 'mssqlx', # ODBC DSN defined in /etc/freetds.conf
'PORT': '12345', # Probably unneeded. Set in mssqlx
'OPTIONS': {
'driver': 'SQL Server', # ODBC driver name in /etc/odbcinst.ini
'extra_params': "TDS_VERSION=7.0" # Probably unneeded. Set in mssqlx
}
},
}
don't know if it will help solve this, but using django-mssql (which only runs on windows), the (working) entry is:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlserver_ado',
'NAME': 'test',
'USER': 'test',
'PASSWORD': 'something_else',
'HOST': '199.555.0.10', # changed for this example
'PORT': '12345',
'OPTIONS': {'provider': 'SQLOLEDB'}
},
}
Don't know what other info might help. Thank you for any help or insight you can offer.
----POST MORTEM ----
Here's what finally worked:
partial entry in settings for DATABASES:
'default': {
'ENGINE' : 'django.db.backends.sql_server.pyodbc',
'NAME' : 'test_db_name',
'USER' : 'test_db_user_name',
'PASSWORD' : 'password',
# ODBC DSN defined in /etc/freetds.conf
'HOST' : 'mssql_test',
# Ignored for Windows; Required for Linux
'OPTIONS' : {
# ODBC driver name in /etc/odbcinst.ini
'driver': 'SQL Server',
# NOTE: dsn option is added dynamically later, for Windows
}
},
# The ODBC DSN name specified above as DATABASES.default.HOST is ignored on
# Windows, where it must be specified as DATABASES.default.OPTIONS.dsn instead.
# However, we haven't found a way to make DATABASES.default.OPTIONS.dsn work in
# Linux (and probably the same for Mac). It causes the error:
# Data source name not found, and no default driver specified
# Therefore we add it here, but only for Windows.
# Note: The username and pwd in the windows dsn file is apparently NOT used
# (b/c server hosts both test and prod database in same MSSQL
# instance, both test and prod dsn files happen to work - they have the
# same ip address and port number, but different username/password's)
#
# On 64-bit Windows, with our current 32-bit version of pyodbc, the DSN
# must be created via:
# C:\Windows\SysWOW64\odbcad32.exe
# instead of the regular "ODBC Data Sources" app in Control Panel, which
# invokes:
# C:\Windows\system32\odbcad32.exe
#
# os.name is...
# nt for Hans' laptop (Windows 7)
# posix for the "Amazon Linux AMI" (CentOS) on AWS
# posix for Fred's Mac
if os.name == 'nt': # Windows
DATABASES['cf']['OPTIONS']['dsn'] = 'mssql_test'
Try using https://github.com/michiya/django-pyodbc-azure. This should work on both Linux and Windows.
Then define your database settings as such:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'dbname',
'HOST': 'dsn_entry',
'PORT': 'port',
'USER': '',
'PASSWORD': 'pass',
'OPTIONS': {
'driver': 'FreeTDS',
'dsn': 'dsn_entry',
'host_is_server': True
}
}
}
Under Windows the 'driver' entry in OPTIONS should be:
'driver': 'SQL Native Client',
Edit: Oops, failed to see that the you had solved the problem. Leaving my answer here as reference.

Django database settings for production server

I have a problem with Django database configuration with 'postgresql_psycopg2'
If I give any arbitrary name to my database like below,
DATABASES = {
'default': {
'ENGINE': 'postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'xyz', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': '${my_password}', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
'OPTIONS': {
"autocommit": True,
}
}
}
>
OperationalError at /
FATAL: database "xyz" does not exist
I surf a lot and findout same that with SQLite we have to specify absolute path to our database; with PostGRE likewise above.
I would like to know:
1) Why I am getting error message with above specification and
2) How I use my database which i am using with Development server lay out in filesystem (windows).
I surf a lot and findout same that
with SQLite we have to specify
absolute path to our database; with
PostGRE likewise above.
Just to be clear (your wording isn't) with postgresql there is no absolute path, it's just the name of the database.
1: The error message is pretty clear, database xyz does not exist.
Go to the dbshell (python manage.py dbshell) and type in \l. If database xyz isn't in there, type in create database xyz, exit the postgres shell and try syncdb again.
2: For your development server, I recommend using sqlite3 for its ease of use.
sqlite is included in python 2.5+, so no extra settings are required. Simply set your ENGINE to sqlite3, specify an absolute path to where you want the database saved, and python manage.py syncdb

Django-pyodbc SQL Server/freetds server connection problems on linux

Error: ('IM002', '[IM002]
[unixODBC][Driver Manager]Data source
name not found, and no default driver
specified (0) (SQLDriverConnectW)')
I'm migrating from developing on a windows development machine to Linux machine in production and I'm having issues with the freetds driver. As far as I can tell that error message means it can't find the driver. I can connect via the cli via sqsh and tsql. I've setup my settings.py as such.
'bc2db': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'DataTEST',
'USER': 'appuser',
'PASSWORD': 'PASS',
'HOST': 'bc2.domain.com',
'options': {
'driver': 'FreeTDS',
}
},
Does anyone have any SQL Server experience with django? do I have to use a dns? (how would I format that?)
I needed to use one of the supported configurations as supported by the freetds driver. I ended up putting the host information in the odbc.ini. The linked documentation has good examples over a few pages.
Here's an example of a database connection for SQL Server and django in case someone needs it, this is how it will look in the settings.py.
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'YourDBname',
'USER': 'YourUsername',
'PASSWORD': '',
'HOST': '',
'OPTIONS' : {
'driver': 'SQL Native Client',
'dsn': 'YourDSNname',
'MARS_Connection': True,
},
},
}
Further information here...

Resources