1

I am trying to use pandas.read_sql_table to get data from MS SQL Server (the server is on a network). I use Windows authentication to access the server. Pandas read_sql_table takes a SQL Alchemy connection as an argument for “connection.” I am having a difficult time finding an example that combines:

  1. SQL Alchemy
  2. MS SQL Server
  3. DSN (the “preferred” specification according to SQL Alchemy)
  4. Windows authentication

I’ve consulted SQL Alchemy, which shows an example using SQL authentication, but not Windows authentication. http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#connecting-to-pyodbc Here are various options I have tried. All return an error.

import pandas as pd
from sqlalchemy import create_engine
import pyodbc
# set some variables
dbname = 'mydbname'
schemaname = 'myschemaname'
servername = 'myservername'
tablename = ‘mytablename’

sqlcon = create_engine('mssql+pyodbc://@' + servername)
#sqlcon = create_engine('mssql+pyodbc://' + servername + '/' + dbname)
#sqlcon = create_engine('mssql+pyodbc://' + servername)
#sqlcon = create_engine('mssql://' + servername + '/' + dbname + '?trusted_connection=yes')
#sqlcon = create_engine('mssql+pyodbc://' + servername + '/' + dbname + '?trusted_connection=yes')
mydataframe = pd.read_sql_table(tablename,con=sqlcon,schema=schemaname)

The error I get is this:

(pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') (Background on this error at: http://sqlalche.me/e/rvf5)

What is especially perplexing is the comment about no default driver being specified. None of the examples refer to specifying a default driver when I use this DSN format.

I have consulted this example, which also fails for me: How do I connect to SQL Server via sqlalchemy using Windows Authentication?

I can connect fine with SSMS. I'm using python 3.6.

2
  • 1
    A Data Source Name (DSN) is an object created beforehand not inside Python or SQLAlchemy. None of your attempts show use of a named DSN. Otherwise you need to use an explicit Driver.
    – Parfait
    Commented Apr 20, 2018 at 19:49
  • Create a user/system DSN on your machine then attempt: engine = create_engine("mssql+pyodbc://user:pwd@my_dsn")
    – Parfait
    Commented Apr 20, 2018 at 19:49

2 Answers 2

10

I found a solution to my question. Posting here for others' reference.

This code worked. I wasn't able to avoid specifying a driver explicitly, though.

sqlcon = create_engine('mssql+pyodbc://@' + servername + '/' + dbname + '?driver=ODBC+Driver+13+for+SQL+Server')
1
  • 2
    could you please explain to me what does driver do?
    – Fatima
    Commented Jan 19, 2020 at 9:03
0

If you are confused with driver version you can use below code

username=<username>
password=<password>
server=<servername> | <host:port> 
database=<database>

#below code is with authentication
engine = create_engine('mssql+pyodbc://'+username+':'+password+'@' + server + '/' + database + '?driver=SQL+Server')
query = '''select * from <table>'''
result = engine.execute(query)


#below code is without authentication
engine = create_engine('mssql+pyodbc://@' + server + '/' + database + '?driver=SQL+Server')
query = '''select * from <table>'''
result = engine.execute(query)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.