Unable to connect to SQL Server via pymssql

落花浮王杯 提交于 2019-11-29 09:48:17

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.

notatoad

It looks like you've got this solved, but for anybody else from google that lands here: check to make sure mixed-mode authorization is turned on on your MS SQL Server. It defaults to only allowing Windows authorization, and that will cause this error in pymssql.

is it a windows machine u working on? specify the port 1433. it seems to be a bug in the mssql client api, which tries to use Namedpipes instead of TCP/IP.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!