SQL Alchemy create_engine connection string won't work

Viewed 753

I'm trying to write to MSSQL using SQL Alchemy but can't get the connection to work. I've got

engine = create_engine('mssql+pyodbc://<Username>@localhost:5432/<DBName>?driver=SQL+Server+Native+Client+11.0')

df.to_sql('<TableName>', con=engine, if_exists='append')

This gives:

OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]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. (10061)')
(Background on this error at: http://sqlalche.me/e/13/e3q8)

I've looked at the error documentation and tried several different things with the connection string but nothing seems to work. Reading from the same database using pyodbc works fine:

con = pyodbc.connect('Driver={SQL Server};'
                     'Server=<Server>;'
                     'Database=<DBName>;'
                     'Trusted_Connection=yes;')

df = pd.read_sql_query('select * from <DBName>.dbo.<TableName>', con)
0 Answers
Related