Vadim  Kovrizhkin
Vadim Kovrizhkin

Reputation: 1781

How to connect to MS SQL Server database remotely by IP in Python using mssql and pymssql

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.

Upvotes: 1

Views: 3255

Answers (1)

Morgan Thrapp
Morgan Thrapp

Reputation: 9986

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}'

Upvotes: 5

Related Questions