问题
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