Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neither DSN nor SERVER keyword supplied

I am trying to connect to SQL database slightly different ways: with and without use of parameter. Why without use of parameters works fine, but with use of parameters - gives me an error. Did I make a syntax error? I went through each letters and couldn't see anything.

import pandas as pd
import pyodbc

#parameters:
server = 'SQLDEV'
db = 'MEJAMES'

#Create the connection
conn = pyodbc.connect('DRIVER={SQL Server};server =' + server + ';DATABASE = ' + db + ';Trusted_Connection=yes;')
# query db
sql = """

select top 10 PolicyNumber, QuoteID, ProducerName from tblQuotes

"""
df = pd.read_sql(sql,conn)
df

The statement above gives me an error enter image description here

But if I do the same but without use of parameters then it works fine:

import pandas as pd
import pyodbc

#parameters:
#server = 'SQLDEV'
#db = 'MEJAMES'

#Create the connection
conn = pyodbc.connect("DRIVER={SQL Server};server=SQLDEV;database=MEJAMES;Trusted_Connection=yes;")
# query db
sql = """

select top 10 PolicyNumber, QuoteID, ProducerName from tblQuotes

"""
df = pd.read_sql(sql,conn)
df

enter image description here

like image 244
Serdia Avatar asked Sep 05 '17 20:09

Serdia


2 Answers

The Windows ODBC Driver Manager is quite fussy about keywords in connection strings. They must be immediately followed by the equal sign, so SERVER=... will work, but SERVER =... will not.

like image 63
Gord Thompson Avatar answered Sep 22 '22 07:09

Gord Thompson


Its crazy but I managed to solve it by actually passing an option: extra_params: server=WHEREYOURSERVERLIVES\DBSERVER

I am using it pyodbc on django BTW.

Must be some bug.

something like this

    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'YOURGREATESTDATABASE',
    'USER': 'YOURGREATESTUSERNAME',
    'PASSWORD': 'YOURGREATESTPASSWORD',
    'HOST': 'WHEREYOURSERVERLIVES\DBSERVER',
    'PORT': '',

    'OPTIONS': {
        'driver': 'ODBC Driver 17 for SQL Server',
        'extra_params': "Persist Security Info=False;server=WHEREYOURSERVERLIVES\\DBSERVER"
like image 38
punggolzenith Avatar answered Sep 22 '22 07:09

punggolzenith