pymssql.OperationalError: DB-Lib error message 20009, severity 9
I encountered the same issue. It turns out that I didn't open up TCP/IP access for my local SQL server.
Do a quick test to check whether it's caused by this, you can try to connect to a remote server with the same code. If it works, it means something is wrong with your local server.
So just open the access to your 127.0.0.1:1433 in the SQL server Configuration Manager.
Steps:
- Start -> All Programs -> Microsoft SQL Server 20XX -> Configuration Tools -> SQL Server Configuration Manager
- SQL Server Network Configuration -> Protocols for MSSQLSERVER
- TCP/IP -> Properties -> IP Addresses. Find 127.0.0.1 and change the "Enabled" to "Yes". You can do it for all the IPs if you want.
I solved my problem by using a different library instead. I use pyodbc
import pyodbccnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=SERVERNAME;DATABASE=testdb;UID=me;PWD=pass')cursor = cnxn.cursor()cursor.execute("select user_id, user_name from users")rows = cursor.fetchall()for row in rows: print row.user_id, row.user_name
check installed python version(32 bit or 64 bit).
Download freetds from below based on your OS architecture.
https://github.com/ramiro/freetds/releasesUnzip it to
C:\
drive (likec:\freetds
)Add Bin (from
c:\freetds\bin
directory) to the environment variables(both).Make sure port 1433 is open and TCP/IP enabled in SQL Configuration.
Restart your PC and everything should be fine.