2

I am using pyodbc to retrieve data from MSSQL and this is the code I am using:

import pyodbc
server = 'xxxxxxxx\DEV'
database = 'SandBox'
username = 'zzzzzzz'
password = 'xxxxxxx'
driver = '{SQL Server}'

cnxn = pyodbc.connect('DRIVER='+driver+';PORT=4853;SERVER='+server+';PORT=4853;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("select * from fieldscreenscheme ")
row = cursor.fetchone()
if row:
    print row

This is the error massage I got:

cnxn = pyodbc.connect('DRIVER='+driver+';PORT=43853;SERVER='+server+';PORT=43853;DATABASE='+database+';UID='+username+';PWD='+ password)

pyodbc.Error: ('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 installed the ODBC driver. Any recommendation how to solve this error? I looked at these two but did not help me to solve this. Python - Can't connect to MS SQL pyodbc + MySQL + Windows: Data source name not found and no default driver specified
Microsoft Documentation: https://github.com/Microsoft/azure-docs/blob/master/articles/sql-database/sql-database-develop-python-simple.md

1
  • Still it does not any other recommendations? Commented Mar 30, 2017 at 15:28

3 Answers 3

5

Two problems:

  1. Normally, one supplies either the \INSTANCENAME or the port number, not both.
  2. ODBC connection strings for SQL Server do not use PORT=, they put the port number in the SERVER= parameter, e.g., SERVER=xxxxxxxx,43853. (Note that the instance name is omitted and the separator is a comma, not a colon.)
Sign up to request clarification or add additional context in comments.

Comments

1

I was facing the same issue whole day wasted and I tried all possible ODBC Driver for SQL Server here is the list, I just try one by one and it works for me

Driver={ODBC Driver 11 for SQL Server} for SQL Server 2005 - 2014
Driver={ODBC Driver 13 for SQL Server} for SQL Server 2005 - 2016
Driver={ODBC Driver 13.1 for SQL Server} for SQL Server 2008 - 2016
Driver={ODBC Driver 17 for SQL Server} for SQL Server 2008 - 2017

And these are some others we can use, somehow in my case the last one works :)

Driver={SQL Server} for SQL Server 2000
Driver={SQL Native Client} for SQL Server 2005
Driver={SQL Server Native Client 10.0} for SQL Server 2008
Driver={SQL Server Native Client 11.0} for SQL Server 2012

Comments

0

I've met same problem and fixed it changing connection string like below. Writing

driver = '{ODBC Driver 13 for SQL Server}'

instead of

driver = '{SQL Server}'

3 Comments

which version of pyodbc ere you using? I used 4.0.26 and I had to do exactly opposite of your answer.
Version of pyodbc at execution environment could be 4.0.23, but I'm not sure.
@Eswar can you see my answer below,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.