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.

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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.