Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to SQL Server via pymssql

Tags:

python

pymssql

I am attempting to connect to SQL Server running on Windows XP system from a *nix system on a local server via pymssql. However, the connection fails as shown below

db = pymssql.connect(host='192.168.1.102',user='www',password='test',database='TestDB')
Traceback (most recent call last):

File "<stdin>", line 1, in <module>
File "pymssql.pyx", line 457, in pymssql.connect (pymssql.c:6041)
raise InterfaceError(e[0])
pymssql.InterfaceError: Connection to the database failed for an unknown reason.

Things I've tried:

  1. Set SQL Server and browser to run as a network server.
  2. Setup a user 'www'. I also tested this user locally in SQL Studio.
  3. Turned off Windows firewall (temporarily of course).

I am missing SOMETHING - I just don't know what it is. I tried all of the infinite menu options on Windows to no avail. One thing I noticed is that if the Windows Firewall is on (I setup an exception for SQL Server) python pauses a long time and then gives the error. If the firewall is off the error is instant.

Are there any logs I can look at in SQL Server?

like image 559
Christopher Avatar asked Nov 11 '10 00:11

Christopher


People also ask

Can Pymysql connect to SQL Server?

The pymssql. connect function is used to connect to SQL Database.

What is Pymssql in Python?

A simple database interface for Python that builds on top of FreeTDS to provide a Python DB-API (PEP-249) interface to Microsoft SQL Server. The 2. x branch of pymssql is built on the latest release of FreeTDS which removes many of the limitations found with older FreeTDS versions and the 1. x branch.

Can Python interact with SQL Server?

You can connect to a SQL Database using Python on Windows, Linux, or macOS.


1 Answers

Got it! I think the source of the problem was not giving Free TDS the attention it needs. Free TDS is apparently the driver behind pymssql and provides for connectivity to other databases - SQL Server being one of them.

The freetds.conf file is located in /usr/local/etc on my system (Mac Book Pro).

This file contains the defaults from the install. However, I had previously added a definition so that I could connect but forgot about it and unfortunately did not take notes on it.

Anyway, here is an example of what I appended to freetds.conf:

[SomeDB]
    host = 192.168.1.102
    port = 1219
    tds version = 7.0

However, what is puzzling is that I set the port to 1219. I had it set manually to 1433 in SQL Studio. Also, I am using TDS version 0.82 so I don't know how 7.0 fits in.

Next, I tested connectivity using 'tsql' as follows:

tsql -S SomeDB -U www

I enter the password and get a command-line which allows for SQL queries.

Next, I tested connecting using pymssql as follows:

db = pymssql.connect(host='SomeDB',user='www',password='cylon',database='TestDB')

As you can see, I needed to use the host name from the freetds.conf file and NOT the IP directly. I then tested a simple query with additional python code to insure I could read from the database.

I hope this helps someone else in the future.

like image 91
Christopher Avatar answered Nov 06 '22 02:11

Christopher