EhsanHK
EhsanHK

Reputation: 21

Why this error when getting data from MSSQL using pyodbc?

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

Upvotes: 2

Views: 5438

Answers (3)

sakulachi8
sakulachi8

Reputation: 300

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

Upvotes: 1

Mil
Mil

Reputation: 331

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}'

Upvotes: 0

Gord Thompson
Gord Thompson

Reputation: 123399

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.)

Upvotes: 5

Related Questions