Connect to MSSQL in Python2.7.11 (Windows 10 Professional)

你说的曾经没有我的故事 提交于 2020-01-07 02:35:29

问题


I tried to connect to a remote MSSQL server by using pymssql package but then I get "ImportError: No module named pymssql".

Yes, I have done a pip install pymssql which worked. I also looked into installing Cython but it seems such install would give more headaches, so not worth to attempt with such. If I am not totally wrong, pymssql recuires Cython.

The OS I am using is Windows 10 Professional.

Remote connection to MSSQL does work, since at the other side of the office they are connected to the MSSQL server by software Qlik. The MSSQL server firewall does not block port 1433.

So, since pymssql seems not to work out of the box, I have tried to import pyodbc, which in this case gives some error message and does not connect. To this point I tried different solutions listed here on SE and none worked so far.

Following appears so far to be the most successful approach:

import pyodbc
con = pyodbc.connect('DRIVER={SQL Server};SERVER=gxn.database.windows.net;DATABASE=instantcopy;UID=testdb@gxn;PASSWORD=verysecret')
cur = con.cursor()
con.close()

The error is:

pyodbc.Error: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'testdb'. (18456) (SQLDriverConnect); [01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'testdb'. (18456); [01S00] [Microsoft][OD BC SQL Server Driver]Invalid connection string attribute (0)")

Any ideas?

Update1:

Using following correction.. I got this line from Azure suggesting following connection string:

Driver={SQL Server Native Client 11.0};Server=tcp:xxx.database.windows.net,1433;Database=xxx-instantorcopy;Uid=xxxtestdb@xxx;Pwd={your_password_here};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;

Solved:

The password was incorrect. So, if anyone is to attempt to connect to mssql I can tell you that I failed with 3 connectors and finally made it with pyodbc.

来源:https://stackoverflow.com/questions/37392476/connect-to-mssql-in-python2-7-11-windows-10-professional

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