Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pymssql - Connecting to Named Instance

I'm trying to connect to a SQL Server named instance from python 3.4 on a remote server, and get an error.

File "C:\Scripts\Backups Integrity Report\Backup Integrity Reports.py", line 269, in
conn = pymssql.connect(host=r'hwcvcs01\HDPS', user='My-office\romano', password='PASS', database='CommServ')
File "pymssql.pyx", line 636, in pymssql.connect (pymssql.c:10178)
pymssql.OperationalError: (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed\n')

Other SQLs are connected without a problem. Also I manage to connect to the SQL using the Management Studio, from the same remote server.

Tried different ports, tried to connect to the host itself rather than the instance, and also tried pypyodbc. What might be the problem?

like image 247
Roman Ost Avatar asked Sep 27 '26 00:09

Roman Ost


2 Answers

For a named instance that looks like this:

myhost\myinstance,1435

You can connect via pymssql with the following:

pymssql.connect(host='myhost', server='myinstance', port='1435', user='user', password='pw')
like image 178
John Galambos Avatar answered Sep 28 '26 13:09

John Galambos


According to the pymssql documentation on the pymssql Connection class, for a named instance containing database theDatabase, looking like this: myhost\myinstance You could connect as follows: pymssql.connect(host=r'myhost\myinstance', database='theDatabase', user='user', password='pw') The r-string is a so-called raw string that does not treat the '' as an escape.

like image 38
W Roos Avatar answered Sep 28 '26 13:09

W Roos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!