Q:

Unable to connect to mssql server with SQLalchemy, operational error and interface error

I have been trying to connect to mssql database with the help of SQLalchemy, but unable to connect. I have encountered 2 different kinds of errors-
Here is my code-
import sqlalchemy
from sqlalchemy import create_engine 
engine = 
create_engine("mssql+pymssql://username:password@DSNsource")
connection = engine.connect()

AND this below code
engine = 
sqlalchemy.create_engine('mssql+pyodbc://user:password@server/database', pool_pre_ping=True)
connection = engine.connect()

are giving me the following error--

InterfaceError: (pymssql.InterfaceError) Connection to the database failed for an unknown reason. (Background on this error at: http://sqlalche.me/e/rvf5)

Tried using pyodbc instead of pymssql but it gave me a different error.
import sqlalchemy
from sqlalchemy import create_engine 
engine = 
create_engine("mssql+pyodbc://username:password@DSNsource")
connection = engine.connect()

OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 13 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].  (53) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (53)') (Background on this error at: http://sqlalche.me/e/e3q8)

A:

The pymssql library is probably deprecated and might become unusable soon. I ended up using pyodbc with the below code:

import pyodbc
dsn="DRIVER={SQL Server name};server=server_name;database=db_name;uid=user_name;pwd=password"
cnxn = pyodbc.connect(dsn)
cursor = cnxn.cursor()

Before that, go to control panel -> Administrative tools ->ODBC data sources (32 or 64 bit based on your system configuration), go to System DSN and make sure the DSN you are trying to use is present. If not, add one.

