Question

How to connect to a remote Microsoft SQL server

I searched the current questions and didn’t find anything about connecting to a remote microsoft sql server. There are plenty of questions about mysql remote connection, but nothing for microsoft sql server.

The microsoft sql server is not installed on Digital Ocean. It is a remote server installed somewhere else.

I did some google searches and this was about the best I came up with,

https://tryolabs.com/blog/2012/06/25/connecting-sql-server-database-python-under-ubuntu/

http://www.stevepiercy.com/articles/how-to-install-and-configure-freetds-as-an-odbc-connector-to-microsoft-sql-server-on-centosrhel-for-lasso-9/

I also ran across this alternative,

https://python-tds.readthedocs.io/en/latest/

Does anyone have any experience with any of this? Thank you in advance for any advice.

I will try the instructions in the first link above and see how it goes.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

How did it go?

Seems like the first link is correct, you’ll need the pyodbc (or PyMSSQL) driver to connect to MS SQL servers.

Simple example from: https://docs.microsoft.com/en-us/sql/connect/python/pyodbc/python-sql-driver-pyodbc

import pyodbc 
server = 'tcp:myserver.database.windows.net' 
database = 'mydb' 
username = 'myusername' 
password = 'mypassword' 
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

Let me know how you made out, would be happy to help you further.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Thanks for the reply. For now, my client has put the MS SQL part on hold and will go with MySQL since I already have that working. At least I always have this as a reference on how to do it. :)

Awesome, great to hear!