1

How can I connect to MS SQL Server database remotely by IP in Python using mssql and pymssql modules. To connect locally I use link = mssql+pymssql://InstanceName/DataBaseName

I enabled TCP/IP Network Configurations. But How can I get the connection link?

Thank you.

1 Answer 1

5

You need to create a Connection object

import pymssql
ip = '127.0.0.1'
database_connection = pymssql.connect(host=ip, port=1433, username='foo', password='bar')

If you're using SQLAlchemy, or another ORM that supports connection strings, you can also use the following format for the connection string.

'mssql+pymssql://{user}:{password}@{host}:{port}'
Sign up to request clarification or add additional context in comments.

4 Comments

Can I generate the string for connection something like this: "mssql+pymssql://InstanceName/DataBaseName". I thought I could just replace the string and not change the code. Because this string is in config file.
@VadimKovrizhkin Are you using SQLAlchemy? How are you using that string?
Yes, it is SQLAlchemy. It is legacy code. I just have this string in the config file.
@VadimKovrizhkin Then use the second chunk of code I edited in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.