help me to fix this issue with database connection

786 views
Skip to first unread message

leb dev

unread,
Aug 29, 2019, 1:34:17 AM8/29/19
to Django users
i have a django project that need to be connected to MS SQL Server  i am using pyodbc package.

once i run the program the system display the below error:

djago.db.utils.operationalError:('08001','[08001] [microsoft][odbc sql server driver]neither dsn nor server keyword supplied (0) (sqldriverconnect); [08001] [microsoft][odbc sql server driver] Invalid connection string attribute (0)')

where is the error and how to fix it ?

from django.shortcuts import render
import pyodbc

def connect(request):
    conn = pyodbc.connect(
                            'Driver={SQL Server};'
                            'Server=ip address;'
                            'Database=I-Base_beSQL;'
                            'Trusted_Connection=yes;'

                        )


    cursor = conn.cursor()
    c = cursor.execute('SELECT "first name" FROM Person   WHERE id = 2 ')

    return render (request,'connect.html',{"c":c})

Amitesh Sahay

unread,
Aug 29, 2019, 3:04:54 AM8/29/19
to Django users
My friend, why don't you use settings.py for Db connection?

Regards,
Amitesh Sahay
91-750 797 8619


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4bdca932-9293-409e-a144-ef535d138422%40googlegroups.com.

leb dev

unread,
Aug 29, 2019, 4:11:09 AM8/29/19
to Django users
i tried in the settings.py 

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME':   'testDB',
        'HOST':   'VSQLSERV',
        'OPTIONS': {
            'driver':' SQL Server',
        }

        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

Amitesh Sahay

unread,
Aug 29, 2019, 4:19:18 AM8/29/19
to Django users
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'Authenticate', # database name
'USER': 'postgres',
'PASSWORD': 'pwd',
'HOST': 'localhost'
}
}
I don't think you need anything other than this. For MySql you can use mysql driver "django.db.backends.mysql". I hope that works.

Regards,
Amitesh Sahay


On Thursday, 29 August, 2019, 01:41:44 pm IST, leb dev <testd...@gmail.com> wrote:


i tried in the settings.py 

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME':   'testDB',
        'HOST':   'VSQLSERV',
        'OPTIONS': {
            'driver':' SQL Server',
        }

        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

On Thursday, August 29, 2019 at 8:34:17 AM UTC+3, leb dev wrote:
i have a django project that need to be connected to MS SQL Server  i am using pyodbc package.

once i run the program the system display the below error:

djago.db.utils. operationalError:('08001','[ 08001] [microsoft][odbc sql server driver]neither dsn nor server keyword supplied (0) (sqldriverconnect); [08001] [microsoft][odbc sql server driver] Invalid connection string attribute (0)')

where is the error and how to fix it ?

from django.shortcuts import render
import pyodbc

def connect(request):
    conn = pyodbc.connect(
                            'Driver={SQL Server};'
                            'Server=ip address;'
                            'Database=I-Base_beSQL;'
                            'Trusted_Connection=yes;'

                        )


    cursor = conn.cursor()
    c = cursor.execute('SELECT "first name" FROM Person   WHERE id = 2 ')

    return render (request,'connect.html',{"c": c})

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit

doddahulugappa

unread,
Aug 29, 2019, 4:22:35 AM8/29/19
to django...@googlegroups.com

 

Pip install pyodbc

And add below code snipped in settings.py. it should work. I am using in this way. It works perfectly

DATABASES = {
   
'default': {
       
'ENGINE': 'sql_server.pyodbc'

,
       
'NAME': 'DB',
       
'USER': 'user',
       
'PASSWORD': 'pwd’,
       
'HOST': 'host',
       
'PORT': 'port',
       
'OPTIONS': {
           
'driver': 'ODBC Driver 17 for SQL Server',
           
'isolation_level': 'READ UNCOMMITTED'# prevent SELECT deadlocks
       
},
    }

leb dev

unread,
Aug 29, 2019, 4:29:21 AM8/29/19
to Django users
i  already installed pyodbc package.

And add below code  in settings.py.

DATABASES = {

    'default': {
        
'ENGINE''sql_server.pyodbc',
        
'NAME''DB',
        
'USER''user',
        
'PASSWORD''pwd’,
        
'HOST''host',


        
'OPTIONS': {
            
'driver''ODBC Driver 17 for SQL Server',
            


        
},
    }

To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.


To view this discussion on the web visit

--
You received this message because you are subscribed to the Google Groups "Django users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

leb dev

unread,
Aug 29, 2019, 4:32:48 AM8/29/19
to Django users
i tried your answer also and did not work  i do not know what is the problem and how to fix it 

doddahulugappa

unread,
Aug 29, 2019, 4:43:21 AM8/29/19
to django...@googlegroups.com

Make sure that port is opened and it is reachable from machine u r trying to access

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of leb dev
Sent: Thursday, August 29, 2019 12:33 PM
To: Django users
Subject: Re: help me to fix this issue with database connection

 

i tried your answer also and did not work  i do not know what is the problem and how to fix it 

 

--

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

leb dev

unread,
Aug 29, 2019, 5:46:36 AM8/29/19
to Django users
yes  i can get a remote connection with the server and i can do ping  so its reachable.

and about the port its the default port used to connect ms sql server 1433. 
isn't this right ?

 

doddahulugappa

unread,
Aug 29, 2019, 6:10:21 AM8/29/19
to django...@googlegroups.com

Yes it is right . r u on same LAN?

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of leb dev
Sent: Thursday, August 29, 2019 1:47 PM
To: Django users
Subject: Re: help me to fix this issue with database connection

 

yes  i can get a remote connection with the server and i can do ping  so its reachable.

 

and about the port its the default port used to connect ms sql server 1433. 
isn't this right ?

 

 

--

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

leb dev

unread,
Aug 29, 2019, 6:23:57 AM8/29/19
to Django users
yes i am on the same LAN and i am able to get remote connection and ping 

Reply all
Reply to author
Forward
0 new messages